Add peer list function

This commit is contained in:
Josh Deprez 2024-04-21 15:14:16 +10:00
parent ecbf63fddd
commit 3b5c71700c
Signed by: josh
SSH key fingerprint: SHA256:zZji7w1Ilh2RuUpbQcqkLPrqmRwpiCSycbF2EfKm6Kw
3 changed files with 40 additions and 2 deletions

View file

@ -1,6 +1,6 @@
ethertalk: ethertalk:
device: enp2s0 device: enp2s0
# ethernet_addr: '08:00:07:fe:dc:ba' #ethernet_addr: '08:00:07:fe:dc:ba'
zone_name: The Twilight Zone zone_name: The Twilight Zone
net_start: 100 net_start: 100
net_end: 100 net_end: 100
@ -8,3 +8,5 @@ ethertalk:
open_peering: true open_peering: true
peers: peers:
- 192.168.86.21 - 192.168.86.21
#peerlist_url: http://example.com/peers.txt

33
main.go
View file

@ -17,6 +17,7 @@
package main package main
import ( import (
"bufio"
"context" "context"
"errors" "errors"
"flag" "flag"
@ -25,9 +26,11 @@ import (
"log" "log"
"math/rand/v2" "math/rand/v2"
"net" "net"
"net/http"
"os" "os"
"os/signal" "os/signal"
"regexp" "regexp"
"strings"
"sync" "sync"
"time" "time"
@ -147,6 +150,31 @@ func main() {
zones.Upsert(cfg.EtherTalk.NetStart, cfg.EtherTalk.ZoneName, true) zones.Upsert(cfg.EtherTalk.NetStart, cfg.EtherTalk.ZoneName, true)
// ------------------------- Configured peer setup ------------------------ // ------------------------- 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 { for _, peerStr := range cfg.Peers {
if !hasPortRE.MatchString(peerStr) { if !hasPortRE.MatchString(peerStr) {
peerStr += ":387" peerStr += ":387"
@ -159,6 +187,11 @@ func main() {
} }
log.Printf("resolved %q to %v", peerStr, raddr) 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{ peer := &router.Peer{
Config: cfg, Config: cfg,
Transport: &aurp.Transport{ Transport: &aurp.Transport{

View file

@ -53,6 +53,9 @@ type Config struct {
// List of peer routers. // List of peer routers.
Peers []string `yaml:"peers"` 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) { func LoadConfig(cfgPath string) (*Config, error) {