clash api: download clash-dashboard if external-ui directory is empty

This commit is contained in:
世界
2023-04-09 12:39:26 +08:00
parent e168de79c7
commit 750f87bb0a
10 changed files with 380 additions and 13 deletions

View File

@@ -47,6 +47,10 @@ type Server struct {
storeFakeIP bool
cacheFilePath string
cacheFile adapter.ClashCacheFile
externalUI string
externalUIDownloadURL string
externalUIDownloadDetour string
}
func NewServer(router adapter.Router, logFactory log.ObservableFactory, options option.ClashAPIOptions) (adapter.ClashServer, error) {
@@ -59,11 +63,13 @@ func NewServer(router adapter.Router, logFactory log.ObservableFactory, options
Addr: options.ExternalController,
Handler: chiRouter,
},
trafficManager: trafficManager,
urlTestHistory: urltest.NewHistoryStorage(),
mode: strings.ToLower(options.DefaultMode),
storeSelected: options.StoreSelected,
storeFakeIP: options.StoreFakeIP,
trafficManager: trafficManager,
urlTestHistory: urltest.NewHistoryStorage(),
mode: strings.ToLower(options.DefaultMode),
storeSelected: options.StoreSelected,
storeFakeIP: options.StoreFakeIP,
externalUIDownloadURL: options.ExternalUIDownloadURL,
externalUIDownloadDetour: options.ExternalUIDownloadDetour,
}
if server.mode == "" {
server.mode = "rule"
@@ -105,8 +111,9 @@ func NewServer(router adapter.Router, logFactory log.ObservableFactory, options
r.Mount("/dns", dnsRouter(router))
})
if options.ExternalUI != "" {
server.externalUI = C.BasePath(os.ExpandEnv(options.ExternalUI))
chiRouter.Group(func(r chi.Router) {
fs := http.StripPrefix("/ui", http.FileServer(http.Dir(C.BasePath(os.ExpandEnv(options.ExternalUI)))))
fs := http.StripPrefix("/ui", http.FileServer(http.Dir(server.externalUI)))
r.Get("/ui", http.RedirectHandler("/ui/", http.StatusTemporaryRedirect).ServeHTTP)
r.Get("/ui/*", func(w http.ResponseWriter, r *http.Request) {
fs.ServeHTTP(w, r)
@@ -128,6 +135,7 @@ func (s *Server) PreStart() error {
}
func (s *Server) Start() error {
s.checkAndDownloadExternalUI()
listener, err := net.Listen("tcp", s.httpServer.Addr)
if err != nil {
return E.Cause(err, "external controller listen error")