mihomo/hub/configs.go

50 lines
863 B
Go
Raw Normal View History

2018-06-20 22:41:02 +08:00
package hub
import (
"net/http"
"github.com/Dreamacro/clash/tunnel"
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()
r.Put("/", updateConfig)
return r
}
type General struct {
Mode string `json:mode`
}
2018-06-20 22:41:02 +08:00
var modeMapping = map[string]tunnel.Mode{
"global": tunnel.Global,
"rule": tunnel.Rule,
"direct": tunnel.Direct,
2018-06-20 22:41:02 +08:00
}
func updateConfig(w http.ResponseWriter, r *http.Request) {
general := &General{}
err := render.DecodeJSON(r.Body, general)
2018-06-20 22:41:02 +08:00
if err != nil {
w.WriteHeader(http.StatusBadRequest)
render.JSON(w, r, Error{
Error: "Format error",
})
return
}
mode, ok := modeMapping[general.Mode]
if !ok {
w.WriteHeader(http.StatusBadRequest)
2018-06-20 22:41:02 +08:00
render.JSON(w, r, Error{
Error: "Mode error",
2018-06-20 22:41:02 +08:00
})
return
}
tun.SetMode(mode)
2018-06-20 22:41:02 +08:00
w.WriteHeader(http.StatusNoContent)
}