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)