2018-06-20 22:41:02 +08:00
|
|
|
package hub
|
|
|
|
|
|
|
|
import (
|
2018-07-15 22:23:20 +08:00
|
|
|
"fmt"
|
2018-06-20 22:41:02 +08:00
|
|
|
"net/http"
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
"github.com/Dreamacro/clash/config"
|
2018-07-15 22:23:20 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/proxy"
|
2018-07-12 23:28:38 +08:00
|
|
|
|
2018-06-20 22:41:02 +08:00
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
)
|
|
|
|
|
|
|
|
func configRouter() http.Handler {
|
|
|
|
r := chi.NewRouter()
|
2018-07-15 22:23:20 +08:00
|
|
|
r.Get("/", getConfigs)
|
|
|
|
r.Put("/", updateConfigs)
|
2018-06-20 22:41:02 +08:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
type configSchema struct {
|
|
|
|
Port int `json:"port"`
|
|
|
|
SocketPort int `json:"socket-port"`
|
|
|
|
AllowLan bool `json:"allow-lan"`
|
|
|
|
Mode string `json:"mode"`
|
|
|
|
LogLevel string `json:"log-level"`
|
2018-07-12 23:28:38 +08:00
|
|
|
}
|
2018-06-20 22:41:02 +08:00
|
|
|
|
2018-07-15 22:23:20 +08:00
|
|
|
func getConfigs(w http.ResponseWriter, r *http.Request) {
|
2018-07-26 00:04:59 +08:00
|
|
|
general := cfg.General()
|
|
|
|
render.JSON(w, r, configSchema{
|
|
|
|
Port: *general.Port,
|
|
|
|
SocketPort: *general.SocketPort,
|
|
|
|
AllowLan: *general.AllowLan,
|
|
|
|
Mode: general.Mode.String(),
|
|
|
|
LogLevel: general.LogLevel.String(),
|
|
|
|
})
|
2018-06-20 22:41:02 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 22:23:20 +08:00
|
|
|
func updateConfigs(w http.ResponseWriter, r *http.Request) {
|
|
|
|
general := &C.General{}
|
2018-07-12 23:28:38 +08:00
|
|
|
err := render.DecodeJSON(r.Body, general)
|
2018-06-20 22:41:02 +08:00
|
|
|
if err != nil {
|
2018-07-12 23:28:38 +08:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
render.JSON(w, r, Error{
|
|
|
|
Error: "Format error",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-15 22:23:20 +08:00
|
|
|
// update errors
|
|
|
|
var proxyErr, modeErr error
|
|
|
|
|
|
|
|
// update proxy
|
|
|
|
listener := proxy.Instance()
|
2018-07-26 00:04:59 +08:00
|
|
|
proxyErr = listener.Update(&config.Base{
|
|
|
|
AllowLan: general.AllowLan,
|
|
|
|
Port: general.Port,
|
|
|
|
SocketPort: general.SocksPort,
|
|
|
|
})
|
2018-07-15 22:23:20 +08:00
|
|
|
|
|
|
|
// update mode
|
|
|
|
if general.Mode != nil {
|
2018-07-26 00:04:59 +08:00
|
|
|
mode, ok := config.ModeMapping[*general.Mode]
|
2018-07-15 22:23:20 +08:00
|
|
|
if !ok {
|
|
|
|
modeErr = fmt.Errorf("Mode error")
|
|
|
|
} else {
|
2018-07-26 00:04:59 +08:00
|
|
|
cfg.SetMode(mode)
|
2018-07-15 22:23:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hasError, errors := formatErrors(map[string]error{
|
|
|
|
"proxy": proxyErr,
|
|
|
|
"mode": modeErr,
|
|
|
|
})
|
|
|
|
|
|
|
|
if hasError {
|
2018-07-12 23:28:38 +08:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2018-07-15 22:23:20 +08:00
|
|
|
render.JSON(w, r, errors)
|
2018-06-20 22:41:02 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|