From 8c8c1b3caa94071d8b88c41be21759ecd4fec152 Mon Sep 17 00:00:00 2001 From: nange Date: Fri, 8 Dec 2023 12:23:39 +0800 Subject: [PATCH] fix: auto_block proxy rule --- cmd/easyss/tray.go | 2 +- config.go | 56 +++++++++++++++++++++++++++++++++++++--------- easyss.go | 26 --------------------- 3 files changed, 47 insertions(+), 37 deletions(-) diff --git a/cmd/easyss/tray.go b/cmd/easyss/tray.go index 36e4792f..64c76ab0 100644 --- a/cmd/easyss/tray.go +++ b/cmd/easyss/tray.go @@ -119,7 +119,7 @@ func (st *SysTray) AddProxyRuleMenu() (*systray.MenuItem, *systray.MenuItem, *sy autoBlock := proxyMenue.AddSubMenuItemCheckbox("自动+屏蔽广告跟踪", "自动判断请求是否走代理或者屏蔽", false) if st.SS().ProxyRule() == easyss.ProxyRuleAutoBlock { - auto.Check() + autoBlock.Check() } reverseAuto := proxyMenue.AddSubMenuItemCheckbox("反向自动(国外访问国内)", "适用国外访问国内IP域名", false) diff --git a/config.go b/config.go index 2a168134..14333560 100644 --- a/config.go +++ b/config.go @@ -17,16 +17,52 @@ import ( "github.com/nange/easyss/v2/util" ) -var Methods = map[string]struct{}{ +type ProxyRule int + +const ( + ProxyRuleUnknown ProxyRule = iota + ProxyRuleAuto + ProxyRuleReverseAuto + ProxyRuleProxy + ProxyRuleDirect + ProxyRuleAutoBlock +) + +var proxyRules = map[string]ProxyRule{ + "auto": ProxyRuleAuto, + "reverse_auto": ProxyRuleReverseAuto, + "proxy": ProxyRuleProxy, + "direct": ProxyRuleDirect, + "auto_block": ProxyRuleAutoBlock, +} + +func ParseProxyRuleFromString(rule string) ProxyRule { + if r, ok := proxyRules[rule]; ok { + return r + } + + return ProxyRuleUnknown +} + +func AllProxyRule() []string { + keys := make([]string, 0, len(proxyRules)) + for k := range proxyRules { + keys = append(keys, k) + } + return keys +} + +var methods = map[string]struct{}{ "aes-256-gcm": {}, "chacha20-poly1305": {}, } -var ProxyRules = map[string]struct{}{ - "auto": {}, - "auto_block": {}, - "proxy": {}, - "direct": {}, +func AllMethod() []string { + keys := make([]string, 0, len(methods)) + for k := range proxyRules { + keys = append(keys, k) + } + return keys } const ( @@ -134,8 +170,8 @@ func (c *Config) Validate() error { } } if c.Method != "" { - if _, ok := Methods[c.Method]; !ok { - return fmt.Errorf("unsupported method:%s, supported methods:[aes-256-gcm, chacha20-poly1305]", c.Method) + if _, ok := methods[c.Method]; !ok { + return fmt.Errorf("unsupported method:%s, supported methods:%v", c.Method, AllMethod()) } } switch c.LogLevel { @@ -144,8 +180,8 @@ func (c *Config) Validate() error { return fmt.Errorf("unsupported log-level:%s, supported log-levels:[debug, info, warn, error]", c.LogLevel) } if c.ProxyRule != "" { - if _, ok := ProxyRules[c.ProxyRule]; !ok { - return fmt.Errorf("unsupported proxy rule:%s, supported rules:[auto, auto_block, proxy, direct]", c.ProxyRule) + if _, ok := proxyRules[c.ProxyRule]; !ok { + return fmt.Errorf("unsupported proxy rule:%s, supported rules:%v", c.ProxyRule, AllProxyRule()) } } if c.OutboundProto != OutboundProtoNative && c.OutboundProto != OutboundProtoHTTP && diff --git a/easyss.go b/easyss.go index 70abf1ec..4237bb7d 100644 --- a/easyss.go +++ b/easyss.go @@ -165,32 +165,6 @@ func (gs *GeoSite) SimpleMatch(domain string, matchSub bool) bool { return false } -type ProxyRule int - -const ( - ProxyRuleUnknown ProxyRule = iota - ProxyRuleAuto - ProxyRuleReverseAuto - ProxyRuleProxy - ProxyRuleDirect - ProxyRuleAutoBlock -) - -func ParseProxyRuleFromString(rule string) ProxyRule { - m := map[string]ProxyRule{ - "auto": ProxyRuleAuto, - "reverse_auto": ProxyRuleReverseAuto, - "proxy": ProxyRuleProxy, - "direct": ProxyRuleDirect, - "auto_block": ProxyRuleAutoBlock, - } - if r, ok := m[rule]; ok { - return r - } - - return ProxyRuleUnknown -} - type HostRule int const (