mirror of
https://codeberg.org/cunicu/cunicu.git
synced 2025-10-05 16:57:01 +08:00
51 lines
1022 B
Go
51 lines
1022 B
Go
package core
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
// EnsureHandshake initiated a new WireGuard handshake if the last one is older than 5 seconds
|
|
func (p *Peer) EnsureHandshake() error {
|
|
// Return if the last handshake happened within the last 5 seconds
|
|
if time.Since(p.LastHandshakeTime) < 5*time.Second {
|
|
return nil
|
|
}
|
|
|
|
if err := p.InitiateHandshake(); err != nil {
|
|
return fmt.Errorf("failed to initiate handshake: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// InitiateHandshake sends a single packet towards the peer
|
|
// which triggers WireGuard to initiate the handshake
|
|
func (p *Peer) InitiateHandshake() error {
|
|
for time.Since(p.LastHandshakeTime) > 5*time.Second {
|
|
p.logger.Debug("Waiting for handshake")
|
|
|
|
ip := p.PublicKey().IPv6Address()
|
|
|
|
ra := &net.UDPAddr{
|
|
IP: ip.IP,
|
|
Zone: p.Interface.Name(),
|
|
Port: 1234,
|
|
}
|
|
|
|
c, err := net.DialUDP("udp6", nil, ra)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err := c.Write([]byte{1}); err != nil {
|
|
return err
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
|
}
|
|
|
|
return nil
|
|
}
|