Skip to content

Commit 50d0cd3

Browse files
committed
chore: auto download external UI when 'external-ui' is set and not empty
1 parent 5bf2242 commit 50d0cd3

File tree

5 files changed

+23
-10
lines changed

5 files changed

+23
-10
lines changed

component/updater/update_core.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func clean() {
230230

231231
// MaxPackageFileSize is a maximum package file length in bytes. The largest
232232
// package whose size is limited by this constant currently has the size of
233-
// approximately 9 MiB.
233+
// approximately 32 MiB.
234234
const MaxPackageFileSize = 32 * 1024 * 1024
235235

236236
// Download package file and save it to disk

component/updater/update_ui.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ func UpdateUI() error {
2929
xdMutex.Lock()
3030
defer xdMutex.Unlock()
3131

32-
err := prepare_ui()
33-
if err != nil {
34-
return err
35-
}
36-
3732
data, err := downloadForBytes(ExternalUIURL)
3833
if err != nil {
3934
return fmt.Errorf("can't download file: %w", err)
@@ -64,7 +59,7 @@ func UpdateUI() error {
6459
return nil
6560
}
6661

67-
func prepare_ui() error {
62+
func PrepareUIPath() error {
6863
if ExternalUIPath == "" || ExternalUIURL == "" {
6964
return ErrIncompleteConf
7065
}
@@ -79,7 +74,6 @@ func prepare_ui() error {
7974
} else {
8075
ExternalUIFolder = ExternalUIPath
8176
}
82-
8377
return nil
8478
}
8579

config/config.go

+5
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,11 @@ func parseGeneral(cfg *RawConfig) (*General, error) {
677677
updater.ExternalUIURL = cfg.ExternalUIURL
678678
}
679679

680+
err := updater.PrepareUIPath()
681+
if err != nil {
682+
log.Errorln("PrepareUIPath error: %s", err)
683+
}
684+
680685
return &General{
681686
Inbound: Inbound{
682687
Port: cfg.Port,

hub/executor/executor.go

+14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/metacubex/mihomo/component/resolver"
2424
SNI "github.com/metacubex/mihomo/component/sniffer"
2525
"github.com/metacubex/mihomo/component/trie"
26+
"github.com/metacubex/mihomo/component/updater"
2627
"github.com/metacubex/mihomo/config"
2728
C "github.com/metacubex/mihomo/constant"
2829
"github.com/metacubex/mihomo/constant/features"
@@ -113,6 +114,7 @@ func ApplyConfig(cfg *config.Config, force bool) {
113114
runtime.GC()
114115
tunnel.OnRunning()
115116
hcCompatibleProvider(cfg.Providers)
117+
initExternalUI()
116118

117119
log.SetLevel(cfg.General.LogLevel)
118120
}
@@ -385,6 +387,18 @@ func updateTunnels(tunnels []LC.Tunnel) {
385387
listener.PatchTunnel(tunnels, tunnel.Tunnel)
386388
}
387389

390+
func initExternalUI() {
391+
if updater.ExternalUIFolder != "" {
392+
dirEntries, _ := os.ReadDir(updater.ExternalUIFolder)
393+
if len(dirEntries) > 0 {
394+
log.Infoln("UI already exists")
395+
} else {
396+
log.Infoln("UI not exists, downloading")
397+
updater.UpdateUI()
398+
}
399+
}
400+
}
401+
388402
func updateGeneral(general *config.General) {
389403
tunnel.SetMode(general.Mode)
390404
tunnel.SetFindProcessMode(general.FindProcessMode)

hub/route/configs.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -402,15 +402,15 @@ func updateConfigs(w http.ResponseWriter, r *http.Request) {
402402
func updateGeoDatabases(w http.ResponseWriter, r *http.Request) {
403403
err := updater.UpdateGeoDatabases()
404404
if err != nil {
405-
log.Errorln("[REST-API] update GEO databases failed: %v", err)
405+
log.Errorln("[GEO] update GEO databases failed: %v", err)
406406
render.Status(r, http.StatusInternalServerError)
407407
render.JSON(w, r, newError(err.Error()))
408408
return
409409
}
410410

411411
cfg, err := executor.ParseWithPath(C.Path.Config())
412412
if err != nil {
413-
log.Errorln("[REST-API] update GEO databases failed: %v", err)
413+
log.Errorln("[GEO] update GEO databases failed: %v", err)
414414
render.Status(r, http.StatusInternalServerError)
415415
render.JSON(w, r, newError("Error parsing configuration"))
416416
return

0 commit comments

Comments
 (0)