2022-10-06 19:23:38 +08:00
|
|
|
package sing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"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
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/adapter/inbound"
|
2023-11-23 08:20:26 +08:00
|
|
|
"github.com/metacubex/mihomo/adapter/outbound"
|
2023-11-03 21:01:45 +08:00
|
|
|
N "github.com/metacubex/mihomo/common/net"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
"github.com/metacubex/mihomo/log"
|
2022-10-06 19:23:38 +08:00
|
|
|
|
2023-06-19 08:23:48 +08:00
|
|
|
vmess "github.com/metacubex/sing-vmess"
|
2023-04-23 19:57:54 +08:00
|
|
|
mux "github.com/sagernet/sing-mux"
|
2022-10-06 19:23:38 +08:00
|
|
|
"github.com/sagernet/sing/common/buf"
|
2023-05-10 16:03:28 +08:00
|
|
|
"github.com/sagernet/sing/common/bufio"
|
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
|
|
|
|
|
2023-11-23 08:20:26 +08:00
|
|
|
type ListenerConfig struct {
|
2023-09-28 18:59:31 +08:00
|
|
|
Tunnel C.Tunnel
|
2023-03-23 14:05:16 +08:00
|
|
|
Type C.Type
|
|
|
|
Additions []inbound.Addition
|
|
|
|
UDPTimeout time.Duration
|
2023-11-23 08:20:26 +08:00
|
|
|
MuxOption MuxOption
|
|
|
|
}
|
|
|
|
|
|
|
|
type MuxOption struct {
|
|
|
|
Padding bool `yaml:"padding" json:"padding,omitempty"`
|
|
|
|
Brutal BrutalOptions `yaml:"brutal" json:"brutal,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type BrutalOptions struct {
|
|
|
|
Enabled bool `yaml:"enabled" json:"enabled"`
|
|
|
|
Up string `yaml:"up" json:"up,omitempty"`
|
|
|
|
Down string `yaml:"down" json:"down,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ListenerHandler struct {
|
|
|
|
ListenerConfig
|
2023-11-18 15:30:35 +08:00
|
|
|
muxService *mux.Service
|
2022-10-06 19:23:38 +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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 12:37:39 +08:00
|
|
|
func ConvertMetadata(metadata *C.Metadata) M.Metadata {
|
|
|
|
return M.Metadata{
|
|
|
|
Protocol: metadata.Type.String(),
|
|
|
|
Source: M.SocksaddrFrom(metadata.SrcIP, metadata.SrcPort),
|
|
|
|
Destination: M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-23 08:20:26 +08:00
|
|
|
func NewListenerHandler(lc ListenerConfig) (h *ListenerHandler, err error) {
|
|
|
|
h = &ListenerHandler{ListenerConfig: lc}
|
2023-11-18 15:30:35 +08:00
|
|
|
h.muxService, err = mux.NewService(mux.ServiceOptions{
|
2023-11-18 00:07:07 +08:00
|
|
|
NewStreamContext: func(ctx context.Context, conn net.Conn) context.Context {
|
|
|
|
return ctx
|
|
|
|
},
|
|
|
|
Logger: log.SingLogger,
|
|
|
|
Handler: h,
|
2023-11-23 08:20:26 +08:00
|
|
|
Padding: lc.MuxOption.Padding,
|
|
|
|
Brutal: mux.BrutalOptions{
|
|
|
|
Enabled: lc.MuxOption.Brutal.Enabled,
|
|
|
|
SendBPS: outbound.StringToBps(lc.MuxOption.Brutal.Up),
|
|
|
|
ReceiveBPS: outbound.StringToBps(lc.MuxOption.Brutal.Down),
|
2023-11-18 00:07:07 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-21 12:37:39 +08:00
|
|
|
func (h *ListenerHandler) IsSpecialFqdn(fqdn string) bool {
|
|
|
|
switch fqdn {
|
2023-11-18 15:30:35 +08:00
|
|
|
case mux.Destination.Fqdn,
|
|
|
|
vmess.MuxDestination.Fqdn,
|
|
|
|
uot.MagicAddress,
|
|
|
|
uot.LegacyMagicAddress:
|
|
|
|
return true
|
2023-08-21 12:37:39 +08:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ListenerHandler) ParseSpecialFqdn(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
2022-10-06 19:23:38 +08:00
|
|
|
switch metadata.Destination.Fqdn {
|
2023-04-23 19:57:54 +08:00
|
|
|
case mux.Destination.Fqdn:
|
2023-11-18 15:30:35 +08:00
|
|
|
return h.muxService.NewConnection(ctx, 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
|
|
|
}
|
2023-08-21 12:37:39 +08:00
|
|
|
return errors.New("not special fqdn")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ListenerHandler) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
|
|
|
if h.IsSpecialFqdn(metadata.Destination.Fqdn) {
|
|
|
|
return h.ParseSpecialFqdn(ctx, conn, metadata)
|
|
|
|
}
|
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-09-28 18:59:31 +08:00
|
|
|
|
2023-10-11 10:55:12 +08:00
|
|
|
cMetadata := &C.Metadata{
|
|
|
|
NetWork: C.TCP,
|
|
|
|
Type: h.Type,
|
|
|
|
}
|
2024-09-10 20:57:28 +08:00
|
|
|
if metadata.Source.IsIP() && metadata.Source.Fqdn == "" {
|
|
|
|
cMetadata.RawSrcAddr = metadata.Source.Unwrap().TCPAddr()
|
|
|
|
}
|
|
|
|
if metadata.Destination.IsIP() && metadata.Destination.Fqdn == "" {
|
|
|
|
cMetadata.RawDstAddr = metadata.Destination.Unwrap().TCPAddr()
|
|
|
|
}
|
2023-10-11 22:54:19 +08:00
|
|
|
inbound.ApplyAdditions(cMetadata, inbound.WithDstAddr(metadata.Destination), inbound.WithSrcAddr(metadata.Source), inbound.WithInAddr(conn.LocalAddr()))
|
|
|
|
inbound.ApplyAdditions(cMetadata, getAdditions(ctx)...)
|
|
|
|
inbound.ApplyAdditions(cMetadata, h.Additions...)
|
2023-10-11 10:55:12 +08:00
|
|
|
|
|
|
|
h.Tunnel.HandleTCPConn(conn, cMetadata) // this goroutine must exit after conn unused
|
2022-10-06 19:23:38 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ListenerHandler) NewPacketConnection(ctx context.Context, conn network.PacketConn, metadata M.Metadata) error {
|
|
|
|
defer func() { _ = conn.Close() }()
|
|
|
|
mutex := sync.Mutex{}
|
2023-12-07 23:32:37 +08:00
|
|
|
conn2 := bufio.NewNetPacketConn(conn) // a new interface to set nil in defer
|
2022-10-06 19:23:38 +08:00
|
|
|
defer func() {
|
|
|
|
mutex.Lock() // this goroutine must exit after all conn.WritePacket() is not running
|
|
|
|
defer mutex.Unlock()
|
|
|
|
conn2 = nil
|
|
|
|
}()
|
2023-12-07 23:32:37 +08:00
|
|
|
rwOptions := network.ReadWaitOptions{}
|
2023-05-12 12:12:22 +08:00
|
|
|
readWaiter, isReadWaiter := bufio.CreatePacketReadWaiter(conn)
|
|
|
|
if isReadWaiter {
|
2023-12-07 23:32:37 +08:00
|
|
|
readWaiter.InitializeReadWaiter(rwOptions)
|
2023-05-12 12:12:22 +08:00
|
|
|
}
|
2022-10-06 19:23:38 +08:00
|
|
|
for {
|
2023-05-10 22:35:50 +08:00
|
|
|
var (
|
2023-12-07 23:32:37 +08:00
|
|
|
buff *buf.Buffer
|
2023-05-10 22:35:50 +08:00
|
|
|
dest M.Socksaddr
|
|
|
|
err error
|
|
|
|
)
|
2023-05-12 12:12:22 +08:00
|
|
|
if isReadWaiter {
|
2023-12-07 23:32:37 +08:00
|
|
|
buff, dest, err = readWaiter.WaitReadPacket()
|
2023-05-10 22:35:50 +08:00
|
|
|
} else {
|
2023-12-07 23:32:37 +08:00
|
|
|
buff = rwOptions.NewPacketBuffer()
|
|
|
|
dest, err = conn.ReadPacket(buff)
|
2023-05-10 22:35:50 +08:00
|
|
|
if buff != nil {
|
2023-12-07 23:32:37 +08:00
|
|
|
rwOptions.PostReturn(buff)
|
2023-05-10 22:35:50 +08:00
|
|
|
}
|
2023-12-07 23:32:37 +08:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2023-10-11 10:55:12 +08:00
|
|
|
cPacket := &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
|
|
|
}
|
2023-09-28 18:59:31 +08:00
|
|
|
|
2023-10-11 10:55:12 +08:00
|
|
|
cMetadata := &C.Metadata{
|
|
|
|
NetWork: C.UDP,
|
|
|
|
Type: h.Type,
|
|
|
|
}
|
2024-09-10 20:57:28 +08:00
|
|
|
if metadata.Source.IsIP() && metadata.Source.Fqdn == "" {
|
|
|
|
cMetadata.RawSrcAddr = metadata.Source.Unwrap().UDPAddr()
|
|
|
|
}
|
|
|
|
if dest.IsIP() && dest.Fqdn == "" {
|
|
|
|
cMetadata.RawDstAddr = dest.Unwrap().UDPAddr()
|
|
|
|
}
|
2023-10-11 22:54:19 +08:00
|
|
|
inbound.ApplyAdditions(cMetadata, inbound.WithDstAddr(dest), inbound.WithSrcAddr(metadata.Source), inbound.WithInAddr(conn.LocalAddr()))
|
|
|
|
inbound.ApplyAdditions(cMetadata, getAdditions(ctx)...)
|
|
|
|
inbound.ApplyAdditions(cMetadata, h.Additions...)
|
2023-10-11 10:55:12 +08:00
|
|
|
|
|
|
|
h.Tunnel.HandleUDPPacket(cPacket, cMetadata)
|
2022-10-06 19:23:38 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ListenerHandler) NewError(ctx context.Context, err error) {
|
|
|
|
log.Warnln("%s listener get error: %+v", h.Type.String(), err)
|
|
|
|
}
|
|
|
|
|
2024-06-17 22:04:51 +08:00
|
|
|
func (h *ListenerHandler) TypeMutation(typ C.Type) *ListenerHandler {
|
|
|
|
handler := *h
|
|
|
|
handler.Type = typ
|
|
|
|
return &handler
|
|
|
|
}
|
|
|
|
|
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-12-07 23:32:37 +08:00
|
|
|
conn *network.NetPacketConn
|
2023-03-23 19:53:28 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
conn := *c.conn
|
|
|
|
if conn == nil {
|
|
|
|
err = errors.New("writeBack to closed connection")
|
|
|
|
return
|
|
|
|
}
|
2023-10-20 08:39:04 +08:00
|
|
|
|
2023-12-07 23:32:37 +08:00
|
|
|
return conn.WriteTo(b, addr)
|
2022-10-06 19:23:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|