Skip to content

Commit

Permalink
chore: auto download external UI when 'external-ui' is set and not empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Larvan2 committed Aug 13, 2024
1 parent 5bf2242 commit 50d0cd3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion component/updater/update_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions component/updater/update_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -64,7 +59,7 @@ func UpdateUI() error {
return nil
}

func prepare_ui() error {
func PrepareUIPath() error {
if ExternalUIPath == "" || ExternalUIURL == "" {
return ErrIncompleteConf
}
Expand All @@ -79,7 +74,6 @@ func prepare_ui() error {
} else {
ExternalUIFolder = ExternalUIPath
}

return nil
}

Expand Down
5 changes: 5 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions hub/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -113,6 +114,7 @@ func ApplyConfig(cfg *config.Config, force bool) {
runtime.GC()
tunnel.OnRunning()
hcCompatibleProvider(cfg.Providers)
initExternalUI()

log.SetLevel(cfg.General.LogLevel)
}
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions hub/route/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,15 +402,15 @@ 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
}

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
Expand Down

0 comments on commit 50d0cd3

Please sign in to comment.