Bugfix, save peer to file ok

This commit is contained in:
KusakabeSi
2021-08-23 17:45:09 +00:00
parent 8a1126dcbf
commit 2beb19c224
4 changed files with 39 additions and 3 deletions

View File

@@ -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()
}

View File

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

View File

@@ -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
static: true

View File

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