diff --git a/component/updater/update_ui.go b/component/updater/update_ui.go index a43648a9c..b29bee9f9 100644 --- a/component/updater/update_ui.go +++ b/component/updater/update_ui.go @@ -2,7 +2,6 @@ package updater import ( "archive/zip" - "errors" "fmt" "io" "os" @@ -12,23 +11,26 @@ import ( "sync" C "github.com/metacubex/mihomo/constant" + "github.com/metacubex/mihomo/log" ) var ( - ExternalUIURL string - ExternalUIPath string - ExternalUIFolder string - ExternalUIName string -) -var ( - ErrIncompleteConf = errors.New("ExternalUI configure incomplete") + ExternalUIURL string + ExternalUIPath string + AutoUpdateUI bool ) + var xdMutex sync.Mutex func UpdateUI() error { xdMutex.Lock() defer xdMutex.Unlock() + err := prepareUIPath() + if err != nil { + return fmt.Errorf("prepare UI path failed: %w", err) + } + data, err := downloadForBytes(ExternalUIURL) if err != nil { return fmt.Errorf("can't download file: %w", err) @@ -40,7 +42,7 @@ func UpdateUI() error { } defer os.Remove(saved) - err = cleanup(ExternalUIFolder) + err = cleanup(ExternalUIPath) if err != nil { if !os.IsNotExist(err) { return fmt.Errorf("cleanup exist file error: %w", err) @@ -52,27 +54,19 @@ func UpdateUI() error { return fmt.Errorf("can't extract zip file: %w", err) } - err = os.Rename(unzipFolder, ExternalUIFolder) + err = os.Rename(unzipFolder, ExternalUIPath) if err != nil { - return fmt.Errorf("can't rename folder: %w", err) + return fmt.Errorf("rename UI folder failed: %w", err) } return nil } -func PrepareUIPath() error { - if ExternalUIPath == "" || ExternalUIURL == "" { - return ErrIncompleteConf - } - - 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 - } +func prepareUIPath() error { + if _, err := os.Stat(ExternalUIPath); os.IsNotExist(err) { + log.Infoln("dir %s does not exist, creating", ExternalUIPath) + if err := os.MkdirAll(ExternalUIPath, os.ModePerm); err != nil { + log.Warnln("create dir %s error: %s", ExternalUIPath, err) } - } else { - ExternalUIFolder = ExternalUIPath } return nil } diff --git a/config/config.go b/config/config.go index ed30bfe45..4490ebf6b 100644 --- a/config/config.go +++ b/config/config.go @@ -7,7 +7,6 @@ import ( "net" "net/netip" "net/url" - "os" "path" "regexp" "strings" @@ -704,33 +703,23 @@ func parseGeneral(cfg *RawConfig) (*General, error) { } N.DisableKeepAlive = cfg.DisableKeepAlive - updater.ExternalUIPath = cfg.ExternalUI // checkout externalUI exist - if updater.ExternalUIPath != "" { - updater.ExternalUIPath = C.Path.Resolve(updater.ExternalUIPath) - if _, err := os.Stat(updater.ExternalUIPath); os.IsNotExist(err) { - defaultUIpath := path.Join(C.Path.HomeDir(), "ui") - log.Warnln("external-ui: %s does not exist, creating folder in %s", updater.ExternalUIPath, defaultUIpath) - if err := os.MkdirAll(defaultUIpath, os.ModePerm); err != nil { - return nil, err - } - updater.ExternalUIPath = defaultUIpath - cfg.ExternalUI = defaultUIpath - } - } - // checkout UIpath/name exist - if cfg.ExternalUIName != "" { - updater.ExternalUIName = cfg.ExternalUIName + if cfg.ExternalUI != "" { + updater.AutoUpdateUI = true + updater.ExternalUIPath = C.Path.Resolve(cfg.ExternalUI) } else { - updater.ExternalUIFolder = updater.ExternalUIPath - } - if cfg.ExternalUIURL != "" { - updater.ExternalUIURL = cfg.ExternalUIURL + // default externalUI path + updater.ExternalUIPath = path.Join(C.Path.HomeDir(), "ui") } - err := updater.PrepareUIPath() - if err != nil { - log.Errorln("PrepareUIPath error: %s", err) + // checkout UIpath/name exist + if cfg.ExternalUIName != "" { + updater.AutoUpdateUI = true + updater.ExternalUIPath = path.Join(updater.ExternalUIPath, cfg.ExternalUIName) + } + + if cfg.ExternalUIURL != "" { + updater.ExternalUIURL = cfg.ExternalUIURL } return &General{ diff --git a/hub/executor/executor.go b/hub/executor/executor.go index e7e9b72c4..c83b254c9 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -381,12 +381,12 @@ func updateTunnels(tunnels []LC.Tunnel) { } func initExternalUI() { - if updater.ExternalUIFolder != "" { - dirEntries, _ := os.ReadDir(updater.ExternalUIFolder) + if updater.AutoUpdateUI { + dirEntries, _ := os.ReadDir(updater.ExternalUIPath) if len(dirEntries) > 0 { - log.Infoln("UI already exists") + log.Infoln("UI already exists, skip downloading") } else { - log.Infoln("UI not exists, downloading") + log.Infoln("External UI downloading ...") updater.UpdateUI() } } diff --git a/hub/route/upgrade.go b/hub/route/upgrade.go index db00af5c8..76ac26a10 100644 --- a/hub/route/upgrade.go +++ b/hub/route/upgrade.go @@ -1,7 +1,6 @@ package route import ( - "errors" "fmt" "net/http" "os" @@ -50,15 +49,9 @@ func upgradeCore(w http.ResponseWriter, r *http.Request) { func updateUI(w http.ResponseWriter, r *http.Request) { err := updater.UpdateUI() if err != nil { - if errors.Is(err, updater.ErrIncompleteConf) { - 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))) - } + log.Warnln("%s", err) + render.Status(r, http.StatusInternalServerError) + render.JSON(w, r, newError(fmt.Sprintf("%s", err))) return }