From 0cb77c669e888fd64119f9a5f27bd60f7f0119d7 Mon Sep 17 00:00:00 2001 From: cnderrauber Date: Mon, 26 Sep 2022 11:10:21 +0800 Subject: [PATCH] IpMapper return locIP if corresponding map not set If we only set v4 or v6 NAT1to1Mapping, then findExternalIP will failed for the unset ip mapper, the gahter will complain 1:1 NAT mapping is enabled but no external IP is found, cause that candidate for udp can't be gathered. --- external_ip_mapper.go | 7 +++++++ external_ip_mapper_test.go | 10 ++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/external_ip_mapper.go b/external_ip_mapper.go index c53e5e1..d5e5ead 100644 --- a/external_ip_mapper.go +++ b/external_ip_mapper.go @@ -17,6 +17,7 @@ func validateIPString(ipStr string) (net.IP, bool, error) { type ipMapping struct { ipSole net.IP // when non-nil, this is the sole external IP for one local IP assumed ipMap map[string]net.IP // local-to-external IP mapping (k: local, v: external) + valid bool // if not set any external IP, valid is false } func (m *ipMapping) setSoleIP(ip net.IP) error { @@ -25,6 +26,7 @@ func (m *ipMapping) setSoleIP(ip net.IP) error { } m.ipSole = ip + m.valid = true return nil } @@ -42,11 +44,16 @@ func (m *ipMapping) addIPMapping(locIP, extIP net.IP) error { } m.ipMap[locIPStr] = extIP + m.valid = true return nil } func (m *ipMapping) findExternalIP(locIP net.IP) (net.IP, error) { + if !m.valid { + return locIP, nil + } + if m.ipSole != nil { return m.ipSole, nil } diff --git a/external_ip_mapper_test.go b/external_ip_mapper_test.go index aa7f2e2..63a3b2f 100644 --- a/external_ip_mapper_test.go +++ b/external_ip_mapper_test.go @@ -289,8 +289,9 @@ func TestExternalIPMapper(t *testing.T) { assert.NoError(t, err, "should succeed") // attempt to find IPv6 that does not exist in the map - _, err = m.findExternalIP("fe80::1") - assert.Error(t, err, "should fail") + extIP, err := m.findExternalIP("fe80::1") + assert.NoError(t, err, "should succeed") + assert.Equal(t, "fe80::1", extIP.String(), "should match") m, err = newExternalIPMapper(CandidateTypeUnspecified, []string{ "2200::1", @@ -298,7 +299,8 @@ func TestExternalIPMapper(t *testing.T) { assert.NoError(t, err, "should succeed") // attempt to find IPv4 that does not exist in the map - _, err = m.findExternalIP("10.0.0.1") - assert.Error(t, err, "should fail") + extIP, err = m.findExternalIP("10.0.0.1") + assert.NoError(t, err, "should succeed") + assert.Equal(t, "10.0.0.1", extIP.String(), "should match") }) }