WIP
This commit is contained in:
@@ -9,7 +9,8 @@ import (
|
||||
)
|
||||
|
||||
type localConfig struct {
|
||||
m.PeerConfig
|
||||
PeerIP byte
|
||||
Network []byte
|
||||
PubKey []byte
|
||||
PrivKey []byte
|
||||
PubSignKey []byte
|
||||
@@ -25,11 +26,11 @@ func configDir(netName string) string {
|
||||
}
|
||||
|
||||
func peerConfigPath(netName string) string {
|
||||
return filepath.Join(configDir(netName), "peer-config.json")
|
||||
return filepath.Join(configDir(netName), "config.json")
|
||||
}
|
||||
|
||||
func peerStatePath(netName string) string {
|
||||
return filepath.Join(configDir(netName), "peer-state.json")
|
||||
return filepath.Join(configDir(netName), "state.json")
|
||||
}
|
||||
|
||||
func storeJson(x any, outPath string) error {
|
||||
|
||||
@@ -16,12 +16,12 @@ func TestFilePaths(t *testing.T) {
|
||||
}
|
||||
|
||||
path := peerConfigPath("netName")
|
||||
if path != filepath.Join(confDir, "peer-config.json") {
|
||||
if path != filepath.Join(confDir, "config.json") {
|
||||
t.Fatal(path)
|
||||
}
|
||||
|
||||
path = peerStatePath("netName")
|
||||
if path != filepath.Join(confDir, "peer-state.json") {
|
||||
if path != filepath.Join(confDir, "state.json") {
|
||||
t.Fatal(path)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func Main() {
|
||||
conf := peerConfig{}
|
||||
conf := mainArgs{}
|
||||
|
||||
flag.StringVar(&conf.NetName, "name", "", "[REQUIRED] The network name.")
|
||||
flag.StringVar(&conf.HubAddress, "hub-address", "", "[REQUIRED] The hub address.")
|
||||
|
||||
@@ -65,6 +65,7 @@ func runMCReaderInner(
|
||||
SrcIP: h.SourceIP,
|
||||
SrcAddr: remoteAddr,
|
||||
}
|
||||
logf("Got discovery packet from peer %d.", h.SourceIP)
|
||||
handleControlMsg(h.SourceIP, msg)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ func runMCWriter(localIP byte, signingKey []byte) {
|
||||
}
|
||||
|
||||
for range time.Tick(broadcastInterval) {
|
||||
log.Printf("[MCWriter] Broadcasting on %v...", multicastAddr)
|
||||
_, err := conn.WriteToUDP(discoveryPacket, multicastAddr)
|
||||
if err != nil {
|
||||
log.Printf("[MCWriter] Failed to write multicast: %v", err)
|
||||
|
||||
60
peer/peer.go
60
peer/peer.go
@@ -25,35 +25,42 @@ type peerMain struct {
|
||||
super *supervisor
|
||||
}
|
||||
|
||||
type peerConfig struct {
|
||||
type mainArgs struct {
|
||||
NetName string
|
||||
HubAddress string
|
||||
APIKey string
|
||||
}
|
||||
|
||||
func newPeerMain(conf peerConfig) *peerMain {
|
||||
func newPeerMain(args mainArgs) *peerMain {
|
||||
logf := func(s string, args ...any) {
|
||||
log.Printf("[Main] "+s, args...)
|
||||
}
|
||||
|
||||
config, err := loadPeerConfig(conf.NetName)
|
||||
config, err := loadPeerConfig(args.NetName)
|
||||
if err != nil {
|
||||
logf("Failed to load configuration: %v", err)
|
||||
logf("Initializing...")
|
||||
initPeerWithHub(conf)
|
||||
initPeerWithHub(args)
|
||||
|
||||
config, err = loadPeerConfig(conf.NetName)
|
||||
config, err = loadPeerConfig(args.NetName)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to load configuration: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
iface, err := openInterface(config.Network, config.PeerIP, conf.NetName)
|
||||
state, err := loadNetworkState(args.NetName)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to load network state: %v", err)
|
||||
}
|
||||
|
||||
iface, err := openInterface(config.Network, config.PeerIP, args.NetName)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open interface: %v", err)
|
||||
}
|
||||
|
||||
myAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", config.Port))
|
||||
localPeer := state.Peers[config.PeerIP]
|
||||
|
||||
myAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", localPeer.Port))
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to resolve UDP address: %v", err)
|
||||
}
|
||||
@@ -80,19 +87,19 @@ func newPeerMain(conf peerConfig) *peerMain {
|
||||
}
|
||||
|
||||
var localAddr netip.AddrPort
|
||||
ip, localAddrValid := netip.AddrFromSlice(config.PublicIP)
|
||||
ip, localAddrValid := netip.AddrFromSlice(localPeer.PublicIP)
|
||||
if localAddrValid {
|
||||
localAddr = netip.AddrPortFrom(ip, config.Port)
|
||||
localAddr = netip.AddrPortFrom(ip, localPeer.Port)
|
||||
}
|
||||
|
||||
rt := newRoutingTable(config.PeerIP, localAddr)
|
||||
rt := newRoutingTable(localPeer.PeerIP, localAddr)
|
||||
rtPtr := &atomic.Pointer[routingTable]{}
|
||||
rtPtr.Store(&rt)
|
||||
|
||||
ifReader := newIFReader(iface, writeToUDPAddrPort, rtPtr)
|
||||
super := newSupervisor(writeToUDPAddrPort, rtPtr, config.PrivKey)
|
||||
connReader := newConnReader(conn.ReadFromUDPAddrPort, writeToUDPAddrPort, iface, super.HandleControlMsg, rtPtr)
|
||||
hubPoller, err := newHubPoller(config.PeerIP, conf.NetName, conf.HubAddress, conf.APIKey, super.HandleControlMsg)
|
||||
hubPoller, err := newHubPoller(config.PeerIP, args.NetName, args.HubAddress, args.APIKey, super.HandleControlMsg)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create hub poller: %v", err)
|
||||
}
|
||||
@@ -123,22 +130,22 @@ func (p *peerMain) Run() {
|
||||
select {}
|
||||
}
|
||||
|
||||
func initPeerWithHub(conf peerConfig) {
|
||||
func initPeerWithHub(args mainArgs) {
|
||||
keys := generateKeys()
|
||||
|
||||
initURL, err := url.Parse(conf.HubAddress)
|
||||
initURL, err := url.Parse(args.HubAddress)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to parse hub URL: %v", err)
|
||||
}
|
||||
initURL.Path = "/peer/init/"
|
||||
|
||||
args := m.PeerInitArgs{
|
||||
initArgs := m.PeerInitArgs{
|
||||
EncPubKey: keys.PubKey,
|
||||
PubSignKey: keys.PubSignKey,
|
||||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
if err := json.NewEncoder(buf).Encode(args); err != nil {
|
||||
if err := json.NewEncoder(buf).Encode(initArgs); err != nil {
|
||||
log.Fatalf("Failed to encode init args: %v", err)
|
||||
}
|
||||
|
||||
@@ -146,7 +153,7 @@ func initPeerWithHub(conf peerConfig) {
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to construct request: %v", err)
|
||||
}
|
||||
req.SetBasicAuth("", conf.APIKey)
|
||||
req.SetBasicAuth("", args.APIKey)
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
@@ -159,17 +166,24 @@ func initPeerWithHub(conf peerConfig) {
|
||||
log.Fatalf("Failed to read response body: %v", err)
|
||||
}
|
||||
|
||||
peerConfig := localConfig{}
|
||||
if err := json.Unmarshal(data, &peerConfig.PeerConfig); err != nil {
|
||||
initResp := m.PeerInitResp{}
|
||||
if err := json.Unmarshal(data, &initResp); err != nil {
|
||||
log.Fatalf("Failed to parse configuration: %v\n%s", err, data)
|
||||
}
|
||||
|
||||
peerConfig.PubKey = keys.PubKey
|
||||
peerConfig.PrivKey = keys.PrivKey
|
||||
peerConfig.PubSignKey = keys.PubSignKey
|
||||
peerConfig.PrivSignKey = keys.PrivSignKey
|
||||
config := localConfig{}
|
||||
config.PeerIP = initResp.PeerIP
|
||||
config.Network = initResp.Network
|
||||
config.PubKey = keys.PubKey
|
||||
config.PrivKey = keys.PrivKey
|
||||
config.PubSignKey = keys.PubSignKey
|
||||
config.PrivSignKey = keys.PrivSignKey
|
||||
|
||||
if err := storePeerConfig(conf.NetName, peerConfig); err != nil {
|
||||
if err := storeNetworkState(args.NetName, initResp.NetworkState); err != nil {
|
||||
log.Fatalf("Failed to store network state: %v", err)
|
||||
}
|
||||
|
||||
if err := storePeerConfig(args.NetName, config); err != nil {
|
||||
log.Fatalf("Failed to store configuration: %v", err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user