diff --git a/config/config.go b/config/config.go index 94e1a872..20f7f1d8 100644 --- a/config/config.go +++ b/config/config.go @@ -189,19 +189,19 @@ func (c *Config) UpdateProxy(pc ProxyConfig) { c.general.AllowLan = *pc.AllowLan } - if (pc.AllowLan != nil || pc.Port != nil) && *pc.Port != 0 { - c.general.Port = *pc.Port - c.event <- &Event{Type: "http-addr", Payload: genAddr(*pc.Port, c.general.AllowLan)} + c.general.Port = *or(pc.Port, &c.general.Port) + if c.general.Port != 0 && (pc.AllowLan != nil || pc.Port != nil) { + c.event <- &Event{Type: "http-addr", Payload: genAddr(c.general.Port, c.general.AllowLan)} } - if (pc.AllowLan != nil || pc.SocksPort != nil) && *pc.SocksPort != 0 { - c.general.SocksPort = *pc.SocksPort - c.event <- &Event{Type: "socks-addr", Payload: genAddr(*pc.SocksPort, c.general.AllowLan)} + c.general.SocksPort = *or(pc.SocksPort, &c.general.SocksPort) + if c.general.SocksPort != 0 && (pc.AllowLan != nil || pc.SocksPort != nil) { + c.event <- &Event{Type: "socks-addr", Payload: genAddr(c.general.SocksPort, c.general.AllowLan)} } - if (pc.AllowLan != nil || pc.RedirPort != nil) && *pc.RedirPort != 0 { - c.general.RedirPort = *pc.RedirPort - c.event <- &Event{Type: "redir-addr", Payload: genAddr(*pc.RedirPort, c.general.AllowLan)} + c.general.RedirPort = *or(pc.RedirPort, &c.general.RedirPort) + if c.general.RedirPort != 0 && (pc.AllowLan != nil || pc.RedirPort != nil) { + c.event <- &Event{Type: "redir-addr", Payload: genAddr(c.general.RedirPort, c.general.AllowLan)} } } diff --git a/config/utils.go b/config/utils.go index 793fcfe7..e196394b 100644 --- a/config/utils.go +++ b/config/utils.go @@ -18,3 +18,12 @@ func genAddr(port int, allowLan bool) string { } return fmt.Sprintf("127.0.0.1:%d", port) } + +func or(pointers ...*int) *int { + for _, p := range pointers { + if p != nil { + return p + } + } + return pointers[len(pointers)-1] +} diff --git a/constant/config.go b/constant/config.go index b0937587..90df19ba 100644 --- a/constant/config.go +++ b/constant/config.go @@ -23,6 +23,7 @@ type General struct { AllowLan *bool `json:"allow-lan,omitempty"` Port *int `json:"port,omitempty"` SocksPort *int `json:"socks-port,omitempty"` + RedirPort *int `json:"redir-port,omitempty"` LogLevel *string `json:"log-level,omitempty"` } diff --git a/hub/configs.go b/hub/configs.go index 2cbe2e3c..49964837 100644 --- a/hub/configs.go +++ b/hub/configs.go @@ -21,6 +21,7 @@ func configRouter() http.Handler { type configSchema struct { Port int `json:"port"` SocksPort int `json:"socket-port"` + RedirPort int `json:"redir-port"` AllowLan bool `json:"allow-lan"` Mode string `json:"mode"` LogLevel string `json:"log-level"` @@ -31,6 +32,7 @@ func getConfigs(w http.ResponseWriter, r *http.Request) { render.JSON(w, r, configSchema{ Port: general.Port, SocksPort: general.SocksPort, + RedirPort: general.RedirPort, AllowLan: general.AllowLan, Mode: general.Mode.String(), LogLevel: general.LogLevel.String(), @@ -87,6 +89,7 @@ func updateConfigs(w http.ResponseWriter, r *http.Request) { AllowLan: general.AllowLan, Port: general.Port, SocksPort: general.SocksPort, + RedirPort: general.RedirPort, }) w.WriteHeader(http.StatusNoContent)