2018-06-20 22:41:02 +08:00
|
|
|
package hub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2018-07-12 23:28:38 +08:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2018-07-12 23:28:38 +08:00
|
|
|
type General struct {
|
|
|
|
Mode string `json:mode`
|
|
|
|
}
|
2018-06-20 22:41:02 +08:00
|
|
|
|
2018-07-12 23:28:38 +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) {
|
2018-07-12 23:28:38 +08:00
|
|
|
general := &General{}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
mode, ok := modeMapping[general.Mode]
|
|
|
|
if !ok {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2018-06-20 22:41:02 +08:00
|
|
|
render.JSON(w, r, Error{
|
2018-07-12 23:28:38 +08:00
|
|
|
Error: "Mode error",
|
2018-06-20 22:41:02 +08:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2018-07-12 23:28:38 +08:00
|
|
|
tun.SetMode(mode)
|
2018-06-20 22:41:02 +08:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|