sing-box/transport/wireguard/client_bind.go

199 lines
4.1 KiB
Go
Raw Normal View History

2022-09-06 00:15:09 +08:00
package wireguard
import (
"context"
"net"
"net/netip"
2022-09-06 00:15:09 +08:00
"sync"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
2022-09-06 00:15:09 +08:00
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/wireguard-go/conn"
2022-09-06 00:15:09 +08:00
)
var _ conn.Bind = (*ClientBind)(nil)
type ClientBind struct {
ctx context.Context
dialer N.Dialer
reservedForEndpoint map[M.Socksaddr][3]uint8
connAccess sync.Mutex
conn *wireConn
done chan struct{}
isConnect bool
connectAddr M.Socksaddr
reserved [3]uint8
2022-09-06 00:15:09 +08:00
}
func NewClientBind(ctx context.Context, dialer N.Dialer, isConnect bool, connectAddr M.Socksaddr, reserved [3]uint8) *ClientBind {
2022-09-06 00:15:09 +08:00
return &ClientBind{
ctx: ctx,
dialer: dialer,
reservedForEndpoint: make(map[M.Socksaddr][3]uint8),
isConnect: isConnect,
connectAddr: connectAddr,
reserved: reserved,
2022-09-06 00:15:09 +08:00
}
}
func (c *ClientBind) SetReservedForEndpoint(destination M.Socksaddr, reserved [3]byte) {
c.reservedForEndpoint[destination] = reserved
}
2022-09-06 00:15:09 +08:00
func (c *ClientBind) connect() (*wireConn, error) {
serverConn := c.conn
if serverConn != nil {
select {
case <-serverConn.done:
serverConn = nil
default:
return serverConn, nil
}
}
c.connAccess.Lock()
defer c.connAccess.Unlock()
serverConn = c.conn
if serverConn != nil {
select {
case <-serverConn.done:
serverConn = nil
default:
return serverConn, nil
}
}
if c.isConnect {
udpConn, err := c.dialer.DialContext(c.ctx, N.NetworkUDP, c.connectAddr)
if err != nil {
return nil, &wireError{err}
}
c.conn = &wireConn{
NetPacketConn: &bufio.UnbindPacketConn{
ExtendedConn: bufio.NewExtendedConn(udpConn),
Addr: c.connectAddr,
},
done: make(chan struct{}),
}
} else {
udpConn, err := c.dialer.ListenPacket(c.ctx, M.Socksaddr{Addr: netip.IPv4Unspecified()})
if err != nil {
return nil, &wireError{err}
}
c.conn = &wireConn{
NetPacketConn: bufio.NewPacketConn(udpConn),
done: make(chan struct{}),
}
2022-09-06 00:15:09 +08:00
}
return c.conn, nil
}
func (c *ClientBind) Open(port uint16) (fns []conn.ReceiveFunc, actualPort uint16, err error) {
2022-09-23 13:14:31 +08:00
select {
case <-c.done:
err = net.ErrClosed
return
default:
}
2022-09-06 00:15:09 +08:00
return []conn.ReceiveFunc{c.receive}, 0, nil
}
func (c *ClientBind) receive(b []byte) (n int, ep conn.Endpoint, err error) {
udpConn, err := c.connect()
if err != nil {
err = &wireError{err}
return
}
buffer := buf.With(b)
destination, err := udpConn.ReadPacket(buffer)
2022-09-06 00:15:09 +08:00
if err != nil {
udpConn.Close()
2022-09-23 13:14:31 +08:00
select {
case <-c.done:
default:
err = &wireError{err}
}
return
2022-09-06 00:15:09 +08:00
}
n = buffer.Len()
if buffer.Start() > 0 {
copy(b, buffer.Bytes())
}
if n > 3 {
b[1] = 0
b[2] = 0
b[3] = 0
}
ep = Endpoint(destination)
2022-09-06 00:15:09 +08:00
return
}
2022-11-06 10:36:19 +08:00
func (c *ClientBind) Reset() {
common.Close(common.PtrOrNil(c.conn))
}
2022-09-06 00:15:09 +08:00
func (c *ClientBind) Close() error {
common.Close(common.PtrOrNil(c.conn))
2022-09-23 13:14:31 +08:00
if c.done == nil {
c.done = make(chan struct{})
return nil
}
select {
case <-c.done:
return net.ErrClosed
default:
close(c.done)
}
2022-09-06 00:15:09 +08:00
return nil
}
func (c *ClientBind) SetMark(mark uint32) error {
return nil
}
func (c *ClientBind) Send(b []byte, ep conn.Endpoint) error {
udpConn, err := c.connect()
if err != nil {
return err
}
destination := M.Socksaddr(ep.(Endpoint))
if len(b) > 3 {
reserved, loaded := c.reservedForEndpoint[destination]
if !loaded {
reserved = c.reserved
}
b[1] = reserved[0]
b[2] = reserved[1]
b[3] = reserved[2]
}
err = udpConn.WritePacket(buf.As(b), destination)
2022-09-06 00:15:09 +08:00
if err != nil {
udpConn.Close()
}
return err
}
func (c *ClientBind) ParseEndpoint(s string) (conn.Endpoint, error) {
return Endpoint(M.ParseSocksaddr(s)), nil
2022-09-06 00:15:09 +08:00
}
type wireConn struct {
N.NetPacketConn
2022-09-06 00:15:09 +08:00
access sync.Mutex
done chan struct{}
}
func (w *wireConn) Close() error {
w.access.Lock()
defer w.access.Unlock()
select {
case <-w.done:
return net.ErrClosed
default:
}
w.NetPacketConn.Close()
2022-09-06 00:15:09 +08:00
close(w.done)
return nil
}