2018-11-21 13:47:46 +08:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
2022-05-26 19:49:12 +08:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2018-11-21 13:47:46 +08:00
|
|
|
"net/http"
|
2018-11-30 17:42:40 +08:00
|
|
|
"path/filepath"
|
2022-05-24 15:04:13 +08:00
|
|
|
"sync"
|
2018-11-21 13:47:46 +08:00
|
|
|
|
2021-02-05 16:43:42 +08:00
|
|
|
"github.com/Dreamacro/clash/component/resolver"
|
2019-12-01 13:22:47 +08:00
|
|
|
"github.com/Dreamacro/clash/config"
|
2021-06-21 17:33:34 +08:00
|
|
|
"github.com/Dreamacro/clash/constant"
|
2018-11-21 13:47:46 +08:00
|
|
|
"github.com/Dreamacro/clash/hub/executor"
|
2021-06-13 17:23:10 +08:00
|
|
|
P "github.com/Dreamacro/clash/listener"
|
2018-11-21 13:47:46 +08:00
|
|
|
"github.com/Dreamacro/clash/log"
|
2020-02-15 21:42:46 +08:00
|
|
|
"github.com/Dreamacro/clash/tunnel"
|
2018-11-21 13:47:46 +08:00
|
|
|
|
2021-04-03 14:59:03 +08:00
|
|
|
"github.com/go-chi/chi/v5"
|
2018-11-21 13:47:46 +08:00
|
|
|
"github.com/go-chi/render"
|
|
|
|
)
|
|
|
|
|
2022-05-24 15:04:13 +08:00
|
|
|
var (
|
|
|
|
updateGeoMux sync.Mutex
|
|
|
|
updatingGeo = false
|
|
|
|
)
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
func configRouter() http.Handler {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
r.Get("/", getConfigs)
|
2018-11-30 17:42:40 +08:00
|
|
|
r.Put("/", updateConfigs)
|
2022-05-24 15:04:13 +08:00
|
|
|
r.Post("/geo", updateGeoDatabases)
|
2018-11-28 10:38:30 +08:00
|
|
|
r.Patch("/", patchConfigs)
|
2018-11-21 13:47:46 +08:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
type configSchema struct {
|
2022-05-26 19:49:12 +08:00
|
|
|
Port *int `json:"port"`
|
|
|
|
SocksPort *int `json:"socks-port"`
|
|
|
|
RedirPort *int `json:"redir-port"`
|
|
|
|
TProxyPort *int `json:"tproxy-port"`
|
|
|
|
MixedPort *int `json:"mixed-port"`
|
|
|
|
Tun *config.Tun `json:"tun"`
|
|
|
|
AllowLan *bool `json:"allow-lan"`
|
|
|
|
BindAddress *string `json:"bind-address"`
|
|
|
|
Mode *tunnel.TunnelMode `json:"mode"`
|
|
|
|
LogLevel *log.LogLevel `json:"log-level"`
|
|
|
|
IPv6 *bool `json:"ipv6"`
|
|
|
|
Sniffing *bool `json:"sniffing"`
|
|
|
|
TcpConcurrent *bool `json:"tcp-concurrent"`
|
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
|
|
|
|
2019-08-08 13:45:07 +08:00
|
|
|
if general.BindAddress != nil {
|
|
|
|
P.SetBindAddress(*general.BindAddress)
|
|
|
|
}
|
|
|
|
|
2022-05-24 12:43:26 +08:00
|
|
|
if general.Sniffing != nil {
|
|
|
|
tunnel.SetSniffing(*general.Sniffing)
|
|
|
|
}
|
|
|
|
|
2022-05-26 19:49:12 +08:00
|
|
|
if general.TcpConcurrent != nil {
|
|
|
|
dialer.SetDial(*general.TcpConcurrent)
|
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
ports := P.GetPorts()
|
2021-06-13 17:23:10 +08:00
|
|
|
|
|
|
|
tcpIn := tunnel.TCPIn()
|
|
|
|
udpIn := tunnel.UDPIn()
|
|
|
|
|
|
|
|
P.ReCreateHTTP(pointerOrDefault(general.Port, ports.Port), tcpIn)
|
|
|
|
P.ReCreateSocks(pointerOrDefault(general.SocksPort, ports.SocksPort), tcpIn, udpIn)
|
|
|
|
P.ReCreateRedir(pointerOrDefault(general.RedirPort, ports.RedirPort), tcpIn, udpIn)
|
|
|
|
P.ReCreateTProxy(pointerOrDefault(general.TProxyPort, ports.TProxyPort), tcpIn, udpIn)
|
|
|
|
P.ReCreateMixed(pointerOrDefault(general.MixedPort, ports.MixedPort), tcpIn, udpIn)
|
2018-11-21 13:47:46 +08:00
|
|
|
|
|
|
|
if general.Mode != nil {
|
2020-02-15 21:42:46 +08:00
|
|
|
tunnel.SetMode(*general.Mode)
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if general.LogLevel != nil {
|
|
|
|
log.SetLevel(*general.LogLevel)
|
|
|
|
}
|
|
|
|
|
2021-02-05 16:43:42 +08:00
|
|
|
if general.IPv6 != nil {
|
|
|
|
resolver.DisableIPv6 = !*general.IPv6
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-12-01 13:22:47 +08:00
|
|
|
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"
|
2019-12-01 13:22:47 +08:00
|
|
|
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 {
|
2021-06-21 17:33:34 +08:00
|
|
|
if req.Path == "" {
|
|
|
|
req.Path = constant.Path.Config()
|
|
|
|
}
|
2019-12-01 13:22:47 +08:00
|
|
|
if !filepath.IsAbs(req.Path) {
|
|
|
|
render.Status(r, http.StatusBadRequest)
|
2020-10-14 19:56:02 +08:00
|
|
|
render.JSON(w, r, newError("path is not a absolute path"))
|
2019-12-01 13:22:47 +08:00
|
|
|
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
|
|
|
}
|
2022-05-24 15:04:13 +08:00
|
|
|
|
|
|
|
func updateGeoDatabases(w http.ResponseWriter, r *http.Request) {
|
|
|
|
updateGeoMux.Lock()
|
|
|
|
|
|
|
|
if updatingGeo {
|
|
|
|
updateGeoMux.Unlock()
|
|
|
|
render.Status(r, http.StatusBadRequest)
|
|
|
|
render.JSON(w, r, newError("updating..."))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
updatingGeo = true
|
|
|
|
updateGeoMux.Unlock()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
updatingGeo = false
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.Warnln("[REST-API] updating GEO databases...")
|
|
|
|
|
|
|
|
if err := config.UpdateGeoDatabases(); err != nil {
|
|
|
|
log.Errorln("[REST-API] update GEO databases failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, err := executor.ParseWithPath(constant.Path.Config())
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("[REST-API] update GEO databases failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Warnln("[REST-API] update GEO databases successful, apply config...")
|
|
|
|
|
|
|
|
executor.ApplyConfig(cfg, false)
|
|
|
|
}()
|
|
|
|
|
|
|
|
render.NoContent(w, r)
|
|
|
|
}
|