From 01e382285d49aa12c76f1bddc2298e443ed7f5c1 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Mon, 28 Nov 2022 18:53:09 +0800 Subject: [PATCH] chore: tuic-server support restful api patch --- hub/executor/executor.go | 1 + hub/route/configs.go | 53 +++++++++++++++++++++++++++++++++++++--- listener/listener.go | 15 ++++++++++-- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/hub/executor/executor.go b/hub/executor/executor.go index dfd3a75d..0dece55c 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -124,6 +124,7 @@ func GetGeneral() *config.General { IPv6: !resolver.DisableIPv6, GeodataLoader: G.LoaderName(), Tun: P.GetTunConf(), + TuicServer: P.GetTuicConf(), Interface: dialer.DefaultInterface.Load(), Sniffing: tunnel.IsSniffing(), TCPConcurrent: dialer.GetDial(), diff --git a/hub/route/configs.go b/hub/route/configs.go index 3563555c..cf0a6a9c 100644 --- a/hub/route/configs.go +++ b/hub/route/configs.go @@ -41,7 +41,7 @@ type configSchema struct { TProxyPort *int `json:"tproxy-port"` MixedPort *int `json:"mixed-port"` Tun *tunSchema `json:"tun"` - TuicServer *config.TuicServer `json:"tuic-server"` + TuicServer *tuicServerSchema `json:"tuic-server"` ShadowSocksConfig *string `json:"ss-config"` VmessConfig *string `json:"vmess-config"` TcptunConfig *string `json:"tcptun-config"` @@ -82,6 +82,19 @@ type tunSchema struct { UDPTimeout *int64 `yaml:"udp-timeout" json:"udp-timeout,omitempty"` } +type tuicServerSchema struct { + Enable bool `yaml:"enable" json:"enable"` + Listen *string `yaml:"listen" json:"listen"` + Token *[]string `yaml:"token" json:"token"` + Certificate *string `yaml:"certificate" json:"certificate"` + PrivateKey *string `yaml:"private-key" json:"private-key"` + CongestionController *string `yaml:"congestion-controller" json:"congestion-controller,omitempty"` + MaxIdleTime *int `yaml:"max-idle-time" json:"max-idle-time,omitempty"` + AuthenticationTimeout *int `yaml:"authentication-timeout" json:"authentication-timeout,omitempty"` + ALPN *[]string `yaml:"alpn" json:"alpn,omitempty"` + MaxUdpRelayPacketSize *int `yaml:"max-udp-relay-packet-size" json:"max-udp-relay-packet-size,omitempty"` +} + func getConfigs(w http.ResponseWriter, r *http.Request) { general := executor.GetGeneral() render.JSON(w, r, general) @@ -161,6 +174,40 @@ func pointerOrDefaultTun(p *tunSchema, def config.Tun) config.Tun { return def } +func pointerOrDefaultTuicServer(p *tuicServerSchema, def config.TuicServer) config.TuicServer { + if p != nil { + def.Enable = p.Enable + if p.Listen != nil { + def.Listen = *p.Listen + } + if p.Token != nil { + def.Token = *p.Token + } + if p.Certificate != nil { + def.Certificate = *p.Certificate + } + if p.PrivateKey != nil { + def.PrivateKey = *p.PrivateKey + } + if p.CongestionController != nil { + def.CongestionController = *p.CongestionController + } + if p.MaxIdleTime != nil { + def.MaxIdleTime = *p.MaxIdleTime + } + if p.AuthenticationTimeout != nil { + def.AuthenticationTimeout = *p.AuthenticationTimeout + } + if p.ALPN != nil { + def.ALPN = *p.ALPN + } + if p.MaxUdpRelayPacketSize != nil { + def.MaxUdpRelayPacketSize = *p.MaxUdpRelayPacketSize + } + } + return def +} + func patchConfigs(w http.ResponseWriter, r *http.Request) { general := &configSchema{} if err := render.DecodeJSON(r.Body, general); err != nil { @@ -204,9 +251,7 @@ func patchConfigs(w http.ResponseWriter, r *http.Request) { P.ReCreateVmess(pointerOrDefaultString(general.VmessConfig, ports.VmessConfig), tcpIn, udpIn) P.ReCreateTcpTun(pointerOrDefaultString(general.TcptunConfig, ports.TcpTunConfig), tcpIn, udpIn) P.ReCreateUdpTun(pointerOrDefaultString(general.UdptunConfig, ports.UdpTunConfig), tcpIn, udpIn) - if general.TuicServer != nil { - P.ReCreateTuic(*general.TuicServer, tcpIn, udpIn) - } + P.ReCreateTuic(pointerOrDefaultTuicServer(general.TuicServer, P.LastTuicConf), tcpIn, udpIn) if general.Mode != nil { tunnel.SetMode(*general.Mode) diff --git a/listener/listener.go b/listener/listener.go index ff8fb37c..ccda571e 100644 --- a/listener/listener.go +++ b/listener/listener.go @@ -64,7 +64,8 @@ var ( autoRedirMux sync.Mutex tcMux sync.Mutex - LastTunConf config.Tun + LastTunConf config.Tun + LastTuicConf config.TuicServer ) type Ports struct { @@ -88,6 +89,13 @@ func GetTunConf() config.Tun { return tunLister.Config() } +func GetTuicConf() config.TuicServer { + if tuicListener == nil { + return config.TuicServer{Enable: false} + } + return tuicListener.Config() +} + func AllowLan() bool { return allowLan } @@ -395,7 +403,10 @@ func ReCreateUdpTun(config string, tcpIn chan<- C.ConnContext, udpIn chan<- *inb func ReCreateTuic(config config.TuicServer, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) { tuicMux.Lock() - defer tuicMux.Unlock() + defer func() { + LastTuicConf = config + tuicMux.Unlock() + }() shouldIgnore := false var err error