Fix: updateConfig api crash

This commit is contained in:
Dreamacro 2018-08-26 22:43:38 +08:00
parent ad25a89dd2
commit 2a2e61652f
4 changed files with 22 additions and 9 deletions

View file

@ -189,19 +189,19 @@ func (c *Config) UpdateProxy(pc ProxyConfig) {
c.general.AllowLan = *pc.AllowLan c.general.AllowLan = *pc.AllowLan
} }
if (pc.AllowLan != nil || pc.Port != nil) && *pc.Port != 0 { c.general.Port = *or(pc.Port, &c.general.Port)
c.general.Port = *pc.Port if c.general.Port != 0 && (pc.AllowLan != nil || pc.Port != nil) {
c.event <- &Event{Type: "http-addr", Payload: genAddr(*pc.Port, c.general.AllowLan)} 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 = *or(pc.SocksPort, &c.general.SocksPort)
c.general.SocksPort = *pc.SocksPort if c.general.SocksPort != 0 && (pc.AllowLan != nil || pc.SocksPort != nil) {
c.event <- &Event{Type: "socks-addr", Payload: genAddr(*pc.SocksPort, c.general.AllowLan)} 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 = *or(pc.RedirPort, &c.general.RedirPort)
c.general.RedirPort = *pc.RedirPort if c.general.RedirPort != 0 && (pc.AllowLan != nil || pc.RedirPort != nil) {
c.event <- &Event{Type: "redir-addr", Payload: genAddr(*pc.RedirPort, c.general.AllowLan)} c.event <- &Event{Type: "redir-addr", Payload: genAddr(c.general.RedirPort, c.general.AllowLan)}
} }
} }

View file

@ -18,3 +18,12 @@ func genAddr(port int, allowLan bool) string {
} }
return fmt.Sprintf("127.0.0.1:%d", port) 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]
}

View file

@ -23,6 +23,7 @@ type General struct {
AllowLan *bool `json:"allow-lan,omitempty"` AllowLan *bool `json:"allow-lan,omitempty"`
Port *int `json:"port,omitempty"` Port *int `json:"port,omitempty"`
SocksPort *int `json:"socks-port,omitempty"` SocksPort *int `json:"socks-port,omitempty"`
RedirPort *int `json:"redir-port,omitempty"`
LogLevel *string `json:"log-level,omitempty"` LogLevel *string `json:"log-level,omitempty"`
} }

View file

@ -21,6 +21,7 @@ func configRouter() http.Handler {
type configSchema struct { type configSchema struct {
Port int `json:"port"` Port int `json:"port"`
SocksPort int `json:"socket-port"` SocksPort int `json:"socket-port"`
RedirPort int `json:"redir-port"`
AllowLan bool `json:"allow-lan"` AllowLan bool `json:"allow-lan"`
Mode string `json:"mode"` Mode string `json:"mode"`
LogLevel string `json:"log-level"` LogLevel string `json:"log-level"`
@ -31,6 +32,7 @@ func getConfigs(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, configSchema{ render.JSON(w, r, configSchema{
Port: general.Port, Port: general.Port,
SocksPort: general.SocksPort, SocksPort: general.SocksPort,
RedirPort: general.RedirPort,
AllowLan: general.AllowLan, AllowLan: general.AllowLan,
Mode: general.Mode.String(), Mode: general.Mode.String(),
LogLevel: general.LogLevel.String(), LogLevel: general.LogLevel.String(),
@ -87,6 +89,7 @@ func updateConfigs(w http.ResponseWriter, r *http.Request) {
AllowLan: general.AllowLan, AllowLan: general.AllowLan,
Port: general.Port, Port: general.Port,
SocksPort: general.SocksPort, SocksPort: general.SocksPort,
RedirPort: general.RedirPort,
}) })
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)