2022-11-28 17:09:25 +08:00
|
|
|
package tuic
|
|
|
|
|
|
|
|
import (
|
2023-08-21 12:37:39 +08:00
|
|
|
"context"
|
2022-11-28 17:09:25 +08:00
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-12-04 14:37:52 +08:00
|
|
|
"github.com/Dreamacro/clash/adapter/inbound"
|
2022-12-04 23:05:13 +08:00
|
|
|
CN "github.com/Dreamacro/clash/common/net"
|
2022-11-28 17:09:25 +08:00
|
|
|
"github.com/Dreamacro/clash/common/sockopt"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2022-12-04 14:37:52 +08:00
|
|
|
LC "github.com/Dreamacro/clash/listener/config"
|
2023-08-21 12:37:39 +08:00
|
|
|
"github.com/Dreamacro/clash/listener/sing"
|
2022-11-28 17:09:25 +08:00
|
|
|
"github.com/Dreamacro/clash/log"
|
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
|
|
|
"github.com/Dreamacro/clash/transport/tuic"
|
2023-06-12 17:44:22 +08:00
|
|
|
|
|
|
|
"github.com/gofrs/uuid/v5"
|
|
|
|
"github.com/metacubex/quic-go"
|
|
|
|
"golang.org/x/exp/slices"
|
2022-11-28 17:09:25 +08:00
|
|
|
)
|
|
|
|
|
2023-01-17 14:25:19 +08:00
|
|
|
const ServerMaxIncomingStreams = (1 << 32) - 1
|
|
|
|
|
2022-11-28 17:09:25 +08:00
|
|
|
type Listener struct {
|
|
|
|
closed bool
|
2022-12-04 14:37:52 +08:00
|
|
|
config LC.TuicServer
|
2022-11-28 17:09:25 +08:00
|
|
|
udpListeners []net.PacketConn
|
2023-06-21 13:53:37 +08:00
|
|
|
servers []*tuic.Server
|
2022-11-28 17:09:25 +08:00
|
|
|
}
|
|
|
|
|
2022-12-05 00:20:50 +08:00
|
|
|
func New(config LC.TuicServer, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, additions ...inbound.Addition) (*Listener, error) {
|
|
|
|
if len(additions) == 0 {
|
2022-12-05 10:12:53 +08:00
|
|
|
additions = []inbound.Addition{
|
|
|
|
inbound.WithInName("DEFAULT-TUIC"),
|
|
|
|
inbound.WithSpecialRules(""),
|
|
|
|
}
|
2022-12-05 00:20:50 +08:00
|
|
|
}
|
2023-08-21 12:37:39 +08:00
|
|
|
h := &sing.ListenerHandler{
|
|
|
|
TcpIn: tcpIn,
|
|
|
|
UdpIn: udpIn,
|
|
|
|
Type: C.TUIC,
|
|
|
|
Additions: additions,
|
|
|
|
}
|
2022-12-04 23:05:13 +08:00
|
|
|
cert, err := CN.ParseCert(config.Certificate, config.PrivateKey)
|
2022-11-28 17:09:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS13,
|
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
}
|
|
|
|
if len(config.ALPN) > 0 {
|
|
|
|
tlsConfig.NextProtos = config.ALPN
|
|
|
|
} else {
|
|
|
|
tlsConfig.NextProtos = []string{"h3"}
|
|
|
|
}
|
|
|
|
quicConfig := &quic.Config{
|
|
|
|
MaxIdleTimeout: time.Duration(config.MaxIdleTime) * time.Millisecond,
|
2023-01-17 14:25:19 +08:00
|
|
|
MaxIncomingStreams: ServerMaxIncomingStreams,
|
|
|
|
MaxIncomingUniStreams: ServerMaxIncomingStreams,
|
2022-11-28 17:09:25 +08:00
|
|
|
EnableDatagrams: true,
|
2023-06-03 16:45:35 +08:00
|
|
|
Allow0RTT: true,
|
2022-11-28 17:09:25 +08:00
|
|
|
}
|
2022-11-28 19:11:55 +08:00
|
|
|
quicConfig.InitialStreamReceiveWindow = tuic.DefaultStreamReceiveWindow / 10
|
|
|
|
quicConfig.MaxStreamReceiveWindow = tuic.DefaultStreamReceiveWindow
|
|
|
|
quicConfig.InitialConnectionReceiveWindow = tuic.DefaultConnectionReceiveWindow / 10
|
|
|
|
quicConfig.MaxConnectionReceiveWindow = tuic.DefaultConnectionReceiveWindow
|
2022-11-28 17:09:25 +08:00
|
|
|
|
2023-06-12 17:44:22 +08:00
|
|
|
packetOverHead := tuic.PacketOverHeadV4
|
|
|
|
if len(config.Token) == 0 {
|
|
|
|
packetOverHead = tuic.PacketOverHeadV5
|
|
|
|
}
|
|
|
|
|
2023-06-21 00:40:33 +08:00
|
|
|
if config.CWND == 0 {
|
|
|
|
config.CWND = 32
|
|
|
|
}
|
|
|
|
|
2023-03-16 21:09:44 +08:00
|
|
|
if config.MaxUdpRelayPacketSize == 0 {
|
|
|
|
config.MaxUdpRelayPacketSize = 1500
|
|
|
|
}
|
2023-06-12 17:44:22 +08:00
|
|
|
maxDatagramFrameSize := config.MaxUdpRelayPacketSize + packetOverHead
|
2023-03-16 21:09:44 +08:00
|
|
|
if maxDatagramFrameSize > 1400 {
|
|
|
|
maxDatagramFrameSize = 1400
|
|
|
|
}
|
2023-06-12 17:44:22 +08:00
|
|
|
config.MaxUdpRelayPacketSize = maxDatagramFrameSize - packetOverHead
|
2023-03-16 21:09:44 +08:00
|
|
|
quicConfig.MaxDatagramFrameSize = int64(maxDatagramFrameSize)
|
|
|
|
|
2023-06-12 17:44:22 +08:00
|
|
|
handleTcpFn := func(conn net.Conn, addr socks5.Addr, _additions ...inbound.Addition) error {
|
|
|
|
newAdditions := additions
|
|
|
|
if len(_additions) > 0 {
|
|
|
|
newAdditions = slices.Clone(additions)
|
|
|
|
newAdditions = append(newAdditions, _additions...)
|
|
|
|
}
|
2023-08-21 12:37:39 +08:00
|
|
|
connCtx := inbound.NewSocket(addr, conn, C.TUIC, newAdditions...)
|
|
|
|
metadata := sing.ConvertMetadata(connCtx.Metadata())
|
|
|
|
if h.IsSpecialFqdn(metadata.Destination.Fqdn) {
|
|
|
|
go func() { // ParseSpecialFqdn will block, so open a new goroutine
|
|
|
|
_ = h.ParseSpecialFqdn(
|
|
|
|
sing.WithAdditions(context.Background(), newAdditions...),
|
|
|
|
conn,
|
|
|
|
metadata,
|
|
|
|
)
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
tcpIn <- connCtx
|
2023-06-12 17:44:22 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
handleUdpFn := func(addr socks5.Addr, packet C.UDPPacket, _additions ...inbound.Addition) error {
|
|
|
|
newAdditions := additions
|
|
|
|
if len(_additions) > 0 {
|
|
|
|
newAdditions = slices.Clone(additions)
|
|
|
|
newAdditions = append(newAdditions, _additions...)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case udpIn <- inbound.NewPacket(addr, packet, C.TUIC, newAdditions...):
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return nil
|
2022-11-28 17:09:25 +08:00
|
|
|
}
|
|
|
|
|
2023-06-21 13:53:37 +08:00
|
|
|
option := &tuic.ServerOption{
|
|
|
|
HandleTcpFn: handleTcpFn,
|
|
|
|
HandleUdpFn: handleUdpFn,
|
|
|
|
TlsConfig: tlsConfig,
|
|
|
|
QuicConfig: quicConfig,
|
|
|
|
CongestionController: config.CongestionController,
|
|
|
|
AuthenticationTimeout: time.Duration(config.AuthenticationTimeout) * time.Millisecond,
|
|
|
|
MaxUdpRelayPacketSize: config.MaxUdpRelayPacketSize,
|
|
|
|
CWND: config.CWND,
|
|
|
|
}
|
2023-06-12 17:44:22 +08:00
|
|
|
if len(config.Token) > 0 {
|
|
|
|
tokens := make([][32]byte, len(config.Token))
|
|
|
|
for i, token := range config.Token {
|
|
|
|
tokens[i] = tuic.GenTKN(token)
|
|
|
|
}
|
2023-06-21 13:53:37 +08:00
|
|
|
option.Tokens = tokens
|
|
|
|
}
|
|
|
|
if len(config.Users) > 0 {
|
2023-06-12 17:44:22 +08:00
|
|
|
users := make(map[[16]byte]string)
|
|
|
|
for _uuid, password := range config.Users {
|
|
|
|
users[uuid.FromStringOrNil(_uuid)] = password
|
|
|
|
}
|
2023-06-21 13:53:37 +08:00
|
|
|
option.Users = users
|
2022-11-28 17:09:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sl := &Listener{false, config, nil, nil}
|
|
|
|
|
|
|
|
for _, addr := range strings.Split(config.Listen, ",") {
|
|
|
|
addr := addr
|
|
|
|
|
|
|
|
ul, err := net.ListenPacket("udp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sockopt.UDPReuseaddr(ul.(*net.UDPConn))
|
|
|
|
if err != nil {
|
|
|
|
log.Warnln("Failed to Reuse UDP Address: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sl.udpListeners = append(sl.udpListeners, ul)
|
|
|
|
|
2023-06-21 13:53:37 +08:00
|
|
|
var server *tuic.Server
|
|
|
|
server, err = tuic.NewServer(option, ul)
|
2022-11-28 17:09:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sl.servers = append(sl.servers, server)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := server.Serve()
|
|
|
|
if err != nil {
|
|
|
|
if sl.closed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
return sl, nil
|
|
|
|
}
|
|
|
|
|
2022-12-04 23:05:13 +08:00
|
|
|
func (l *Listener) Close() error {
|
2022-11-28 17:09:25 +08:00
|
|
|
l.closed = true
|
2022-12-04 23:05:13 +08:00
|
|
|
var retErr error
|
2022-11-28 17:09:25 +08:00
|
|
|
for _, lis := range l.servers {
|
2022-12-04 23:05:13 +08:00
|
|
|
err := lis.Close()
|
|
|
|
if err != nil {
|
|
|
|
retErr = err
|
|
|
|
}
|
2022-11-28 17:09:25 +08:00
|
|
|
}
|
|
|
|
for _, lis := range l.udpListeners {
|
2022-12-04 23:05:13 +08:00
|
|
|
err := lis.Close()
|
|
|
|
if err != nil {
|
|
|
|
retErr = err
|
|
|
|
}
|
2022-11-28 17:09:25 +08:00
|
|
|
}
|
2022-12-04 23:05:13 +08:00
|
|
|
return retErr
|
2022-11-28 17:09:25 +08:00
|
|
|
}
|
|
|
|
|
2022-12-04 14:37:52 +08:00
|
|
|
func (l *Listener) Config() LC.TuicServer {
|
2022-11-28 17:09:25 +08:00
|
|
|
return l.config
|
|
|
|
}
|
2022-12-04 23:05:13 +08:00
|
|
|
|
|
|
|
func (l *Listener) AddrList() (addrList []net.Addr) {
|
|
|
|
for _, lis := range l.udpListeners {
|
|
|
|
addrList = append(addrList, lis.LocalAddr())
|
|
|
|
}
|
|
|
|
return
|
2022-12-05 00:20:50 +08:00
|
|
|
}
|