From 3b5c71700cf952446ed06ee67539dec02384ae13 Mon Sep 17 00:00:00 2001 From: Josh Deprez Date: Sun, 21 Apr 2024 15:14:16 +1000 Subject: [PATCH] Add peer list function --- jrouter.yaml | 6 ++++-- main.go | 33 +++++++++++++++++++++++++++++++++ router/config.go | 3 +++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/jrouter.yaml b/jrouter.yaml index 26d301b..a4fa3b2 100644 --- a/jrouter.yaml +++ b/jrouter.yaml @@ -1,10 +1,12 @@ ethertalk: device: enp2s0 - # ethernet_addr: '08:00:07:fe:dc:ba' + #ethernet_addr: '08:00:07:fe:dc:ba' zone_name: The Twilight Zone net_start: 100 net_end: 100 open_peering: true peers: -- 192.168.86.21 \ No newline at end of file +- 192.168.86.21 + +#peerlist_url: http://example.com/peers.txt \ No newline at end of file diff --git a/main.go b/main.go index 15f31e0..a6beaa4 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ package main import ( + "bufio" "context" "errors" "flag" @@ -25,9 +26,11 @@ import ( "log" "math/rand/v2" "net" + "net/http" "os" "os/signal" "regexp" + "strings" "sync" "time" @@ -147,6 +150,31 @@ func main() { zones.Upsert(cfg.EtherTalk.NetStart, cfg.EtherTalk.ZoneName, true) // ------------------------- Configured peer setup ------------------------ + if cfg.PeerListURL != "" { + log.Printf("Fetching peer list from %s...", cfg.PeerListURL) + existing := len(cfg.Peers) + func() { + resp, err := http.Get(cfg.PeerListURL) + if err != nil { + log.Fatalf("Couldn't fetch peer list: %v", err) + } + defer resp.Body.Close() + + sc := bufio.NewScanner(resp.Body) + for sc.Scan() { + p := strings.TrimSpace(sc.Text()) + if p == "" { + continue + } + cfg.Peers = append(cfg.Peers, p) + } + if err := sc.Err(); err != nil { + log.Fatalf("Couldn't scan peer list response: %v", err) + } + }() + log.Printf("Fetched list containing %d peers", len(cfg.Peers)-existing) + } + for _, peerStr := range cfg.Peers { if !hasPortRE.MatchString(peerStr) { peerStr += ":387" @@ -159,6 +187,11 @@ func main() { } log.Printf("resolved %q to %v", peerStr, raddr) + if raddr.IP.Equal(localIP) { + log.Printf("%v == %v == me, skipping", peerStr, raddr) + continue + } + peer := &router.Peer{ Config: cfg, Transport: &aurp.Transport{ diff --git a/router/config.go b/router/config.go index 6ee5d76..1b3ba4d 100644 --- a/router/config.go +++ b/router/config.go @@ -53,6 +53,9 @@ type Config struct { // List of peer routers. Peers []string `yaml:"peers"` + + // Or a URL to fetch a list of peers from. + PeerListURL string `yaml:"peerlist_url"` } func LoadConfig(cfgPath string) (*Config, error) {