mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2024-11-09 02:41:22 +08:00
chore: rebuild external ui updater
This commit is contained in:
parent
c63a851bba
commit
9286e21026
@ -14,24 +14,69 @@ import (
|
||||
"github.com/metacubex/mihomo/log"
|
||||
)
|
||||
|
||||
var (
|
||||
ExternalUIURL string
|
||||
ExternalUIPath string
|
||||
AutoDownloadUI bool
|
||||
)
|
||||
type UIUpdater struct {
|
||||
externalUIURL string
|
||||
externalUIPath string
|
||||
autoDownloadUI bool
|
||||
|
||||
var xdMutex sync.Mutex
|
||||
mutex sync.Mutex
|
||||
}
|
||||
|
||||
func DownloadUI() error {
|
||||
xdMutex.Lock()
|
||||
defer xdMutex.Unlock()
|
||||
var DefaultUiUpdater = &UIUpdater{}
|
||||
|
||||
err := prepareUIPath()
|
||||
func NewUiUpdater(externalUI, externalUIURL, externalUIName string) *UIUpdater {
|
||||
updater := &UIUpdater{}
|
||||
// checkout externalUI exist
|
||||
if externalUI != "" {
|
||||
updater.autoDownloadUI = true
|
||||
updater.externalUIPath = C.Path.Resolve(externalUI)
|
||||
} else {
|
||||
// default externalUI path
|
||||
updater.externalUIPath = path.Join(C.Path.HomeDir(), "ui")
|
||||
}
|
||||
|
||||
// checkout UIpath/name exist
|
||||
if externalUIName != "" {
|
||||
updater.autoDownloadUI = true
|
||||
updater.externalUIPath = path.Join(updater.externalUIPath, externalUIName)
|
||||
}
|
||||
|
||||
if externalUIURL != "" {
|
||||
updater.externalUIURL = externalUIURL
|
||||
}
|
||||
return updater
|
||||
}
|
||||
|
||||
func (u *UIUpdater) AutoDownloadUI() {
|
||||
u.mutex.Lock()
|
||||
defer u.mutex.Unlock()
|
||||
if u.autoDownloadUI {
|
||||
dirEntries, _ := os.ReadDir(u.externalUIPath)
|
||||
if len(dirEntries) > 0 {
|
||||
log.Infoln("UI already exists, skip downloading")
|
||||
} else {
|
||||
log.Infoln("External UI downloading ...")
|
||||
err := u.downloadUI()
|
||||
if err != nil {
|
||||
log.Errorln("Error downloading UI:", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (u *UIUpdater) DownloadUI() error {
|
||||
u.mutex.Lock()
|
||||
defer u.mutex.Unlock()
|
||||
return u.downloadUI()
|
||||
}
|
||||
|
||||
func (u *UIUpdater) downloadUI() error {
|
||||
err := u.prepareUIPath()
|
||||
if err != nil {
|
||||
return fmt.Errorf("prepare UI path failed: %w", err)
|
||||
}
|
||||
|
||||
data, err := downloadForBytes(ExternalUIURL)
|
||||
data, err := downloadForBytes(u.externalUIURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't download file: %w", err)
|
||||
}
|
||||
@ -42,7 +87,7 @@ func DownloadUI() error {
|
||||
}
|
||||
defer os.Remove(saved)
|
||||
|
||||
err = cleanup(ExternalUIPath)
|
||||
err = cleanup(u.externalUIPath)
|
||||
if err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
return fmt.Errorf("cleanup exist file error: %w", err)
|
||||
@ -54,18 +99,18 @@ func DownloadUI() error {
|
||||
return fmt.Errorf("can't extract zip file: %w", err)
|
||||
}
|
||||
|
||||
err = os.Rename(unzipFolder, ExternalUIPath)
|
||||
err = os.Rename(unzipFolder, u.externalUIPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("rename UI folder failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
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)
|
||||
func (u *UIUpdater) prepareUIPath() error {
|
||||
if _, err := os.Stat(u.externalUIPath); os.IsNotExist(err) {
|
||||
log.Infoln("dir %s does not exist, creating", u.externalUIPath)
|
||||
if err := os.MkdirAll(u.externalUIPath, os.ModePerm); err != nil {
|
||||
log.Warnln("create dir %s error: %s", u.externalUIPath, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
@ -7,7 +7,6 @@ import (
|
||||
"net"
|
||||
"net/netip"
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -105,6 +104,8 @@ type Controller struct {
|
||||
ExternalControllerUnix string
|
||||
ExternalControllerPipe string
|
||||
ExternalUI string
|
||||
ExternalUIURL string
|
||||
ExternalUIName string
|
||||
ExternalDohServer string
|
||||
Secret string
|
||||
Cors Cors
|
||||
@ -718,33 +719,10 @@ func parseGeneral(cfg *RawConfig) (*General, error) {
|
||||
mihomoHttp.SetUA(cfg.GlobalUA)
|
||||
resource.SetETag(cfg.ETagSupport)
|
||||
|
||||
if cfg.KeepAliveIdle != 0 {
|
||||
keepalive.SetKeepAliveIdle(time.Duration(cfg.KeepAliveIdle) * time.Second)
|
||||
}
|
||||
if cfg.KeepAliveInterval != 0 {
|
||||
keepalive.SetKeepAliveInterval(time.Duration(cfg.KeepAliveInterval) * time.Second)
|
||||
}
|
||||
keepalive.SetKeepAliveIdle(time.Duration(cfg.KeepAliveIdle) * time.Second)
|
||||
keepalive.SetKeepAliveInterval(time.Duration(cfg.KeepAliveInterval) * time.Second)
|
||||
keepalive.SetDisableKeepAlive(cfg.DisableKeepAlive)
|
||||
|
||||
// checkout externalUI exist
|
||||
if cfg.ExternalUI != "" {
|
||||
updater.AutoDownloadUI = true
|
||||
updater.ExternalUIPath = C.Path.Resolve(cfg.ExternalUI)
|
||||
} else {
|
||||
// default externalUI path
|
||||
updater.ExternalUIPath = path.Join(C.Path.HomeDir(), "ui")
|
||||
}
|
||||
|
||||
// checkout UIpath/name exist
|
||||
if cfg.ExternalUIName != "" {
|
||||
updater.AutoDownloadUI = true
|
||||
updater.ExternalUIPath = path.Join(updater.ExternalUIPath, cfg.ExternalUIName)
|
||||
}
|
||||
|
||||
if cfg.ExternalUIURL != "" {
|
||||
updater.ExternalUIURL = cfg.ExternalUIURL
|
||||
}
|
||||
|
||||
return &General{
|
||||
Inbound: Inbound{
|
||||
Port: cfg.Port,
|
||||
@ -790,6 +768,8 @@ func parseController(cfg *RawConfig) (*Controller, error) {
|
||||
return &Controller{
|
||||
ExternalController: cfg.ExternalController,
|
||||
ExternalUI: cfg.ExternalUI,
|
||||
ExternalUIURL: cfg.ExternalUIURL,
|
||||
ExternalUIName: cfg.ExternalUIName,
|
||||
Secret: cfg.Secret,
|
||||
ExternalControllerPipe: cfg.ExternalControllerPipe,
|
||||
ExternalControllerUnix: cfg.ExternalControllerUnix,
|
||||
|
@ -117,7 +117,7 @@ func ApplyConfig(cfg *config.Config, force bool) {
|
||||
runtime.GC()
|
||||
tunnel.OnRunning()
|
||||
hcCompatibleProvider(cfg.Providers)
|
||||
initExternalUI()
|
||||
initExternalUI(cfg.Controller)
|
||||
|
||||
resolver.ResetConnection()
|
||||
}
|
||||
@ -394,16 +394,9 @@ func updateTunnels(tunnels []LC.Tunnel) {
|
||||
listener.PatchTunnel(tunnels, tunnel.Tunnel)
|
||||
}
|
||||
|
||||
func initExternalUI() {
|
||||
if updater.AutoDownloadUI {
|
||||
dirEntries, _ := os.ReadDir(updater.ExternalUIPath)
|
||||
if len(dirEntries) > 0 {
|
||||
log.Infoln("UI already exists, skip downloading")
|
||||
} else {
|
||||
log.Infoln("External UI downloading ...")
|
||||
updater.DownloadUI()
|
||||
}
|
||||
}
|
||||
func initExternalUI(controller *config.Controller) {
|
||||
updater.DefaultUiUpdater = updater.NewUiUpdater(controller.ExternalUI, controller.ExternalUIURL, controller.ExternalUIName)
|
||||
updater.DefaultUiUpdater.AutoDownloadUI()
|
||||
}
|
||||
|
||||
func updateGeneral(general *config.General) {
|
||||
|
@ -47,7 +47,7 @@ func upgradeCore(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func updateUI(w http.ResponseWriter, r *http.Request) {
|
||||
err := updater.DownloadUI()
|
||||
err := updater.DefaultUiUpdater.DownloadUI()
|
||||
if err != nil {
|
||||
log.Warnln("%s", err)
|
||||
render.Status(r, http.StatusInternalServerError)
|
||||
|
Loading…
Reference in New Issue
Block a user