From ab8181de1b412ac3ea7ce439205747816a646fec Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Sat, 4 Aug 2018 23:04:16 +0800 Subject: [PATCH] Fixed: global mode and update log level --- config/config.go | 7 +++++++ constant/config.go | 1 + hub/configs.go | 17 ++++++++++++++--- tunnel/tunnel.go | 6 ++---- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/config/config.go b/config/config.go index c6bb4e94..06e17a18 100644 --- a/config/config.go +++ b/config/config.go @@ -94,6 +94,12 @@ func (c *Config) SetMode(mode Mode) { c.event <- &Event{Type: "mode", Payload: mode} } +// SetLogLevel change log level of clash +func (c *Config) SetLogLevel(level C.LogLevel) { + c.general.LogLevel = level + c.event <- &Event{Type: "log-level", Payload: level} +} + // General return clash general config func (c *Config) General() General { return *c.general @@ -225,6 +231,7 @@ func (c *Config) parseProxies(cfg *ini.File) error { } // init proxy + proxies["GLOBAL"], _ = adapters.NewSelector("GLOBAL", proxies) proxies["DIRECT"] = adapters.NewDirect() proxies["REJECT"] = adapters.NewReject() diff --git a/constant/config.go b/constant/config.go index b090edfb..3b8016b8 100644 --- a/constant/config.go +++ b/constant/config.go @@ -25,6 +25,7 @@ type General struct { AllowLan *bool `json:"allow-lan,omitempty"` Port *int `json:"port,omitempty"` SocksPort *int `json:"socks-port,omitempty"` + LogLevel *string `json:"log-level,omitempty"` } func init() { diff --git a/hub/configs.go b/hub/configs.go index 86019093..8082bcc7 100644 --- a/hub/configs.go +++ b/hub/configs.go @@ -50,7 +50,7 @@ func updateConfigs(w http.ResponseWriter, r *http.Request) { } // update errors - var proxyErr, modeErr error + var proxyErr, modeErr, logLevelErr error // update proxy listener := proxy.Instance() @@ -70,9 +70,20 @@ func updateConfigs(w http.ResponseWriter, r *http.Request) { } } + // update log-level + if general.LogLevel != nil { + level, ok := C.LogLevelMapping[*general.LogLevel] + if !ok { + logLevelErr = fmt.Errorf("Log Level error") + } else { + cfg.SetLogLevel(level) + } + } + hasError, errors := formatErrors(map[string]error{ - "proxy": proxyErr, - "mode": modeErr, + "proxy": proxyErr, + "mode": modeErr, + "log-level": logLevelErr, }) if hasError { diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index 8d84a9d6..1b591b8a 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -5,7 +5,6 @@ import ( "time" LocalAdapter "github.com/Dreamacro/clash/adapters/local" - RemoteAdapter "github.com/Dreamacro/clash/adapters/remote" cfg "github.com/Dreamacro/clash/config" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/observable" @@ -27,8 +26,7 @@ type Tunnel struct { traffic *C.Traffic // Outbound Rule - mode cfg.Mode - selector *RemoteAdapter.Selector + mode cfg.Mode // Log logCh chan interface{} @@ -93,7 +91,7 @@ func (t *Tunnel) handleConn(localConn C.ServerAdapter) { case cfg.Direct: proxy = t.proxies["DIRECT"] case cfg.Global: - proxy = t.selector + proxy = t.proxies["GLOBAL"] // Rule default: proxy = t.match(addr)