2022-11-25 00:08:14 +00:00
|
|
|
package outbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/pem"
|
2023-08-21 04:37:39 +00:00
|
|
|
"errors"
|
2022-11-25 00:08:14 +00:00
|
|
|
"fmt"
|
2022-11-27 01:38:20 +00:00
|
|
|
"math"
|
2022-11-25 00:08:14 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2023-04-10 03:20:28 +00:00
|
|
|
"github.com/Dreamacro/clash/component/proxydialer"
|
2023-08-21 04:37:39 +00:00
|
|
|
"github.com/Dreamacro/clash/component/resolver"
|
2022-11-25 00:08:14 +00:00
|
|
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/transport/tuic"
|
2023-06-12 09:44:22 +00:00
|
|
|
|
|
|
|
"github.com/gofrs/uuid/v5"
|
|
|
|
"github.com/metacubex/quic-go"
|
2023-08-21 04:37:39 +00:00
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
"github.com/sagernet/sing/common/uot"
|
2022-11-25 00:08:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Tuic struct {
|
|
|
|
*Base
|
2023-04-10 03:20:28 +00:00
|
|
|
option *TuicOption
|
2022-11-26 15:53:59 +00:00
|
|
|
client *tuic.PoolClient
|
2022-11-25 00:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TuicOption struct {
|
|
|
|
BasicOption
|
2022-11-25 04:43:23 +00:00
|
|
|
Name string `proxy:"name"`
|
|
|
|
Server string `proxy:"server"`
|
|
|
|
Port int `proxy:"port"`
|
2023-06-12 09:44:22 +00:00
|
|
|
Token string `proxy:"token,omitempty"`
|
|
|
|
UUID string `proxy:"uuid,omitempty"`
|
|
|
|
Password string `proxy:"password,omitempty"`
|
2022-11-25 04:43:23 +00:00
|
|
|
Ip string `proxy:"ip,omitempty"`
|
2022-11-26 13:35:47 +00:00
|
|
|
HeartbeatInterval int `proxy:"heartbeat-interval,omitempty"`
|
2022-11-25 04:43:23 +00:00
|
|
|
ALPN []string `proxy:"alpn,omitempty"`
|
2022-11-26 13:35:47 +00:00
|
|
|
ReduceRtt bool `proxy:"reduce-rtt,omitempty"`
|
|
|
|
RequestTimeout int `proxy:"request-timeout,omitempty"`
|
|
|
|
UdpRelayMode string `proxy:"udp-relay-mode,omitempty"`
|
|
|
|
CongestionController string `proxy:"congestion-controller,omitempty"`
|
|
|
|
DisableSni bool `proxy:"disable-sni,omitempty"`
|
|
|
|
MaxUdpRelayPacketSize int `proxy:"max-udp-relay-packet-size,omitempty"`
|
2022-11-25 00:08:14 +00:00
|
|
|
|
2023-03-16 13:09:44 +00:00
|
|
|
FastOpen bool `proxy:"fast-open,omitempty"`
|
|
|
|
MaxOpenStreams int `proxy:"max-open-streams,omitempty"`
|
2023-06-17 16:47:26 +00:00
|
|
|
CWND int `proxy:"cwnd,omitempty"`
|
2023-03-16 13:09:44 +00:00
|
|
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
|
|
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
|
|
|
CustomCA string `proxy:"ca,omitempty"`
|
|
|
|
CustomCAString string `proxy:"ca-str,omitempty"`
|
|
|
|
ReceiveWindowConn int `proxy:"recv-window-conn,omitempty"`
|
|
|
|
ReceiveWindow int `proxy:"recv-window,omitempty"`
|
|
|
|
DisableMTUDiscovery bool `proxy:"disable-mtu-discovery,omitempty"`
|
|
|
|
MaxDatagramFrameSize int `proxy:"max-datagram-frame-size,omitempty"`
|
|
|
|
SNI string `proxy:"sni,omitempty"`
|
2023-08-21 04:37:39 +00:00
|
|
|
|
|
|
|
UDPOverStream bool `proxy:"udp-over-stream,omitempty"`
|
|
|
|
UDPOverStreamVersion int `proxy:"udp-over-stream-version,omitempty"`
|
2022-11-25 00:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DialContext implements C.ProxyAdapter
|
|
|
|
func (t *Tuic) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
2022-12-22 01:53:11 +00:00
|
|
|
return t.DialContextWithDialer(ctx, dialer.NewDialer(t.Base.DialOptions(opts...)...), metadata)
|
2022-11-25 00:08:14 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 16:11:02 +00:00
|
|
|
// DialContextWithDialer implements C.ProxyAdapter
|
|
|
|
func (t *Tuic) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (C.Conn, error) {
|
2022-12-22 01:53:11 +00:00
|
|
|
conn, err := t.client.DialContextWithDialer(ctx, metadata, dialer, t.dialWithDialer)
|
2022-12-19 16:11:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewConn(conn, t), err
|
|
|
|
}
|
|
|
|
|
2022-11-25 00:08:14 +00:00
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
|
|
|
func (t *Tuic) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
2022-12-22 01:53:11 +00:00
|
|
|
return t.ListenPacketWithDialer(ctx, dialer.NewDialer(t.Base.DialOptions(opts...)...), metadata)
|
2022-11-25 00:08:14 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 16:11:02 +00:00
|
|
|
// ListenPacketWithDialer implements C.ProxyAdapter
|
|
|
|
func (t *Tuic) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
2023-08-21 04:37:39 +00:00
|
|
|
if t.option.UDPOverStream {
|
|
|
|
uotDestination := uot.RequestDestination(uint8(t.option.UDPOverStreamVersion))
|
|
|
|
uotMetadata := *metadata
|
|
|
|
uotMetadata.Host = uotDestination.Fqdn
|
|
|
|
uotMetadata.DstPort = uotDestination.Port
|
|
|
|
c, err := t.DialContextWithDialer(ctx, dialer, &uotMetadata)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// tuic uos use stream-oriented udp with a special address, so we need a net.UDPAddr
|
|
|
|
if !metadata.Resolved() {
|
|
|
|
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("can't resolve ip")
|
|
|
|
}
|
|
|
|
metadata.DstIP = ip
|
|
|
|
}
|
|
|
|
|
|
|
|
destination := M.SocksaddrFromNet(metadata.UDPAddr())
|
|
|
|
if t.option.UDPOverStreamVersion == uot.LegacyVersion {
|
|
|
|
return newPacketConn(uot.NewConn(c, uot.Request{Destination: destination}), t), nil
|
|
|
|
} else {
|
|
|
|
return newPacketConn(uot.NewLazyConn(c, uot.Request{Destination: destination}), t), nil
|
|
|
|
}
|
|
|
|
}
|
2022-12-22 01:53:11 +00:00
|
|
|
pc, err := t.client.ListenPacketWithDialer(ctx, metadata, dialer, t.dialWithDialer)
|
2022-12-19 16:11:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newPacketConn(pc, t), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SupportWithDialer implements C.ProxyAdapter
|
2023-04-11 04:51:24 +00:00
|
|
|
func (t *Tuic) SupportWithDialer() C.NetWork {
|
|
|
|
return C.ALLNet
|
2022-12-19 16:11:02 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 08:45:35 +00:00
|
|
|
func (t *Tuic) dialWithDialer(ctx context.Context, dialer C.Dialer) (transport *quic.Transport, addr net.Addr, err error) {
|
2023-04-10 03:20:28 +00:00
|
|
|
if len(t.option.DialerProxy) > 0 {
|
|
|
|
dialer, err = proxydialer.NewByName(t.option.DialerProxy, dialer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
2022-12-11 01:25:46 +00:00
|
|
|
udpAddr, err := resolveUDPAddrWithPrefer(ctx, "udp", t.addr, t.prefer)
|
2022-11-26 15:53:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2022-12-11 01:25:46 +00:00
|
|
|
addr = udpAddr
|
2023-06-03 08:45:35 +00:00
|
|
|
var pc net.PacketConn
|
2022-12-19 16:11:02 +00:00
|
|
|
pc, err = dialer.ListenPacket(ctx, "udp", "", udpAddr.AddrPort())
|
2022-11-26 15:53:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2023-06-03 08:45:35 +00:00
|
|
|
transport = &quic.Transport{Conn: pc}
|
|
|
|
transport.SetCreatedConn(true) // auto close conn
|
|
|
|
transport.SetSingleUse(true) // auto close transport
|
2022-11-26 15:53:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-25 00:08:14 +00:00
|
|
|
func NewTuic(option TuicOption) (*Tuic, error) {
|
|
|
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
|
|
|
serverName := option.Server
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: serverName,
|
|
|
|
InsecureSkipVerify: option.SkipCertVerify,
|
|
|
|
MinVersion: tls.VersionTLS13,
|
|
|
|
}
|
2023-02-19 08:20:30 +00:00
|
|
|
if option.SNI != "" {
|
|
|
|
tlsConfig.ServerName = option.SNI
|
|
|
|
}
|
2022-11-25 00:08:14 +00:00
|
|
|
|
|
|
|
var bs []byte
|
|
|
|
var err error
|
|
|
|
if len(option.CustomCA) > 0 {
|
|
|
|
bs, err = os.ReadFile(option.CustomCA)
|
|
|
|
if err != nil {
|
2022-11-25 02:45:06 +00:00
|
|
|
return nil, fmt.Errorf("tuic %s load ca error: %w", addr, err)
|
2022-11-25 00:08:14 +00:00
|
|
|
}
|
|
|
|
} else if option.CustomCAString != "" {
|
|
|
|
bs = []byte(option.CustomCAString)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bs) > 0 {
|
|
|
|
block, _ := pem.Decode(bs)
|
|
|
|
if block == nil {
|
|
|
|
return nil, fmt.Errorf("CA cert is not PEM")
|
|
|
|
}
|
|
|
|
|
|
|
|
fpBytes := sha256.Sum256(block.Bytes)
|
|
|
|
if len(option.Fingerprint) == 0 {
|
|
|
|
option.Fingerprint = hex.EncodeToString(fpBytes[:])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(option.Fingerprint) != 0 {
|
|
|
|
var err error
|
|
|
|
tlsConfig, err = tlsC.GetSpecifiedFingerprintTLSConfig(tlsConfig, option.Fingerprint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2023-01-14 13:08:06 +00:00
|
|
|
tlsConfig = tlsC.GetGlobalTLSConfig(tlsConfig)
|
2022-11-25 00:08:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-16 02:29:43 +00:00
|
|
|
if option.ALPN != nil { // structure's Decode will ensure value not nil when input has value even it was set an empty array
|
2022-11-25 00:08:14 +00:00
|
|
|
tlsConfig.NextProtos = option.ALPN
|
|
|
|
} else {
|
|
|
|
tlsConfig.NextProtos = []string{"h3"}
|
|
|
|
}
|
|
|
|
|
|
|
|
if option.RequestTimeout == 0 {
|
|
|
|
option.RequestTimeout = 8000
|
|
|
|
}
|
|
|
|
|
|
|
|
if option.HeartbeatInterval <= 0 {
|
|
|
|
option.HeartbeatInterval = 10000
|
|
|
|
}
|
|
|
|
|
2023-06-13 09:50:10 +00:00
|
|
|
udpRelayMode := tuic.QUIC
|
2022-11-25 00:08:14 +00:00
|
|
|
if option.UdpRelayMode != "quic" {
|
2023-06-13 09:50:10 +00:00
|
|
|
udpRelayMode = tuic.NATIVE
|
2022-11-25 00:08:14 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 04:43:23 +00:00
|
|
|
if option.MaxUdpRelayPacketSize == 0 {
|
2023-01-24 13:48:15 +00:00
|
|
|
option.MaxUdpRelayPacketSize = 1252
|
2022-11-25 04:43:23 +00:00
|
|
|
}
|
|
|
|
|
2022-11-27 01:38:20 +00:00
|
|
|
if option.MaxOpenStreams == 0 {
|
|
|
|
option.MaxOpenStreams = 100
|
|
|
|
}
|
|
|
|
|
2023-06-17 16:47:26 +00:00
|
|
|
if option.CWND == 0 {
|
|
|
|
option.CWND = 32
|
|
|
|
}
|
|
|
|
|
2023-06-12 09:44:22 +00:00
|
|
|
packetOverHead := tuic.PacketOverHeadV4
|
|
|
|
if len(option.Token) == 0 {
|
|
|
|
packetOverHead = tuic.PacketOverHeadV5
|
|
|
|
}
|
|
|
|
|
2023-03-16 13:09:44 +00:00
|
|
|
if option.MaxDatagramFrameSize == 0 {
|
2023-06-12 09:44:22 +00:00
|
|
|
option.MaxDatagramFrameSize = option.MaxUdpRelayPacketSize + packetOverHead
|
2023-03-16 13:09:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if option.MaxDatagramFrameSize > 1400 {
|
|
|
|
option.MaxDatagramFrameSize = 1400
|
|
|
|
}
|
2023-06-12 09:44:22 +00:00
|
|
|
option.MaxUdpRelayPacketSize = option.MaxDatagramFrameSize - packetOverHead
|
2023-03-16 13:09:44 +00:00
|
|
|
|
2022-11-27 01:38:20 +00:00
|
|
|
// ensure server's incoming stream can handle correctly, increase to 1.1x
|
|
|
|
quicMaxOpenStreams := int64(option.MaxOpenStreams)
|
|
|
|
quicMaxOpenStreams = quicMaxOpenStreams + int64(math.Ceil(float64(quicMaxOpenStreams)/10.0))
|
2022-11-25 00:08:14 +00:00
|
|
|
quicConfig := &quic.Config{
|
|
|
|
InitialStreamReceiveWindow: uint64(option.ReceiveWindowConn),
|
|
|
|
MaxStreamReceiveWindow: uint64(option.ReceiveWindowConn),
|
|
|
|
InitialConnectionReceiveWindow: uint64(option.ReceiveWindow),
|
|
|
|
MaxConnectionReceiveWindow: uint64(option.ReceiveWindow),
|
2022-11-27 01:38:20 +00:00
|
|
|
MaxIncomingStreams: quicMaxOpenStreams,
|
|
|
|
MaxIncomingUniStreams: quicMaxOpenStreams,
|
2022-11-25 00:08:14 +00:00
|
|
|
KeepAlivePeriod: time.Duration(option.HeartbeatInterval) * time.Millisecond,
|
|
|
|
DisablePathMTUDiscovery: option.DisableMTUDiscovery,
|
2023-03-16 13:09:44 +00:00
|
|
|
MaxDatagramFrameSize: int64(option.MaxDatagramFrameSize),
|
2022-11-25 00:08:14 +00:00
|
|
|
EnableDatagrams: true,
|
|
|
|
}
|
|
|
|
if option.ReceiveWindowConn == 0 {
|
2022-11-28 11:11:55 +00:00
|
|
|
quicConfig.InitialStreamReceiveWindow = tuic.DefaultStreamReceiveWindow / 10
|
|
|
|
quicConfig.MaxStreamReceiveWindow = tuic.DefaultStreamReceiveWindow
|
2022-11-25 00:08:14 +00:00
|
|
|
}
|
|
|
|
if option.ReceiveWindow == 0 {
|
2022-11-28 11:11:55 +00:00
|
|
|
quicConfig.InitialConnectionReceiveWindow = tuic.DefaultConnectionReceiveWindow / 10
|
|
|
|
quicConfig.MaxConnectionReceiveWindow = tuic.DefaultConnectionReceiveWindow
|
2022-11-25 00:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(option.Ip) > 0 {
|
|
|
|
addr = net.JoinHostPort(option.Ip, strconv.Itoa(option.Port))
|
|
|
|
}
|
|
|
|
if option.DisableSni {
|
|
|
|
tlsConfig.ServerName = ""
|
2023-06-12 09:44:22 +00:00
|
|
|
tlsConfig.InsecureSkipVerify = true // tls: either ServerName or InsecureSkipVerify must be specified in the tls.Config
|
2022-11-25 00:08:14 +00:00
|
|
|
}
|
2022-11-25 14:42:07 +00:00
|
|
|
|
2023-08-21 04:37:39 +00:00
|
|
|
switch option.UDPOverStreamVersion {
|
|
|
|
case uot.Version, uot.LegacyVersion:
|
|
|
|
case 0:
|
|
|
|
option.UDPOverStreamVersion = uot.LegacyVersion
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("tuic %s unknown udp over stream protocol version: %d", addr, option.UDPOverStreamVersion)
|
|
|
|
}
|
|
|
|
|
2022-11-25 14:42:07 +00:00
|
|
|
t := &Tuic{
|
|
|
|
Base: &Base{
|
|
|
|
name: option.Name,
|
|
|
|
addr: addr,
|
|
|
|
tp: C.Tuic,
|
|
|
|
udp: true,
|
2022-11-28 11:52:55 +00:00
|
|
|
tfo: option.FastOpen,
|
2022-11-25 14:42:07 +00:00
|
|
|
iface: option.Interface,
|
2023-02-24 06:02:20 +00:00
|
|
|
rmark: option.RoutingMark,
|
2022-11-25 14:42:07 +00:00
|
|
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
|
|
|
},
|
2023-04-10 03:20:28 +00:00
|
|
|
option: &option,
|
2022-11-25 14:42:07 +00:00
|
|
|
}
|
2023-01-24 13:48:15 +00:00
|
|
|
|
2022-11-27 01:38:20 +00:00
|
|
|
clientMaxOpenStreams := int64(option.MaxOpenStreams)
|
2023-01-24 13:48:15 +00:00
|
|
|
|
|
|
|
// to avoid tuic's "too many open streams", decrease to 0.9x
|
|
|
|
if clientMaxOpenStreams == 100 {
|
|
|
|
clientMaxOpenStreams = clientMaxOpenStreams - int64(math.Ceil(float64(clientMaxOpenStreams)/10.0))
|
|
|
|
}
|
|
|
|
|
2022-11-27 01:38:20 +00:00
|
|
|
if clientMaxOpenStreams < 1 {
|
|
|
|
clientMaxOpenStreams = 1
|
|
|
|
}
|
2022-11-25 14:42:07 +00:00
|
|
|
|
2023-06-12 09:44:22 +00:00
|
|
|
if len(option.Token) > 0 {
|
|
|
|
tkn := tuic.GenTKN(option.Token)
|
|
|
|
clientOption := &tuic.ClientOptionV4{
|
|
|
|
TlsConfig: tlsConfig,
|
|
|
|
QuicConfig: quicConfig,
|
|
|
|
Token: tkn,
|
2023-06-13 09:50:10 +00:00
|
|
|
UdpRelayMode: udpRelayMode,
|
2023-06-12 09:44:22 +00:00
|
|
|
CongestionController: option.CongestionController,
|
|
|
|
ReduceRtt: option.ReduceRtt,
|
|
|
|
RequestTimeout: time.Duration(option.RequestTimeout) * time.Millisecond,
|
|
|
|
MaxUdpRelayPacketSize: option.MaxUdpRelayPacketSize,
|
|
|
|
FastOpen: option.FastOpen,
|
|
|
|
MaxOpenStreams: clientMaxOpenStreams,
|
2023-06-17 16:47:26 +00:00
|
|
|
CWND: option.CWND,
|
2023-06-12 09:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.client = tuic.NewPoolClientV4(clientOption)
|
|
|
|
} else {
|
|
|
|
clientOption := &tuic.ClientOptionV5{
|
|
|
|
TlsConfig: tlsConfig,
|
|
|
|
QuicConfig: quicConfig,
|
|
|
|
Uuid: uuid.FromStringOrNil(option.UUID),
|
|
|
|
Password: option.Password,
|
2023-06-13 09:50:10 +00:00
|
|
|
UdpRelayMode: udpRelayMode,
|
2023-06-12 09:44:22 +00:00
|
|
|
CongestionController: option.CongestionController,
|
|
|
|
ReduceRtt: option.ReduceRtt,
|
|
|
|
MaxUdpRelayPacketSize: option.MaxUdpRelayPacketSize,
|
|
|
|
MaxOpenStreams: clientMaxOpenStreams,
|
2023-06-17 16:47:26 +00:00
|
|
|
CWND: option.CWND,
|
2023-06-12 09:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.client = tuic.NewPoolClientV5(clientOption)
|
|
|
|
}
|
2022-11-25 14:42:07 +00:00
|
|
|
|
|
|
|
return t, nil
|
2022-11-25 00:08:14 +00:00
|
|
|
}
|