This commit is contained in:
jdl
2026-06-15 19:45:07 +02:00
parent c47d00e694
commit b86b43f1de
4 changed files with 33 additions and 29 deletions

View File

@@ -38,21 +38,27 @@ func (a *App) devPeers() []wgtypes.Peer {
return peers return peers
} }
func (a *App) devAddPeer(p *Peer) { func (a *App) devAddRelayed(p *Peer) {
log.Printf("RELAYED: %s - %s ", p.Name, p.VPNIP.String()) log.Printf("RELAYED: %s - %s ", p.Name, p.VPNIP.String())
devRetry(p.VPNIP, "AddPeer", func() error { return a.dev.AddPeer(p.PubKey()) }) devRetry(p.VPNIP, "AddPeer", func() error { return a.dev.AddPeer(p.PubKey()) })
p.State = StateRelayed p.State = StateRelayed
p.EndpointV4 = netip.AddrPort{}
p.EndpointV6 = netip.AddrPort{}
p.EndpointLAN = netip.AddrPort{}
} }
func (a *App) devAddDirect(p *Peer, endpoint netip.AddrPort) { func (a *App) devAddDirect(p *Peer, endpoint netip.AddrPort) {
log.Printf("DIRECT: %s - %s @ %s", p.Name, p.VPNIP.String(), endpoint.String()) log.Printf("DIRECT: %s - %s @ %s", p.Name, p.VPNIP.String(), endpoint.String())
devRetry(p.VPNIP, "AddDirect", func() error { return a.dev.AddDirect(p.PubKey(), endpoint, p.VPNIP) }) devRetry(p.VPNIP, "AddDirect", func() error { return a.dev.AddDirect(p.PubKey(), endpoint, p.VPNIP) })
p.State = StateDirect p.State = StateDirect
} }
func (a *App) devSetRelay(p *Peer, endpoint netip.AddrPort) { func (a *App) devSetRelay(p *Peer, endpoint netip.AddrPort) {
log.Printf("RELAY: %s - %s @ %s", p.Name, p.VPNIP.String(), endpoint.String()) log.Printf("RELAY: %s - %s @ %s", p.Name, p.VPNIP.String(), endpoint.String())
devRetry(p.VPNIP, "SetRelay", func() error { return a.dev.SetRelay(p.PubKey(), endpoint, a.vpnNet) }) devRetry(p.VPNIP, "SetRelay", func() error { return a.dev.SetRelay(p.PubKey(), endpoint, a.vpnNet) })
p.State = StateDirect // Direct connection. The app marks peer as relay. p.State = StateDirect // Direct connection. The app marks peer as relay.
} }
@@ -64,13 +70,18 @@ func (a *App) devPromote(p *Peer) {
log.Printf("DIRECT: %s - %s (waiting for handshake)", p.Name, p.VPNIP.String()) log.Printf("DIRECT: %s - %s (waiting for handshake)", p.Name, p.VPNIP.String())
} }
devRetry(p.VPNIP, "Promote", func() error { return a.dev.Promote(p.PubKey(), p.VPNIP) }) devRetry(p.VPNIP, "Promote", func() error { return a.dev.Promote(p.PubKey(), p.VPNIP) })
p.State = StateDirect p.State = StateDirect
p.LastPing = time.Now() // Assume the peer is up after being promoted.
} }
func (a *App) devAddProbe(p *Peer, endpoint netip.AddrPort) { func (a *App) devAddProbe(p *Peer, endpoint netip.AddrPort) {
log.Printf("PROBE: %s - %s @ %s", p.Name, p.VPNIP.String(), endpoint.String()) log.Printf("PROBE: %s - %s @ %s", p.Name, p.VPNIP.String(), endpoint.String())
devRetry(p.VPNIP, "AddProbe", func() error { return a.dev.AddProbe(p.PubKey(), endpoint) }) devRetry(p.VPNIP, "AddProbe", func() error { return a.dev.AddProbe(p.PubKey(), endpoint) })
p.State = StateProbing p.State = StateProbing
p.ProbeStart = time.Now()
p.ProbeEndpoint = endpoint
} }
func (a *App) devRemove(p *Peer) { func (a *App) devRemove(p *Peer) {

View File

@@ -43,7 +43,7 @@ func (a *App) onAddPeer(p m.Peer) {
// endpoint from the incoming handshake automatically. // endpoint from the incoming handshake automatically.
a.devPromote(peer) a.devPromote(peer)
} else { } else {
a.devAddPeer(peer) a.devAddRelayed(peer)
} }
return return
} }

View File

@@ -2,7 +2,6 @@ package peer
import ( import (
"log" "log"
"net/netip"
"time" "time"
"vppn/peer/wginterface" "vppn/peer/wginterface"
@@ -31,7 +30,6 @@ func (a *App) onStateTick() {
case StateRelayed: case StateRelayed:
// If we have an ep to probe, add it. // If we have an ep to probe, add it.
if ep := p.PreferredEndpoint(); ep.IsValid() { if ep := p.PreferredEndpoint(); ep.IsValid() {
p.ProbeStart = time.Now()
a.devAddProbe(p, ep) a.devAddProbe(p, ep)
} }
@@ -40,15 +38,12 @@ func (a *App) onStateTick() {
// Promote probing peers to direct once alive (direct path confirmed // Promote probing peers to direct once alive (direct path confirmed
// working). // working).
a.devPromote(p) a.devPromote(p)
} else if ep := p.PreferredEndpoint(); ep.IsValid() && ep != p.WGEndpoint() { } else if ep := p.PreferredEndpoint(); ep.IsValid() && ep != p.ProbeEndpoint {
// Update the probe address if it's changed. // Re-start probing if we see a new endpoint.
a.devAddProbe(p, ep) a.devAddProbe(p, ep)
} else if time.Since(p.ProbeStart) > 8*wginterface.ProbeKeepalive { } else if time.Since(p.ProbeStart) > 8*wginterface.ProbeKeepalive {
// Give up probing if we haven't been able to handshake. // Give up probing if we haven't been able to handshake.
p.EndpointV4 = netip.AddrPort{} a.devAddRelayed(p)
p.EndpointV6 = netip.AddrPort{}
p.EndpointLAN = netip.AddrPort{}
a.devAddPeer(p)
} }
case StateDirect: case StateDirect:
@@ -58,10 +53,7 @@ func (a *App) onStateTick() {
// Stale non-public direct peer: demote to relayed and wait for new IP // Stale non-public direct peer: demote to relayed and wait for new IP
// information. // information.
p.EndpointV4 = netip.AddrPort{} a.devAddRelayed(p)
p.EndpointV6 = netip.AddrPort{}
p.EndpointLAN = netip.AddrPort{}
a.devAddPeer(p)
} }
} }

View File

@@ -18,21 +18,22 @@ const (
) )
type Peer struct { type Peer struct {
wgPeer wgtypes.Peer wgPeer wgtypes.Peer
VPNIP netip.Addr // VPN IP address. VPNIP netip.Addr // VPN IP address.
Name string // Human-readable DNS label. Name string // Human-readable DNS label.
IsRelay bool // Peer is a relay. IsRelay bool // Peer is a relay.
IsPublic bool // Peer has a public IP. IsPublic bool // Peer has a public IP.
EndpointV4 netip.AddrPort // Reported IPv4 endpoint. EndpointV4 netip.AddrPort // Reported IPv4 endpoint.
EndpointV6 netip.AddrPort // Reported IPv6 endpoint. EndpointV6 netip.AddrPort // Reported IPv6 endpoint.
EndpointLAN netip.AddrPort // Discovered via multicast. EndpointLAN netip.AddrPort // Discovered via multicast.
EndpointWG netip.AddrPort // Current wireguard endpoint. EndpointWG netip.AddrPort // Current wireguard endpoint.
RTT time.Duration // Round-trip time. RTT time.Duration // Round-trip time.
LastPing time.Time // Last time we had a ping. LastPing time.Time // Last time we had a ping.
ProbeStart time.Time // When we started probing. ProbeStart time.Time // When we started probing.
State PeerState // Current routing state; updated on each devXxx call. ProbeEndpoint netip.AddrPort
Role control.Role // Role in relation to the local application. State PeerState // Current routing state; updated on each devXxx call.
SignPubKey [32]byte // nacl/sign public key for verifying multicast beacons. Role control.Role // Role in relation to the local application.
SignPubKey [32]byte // nacl/sign public key for verifying multicast beacons.
} }
// PubKey is the wireguard public key. // PubKey is the wireguard public key.