From 918521682cc2d14a614c3fca431728af1e5c0f70 Mon Sep 17 00:00:00 2001 From: nitezs Date: Sat, 16 Sep 2023 21:46:59 +0800 Subject: [PATCH] =?UTF-8?q?modify:=20=E6=A0=B9=E6=8D=AEClash=E7=AD=9B?= =?UTF-8?q?=E9=80=89=E8=BF=94=E5=9B=9E=E9=85=8D=E7=BD=AE=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/controller/clash.go | 3 ++- api/controller/default.go | 6 +++--- api/controller/meta.go | 3 ++- go.mod | 2 +- model/clash.go | 29 +++++++++++++++++++++++++++++ model/rule_provider.go | 13 +++++++++++++ model/sub.go | 12 ------------ utils/proxy.go | 10 ++++++++-- 8 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 model/clash.go create mode 100644 model/rule_provider.go diff --git a/api/controller/clash.go b/api/controller/clash.go index 1a70fcc..4b873ef 100644 --- a/api/controller/clash.go +++ b/api/controller/clash.go @@ -5,6 +5,7 @@ import ( "gopkg.in/yaml.v3" "net/http" "sub2clash/config" + "sub2clash/model" "sub2clash/validator" ) @@ -15,7 +16,7 @@ func SubmodHandler(c *gin.Context) { c.String(http.StatusBadRequest, err.Error()) return } - sub, err := BuildSub(query, config.Default.ClashTemplate) + sub, err := BuildSub(model.Clash, query, config.Default.ClashTemplate) if err != nil { c.String(http.StatusInternalServerError, err.Error()) return diff --git a/api/controller/default.go b/api/controller/default.go index 6f9a0c5..6b96337 100644 --- a/api/controller/default.go +++ b/api/controller/default.go @@ -14,7 +14,7 @@ import ( "sub2clash/validator" ) -func BuildSub(query validator.SubQuery, template string) ( +func BuildSub(clashType model.ClashType, query validator.SubQuery, template string) ( *model.Subscription, error, ) { // 定义变量 @@ -67,11 +67,11 @@ func BuildSub(query validator.SubQuery, template string) ( } else { proxyList = sub.Proxies } - utils.AddProxy(sub, query.AutoTest, query.Lazy, query.Sort, proxyList...) + utils.AddProxy(sub, query.AutoTest, query.Lazy, query.Sort, clashType, proxyList...) } // 处理自定义代理 utils.AddProxy( - sub, query.AutoTest, query.Lazy, query.Sort, + sub, query.AutoTest, query.Lazy, query.Sort, clashType, utils.ParseProxy(query.Proxies...)..., ) MergeSubAndTemplate(temp, sub) diff --git a/api/controller/meta.go b/api/controller/meta.go index 8b28dec..f6a2fe9 100644 --- a/api/controller/meta.go +++ b/api/controller/meta.go @@ -6,6 +6,7 @@ import ( "gopkg.in/yaml.v3" "net/http" "sub2clash/config" + "sub2clash/model" "sub2clash/validator" ) @@ -16,7 +17,7 @@ func SubHandler(c *gin.Context) { c.String(http.StatusBadRequest, err.Error()) return } - sub, err := BuildSub(query, config.Default.MetaTemplate) + sub, err := BuildSub(model.ClashMeta, query, config.Default.MetaTemplate) if err != nil { c.String(http.StatusInternalServerError, err.Error()) return diff --git a/go.mod b/go.mod index c29cffc..f7e4daa 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/gin-gonic/gin v1.9.1 github.com/joho/godotenv v1.5.1 go.uber.org/zap v1.25.0 + golang.org/x/text v0.13.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -33,6 +34,5 @@ require ( golang.org/x/crypto v0.13.0 // indirect golang.org/x/net v0.15.0 // indirect golang.org/x/sys v0.12.0 // indirect - golang.org/x/text v0.13.0 // indirect google.golang.org/protobuf v1.31.0 // indirect ) diff --git a/model/clash.go b/model/clash.go new file mode 100644 index 0000000..4425fc3 --- /dev/null +++ b/model/clash.go @@ -0,0 +1,29 @@ +package model + +type ClashType int + +const ( + Clash ClashType = 1 + iota + ClashMeta +) + +func GetSupportProxyTypes(clashType ClashType) map[string]bool { + if clashType == Clash { + return map[string]bool{ + "ss": true, + "ssr": true, + "vmess": true, + "trojan": true, + } + } + if clashType == ClashMeta { + return map[string]bool{ + "ss": true, + "ssr": true, + "vmess": true, + "trojan": true, + "vless": true, + } + } + return nil +} diff --git a/model/rule_provider.go b/model/rule_provider.go new file mode 100644 index 0000000..0991c95 --- /dev/null +++ b/model/rule_provider.go @@ -0,0 +1,13 @@ +package model + +type RuleProvider struct { + Type string `yaml:"type,omitempty"` + Behavior string `yaml:"behavior,omitempty"` + Url string `yaml:"url,omitempty"` + Path string `yaml:"path,omitempty"` + Interval int `yaml:"interval,omitempty"` +} + +type Payload struct { + Rules []string `yaml:"payload,omitempty"` +} diff --git a/model/sub.go b/model/sub.go index 9043450..7d255b6 100644 --- a/model/sub.go +++ b/model/sub.go @@ -12,15 +12,3 @@ type Subscription struct { Rules []string `yaml:"rules,omitempty"` RuleProviders map[string]RuleProvider `yaml:"rule-providers,omitempty,omitempty"` } - -type RuleProvider struct { - Type string `yaml:"type,omitempty"` - Behavior string `yaml:"behavior,omitempty"` - Url string `yaml:"url,omitempty"` - Path string `yaml:"path,omitempty"` - Interval int `yaml:"interval,omitempty"` -} - -type Payload struct { - Rules []string `yaml:"payload,omitempty"` -} diff --git a/utils/proxy.go b/utils/proxy.go index 130f771..4623bde 100644 --- a/utils/proxy.go +++ b/utils/proxy.go @@ -29,12 +29,18 @@ func GetContryName(proxy model.Proxy) string { } func AddProxy( - sub *model.Subscription, autotest bool, lazy bool, sortStrategy string, - proxies ...model.Proxy, + sub *model.Subscription, autotest bool, + lazy bool, sortStrategy string, + clashType model.ClashType, proxies ...model.Proxy, ) { newCountryGroupNames := make([]string, 0) + proxyTypes := model.GetSupportProxyTypes(clashType) + // 添加节点 for _, proxy := range proxies { + if !proxyTypes[proxy.Type] { + continue + } sub.Proxies = append(sub.Proxies, proxy) haveProxyGroup := false countryName := GetContryName(proxy)