diff --git a/device/peer.go b/device/peer.go index dc3b336..c776487 100644 --- a/device/peer.go +++ b/device/peer.go @@ -304,6 +304,7 @@ func (peer *Peer) SetEndpointFromPacket(endpoint conn.Endpoint) { return } peer.Lock() + peer.device.SaveToConfig(peer, endpoint.DstToString()) peer.endpoint = endpoint peer.Unlock() } diff --git a/device/receivesendproc.go b/device/receivesendproc.go index 759ffaf..1c9b61d 100644 --- a/device/receivesendproc.go +++ b/device/receivesendproc.go @@ -353,6 +353,7 @@ func (device *Device) RoutineSetEndpoint() { //Send Ping message to it packet, err := device.GeneratePingPacket(device.ID) device.SendPacket(thepeer, packet, MessageTransportOffsetContent) + return false } return true @@ -369,12 +370,44 @@ func (device *Device) RoutineSetEndpoint() { } time.Sleep(path.S2TD(device.DRoute.P2P.PeerAliveTimeout)) if NextRun { - go device.SaveConfig() device.event_tryendpoint <- struct{}{} } } } +func (device *Device) SaveToConfig(thepeer *Peer, url string) { + if thepeer.LastPingReceived.Add(path.S2TD(device.DRoute.P2P.PeerAliveTimeout)).After(time.Now()) { + //Peer alives + return + } + foundInFile := false + pubkeystr := PubKey2Str(thepeer.handshake.remoteStatic) + pskstr := PSKeyStr(thepeer.handshake.presharedKey) + if bytes.Equal(thepeer.handshake.presharedKey[:], make([]byte, 32)) { + pskstr = "" + } + for _, peerfile := range device.EdgeConfig.Peers { + if peerfile.NodeID == thepeer.ID && peerfile.PubKey == pubkeystr { + foundInFile = true + if peerfile.Static == false { + peerfile.EndPoint = url + } + } else if peerfile.NodeID == thepeer.ID || peerfile.PubKey == pubkeystr { + panic("Found NodeID match " + strconv.Itoa(int(thepeer.ID)) + ", but PubKey Not match %s enrties in config file" + pubkeystr) + } + } + if !foundInFile { + device.EdgeConfig.Peers = append(device.EdgeConfig.Peers, config.PeerInfo{ + NodeID: thepeer.ID, + PubKey: pubkeystr, + PSKey: pskstr, + EndPoint: url, + Static: false, + }) + } + go device.SaveConfig() +} + func (device *Device) SaveConfig() { if device.DRoute.SaveNewPeers { configbytes, _ := yaml.Marshal(device.EdgeConfig) diff --git a/example_config/p2p_mode/n1.yaml b/example_config/p2p_mode/n1.yaml index 2a26d5b..29ba4fe 100644 --- a/example_config/p2p_mode/n1.yaml +++ b/example_config/p2p_mode/n1.yaml @@ -12,7 +12,7 @@ nodename: Node01 privkey: aABzjKhWdkFfQ29ZuijtMp1h1TNJe66SDCwvfmvQznw= listenport: 3001 loglevel: - loglevel: normal` + loglevel: normal` logtransit: true logcontrol: true dynamicroute: @@ -98,4 +98,4 @@ peers: pubkey: csT+hco4Jpa7btMeC9subHk2ZqzxcljcBk/57V0cSEk= pskey: "" endpoint: 127.0.0.1:3002 - static: true \ No newline at end of file + static: true diff --git a/path/path.go b/path/path.go index 18992f5..2ec711d 100644 --- a/path/path.go +++ b/path/path.go @@ -145,6 +145,8 @@ func (g *IG) UpdateLentancy(u, v config.Vertex, dt time.Duration, checkchange bo } func (g IG) Vertices() map[config.Vertex]bool { vr := make(map[config.Vertex]bool) + g.edgelock.RLock() + defer g.edgelock.RUnlock() for k, v := range g.Vert { //copy a new list vr[k] = v }