From 83d2550b2d1c6351202d199bc9023e1f979f07ff Mon Sep 17 00:00:00 2001 From: Ryan Huber Date: Sat, 23 Nov 2019 21:46:45 +0000 Subject: [PATCH 1/2] add an error (non fatal) when a lighthouse host has no static entry --- lighthouse.go | 12 ++++++++++++ main.go | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/lighthouse.go b/lighthouse.go index 5e19d65..26e346c 100644 --- a/lighthouse.go +++ b/lighthouse.go @@ -53,6 +53,18 @@ func NewLightHouse(amLighthouse bool, myIp uint32, ips []string, interval int, n return &h } +func (lh *LightHouse) ValidateLHStaticEntries() (bool, error) { + for lhIP, _ := range lh.lighthouses { + for ip, _ := range lh.staticList { + if lhIP == ip { + continue + } + return false, fmt.Errorf("Lighthouse %s does not have a static_host_map entry", IntIp(lhIP)) + } + } + return true, nil +} + func (lh *LightHouse) Query(ip uint32, f EncWriter) ([]udpAddr, error) { if !lh.IsLighthouseIP(ip) { lh.QueryServer(ip, f) diff --git a/main.go b/main.go index ab23331..a261e9b 100644 --- a/main.go +++ b/main.go @@ -236,6 +236,11 @@ func Main(configPath string, configTest bool, buildVersion string) { } } + _, err = lightHouse.ValidateLHStaticEntries() + if err != nil { + l.Error(err) + } + handshakeManager := NewHandshakeManager(tunCidr, preferredRanges, hostMap, lightHouse, udpServer) //TODO: These will be reused for psk From 08915315ffebc59853be159ec8967001235738e7 Mon Sep 17 00:00:00 2001 From: Ryan Huber Date: Sat, 23 Nov 2019 23:55:23 +0000 Subject: [PATCH 2/2] add tests and improve error --- lighthouse.go | 6 +++--- lighthouse_test.go | 21 +++++++++++++++++++++ main.go | 5 +++-- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lighthouse.go b/lighthouse.go index 26e346c..c19cbb7 100644 --- a/lighthouse.go +++ b/lighthouse.go @@ -53,16 +53,16 @@ func NewLightHouse(amLighthouse bool, myIp uint32, ips []string, interval int, n return &h } -func (lh *LightHouse) ValidateLHStaticEntries() (bool, error) { +func (lh *LightHouse) ValidateLHStaticEntries() error { for lhIP, _ := range lh.lighthouses { for ip, _ := range lh.staticList { if lhIP == ip { continue } - return false, fmt.Errorf("Lighthouse %s does not have a static_host_map entry", IntIp(lhIP)) + return fmt.Errorf("Lighthouse %s does not have a static_host_map entry", IntIp(lhIP)) } } - return true, nil + return nil } func (lh *LightHouse) Query(ip uint32, f EncWriter) ([]udpAddr, error) { diff --git a/lighthouse_test.go b/lighthouse_test.go index b0ff492..08a9857 100644 --- a/lighthouse_test.go +++ b/lighthouse_test.go @@ -46,6 +46,27 @@ func TestNewipandportsfromudpaddrs(t *testing.T) { } +func Test_lhStaticMapping(t *testing.T) { + lh1 := "10.128.0.2" + + lh1IP := net.ParseIP(lh1) + + udpServer, _ := NewListener("0.0.0.0", 0, true) + + meh := NewLightHouse(true, 1, []string{lh1}, 10, 10003, udpServer, false) + meh.AddRemote(ip2int(lh1IP), NewUDPAddr(ip2int(lh1IP), uint16(4242)), true) + err := meh.ValidateLHStaticEntries() + assert.Nil(t, err) + + lh2 := "10.128.0.3" + meh = NewLightHouse(true, 1, []string{lh1, lh2}, 10, 10003, udpServer, false) + meh.AddRemote(ip2int(lh1IP), NewUDPAddr(ip2int(lh1IP), uint16(4242)), true) + err = meh.ValidateLHStaticEntries() + assert.EqualError(t, err, "Lighthouse 10.128.0.3 does not have a static_host_map entry") +} + +//func NewLightHouse(amLighthouse bool, myIp uint32, ips []string, interval int, nebulaPort int, pc *udpConn, punchBack bool) *LightHouse { + /* func TestLHQuery(t *testing.T) { //n := NewLhQueryByIpString("10.128.0.3") diff --git a/main.go b/main.go index a261e9b..c101774 100644 --- a/main.go +++ b/main.go @@ -205,6 +205,7 @@ func Main(configPath string, configTest bool, buildVersion string) { go dnsMain(hostMap) } + //TODO: Move all of this inside functions in lighthouse.go for k, v := range config.GetMap("static_host_map", map[interface{}]interface{}{}) { vpnIp := net.ParseIP(fmt.Sprintf("%v", k)) vals, ok := v.([]interface{}) @@ -236,9 +237,9 @@ func Main(configPath string, configTest bool, buildVersion string) { } } - _, err = lightHouse.ValidateLHStaticEntries() + err = lightHouse.ValidateLHStaticEntries() if err != nil { - l.Error(err) + l.WithError(err).Error("Lighthouse unreachable") } handshakeManager := NewHandshakeManager(tunCidr, preferredRanges, hostMap, lightHouse, udpServer)