Some checks failed
Windows project source guards / test (push) Has been cancelled
Owner request 2026-11-26 ("so everything works with Hysteria, AWG and the rest"). The helpers that dial with
their own UDP socket talk to a loopback `direct` UDP inbound of sing-box (override address = the real server) whose
rule leads into the WARP hop: the AmneziaWG helper in both modes (peer 127.0.0.1:relay, new `unbound` mode without the
physical-interface binding, IPv4 tunnel with an inner MTU of 1250, started after the front), the main Hysteria2
helper in the proxy mode (server name kept in the TLS SNI). Slot helpers stay TUN-only. Verified on win10 with the
real components: the real AmneziaWG server brings a real page through the hop in the proxy mode and under the TUN;
Go tests of the helper pass as a Windows exe. Hysteria2 through the relay is unit-tested on real nodes but not live:
the test person's Hysteria2 servers do not answer from the VM with or without the hop.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
108 lines
3.5 KiB
Go
108 lines
3.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/amnezia-vpn/amneziawg-go/v3/conn"
|
|
"net/netip"
|
|
)
|
|
|
|
// Use the official portable UDP bind, not WinRing's packet buffer layout.
|
|
// Apply physical-interface binding on every Open, before any handshake send.
|
|
type protectedBind struct {
|
|
*conn.StdNetBind
|
|
index uint32
|
|
unbound bool
|
|
allow4, allow6 bool
|
|
// race is set when the only peer has addresses of both families.
|
|
race *familyRace
|
|
raceEnds map[netip.AddrPort]conn.Endpoint
|
|
raceReady bool
|
|
// protect binds the sockets to the physical interface (protectBind).
|
|
// Loopback tests of the race replace it: interface binding is Windows
|
|
// policy, not part of the race.
|
|
protect func(*protectedBind) error
|
|
}
|
|
|
|
func newProtectedBind(c config, onLock func(netip.AddrPort)) *protectedBind {
|
|
b := &protectedBind{StdNetBind: conn.NewStdNetBind().(*conn.StdNetBind), index: c.InterfaceIndex, unbound: c.Unbound, protect: protectBind}
|
|
for _, peer := range c.Endpoint.Peers {
|
|
ip := netip.MustParseAddr(peer.Address).Unmap()
|
|
b.allow4 = b.allow4 || ip.Is4()
|
|
b.allow6 = b.allow6 || ip.Is6()
|
|
}
|
|
if len(c.Endpoint.Peers) == 1 && c.Endpoint.Peers[0].RaceAddress != "" {
|
|
peer := c.Endpoint.Peers[0]
|
|
preferred := netip.AddrPortFrom(netip.MustParseAddr(peer.Address).Unmap(), peer.Port)
|
|
other := netip.AddrPortFrom(netip.MustParseAddr(peer.RaceAddress).Unmap(), peer.Port)
|
|
b.allow4, b.allow6 = true, true
|
|
b.race = newFamilyRace(func() (netip.AddrPort, netip.AddrPort) { return preferred, other }, onLock, nil)
|
|
b.raceEnds = map[netip.AddrPort]conn.Endpoint{}
|
|
for _, address := range []netip.AddrPort{preferred, other} {
|
|
endpoint, err := b.StdNetBind.ParseEndpoint(address.String())
|
|
if err == nil {
|
|
b.raceEnds[address] = endpoint
|
|
}
|
|
}
|
|
b.raceReady = len(b.raceEnds) == 2
|
|
}
|
|
return b
|
|
}
|
|
|
|
func (b *protectedBind) Send(packets [][]byte, endpoint conn.Endpoint) error {
|
|
ip := endpoint.DstIP()
|
|
if (ip.Is4() && !b.allow4) || (ip.Is6() && !b.allow6) || !ip.IsValid() {
|
|
return fmt.Errorf("peer address family has no protected physical socket")
|
|
}
|
|
if !b.raceReady {
|
|
return b.StdNetBind.Send(packets, endpoint)
|
|
}
|
|
// One unreachable family must not fail the send while the other delivers.
|
|
var err error
|
|
delivered := false
|
|
for _, target := range b.race.targets() {
|
|
if sendErr := b.StdNetBind.Send(packets, b.raceEnds[target]); sendErr != nil {
|
|
err = sendErr
|
|
} else {
|
|
delivered = true
|
|
}
|
|
}
|
|
if !delivered {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (b *protectedBind) Open(port uint16) ([]conn.ReceiveFunc, uint16, error) {
|
|
fns, actual, err := b.StdNetBind.Open(port)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
// Entering through the hop (unbound): the socket stays on the default route, which is the TUN.
|
|
if !b.unbound {
|
|
if err = b.protect(b); err != nil {
|
|
b.StdNetBind.Close()
|
|
return nil, 0, err
|
|
}
|
|
}
|
|
if b.raceReady {
|
|
b.race.restart()
|
|
for index, receive := range fns {
|
|
fns[index] = b.watchRace(receive)
|
|
}
|
|
}
|
|
return fns, actual, nil
|
|
}
|
|
|
|
// watchRace reports the sources of received packets to the race until it is
|
|
// decided; after that a packet costs one atomic load.
|
|
func (b *protectedBind) watchRace(receive conn.ReceiveFunc) conn.ReceiveFunc {
|
|
return func(packets [][]byte, sizes []int, endpoints []conn.Endpoint) (int, error) {
|
|
n, err := receive(packets, sizes, endpoints)
|
|
for index := 0; index < n && !b.race.settled(); index++ {
|
|
if endpoint, ok := endpoints[index].(*conn.StdNetEndpoint); ok {
|
|
b.race.received(endpoint.AddrPort)
|
|
}
|
|
}
|
|
return n, err
|
|
}
|
|
}
|