47 lines
1.3 KiB
Go
47 lines
1.3 KiB
Go
package peer
|
|
|
|
import (
|
|
"net/netip"
|
|
"time"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
"vppn/peer/control"
|
|
)
|
|
|
|
type PeerState string
|
|
|
|
const (
|
|
StateRelayed = PeerState("RELAY")
|
|
StateProbing = PeerState("PROBE")
|
|
StateDirect = PeerState("DIRECT")
|
|
)
|
|
|
|
type Peer struct {
|
|
PubKey wgtypes.Key // WireGuard public key.
|
|
VPNIP netip.Addr // VPN IP address.
|
|
IsRelay bool // Peer is a relay.
|
|
IsPublic bool // Peer has a public IP.
|
|
Endpoint4 netip.AddrPort // Reported IPv4 endpoint.
|
|
Endpoint6 netip.AddrPort // Reported IPv6 endpoint.
|
|
ObservedEndpoint netip.AddrPort // If we're public: WG handshake source endpoint.
|
|
LastPing time.Time // Time of last ping received.
|
|
RTT time.Duration // Round-trip time.
|
|
Role control.Role // Client initiates pings; server responds.
|
|
State PeerState // Current connection state.
|
|
Up bool // Is the peer alive.
|
|
WGEndpoint netip.AddrPort // Endpoint currently in WG (direct/probe).
|
|
}
|
|
|
|
func (p *Peer) Alive() bool {
|
|
return time.Since(p.LastPing) < TimeoutInterval
|
|
}
|
|
|
|
func (p *Peer) CanRelay() bool {
|
|
return p.IsRelay && p.Up && p.WGEndpoint.IsValid()
|
|
}
|
|
|
|
func (p *Peer) PreferredEndpoint() netip.AddrPort {
|
|
return preferredEndpoint(p.Endpoint4, p.Endpoint6)
|
|
}
|