chore: updateUI API return 501 when config incomplete

This commit is contained in:
Larvan2 2023-09-23 17:59:59 +08:00
parent 34f62a0919
commit 8f515ecc05
4 changed files with 44 additions and 19 deletions

View file

@ -9,7 +9,6 @@ import (
"net/url" "net/url"
"os" "os"
"path" "path"
"path/filepath"
"regexp" "regexp"
"strings" "strings"
"time" "time"
@ -582,9 +581,6 @@ func parseGeneral(cfg *RawConfig) (*General, error) {
N.KeepAliveInterval = time.Duration(cfg.KeepAliveInterval) * time.Second N.KeepAliveInterval = time.Duration(cfg.KeepAliveInterval) * time.Second
} }
if cfg.ExternalUIURL != "" {
ExternalUIURL = cfg.ExternalUIURL
}
ExternalUIPath = cfg.ExternalUI ExternalUIPath = cfg.ExternalUI
// checkout externalUI exist // checkout externalUI exist
if ExternalUIPath != "" { if ExternalUIPath != "" {
@ -602,15 +598,12 @@ func parseGeneral(cfg *RawConfig) (*General, error) {
// checkout UIpath/name exist // checkout UIpath/name exist
if cfg.ExternalUIName != "" { if cfg.ExternalUIName != "" {
ExternalUIName = cfg.ExternalUIName ExternalUIName = cfg.ExternalUIName
ExternalUIFolder = filepath.Clean(path.Join(ExternalUIPath, cfg.ExternalUIName))
if _, err := os.Stat(ExternalUIPath); os.IsNotExist(err) {
if err := os.MkdirAll(ExternalUIPath, os.ModePerm); err != nil {
return nil, err
}
}
} else { } else {
ExternalUIFolder = ExternalUIPath ExternalUIFolder = ExternalUIPath
} }
if cfg.ExternalUIURL != "" {
ExternalUIURL = cfg.ExternalUIURL
}
cfg.Tun.RedirectToTun = cfg.EBpf.RedirectToTun cfg.Tun.RedirectToTun = cfg.EBpf.RedirectToTun
return &General{ return &General{

View file

@ -2,6 +2,7 @@ package config
import ( import (
"archive/zip" "archive/zip"
"errors"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -19,15 +20,18 @@ var (
ExternalUIFolder string ExternalUIFolder string
ExternalUIName string ExternalUIName string
) )
var (
ErrImcompleteConf = errors.New("ExternalUI configure incomplete")
)
var xdMutex sync.Mutex var xdMutex sync.Mutex
func UpdateUI() error { func UpdateUI() error {
xdMutex.Lock() xdMutex.Lock()
defer xdMutex.Unlock() defer xdMutex.Unlock()
if ExternalUIPath == "" || ExternalUIFolder == "" { err := prepare()
return fmt.Errorf("ExternalUI configure incomplete") if err != nil {
return err
} }
data, err := downloadForBytes(ExternalUIURL) data, err := downloadForBytes(ExternalUIURL)
@ -60,6 +64,25 @@ func UpdateUI() error {
return nil return nil
} }
func prepare() error {
if ExternalUIPath == "" || ExternalUIURL == "" {
return ErrImcompleteConf
}
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
}
}
} else {
ExternalUIFolder = ExternalUIPath
}
return nil
}
func unzip(src, dest string) (string, error) { func unzip(src, dest string) (string, error) {
r, err := zip.OpenReader(src) r, err := zip.OpenReader(src)
if err != nil { if err != nil {

View file

@ -2,7 +2,6 @@ package executor
import ( import (
"fmt" "fmt"
"github.com/Dreamacro/clash/ntp"
"net" "net"
"net/netip" "net/netip"
"os" "os"
@ -12,6 +11,8 @@ import (
"sync" "sync"
"time" "time"
"github.com/Dreamacro/clash/ntp"
"github.com/Dreamacro/clash/adapter" "github.com/Dreamacro/clash/adapter"
"github.com/Dreamacro/clash/adapter/inbound" "github.com/Dreamacro/clash/adapter/inbound"
"github.com/Dreamacro/clash/adapter/outboundgroup" "github.com/Dreamacro/clash/adapter/outboundgroup"
@ -142,6 +143,7 @@ func GetGeneral() *config.General {
AllowLan: listener.AllowLan(), AllowLan: listener.AllowLan(),
BindAddress: listener.BindAddress(), BindAddress: listener.BindAddress(),
}, },
Controller: config.Controller{},
Mode: tunnel.Mode(), Mode: tunnel.Mode(),
LogLevel: log.Level(), LogLevel: log.Level(),
IPv6: !resolver.DisableIPv6, IPv6: !resolver.DisableIPv6,

View file

@ -1,6 +1,7 @@
package route package route
import ( import (
"errors"
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
@ -15,12 +16,12 @@ import (
func upgradeRouter() http.Handler { func upgradeRouter() http.Handler {
r := chi.NewRouter() r := chi.NewRouter()
r.Post("/", upgrade) r.Post("/", upgradeCore)
r.Post("/ui", updateUI) r.Post("/ui", updateUI)
return r return r
} }
func upgrade(w http.ResponseWriter, r *http.Request) { func upgradeCore(w http.ResponseWriter, r *http.Request) {
// modify from https://github.com/AdguardTeam/AdGuardHome/blob/595484e0b3fb4c457f9bb727a6b94faa78a66c5f/internal/home/controlupdate.go#L108 // modify from https://github.com/AdguardTeam/AdGuardHome/blob/595484e0b3fb4c457f9bb727a6b94faa78a66c5f/internal/home/controlupdate.go#L108
log.Infoln("start update") log.Infoln("start update")
execPath, err := os.Executable() execPath, err := os.Executable()
@ -49,9 +50,15 @@ func upgrade(w http.ResponseWriter, r *http.Request) {
func updateUI(w http.ResponseWriter, r *http.Request) { func updateUI(w http.ResponseWriter, r *http.Request) {
err := config.UpdateUI() err := config.UpdateUI()
if err != nil { if err != nil {
log.Warnln("%s", err) if errors.Is(err, config.ErrImcompleteConf) {
render.Status(r, http.StatusInternalServerError) log.Warnln("%s", err)
render.JSON(w, r, newError(fmt.Sprintf("%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)))
}
return return
} }