Skip to content

Commit

Permalink
Fix WireGuard client bind
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Mar 20, 2024
1 parent 59d437b commit c49cd11
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions transport/wireguard/client_bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ func (c *ClientBind) connect() (*wireConn, error) {
func (c *ClientBind) Open(port uint16) (fns []conn.ReceiveFunc, actualPort uint16, err error) {
select {
case <-c.done:
err = net.ErrClosed
return
c.done = make(chan struct{})
default:
}
return []conn.ReceiveFunc{c.receive}, 0, nil
Expand Down Expand Up @@ -165,7 +164,7 @@ func (c *ClientBind) Send(bufs [][]byte, ep conn.Endpoint) error {
}
copy(b[1:4], reserved[:])
}
_, err = udpConn.WriteTo(b, M.SocksaddrFromNetIP(destination))
_, err = udpConn.WriteToUDPAddrPort(b, destination)
if err != nil {
udpConn.Close()
return err
Expand All @@ -192,10 +191,18 @@ func (c *ClientBind) SetReservedForEndpoint(destination netip.AddrPort, reserved

type wireConn struct {
net.PacketConn
conn net.Conn
access sync.Mutex
done chan struct{}
}

func (w *wireConn) WriteToUDPAddrPort(b []byte, addr netip.AddrPort) (int, error) {
if w.conn != nil {
return w.conn.Write(b)
}
return w.PacketConn.WriteTo(b, M.SocksaddrFromNetIP(addr).UDPAddr())
}

func (w *wireConn) Close() error {
w.access.Lock()
defer w.access.Unlock()
Expand Down

0 comments on commit c49cd11

Please sign in to comment.