Separate out generate address function and make it's SAM port configurable

This commit is contained in:
eyedeekay
2024-11-29 18:01:00 -05:00
parent 40e34d7089
commit d8a31854b9
6 changed files with 212 additions and 215 deletions

View File

@ -70,7 +70,7 @@ func Test_NewI2PAddrFromString(t *testing.T) {
}
})
t.Run("Address with .i2p suffix", func(t *testing.T) { //CHECK
t.Run("Address with .i2p suffix", func(t *testing.T) { // CHECK
addr, err := NewI2PAddrFromString(validI2PAddrB64 + ".i2p")
if err != nil {
t.Fatalf("NewI2PAddrFromString failed for address with .i2p suffix: '%v'", err)
@ -169,15 +169,15 @@ func Test_KeyGenerationAndHandling(t *testing.T) {
t.Fatalf("Failed to generate new I2P keys: %v", err)
}
t.Run("LoadKeysIncompat", func(t *testing.T) {
//extract keys
// extract keys
addr := keys.Address
fmt.Println(addr)
//both := removeNewlines(keys.Both)
// both := removeNewlines(keys.Both)
both := keys.Both
fmt.Println(both)
//FORMAT TO LOAD: (Address, Both)
// FORMAT TO LOAD: (Address, Both)
addrload := addr.Base64() + "\n" + both
r := strings.NewReader(addrload)
@ -187,9 +187,8 @@ func Test_KeyGenerationAndHandling(t *testing.T) {
}
if loadedKeys.Address != keys.Address {
//fmt.Printf("loadedKeys.Address md5hash: '%s'\n keys.Address md5hash: '%s'\n", getMD5Hash(string(loadedKeys.Address)), getMD5Hash(string(keys.Address)))
// fmt.Printf("loadedKeys.Address md5hash: '%s'\n keys.Address md5hash: '%s'\n", getMD5Hash(string(loadedKeys.Address)), getMD5Hash(string(keys.Address)))
t.Errorf("LoadKeysIncompat returned incorrect address. Got '%s', want '%s'", loadedKeys.Address, keys.Address)
}
if loadedKeys.Both != keys.Both {
t.Errorf("LoadKeysIncompat returned incorrect pair. Got '%s'\nwant '%s'\n", loadedKeys.Both, keys.Both)
@ -199,7 +198,6 @@ func Test_KeyGenerationAndHandling(t *testing.T) {
}
*/
}
})
expected := keys.Address.Base64() + "\n" + keys.Both

View File

@ -128,6 +128,7 @@ func StoreKeysIncompat(k I2PKeys, w io.Writer) error {
log.WithField("keys", k).Debug("Keys stored successfully")
return nil
}
func StoreKeys(k I2PKeys, r string) error {
log.WithField("filename", r).Debug("Storing keys to file")
if _, err := os.Stat(r); err != nil {
@ -190,7 +191,7 @@ func (k I2PKeys) PrivateKey() crypto.PrivateKey {
_, err := pk.Sign(rand.Reader, []byte("nonsense"), crypto.Hash(0))
if err != nil {
log.WithError(err).Warn("Error in private key signature")
//TODO: Elgamal, P256, P384, P512, GOST? keys?
// TODO: Elgamal, P256, P384, P512, GOST? keys?
}
return pk
}
@ -225,4 +226,3 @@ func (k I2PKeys) HostnameEntry(hostname string, opts crypto.SignerOpts) (string,
}
return string(sig), nil
}

4
log.go
View File

@ -4,9 +4,7 @@ import (
"github.com/go-i2p/logger"
)
var (
log *logger.Logger
)
var log *logger.Logger
func InitializeI2PKeysLogger() {
logger.InitializeGoI2PLogger()

5
new.go
View File

@ -9,8 +9,9 @@ import (
"time"
)
var DefaultSAMAddress = "127.0.0.1:7656"
const (
defaultSAMAddress = "127.0.0.1:7656"
defaultTimeout = 30 * time.Second
maxResponseSize = 4096
@ -30,7 +31,7 @@ type samClient struct {
// newSAMClient creates a new SAM client with optional configuration
func newSAMClient(options ...func(*samClient)) *samClient {
client := &samClient{
addr: defaultSAMAddress,
addr: DefaultSAMAddress,
timeout: defaultTimeout,
}