mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2024-11-14 21:31:16 +08:00
Feature: add custom ui support in API
This commit is contained in:
parent
afc4644dd1
commit
a6bbc67afb
@ -6,6 +6,7 @@ import (
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
adapters "github.com/Dreamacro/clash/adapters/outbound"
|
||||
@ -27,8 +28,9 @@ type General struct {
|
||||
AllowLan bool `json:"allow-lan"`
|
||||
Mode T.Mode `json:"mode"`
|
||||
LogLevel log.LogLevel `json:"log-level"`
|
||||
ExternalController string `json:"external-controller,omitempty"`
|
||||
Secret string `json:"secret,omitempty"`
|
||||
ExternalController string
|
||||
ExternalUI string
|
||||
Secret string
|
||||
}
|
||||
|
||||
// DNS config
|
||||
@ -66,6 +68,7 @@ type rawConfig struct {
|
||||
Mode T.Mode `yaml:"mode"`
|
||||
LogLevel log.LogLevel `yaml:"log-level"`
|
||||
ExternalController string `yaml:"external-controller"`
|
||||
ExternalUI string `yaml:"external-ui"`
|
||||
Secret string `yaml:"secret"`
|
||||
|
||||
DNS rawDNS `yaml:"dns"`
|
||||
@ -145,10 +148,19 @@ func parseGeneral(cfg *rawConfig) (*General, error) {
|
||||
redirPort := cfg.RedirPort
|
||||
allowLan := cfg.AllowLan
|
||||
externalController := cfg.ExternalController
|
||||
externalUI := cfg.ExternalUI
|
||||
secret := cfg.Secret
|
||||
mode := cfg.Mode
|
||||
logLevel := cfg.LogLevel
|
||||
|
||||
if !filepath.IsAbs(externalUI) {
|
||||
externalUI = filepath.Join(C.Path.HomeDir(), externalUI)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(externalUI); os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("external-ui: %s not exist", externalUI)
|
||||
}
|
||||
|
||||
general := &General{
|
||||
Port: port,
|
||||
SocksPort: socksPort,
|
||||
@ -157,6 +169,7 @@ func parseGeneral(cfg *rawConfig) (*General, error) {
|
||||
Mode: mode,
|
||||
LogLevel: logLevel,
|
||||
ExternalController: externalController,
|
||||
ExternalUI: externalUI,
|
||||
Secret: secret,
|
||||
}
|
||||
return general, nil
|
||||
|
@ -12,6 +12,10 @@ func Parse() error {
|
||||
return err
|
||||
}
|
||||
|
||||
if cfg.General.ExternalUI != "" {
|
||||
route.SetUIPath(cfg.General.ExternalUI)
|
||||
}
|
||||
|
||||
if cfg.General.ExternalController != "" {
|
||||
go route.Start(cfg.General.ExternalController, cfg.General.Secret)
|
||||
}
|
||||
|
@ -17,6 +17,8 @@ import (
|
||||
var (
|
||||
serverSecret = ""
|
||||
serverAddr = ""
|
||||
|
||||
uiPath = ""
|
||||
)
|
||||
|
||||
type Traffic struct {
|
||||
@ -24,6 +26,10 @@ type Traffic struct {
|
||||
Down int64 `json:"down"`
|
||||
}
|
||||
|
||||
func SetUIPath(path string) {
|
||||
uiPath = path
|
||||
}
|
||||
|
||||
func Start(addr string, secret string) {
|
||||
if serverAddr != "" {
|
||||
return
|
||||
@ -49,6 +55,14 @@ func Start(addr string, secret string) {
|
||||
r.Mount("/proxies", proxyRouter())
|
||||
r.Mount("/rules", ruleRouter())
|
||||
|
||||
if uiPath != "" {
|
||||
fs := http.StripPrefix("/ui", http.FileServer(http.Dir(uiPath)))
|
||||
r.Get("/ui", http.RedirectHandler("/ui/", 301).ServeHTTP)
|
||||
r.Get("/ui/*", func(w http.ResponseWriter, r *http.Request) {
|
||||
fs.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
log.Infoln("RESTful API listening at: %s", addr)
|
||||
err := http.ListenAndServe(addr, r)
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user