2022-10-06 19:23:38 +08:00
|
|
|
package sing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-12-05 10:12:53 +08:00
|
|
|
"golang.org/x/exp/slices"
|
2022-10-06 19:23:38 +08:00
|
|
|
"net"
|
2023-03-15 14:08:52 +08:00
|
|
|
"net/netip"
|
2022-10-06 19:23:38 +08:00
|
|
|
"sync"
|
2022-10-10 22:10:36 +08:00
|
|
|
"time"
|
2022-10-06 19:23:38 +08:00
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/adapter/inbound"
|
2023-04-02 22:24:46 +08:00
|
|
|
N "github.com/Dreamacro/clash/common/net"
|
2022-10-06 19:23:38 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/log"
|
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
|
|
|
|
2023-04-23 19:57:54 +08:00
|
|
|
mux "github.com/sagernet/sing-mux"
|
2022-10-06 19:23:38 +08:00
|
|
|
vmess "github.com/sagernet/sing-vmess"
|
|
|
|
"github.com/sagernet/sing/common/buf"
|
2023-04-17 19:29:07 +08:00
|
|
|
"github.com/sagernet/sing/common/bufio/deadline"
|
2022-10-06 19:23:38 +08:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
"github.com/sagernet/sing/common/network"
|
|
|
|
"github.com/sagernet/sing/common/uot"
|
|
|
|
)
|
|
|
|
|
2022-10-10 22:10:36 +08:00
|
|
|
const UDPTimeout = 5 * time.Minute
|
|
|
|
|
2022-10-06 19:23:38 +08:00
|
|
|
type ListenerHandler struct {
|
2023-03-23 14:05:16 +08:00
|
|
|
TcpIn chan<- C.ConnContext
|
|
|
|
UdpIn chan<- C.PacketAdapter
|
|
|
|
Type C.Type
|
|
|
|
Additions []inbound.Addition
|
|
|
|
UDPTimeout time.Duration
|
2022-10-06 19:23:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type waitCloseConn struct {
|
2023-04-02 22:24:46 +08:00
|
|
|
N.ExtendedConn
|
2022-10-06 19:23:38 +08:00
|
|
|
wg *sync.WaitGroup
|
|
|
|
close sync.Once
|
2022-10-09 13:16:13 +08:00
|
|
|
rAddr net.Addr
|
2022-10-06 19:23:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *waitCloseConn) Close() error { // call from handleTCPConn(connCtx C.ConnContext)
|
|
|
|
c.close.Do(func() {
|
|
|
|
c.wg.Done()
|
|
|
|
})
|
2023-04-02 22:24:46 +08:00
|
|
|
return c.ExtendedConn.Close()
|
2022-10-06 19:23:38 +08:00
|
|
|
}
|
|
|
|
|
2022-10-09 13:16:13 +08:00
|
|
|
func (c *waitCloseConn) RemoteAddr() net.Addr {
|
|
|
|
return c.rAddr
|
|
|
|
}
|
|
|
|
|
2023-01-16 13:26:30 +08:00
|
|
|
func (c *waitCloseConn) Upstream() any {
|
2023-04-02 22:24:46 +08:00
|
|
|
return c.ExtendedConn
|
2023-01-16 13:26:30 +08:00
|
|
|
}
|
|
|
|
|
2023-04-23 19:57:54 +08:00
|
|
|
func UpstreamMetadata(metadata M.Metadata) M.Metadata {
|
|
|
|
return M.Metadata{
|
|
|
|
Source: metadata.Source,
|
|
|
|
Destination: metadata.Destination,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-06 19:23:38 +08:00
|
|
|
func (h *ListenerHandler) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
2022-12-05 10:12:53 +08:00
|
|
|
additions := h.Additions
|
|
|
|
if ctxAdditions := getAdditions(ctx); len(ctxAdditions) > 0 {
|
|
|
|
additions = slices.Clone(additions)
|
|
|
|
additions = append(additions, ctxAdditions...)
|
|
|
|
}
|
2022-10-06 19:23:38 +08:00
|
|
|
switch metadata.Destination.Fqdn {
|
2023-04-23 19:57:54 +08:00
|
|
|
case mux.Destination.Fqdn:
|
|
|
|
return mux.HandleConnection(ctx, h, log.SingLogger, conn, UpstreamMetadata(metadata))
|
2022-10-06 19:23:38 +08:00
|
|
|
case vmess.MuxDestination.Fqdn:
|
|
|
|
return vmess.HandleMuxConnection(ctx, conn, h)
|
2023-03-15 14:08:52 +08:00
|
|
|
case uot.MagicAddress:
|
|
|
|
request, err := uot.ReadRequest(conn)
|
|
|
|
if err != nil {
|
|
|
|
return E.Cause(err, "read UoT request")
|
|
|
|
}
|
|
|
|
metadata.Destination = request.Destination
|
2023-03-17 13:23:37 +08:00
|
|
|
return h.NewPacketConnection(ctx, uot.NewConn(conn, *request), metadata)
|
2023-03-15 14:08:52 +08:00
|
|
|
case uot.LegacyMagicAddress:
|
|
|
|
metadata.Destination = M.Socksaddr{Addr: netip.IPv4Unspecified()}
|
2023-03-17 13:23:37 +08:00
|
|
|
return h.NewPacketConnection(ctx, uot.NewConn(conn, uot.Request{}), metadata)
|
2022-10-06 19:23:38 +08:00
|
|
|
}
|
|
|
|
target := socks5.ParseAddr(metadata.Destination.String())
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
defer wg.Wait() // this goroutine must exit after conn.Close()
|
|
|
|
wg.Add(1)
|
2022-12-05 10:12:53 +08:00
|
|
|
|
2023-04-17 19:29:07 +08:00
|
|
|
if deadline.NeedAdditionalReadDeadline(conn) {
|
|
|
|
conn = N.NewDeadlineConn(conn) // conn from sing should check NeedAdditionalReadDeadline
|
|
|
|
}
|
2023-04-02 22:24:46 +08:00
|
|
|
h.TcpIn <- inbound.NewSocket(target, &waitCloseConn{ExtendedConn: N.NewExtendedConn(conn), wg: wg, rAddr: metadata.Source.TCPAddr()}, h.Type, additions...)
|
2022-10-06 19:23:38 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ListenerHandler) NewPacketConnection(ctx context.Context, conn network.PacketConn, metadata M.Metadata) error {
|
2022-12-05 10:12:53 +08:00
|
|
|
additions := h.Additions
|
|
|
|
if ctxAdditions := getAdditions(ctx); len(ctxAdditions) > 0 {
|
|
|
|
additions = slices.Clone(additions)
|
|
|
|
additions = append(additions, ctxAdditions...)
|
|
|
|
}
|
2022-10-06 19:23:38 +08:00
|
|
|
defer func() { _ = conn.Close() }()
|
|
|
|
mutex := sync.Mutex{}
|
|
|
|
conn2 := conn // a new interface to set nil in defer
|
|
|
|
defer func() {
|
|
|
|
mutex.Lock() // this goroutine must exit after all conn.WritePacket() is not running
|
|
|
|
defer mutex.Unlock()
|
|
|
|
conn2 = nil
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
buff := buf.NewPacket() // do not use stack buffer
|
|
|
|
dest, err := conn.ReadPacket(buff)
|
|
|
|
if err != nil {
|
|
|
|
buff.Release()
|
2023-04-09 23:06:56 +08:00
|
|
|
if ShouldIgnorePacketError(err) {
|
2022-10-06 19:23:38 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
target := socks5.ParseAddr(dest.String())
|
|
|
|
packet := &packet{
|
2023-03-23 19:53:28 +08:00
|
|
|
conn: &conn2,
|
|
|
|
mutex: &mutex,
|
|
|
|
rAddr: metadata.Source.UDPAddr(),
|
|
|
|
lAddr: conn.LocalAddr(),
|
|
|
|
buff: buff,
|
2022-10-06 19:23:38 +08:00
|
|
|
}
|
|
|
|
select {
|
2022-12-05 10:12:53 +08:00
|
|
|
case h.UdpIn <- inbound.NewPacket(target, packet, h.Type, additions...):
|
2022-10-06 19:23:38 +08:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ListenerHandler) NewError(ctx context.Context, err error) {
|
|
|
|
log.Warnln("%s listener get error: %+v", h.Type.String(), err)
|
|
|
|
}
|
|
|
|
|
2023-04-09 23:06:56 +08:00
|
|
|
func ShouldIgnorePacketError(err error) bool {
|
|
|
|
// ignore simple error
|
|
|
|
if E.IsTimeout(err) || E.IsClosed(err) || E.IsCanceled(err) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-06 19:23:38 +08:00
|
|
|
type packet struct {
|
2023-03-23 19:53:28 +08:00
|
|
|
conn *network.PacketConn
|
|
|
|
mutex *sync.Mutex
|
|
|
|
rAddr net.Addr
|
|
|
|
lAddr net.Addr
|
|
|
|
buff *buf.Buffer
|
2022-10-06 19:23:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *packet) Data() []byte {
|
|
|
|
return c.buff.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteBack wirtes UDP packet with source(ip, port) = `addr`
|
|
|
|
func (c *packet) WriteBack(b []byte, addr net.Addr) (n int, err error) {
|
|
|
|
if addr == nil {
|
|
|
|
err = errors.New("address is invalid")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
buff := buf.NewPacket()
|
|
|
|
defer buff.Release()
|
|
|
|
n, err = buff.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
conn := *c.conn
|
|
|
|
if conn == nil {
|
|
|
|
err = errors.New("writeBack to closed connection")
|
|
|
|
return
|
|
|
|
}
|
2023-01-16 12:47:22 +08:00
|
|
|
err = conn.WritePacket(buff, M.SocksaddrFromNet(addr))
|
2023-03-23 14:05:16 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-10-06 19:23:38 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocalAddr returns the source IP/Port of UDP Packet
|
|
|
|
func (c *packet) LocalAddr() net.Addr {
|
|
|
|
return c.rAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *packet) Drop() {
|
|
|
|
c.buff.Release()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *packet) InAddr() net.Addr {
|
|
|
|
return c.lAddr
|
|
|
|
}
|
2023-02-17 16:31:15 +08:00
|
|
|
|
|
|
|
func (c *packet) SetNatTable(natTable C.NatTable) {
|
|
|
|
// no need
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *packet) SetUdpInChan(in chan<- C.PacketAdapter) {
|
|
|
|
// no need
|
|
|
|
}
|