From 50d0cd363c84c14a4982731d24082e0ee65265f0 Mon Sep 17 00:00:00 2001 From: Larvan2 <78135608+Larvan2@users.noreply.github.com> Date: Tue, 13 Aug 2024 14:19:34 +0800 Subject: [PATCH] chore: auto download external UI when 'external-ui' is set and not empty --- component/updater/update_core.go | 2 +- component/updater/update_ui.go | 8 +------- config/config.go | 5 +++++ hub/executor/executor.go | 14 ++++++++++++++ hub/route/configs.go | 4 ++-- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/component/updater/update_core.go b/component/updater/update_core.go index 0070fbb1..9f13af9c 100644 --- a/component/updater/update_core.go +++ b/component/updater/update_core.go @@ -230,7 +230,7 @@ func clean() { // MaxPackageFileSize is a maximum package file length in bytes. The largest // package whose size is limited by this constant currently has the size of -// approximately 9 MiB. +// approximately 32 MiB. const MaxPackageFileSize = 32 * 1024 * 1024 // Download package file and save it to disk diff --git a/component/updater/update_ui.go b/component/updater/update_ui.go index 85452ba5..a43648a9 100644 --- a/component/updater/update_ui.go +++ b/component/updater/update_ui.go @@ -29,11 +29,6 @@ func UpdateUI() error { xdMutex.Lock() defer xdMutex.Unlock() - err := prepare_ui() - if err != nil { - return err - } - data, err := downloadForBytes(ExternalUIURL) if err != nil { return fmt.Errorf("can't download file: %w", err) @@ -64,7 +59,7 @@ func UpdateUI() error { return nil } -func prepare_ui() error { +func PrepareUIPath() error { if ExternalUIPath == "" || ExternalUIURL == "" { return ErrIncompleteConf } @@ -79,7 +74,6 @@ func prepare_ui() error { } else { ExternalUIFolder = ExternalUIPath } - return nil } diff --git a/config/config.go b/config/config.go index ecd94c7d..5f2b6845 100644 --- a/config/config.go +++ b/config/config.go @@ -677,6 +677,11 @@ func parseGeneral(cfg *RawConfig) (*General, error) { updater.ExternalUIURL = cfg.ExternalUIURL } + err := updater.PrepareUIPath() + if err != nil { + log.Errorln("PrepareUIPath error: %s", err) + } + return &General{ Inbound: Inbound{ Port: cfg.Port, diff --git a/hub/executor/executor.go b/hub/executor/executor.go index 55c40b6d..1e578190 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -23,6 +23,7 @@ import ( "github.com/metacubex/mihomo/component/resolver" SNI "github.com/metacubex/mihomo/component/sniffer" "github.com/metacubex/mihomo/component/trie" + "github.com/metacubex/mihomo/component/updater" "github.com/metacubex/mihomo/config" C "github.com/metacubex/mihomo/constant" "github.com/metacubex/mihomo/constant/features" @@ -113,6 +114,7 @@ func ApplyConfig(cfg *config.Config, force bool) { runtime.GC() tunnel.OnRunning() hcCompatibleProvider(cfg.Providers) + initExternalUI() log.SetLevel(cfg.General.LogLevel) } @@ -385,6 +387,18 @@ func updateTunnels(tunnels []LC.Tunnel) { listener.PatchTunnel(tunnels, tunnel.Tunnel) } +func initExternalUI() { + if updater.ExternalUIFolder != "" { + dirEntries, _ := os.ReadDir(updater.ExternalUIFolder) + if len(dirEntries) > 0 { + log.Infoln("UI already exists") + } else { + log.Infoln("UI not exists, downloading") + updater.UpdateUI() + } + } +} + func updateGeneral(general *config.General) { tunnel.SetMode(general.Mode) tunnel.SetFindProcessMode(general.FindProcessMode) diff --git a/hub/route/configs.go b/hub/route/configs.go index 17d858d4..a4dcaa52 100644 --- a/hub/route/configs.go +++ b/hub/route/configs.go @@ -402,7 +402,7 @@ func updateConfigs(w http.ResponseWriter, r *http.Request) { func updateGeoDatabases(w http.ResponseWriter, r *http.Request) { err := updater.UpdateGeoDatabases() if err != nil { - log.Errorln("[REST-API] update GEO databases failed: %v", err) + log.Errorln("[GEO] update GEO databases failed: %v", err) render.Status(r, http.StatusInternalServerError) render.JSON(w, r, newError(err.Error())) return @@ -410,7 +410,7 @@ func updateGeoDatabases(w http.ResponseWriter, r *http.Request) { cfg, err := executor.ParseWithPath(C.Path.Config()) if err != nil { - log.Errorln("[REST-API] update GEO databases failed: %v", err) + log.Errorln("[GEO] update GEO databases failed: %v", err) render.Status(r, http.StatusInternalServerError) render.JSON(w, r, newError("Error parsing configuration")) return