Clash.Meta/listener/tuic/server.go

146 lines
3.5 KiB
Go
Raw Normal View History

2022-11-28 17:09:25 +08:00
package tuic
import (
"crypto/tls"
2022-12-04 13:37:14 +08:00
"encoding/json"
2022-11-28 17:09:25 +08:00
"net"
"strings"
"time"
"github.com/metacubex/quic-go"
"github.com/Dreamacro/clash/common/sockopt"
C "github.com/Dreamacro/clash/constant"
2022-12-04 13:37:14 +08:00
"github.com/Dreamacro/clash/adapter/inbound"
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"
)
2022-12-04 13:37:14 +08:00
type TuicServer struct {
Enable bool
Listen string
Token []string
Certificate string
PrivateKey string
CongestionController string
MaxIdleTime int
AuthenticationTimeout int
ALPN []string
MaxUdpRelayPacketSize int
}
func (t TuicServer) String() string {
b, _ := json.Marshal(t)
return string(b)
}
2022-11-28 17:09:25 +08:00
type Listener struct {
closed bool
2022-12-04 13:37:14 +08:00
config TuicServer
2022-11-28 17:09:25 +08:00
udpListeners []net.PacketConn
servers []*tuic.Server
}
2022-12-04 13:37:14 +08:00
func New(config TuicServer, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) (*Listener, error) {
2022-11-28 17:09:25 +08:00
cert, err := tls.LoadX509KeyPair(config.Certificate, config.PrivateKey)
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,
MaxIncomingStreams: 1 >> 32,
MaxIncomingUniStreams: 1 >> 32,
EnableDatagrams: true,
}
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
tokens := make([][32]byte, len(config.Token))
for i, token := range config.Token {
tokens[i] = tuic.GenTKN(token)
}
option := &tuic.ServerOption{
HandleTcpFn: func(conn net.Conn, addr socks5.Addr) error {
tcpIn <- inbound.NewSocket(addr, conn, C.TUIC)
2022-11-28 17:09:25 +08:00
return nil
},
HandleUdpFn: func(addr socks5.Addr, packet C.UDPPacket) error {
2022-11-28 17:09:25 +08:00
select {
case udpIn <- inbound.NewPacket(addr, packet, C.TUIC):
2022-11-28 17:09:25 +08:00
default:
}
return nil
},
TlsConfig: tlsConfig,
QuicConfig: quicConfig,
Tokens: tokens,
CongestionController: config.CongestionController,
AuthenticationTimeout: time.Duration(config.AuthenticationTimeout) * time.Millisecond,
MaxUdpRelayPacketSize: config.MaxUdpRelayPacketSize,
}
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)
server, err := tuic.NewServer(option, ul)
if err != nil {
return nil, err
}
sl.servers = append(sl.servers, server)
go func() {
log.Infoln("Tuic proxy listening at: %s", ul.LocalAddr().String())
err := server.Serve()
if err != nil {
if sl.closed {
return
}
}
}()
}
return sl, nil
}
func (l *Listener) Close() {
l.closed = true
for _, lis := range l.servers {
_ = lis.Close()
}
for _, lis := range l.udpListeners {
_ = lis.Close()
}
}
2022-12-04 13:37:14 +08:00
func (l *Listener) Config() TuicServer {
2022-11-28 17:09:25 +08:00
return l.config
}