Skip to content

Commit

Permalink
chore: tuic-server support restful api patch
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Nov 28, 2022
1 parent 4b1d4a3 commit 01e3822
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
1 change: 1 addition & 0 deletions hub/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
53 changes: 49 additions & 4 deletions hub/route/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 13 additions & 2 deletions listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ var (
autoRedirMux sync.Mutex
tcMux sync.Mutex

LastTunConf config.Tun
LastTunConf config.Tun
LastTuicConf config.TuicServer
)

type Ports struct {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 01e3822

Please sign in to comment.