From 3ca9e86a297f9771679712937b9bd3c10fe5b2fb Mon Sep 17 00:00:00 2001 From: zu1k Date: Fri, 14 Aug 2020 13:13:11 +0800 Subject: [PATCH] add type filter --- api/router.go | 16 ++++++++++++---- provider/clash.go | 30 ++++++++++++++++++++++-------- proxy/base.go | 1 + proxy/shadowsocks.go | 4 ++++ proxy/shadowsocksr.go | 4 ++++ proxy/vmess.go | 4 ++++ 6 files changed, 47 insertions(+), 12 deletions(-) diff --git a/api/router.go b/api/router.go index 4d1234e..69eb77c 100644 --- a/api/router.go +++ b/api/router.go @@ -19,12 +19,20 @@ func setupRouter() { router.StaticFile("/clash/config", "assets/clash-config.yaml") router.StaticFile("/surge/config", "assets/surge.conf") router.GET("/clash/proxies", func(c *gin.Context) { - text := cache.GetString("clashproxies") - if text == "" { + proxyTypes := c.DefaultQuery("type", "all") + text := "" + if proxyTypes == "all" { + text = cache.GetString("clashproxies") + if text == "" { + proxies := cache.GetProxies() + clash := provider.Clash{Proxies: proxies} + text = clash.Provide() + cache.SetString("clashproxies", text) + } + } else { proxies := cache.GetProxies() - clash := provider.Clash{Proxies: proxies} + clash := provider.Clash{Proxies: proxies, Types: proxyTypes} text = clash.Provide() - cache.SetString("clashproxies", text) } c.String(200, text) }) diff --git a/provider/clash.go b/provider/clash.go index 3cd58f3..a10480a 100644 --- a/provider/clash.go +++ b/provider/clash.go @@ -8,15 +8,29 @@ import ( type Clash struct { Proxies []proxy.Proxy `yaml:"proxies"` + Types string `yaml:"type"` } func (c Clash) Provide() string { var resultBuilder strings.Builder - resultBuilder.WriteString("proxies:\n") - for _, p := range c.Proxies { - if checkClashSupport(p) { - resultBuilder.WriteString(p.ToClash() + "\n") + + if c.Types == "" || c.Types == "all" { + for _, p := range c.Proxies { + if checkClashSupport(p) { + resultBuilder.WriteString(p.ToClash() + "\n") + } + } + } else { + types := strings.Split(c.Types, ",") + for _, p := range c.Proxies { + if checkClashSupport(p) { + for _, t := range types { + if p.Type() == t { + resultBuilder.WriteString(p.ToClash() + "\n") + } + } + } } } @@ -24,18 +38,18 @@ func (c Clash) Provide() string { } func checkClashSupport(p proxy.Proxy) bool { - switch p.(type) { - case *proxy.ShadowsocksR: + switch p.Type() { + case "ssr": ssr := p.(*proxy.ShadowsocksR) if checkInList(ssrCipherList, ssr.Cipher) && checkInList(ssrProtocolList, ssr.Protocol) && checkInList(ssrObfsList, ssr.Obfs) { return true } - case *proxy.Vmess: + case "vmess": vmess := p.(*proxy.Vmess) if checkInList(vmessCipherList, vmess.Cipher) { return true } - case *proxy.Shadowsocks: + case "ss": ss := p.(*proxy.Shadowsocks) if checkInList(ssCipherList, ss.Cipher) { return true diff --git a/proxy/base.go b/proxy/base.go index 608ba19..b776d28 100644 --- a/proxy/base.go +++ b/proxy/base.go @@ -14,6 +14,7 @@ type Proxy interface { ToSurge() string Identifier() string SetName(name string) + Type() string } func Deduplication(src []Proxy) []Proxy { diff --git a/proxy/shadowsocks.go b/proxy/shadowsocks.go index a50726e..0ee227b 100644 --- a/proxy/shadowsocks.go +++ b/proxy/shadowsocks.go @@ -65,6 +65,10 @@ func (ss *Shadowsocks) SetName(name string) { ss.Name = name } +func (ss *Shadowsocks) Type() string { + return "ss" +} + func ParseSSLink(link string) (*Shadowsocks, error) { if !strings.HasPrefix(link, "ss://") { return nil, ErrorNotSSRLink diff --git a/proxy/shadowsocksr.go b/proxy/shadowsocksr.go index 521edb7..b5df73c 100644 --- a/proxy/shadowsocksr.go +++ b/proxy/shadowsocksr.go @@ -61,6 +61,10 @@ func (ssr *ShadowsocksR) SetName(name string) { ssr.Name = name } +func (ssr *ShadowsocksR) Type() string { + return "ssr" +} + func ParseSSRLink(link string) (*ShadowsocksR, error) { if !strings.HasPrefix(link, "ssr") { return nil, ErrorNotSSRLink diff --git a/proxy/vmess.go b/proxy/vmess.go index b48c71c..3409464 100644 --- a/proxy/vmess.go +++ b/proxy/vmess.go @@ -86,6 +86,10 @@ func (v *Vmess) SetName(name string) { v.Name = name } +func (v *Vmess) Type() string { + return "vmess" +} + type vmessLinkJson struct { Add string `json:"add"` V string `json:"v"`