2022-09-05 16:15:09 +00:00
|
|
|
package wireguard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
2023-03-31 04:31:26 +00:00
|
|
|
"net/netip"
|
2022-09-05 16:15:09 +00:00
|
|
|
"sync"
|
2023-08-07 09:46:51 +00:00
|
|
|
"time"
|
2022-09-05 16:15:09 +00:00
|
|
|
|
|
|
|
"github.com/sagernet/sing/common"
|
2023-03-31 04:31:26 +00:00
|
|
|
"github.com/sagernet/sing/common/bufio"
|
2023-04-13 08:02:28 +00:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2022-09-05 16:15:09 +00:00
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
2022-11-07 08:35:15 +00:00
|
|
|
"github.com/sagernet/wireguard-go/conn"
|
2022-09-05 16:15:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ conn.Bind = (*ClientBind)(nil)
|
|
|
|
|
|
|
|
type ClientBind struct {
|
2023-03-31 04:31:26 +00:00
|
|
|
ctx context.Context
|
2023-04-13 08:02:28 +00:00
|
|
|
errorHandler E.Handler
|
2023-03-31 04:31:26 +00:00
|
|
|
dialer N.Dialer
|
2023-12-15 04:49:31 +00:00
|
|
|
reservedForEndpoint map[netip.AddrPort][3]uint8
|
2023-03-31 04:31:26 +00:00
|
|
|
connAccess sync.Mutex
|
|
|
|
conn *wireConn
|
|
|
|
done chan struct{}
|
|
|
|
isConnect bool
|
2023-12-15 04:49:31 +00:00
|
|
|
connectAddr netip.AddrPort
|
2023-03-31 04:31:26 +00:00
|
|
|
reserved [3]uint8
|
2022-09-05 16:15:09 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 04:49:31 +00:00
|
|
|
func NewClientBind(ctx context.Context, errorHandler E.Handler, dialer N.Dialer, isConnect bool, connectAddr netip.AddrPort, reserved [3]uint8) *ClientBind {
|
2022-09-05 16:15:09 +00:00
|
|
|
return &ClientBind{
|
2023-03-31 04:31:26 +00:00
|
|
|
ctx: ctx,
|
2023-04-13 08:02:28 +00:00
|
|
|
errorHandler: errorHandler,
|
2023-03-31 04:31:26 +00:00
|
|
|
dialer: dialer,
|
2023-12-15 04:49:31 +00:00
|
|
|
reservedForEndpoint: make(map[netip.AddrPort][3]uint8),
|
2023-03-31 04:31:26 +00:00
|
|
|
isConnect: isConnect,
|
|
|
|
connectAddr: connectAddr,
|
|
|
|
reserved: reserved,
|
2022-09-05 16:15:09 +00: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
|
|
|
|
}
|
|
|
|
}
|
2023-03-31 04:31:26 +00:00
|
|
|
if c.isConnect {
|
2023-12-15 04:49:31 +00:00
|
|
|
udpConn, err := c.dialer.DialContext(c.ctx, N.NetworkUDP, M.SocksaddrFromNetIP(c.connectAddr))
|
2023-03-31 04:31:26 +00:00
|
|
|
if err != nil {
|
2023-04-13 08:02:28 +00:00
|
|
|
return nil, err
|
2023-03-31 04:31:26 +00:00
|
|
|
}
|
|
|
|
c.conn = &wireConn{
|
2023-12-15 04:49:31 +00:00
|
|
|
PacketConn: bufio.NewUnbindPacketConn(udpConn),
|
|
|
|
done: make(chan struct{}),
|
2023-03-31 04:31:26 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
udpConn, err := c.dialer.ListenPacket(c.ctx, M.Socksaddr{Addr: netip.IPv4Unspecified()})
|
|
|
|
if err != nil {
|
2023-04-13 08:02:28 +00:00
|
|
|
return nil, err
|
2023-03-31 04:31:26 +00:00
|
|
|
}
|
|
|
|
c.conn = &wireConn{
|
2023-04-13 08:02:28 +00:00
|
|
|
PacketConn: bufio.NewPacketConn(udpConn),
|
|
|
|
done: make(chan struct{}),
|
2023-03-31 04:31:26 +00:00
|
|
|
}
|
2022-09-05 16:15:09 +00:00
|
|
|
}
|
|
|
|
return c.conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientBind) Open(port uint16) (fns []conn.ReceiveFunc, actualPort uint16, err error) {
|
2022-09-23 05:14:31 +00:00
|
|
|
select {
|
|
|
|
case <-c.done:
|
|
|
|
err = net.ErrClosed
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2022-09-05 16:15:09 +00:00
|
|
|
return []conn.ReceiveFunc{c.receive}, 0, nil
|
|
|
|
}
|
|
|
|
|
2023-04-20 05:16:31 +00:00
|
|
|
func (c *ClientBind) receive(packets [][]byte, sizes []int, eps []conn.Endpoint) (count int, err error) {
|
2022-09-05 16:15:09 +00:00
|
|
|
udpConn, err := c.connect()
|
|
|
|
if err != nil {
|
2023-04-13 08:02:28 +00:00
|
|
|
select {
|
|
|
|
case <-c.done:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
c.errorHandler.NewError(context.Background(), E.Cause(err, "connect to server"))
|
|
|
|
err = nil
|
2023-08-07 09:46:51 +00:00
|
|
|
time.Sleep(time.Second)
|
2022-09-05 16:15:09 +00:00
|
|
|
return
|
|
|
|
}
|
2023-04-20 05:16:31 +00:00
|
|
|
n, addr, err := udpConn.ReadFrom(packets[0])
|
2022-09-05 16:15:09 +00:00
|
|
|
if err != nil {
|
|
|
|
udpConn.Close()
|
2022-09-23 05:14:31 +00:00
|
|
|
select {
|
|
|
|
case <-c.done:
|
|
|
|
default:
|
2023-04-13 08:02:28 +00:00
|
|
|
c.errorHandler.NewError(context.Background(), E.Cause(err, "read packet"))
|
2023-04-20 05:16:31 +00:00
|
|
|
err = nil
|
2022-09-23 05:14:31 +00:00
|
|
|
}
|
|
|
|
return
|
2022-09-05 16:15:09 +00:00
|
|
|
}
|
2023-04-20 05:16:31 +00:00
|
|
|
sizes[0] = n
|
2022-10-26 12:21:07 +00:00
|
|
|
if n > 3 {
|
2023-04-20 05:16:31 +00:00
|
|
|
b := packets[0]
|
2023-12-15 04:49:31 +00:00
|
|
|
common.ClearArray(b[1:4])
|
2022-10-26 12:21:07 +00:00
|
|
|
}
|
2023-12-15 04:49:31 +00:00
|
|
|
eps[0] = Endpoint(M.AddrPortFromNet(addr))
|
2023-04-20 05:16:31 +00:00
|
|
|
count = 1
|
2022-09-05 16:15:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-06 02:36:19 +00:00
|
|
|
func (c *ClientBind) Reset() {
|
|
|
|
common.Close(common.PtrOrNil(c.conn))
|
|
|
|
}
|
|
|
|
|
2022-09-05 16:15:09 +00:00
|
|
|
func (c *ClientBind) Close() error {
|
|
|
|
common.Close(common.PtrOrNil(c.conn))
|
2022-09-23 05:14:31 +00: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-05 16:15:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientBind) SetMark(mark uint32) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-20 05:16:31 +00:00
|
|
|
func (c *ClientBind) Send(bufs [][]byte, ep conn.Endpoint) error {
|
2022-09-05 16:15:09 +00:00
|
|
|
udpConn, err := c.connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-15 04:49:31 +00:00
|
|
|
destination := netip.AddrPort(ep.(Endpoint))
|
2023-04-20 05:16:31 +00:00
|
|
|
for _, b := range bufs {
|
|
|
|
if len(b) > 3 {
|
|
|
|
reserved, loaded := c.reservedForEndpoint[destination]
|
|
|
|
if !loaded {
|
|
|
|
reserved = c.reserved
|
|
|
|
}
|
2023-12-15 04:49:31 +00:00
|
|
|
copy(b[1:4], reserved[:])
|
2023-04-20 05:16:31 +00:00
|
|
|
}
|
2023-12-15 04:49:31 +00:00
|
|
|
_, err = udpConn.WriteTo(b, M.SocksaddrFromNetIP(destination))
|
2023-04-20 05:16:31 +00:00
|
|
|
if err != nil {
|
|
|
|
udpConn.Close()
|
|
|
|
return err
|
2023-03-31 04:31:26 +00:00
|
|
|
}
|
2022-09-05 16:15:09 +00:00
|
|
|
}
|
2023-04-20 05:16:31 +00:00
|
|
|
return nil
|
2022-09-05 16:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientBind) ParseEndpoint(s string) (conn.Endpoint, error) {
|
2023-12-15 04:49:31 +00:00
|
|
|
ap, err := netip.ParseAddrPort(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return Endpoint(ap), nil
|
2022-09-05 16:15:09 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 05:16:31 +00:00
|
|
|
func (c *ClientBind) BatchSize() int {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2023-12-15 04:49:31 +00:00
|
|
|
func (c *ClientBind) SetReservedForEndpoint(destination netip.AddrPort, reserved [3]byte) {
|
|
|
|
c.reservedForEndpoint[destination] = reserved
|
|
|
|
}
|
|
|
|
|
2022-09-05 16:15:09 +00:00
|
|
|
type wireConn struct {
|
2023-04-13 08:02:28 +00:00
|
|
|
net.PacketConn
|
2022-09-05 16:15:09 +00: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:
|
|
|
|
}
|
2023-04-13 08:02:28 +00:00
|
|
|
w.PacketConn.Close()
|
2022-09-05 16:15:09 +00:00
|
|
|
close(w.done)
|
|
|
|
return nil
|
|
|
|
}
|