WIP
This commit is contained in:
66
peer/on_ping.go
Normal file
66
peer/on_ping.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package peer
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
"vppn/peer/control"
|
||||
)
|
||||
|
||||
func (a *App) onPing(e PingEvent) {
|
||||
peer, ok := a.peersByIP[e.srcVPNIP]
|
||||
if !ok {
|
||||
// TODO: Log here.
|
||||
return
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
peer.LastPing = now
|
||||
peer.Up = true
|
||||
|
||||
// If we're the server, respond.
|
||||
if peer.Role == control.Server {
|
||||
a.sendPing(peer, e.ping.ID, e.ping.PingTS)
|
||||
}
|
||||
|
||||
// Compute RTT from server echo.
|
||||
if peer.Role == control.Client {
|
||||
peer.RTT = now.Sub(time.Unix(0, e.ping.PingTS))
|
||||
}
|
||||
|
||||
// If we're public, nothing more to do.
|
||||
if a.isPublic {
|
||||
return
|
||||
}
|
||||
|
||||
// We can only learn our own endpoint from directly-connected peers — Dst
|
||||
// is the sender's observation of our WG handshake source.
|
||||
if peer.State == StateDirect {
|
||||
if dst := e.ping.Dst; dst.IsValid() {
|
||||
if dst.Addr().Is4() {
|
||||
a.selfV4 = dst
|
||||
} else {
|
||||
a.selfV6 = dst
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
a.addProbe(peer, e.ping.SrcV4, e.ping.SrcV6)
|
||||
}
|
||||
|
||||
func (a *App) addProbe(peer *Peer, v4, v6 netip.AddrPort) {
|
||||
endpoint := preferredEndpoint(v4, v6)
|
||||
if !endpoint.IsValid() || endpoint == peer.WGEndpoint {
|
||||
return
|
||||
}
|
||||
|
||||
peer.Endpoint4 = v4
|
||||
peer.Endpoint6 = v6
|
||||
peer.WGEndpoint = endpoint
|
||||
|
||||
if peer.State == StateRelayed {
|
||||
peer.State = StateProbing
|
||||
}
|
||||
a.devAddProbe(peer, endpoint)
|
||||
}
|
||||
Reference in New Issue
Block a user