Fix initial WireGuard handshake for domain peers

This commit is contained in:
世界
2026-09-20 22:24:38 +08:00
parent 5ae597bfba
commit 96b004bab1
3 changed files with 26 additions and 19 deletions
+1 -1
View File
@@ -60,7 +60,7 @@ require (
github.com/sagernet/sing-vmess v0.2.8
github.com/sagernet/smux v1.5.50-sing-box-mod.1
github.com/sagernet/tailscale v1.102.1-sing-box-1.14-mod.5
github.com/sagernet/wireguard-go v0.0.6
github.com/sagernet/wireguard-go v0.0.7
github.com/sagernet/ws v0.0.0-20231204124109-acfe8907c854
github.com/spf13/cobra v1.10.2
github.com/stretchr/testify v1.12.0
+2 -2
View File
@@ -348,8 +348,8 @@ github.com/sagernet/smux v1.5.50-sing-box-mod.1 h1:XkJcivBC9V4wBjiGXIXZ229aZCU1h
github.com/sagernet/smux v1.5.50-sing-box-mod.1/go.mod h1:NjhsCEWedJm7eFLyhuBgIEzwfhRmytrUoiLluxs5Sk8=
github.com/sagernet/tailscale v1.102.1-sing-box-1.14-mod.5 h1:mGLWVgj2Pl2NlgE1+sEUIDIMB42JdWEPwYmxTnVWg44=
github.com/sagernet/tailscale v1.102.1-sing-box-1.14-mod.5/go.mod h1:WLUSOPmTcf7VN9gLCe01qUSIvD+/cKC177neENyZPkI=
github.com/sagernet/wireguard-go v0.0.6 h1:zAjWSzufYThDsyLHxPLyyR+XdpBMlFDWD7WxJAvynSw=
github.com/sagernet/wireguard-go v0.0.6/go.mod h1:er10sELpmzLXq7S7Pbc1Zsbyapcr+/gxNAHKTo6fzVA=
github.com/sagernet/wireguard-go v0.0.7 h1:FmP7QY5OcFlhKtDVB81Xd+uoBv4BjW5GORBTUYwpuu0=
github.com/sagernet/wireguard-go v0.0.7/go.mod h1:er10sELpmzLXq7S7Pbc1Zsbyapcr+/gxNAHKTo6fzVA=
github.com/sagernet/ws v0.0.0-20231204124109-acfe8907c854 h1:6uUiZcDRnZSAegryaUGwPC/Fj13JSHwiTftrXhMmYOc=
github.com/sagernet/ws v0.0.0-20231204124109-acfe8907c854/go.mod h1:LtfoSK3+NG57tvnVEHgcuBW9ujgE8enPSgzgwStwCAA=
github.com/smallstep/pkcs7 v0.1.1 h1:x+rPdt2W088V9Vkjho4KtoggyktZJlMduZAtRHm68LU=
+23 -16
View File
@@ -202,28 +202,25 @@ func (e *Endpoint) Start(postStart bool) error {
}
wgDevice := device.NewDevice(e.options.Context, e.returnDevice, bind, logger, e.options.Workers)
e.tunDevice.SetDevice(wgDevice)
var ipcConf strings.Builder
ipcConf.WriteString(e.ipcConf)
for _, peer := range e.peers {
ipcConf.WriteString(peer.GenerateIpcLines())
}
err = wgDevice.IpcSet(ipcConf.String())
if err != nil {
wgDevice.Close()
return E.Cause(err, "setup wireguard: \n", ipcConf.String())
}
for _, peer := range e.peers {
domainPeers := make(map[device.NoisePublicKey]*peerConfig)
for peerIndex, peer := range e.peers {
if !peer.destination.IsDomain() {
continue
}
var publicKey device.NoisePublicKey
common.Must(publicKey.FromHex(peer.publicKeyHex))
wgPeer, found := wgDevice.LookupActivePeer(publicKey)
if !found {
err = publicKey.FromHex(peer.publicKeyHex)
if err != nil {
wgDevice.Close()
return E.New("missing configured peer: ", peer.destination)
return E.Cause(err, "decode public key for peer ", peerIndex)
}
wgPeer.SetEndpointResolver(func() ([]conn.Endpoint, error) {
domainPeers[publicKey] = &e.peers[peerIndex]
}
if len(domainPeers) > 0 {
wgDevice.SetEndpointResolverFunc(func(publicKey device.NoisePublicKey) ([]conn.Endpoint, error) {
peer, found := domainPeers[publicKey]
if !found {
return nil, nil
}
addresses, lookupErr := e.options.ResolvePeer(peer.destination.Fqdn)
if lookupErr != nil {
return nil, lookupErr
@@ -243,6 +240,16 @@ func (e *Endpoint) Start(postStart bool) error {
return endpoints, nil
})
}
var ipcConf strings.Builder
ipcConf.WriteString(e.ipcConf)
for _, peer := range e.peers {
ipcConf.WriteString(peer.GenerateIpcLines())
}
err = wgDevice.IpcSet(ipcConf.String())
if err != nil {
wgDevice.Close()
return E.Cause(err, "setup wireguard: \n", ipcConf.String())
}
e.device = wgDevice
e.pause = service.FromContext[pause.Manager](e.options.Context)
if e.pause != nil {