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,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
- 192.168.86.21
#peerlist_url: http://example.com/peers.txt

33
main.go
View File

@ -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{

View File

@ -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) {