diff --git a/p2p/kademlia/dht.go b/p2p/kademlia/dht.go index 3103d0a2..843472f0 100644 --- a/p2p/kademlia/dht.go +++ b/p2p/kademlia/dht.go @@ -6,6 +6,7 @@ import ( "encoding/hex" "fmt" "math" + "os" "sync" "sync/atomic" "time" @@ -168,15 +169,18 @@ func (s *DHT) NodesLen() int { func (s *DHT) getExternalIP() (string, error) { s.mtx.Lock() defer s.mtx.Unlock() - // if listen IP is localhost - then return itself - if s.ht.self.IP == "127.0.0.1" || s.ht.self.IP == "localhost" || s.ht.self.IP == "0.0.0.0" { - return s.ht.self.IP, nil - } + // Return cached value if already determined if s.externalIP != "" { return s.externalIP, nil } + // Check environment variable to control IP behavior + if useExternal := os.Getenv("P2P_USE_EXTERNAL_IP"); useExternal == "false" || useExternal == "0" { + s.externalIP = s.ht.self.IP + return s.externalIP, nil + } + externalIP, err := utils.GetExternalIPAddress() if err != nil { return "", fmt.Errorf("get external ip addr: %s", err) diff --git a/tests/integration/p2p/p2p_integration_test.go b/tests/integration/p2p/p2p_integration_test.go index 267949be..f38e3aa4 100644 --- a/tests/integration/p2p/p2p_integration_test.go +++ b/tests/integration/p2p/p2p_integration_test.go @@ -27,6 +27,9 @@ import ( func TestP2PBasicIntegration(t *testing.T) { log.Println("Starting P2P test...") + os.Setenv("P2P_USE_EXTERNAL_IP", "false") + defer os.Unsetenv("P2P_USE_EXTERNAL_IP") + snkeyring.InitSDKConfig() conn.RegisterALTSRecordProtocols() defer conn.UnregisterALTSRecordProtocols() diff --git a/tests/system/e2e_cascade_test.go b/tests/system/e2e_cascade_test.go index e041e592..851ef28e 100644 --- a/tests/system/e2e_cascade_test.go +++ b/tests/system/e2e_cascade_test.go @@ -49,6 +49,8 @@ func TestCascadeE2E(t *testing.T) { // --------------------------------------- os.Setenv("SYSTEM_TEST", "true") defer os.Unsetenv("SYSTEM_TEST") + os.Setenv("P2P_USE_EXTERNAL_IP", "false") + defer os.Unsetenv("P2P_USE_EXTERNAL_IP") // Test account credentials - these values are consistent across test runs const testMnemonic = "odor kiss switch swarm spell make planet bundle skate ozone path planet exclude butter atom ahead angle royal shuffle door prevent merry alter robust" const expectedAddress = "lumera1em87kgrvgttrkvuamtetyaagjrhnu3vjy44at4"