clash/hub/route/configs.go

124 lines
2.9 KiB
Go
Raw Normal View History

2018-11-21 13:47:46 +08:00
package route
import (
"net/http"
2018-11-30 17:42:40 +08:00
"path/filepath"
2018-11-21 13:47:46 +08:00
"github.com/Dreamacro/clash/config"
2018-11-21 13:47:46 +08:00
"github.com/Dreamacro/clash/hub/executor"
"github.com/Dreamacro/clash/log"
P "github.com/Dreamacro/clash/proxy"
"github.com/Dreamacro/clash/tunnel"
2018-11-21 13:47:46 +08:00
"github.com/go-chi/chi"
"github.com/go-chi/render"
)
func configRouter() http.Handler {
r := chi.NewRouter()
r.Get("/", getConfigs)
2018-11-30 17:42:40 +08:00
r.Put("/", updateConfigs)
2018-11-28 10:38:30 +08:00
r.Patch("/", patchConfigs)
2018-11-21 13:47:46 +08:00
return r
}
type configSchema struct {
Port *int `json:"port"`
SocksPort *int `json:"socks-port"`
RedirPort *int `json:"redir-port"`
MixedPort *int `json:"mixed-port"`
AllowLan *bool `json:"allow-lan"`
BindAddress *string `json:"bind-address"`
Mode *tunnel.TunnelMode `json:"mode"`
LogLevel *log.LogLevel `json:"log-level"`
2018-11-21 13:47:46 +08:00
}
func getConfigs(w http.ResponseWriter, r *http.Request) {
general := executor.GetGeneral()
2018-12-10 11:33:37 +08:00
render.JSON(w, r, general)
2018-11-21 13:47:46 +08:00
}
2018-11-28 10:38:30 +08:00
func pointerOrDefault(p *int, def int) int {
2018-11-21 13:47:46 +08:00
if p != nil {
return *p
}
return def
}
2018-11-28 10:38:30 +08:00
func patchConfigs(w http.ResponseWriter, r *http.Request) {
2018-11-21 13:47:46 +08:00
general := &configSchema{}
if err := render.DecodeJSON(r.Body, general); err != nil {
2018-12-10 11:33:37 +08:00
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, ErrBadRequest)
2018-11-21 13:47:46 +08:00
return
}
if general.AllowLan != nil {
P.SetAllowLan(*general.AllowLan)
}
2018-11-28 10:38:30 +08:00
if general.BindAddress != nil {
P.SetBindAddress(*general.BindAddress)
}
2018-11-21 13:47:46 +08:00
ports := P.GetPorts()
P.ReCreateHTTP(pointerOrDefault(general.Port, ports.Port))
P.ReCreateSocks(pointerOrDefault(general.SocksPort, ports.SocksPort))
P.ReCreateRedir(pointerOrDefault(general.RedirPort, ports.RedirPort))
P.ReCreateMixed(pointerOrDefault(general.MixedPort, ports.MixedPort))
2018-11-21 13:47:46 +08:00
if general.Mode != nil {
tunnel.SetMode(*general.Mode)
2018-11-21 13:47:46 +08:00
}
if general.LogLevel != nil {
log.SetLevel(*general.LogLevel)
}
2018-12-10 11:33:37 +08:00
render.NoContent(w, r)
2018-11-21 13:47:46 +08:00
}
2018-11-30 17:42:40 +08:00
type updateConfigRequest struct {
Path string `json:"path"`
Payload string `json:"payload"`
2018-11-30 17:42:40 +08:00
}
func updateConfigs(w http.ResponseWriter, r *http.Request) {
req := updateConfigRequest{}
if err := render.DecodeJSON(r.Body, &req); err != nil {
2018-12-10 11:33:37 +08:00
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, ErrBadRequest)
2018-11-30 17:42:40 +08:00
return
}
force := r.URL.Query().Get("force") == "true"
var cfg *config.Config
var err error
if req.Payload != "" {
cfg, err = executor.ParseWithBytes([]byte(req.Payload))
if err != nil {
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, newError(err.Error()))
return
}
} else {
if !filepath.IsAbs(req.Path) {
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, newError("path is not a absoluted path"))
return
}
cfg, err = executor.ParseWithPath(req.Path)
if err != nil {
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, newError(err.Error()))
return
}
2018-11-30 17:42:40 +08:00
}
executor.ApplyConfig(cfg, force)
2018-12-10 11:33:37 +08:00
render.NoContent(w, r)
2018-11-30 17:42:40 +08:00
}