feat: RESTful API support update Geo file
and can set update url by user, eg. geox-url: geoip: "http://xxxx/gepip.dat" mmdb: "http://xxxx/country.mmdb" geosite: "http://xxxx/geosite.dat"
This commit is contained in:
@ -3,6 +3,7 @@ package route
|
||||
import (
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/Dreamacro/clash/component/resolver"
|
||||
"github.com/Dreamacro/clash/config"
|
||||
@ -16,10 +17,16 @@ import (
|
||||
"github.com/go-chi/render"
|
||||
)
|
||||
|
||||
var (
|
||||
updateGeoMux sync.Mutex
|
||||
updatingGeo = false
|
||||
)
|
||||
|
||||
func configRouter() http.Handler {
|
||||
r := chi.NewRouter()
|
||||
r.Get("/", getConfigs)
|
||||
r.Put("/", updateConfigs)
|
||||
r.Post("/geo", updateGeoDatabases)
|
||||
r.Patch("/", patchConfigs)
|
||||
return r
|
||||
}
|
||||
@ -143,3 +150,42 @@ func updateConfigs(w http.ResponseWriter, r *http.Request) {
|
||||
executor.ApplyConfig(cfg, force)
|
||||
render.NoContent(w, r)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user