diff --git a/config/config.go b/config/config.go index d0e69e49..fd1a4116 100644 --- a/config/config.go +++ b/config/config.go @@ -9,7 +9,6 @@ import ( "net/url" "os" "path" - "path/filepath" "regexp" "strings" "time" @@ -582,9 +581,6 @@ func parseGeneral(cfg *RawConfig) (*General, error) { N.KeepAliveInterval = time.Duration(cfg.KeepAliveInterval) * time.Second } - if cfg.ExternalUIURL != "" { - ExternalUIURL = cfg.ExternalUIURL - } ExternalUIPath = cfg.ExternalUI // checkout externalUI exist if ExternalUIPath != "" { @@ -602,15 +598,12 @@ func parseGeneral(cfg *RawConfig) (*General, error) { // checkout UIpath/name exist if cfg.ExternalUIName != "" { ExternalUIName = cfg.ExternalUIName - ExternalUIFolder = filepath.Clean(path.Join(ExternalUIPath, cfg.ExternalUIName)) - if _, err := os.Stat(ExternalUIPath); os.IsNotExist(err) { - if err := os.MkdirAll(ExternalUIPath, os.ModePerm); err != nil { - return nil, err - } - } } else { ExternalUIFolder = ExternalUIPath } + if cfg.ExternalUIURL != "" { + ExternalUIURL = cfg.ExternalUIURL + } cfg.Tun.RedirectToTun = cfg.EBpf.RedirectToTun return &General{ diff --git a/config/update_ui.go b/config/update_ui.go index 5bc2596e..3526836e 100644 --- a/config/update_ui.go +++ b/config/update_ui.go @@ -2,6 +2,7 @@ package config import ( "archive/zip" + "errors" "fmt" "io" "os" @@ -19,15 +20,18 @@ var ( ExternalUIFolder string ExternalUIName string ) - +var ( + ErrImcompleteConf = errors.New("ExternalUI configure incomplete") +) var xdMutex sync.Mutex func UpdateUI() error { xdMutex.Lock() defer xdMutex.Unlock() - if ExternalUIPath == "" || ExternalUIFolder == "" { - return fmt.Errorf("ExternalUI configure incomplete") + err := prepare() + if err != nil { + return err } data, err := downloadForBytes(ExternalUIURL) @@ -60,6 +64,25 @@ func UpdateUI() error { return nil } +func prepare() error { + if ExternalUIPath == "" || ExternalUIURL == "" { + return ErrImcompleteConf + } + + if ExternalUIName != "" { + ExternalUIFolder = filepath.Clean(path.Join(ExternalUIPath, ExternalUIName)) + if _, err := os.Stat(ExternalUIPath); os.IsNotExist(err) { + if err := os.MkdirAll(ExternalUIPath, os.ModePerm); err != nil { + return err + } + } + } else { + ExternalUIFolder = ExternalUIPath + } + + return nil +} + func unzip(src, dest string) (string, error) { r, err := zip.OpenReader(src) if err != nil { diff --git a/hub/executor/executor.go b/hub/executor/executor.go index 88cdfd6c..d1636754 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -2,7 +2,6 @@ package executor import ( "fmt" - "github.com/Dreamacro/clash/ntp" "net" "net/netip" "os" @@ -12,6 +11,8 @@ import ( "sync" "time" + "github.com/Dreamacro/clash/ntp" + "github.com/Dreamacro/clash/adapter" "github.com/Dreamacro/clash/adapter/inbound" "github.com/Dreamacro/clash/adapter/outboundgroup" @@ -142,6 +143,7 @@ func GetGeneral() *config.General { AllowLan: listener.AllowLan(), BindAddress: listener.BindAddress(), }, + Controller: config.Controller{}, Mode: tunnel.Mode(), LogLevel: log.Level(), IPv6: !resolver.DisableIPv6, diff --git a/hub/route/upgrade.go b/hub/route/upgrade.go index be226616..28acb23a 100644 --- a/hub/route/upgrade.go +++ b/hub/route/upgrade.go @@ -1,6 +1,7 @@ package route import ( + "errors" "fmt" "net/http" "os" @@ -15,12 +16,12 @@ import ( func upgradeRouter() http.Handler { r := chi.NewRouter() - r.Post("/", upgrade) + r.Post("/", upgradeCore) r.Post("/ui", updateUI) return r } -func upgrade(w http.ResponseWriter, r *http.Request) { +func upgradeCore(w http.ResponseWriter, r *http.Request) { // modify from https://github.com/AdguardTeam/AdGuardHome/blob/595484e0b3fb4c457f9bb727a6b94faa78a66c5f/internal/home/controlupdate.go#L108 log.Infoln("start update") execPath, err := os.Executable() @@ -49,9 +50,15 @@ func upgrade(w http.ResponseWriter, r *http.Request) { func updateUI(w http.ResponseWriter, r *http.Request) { err := config.UpdateUI() if err != nil { - log.Warnln("%s", err) - render.Status(r, http.StatusInternalServerError) - render.JSON(w, r, newError(fmt.Sprintf("%s", err))) + if errors.Is(err, config.ErrImcompleteConf) { + log.Warnln("%s", err) + render.Status(r, http.StatusNotImplemented) + render.JSON(w, r, newError(fmt.Sprintf("%s", err))) + } else { + log.Warnln("%s", err) + render.Status(r, http.StatusInternalServerError) + render.JSON(w, r, newError(fmt.Sprintf("%s", err))) + } return }