From 82f6e1dae89e22e01f1ec139caa2fd68d268baa8 Mon Sep 17 00:00:00 2001 From: zu1k Date: Fri, 4 Sep 2020 16:40:31 +0800 Subject: [PATCH] add sub support --- api/router.go | 101 +++++++++++++++++++++++++++++++++------ pkg/provider/base.go | 75 +++++++++++++++++++++++++++++ pkg/provider/clash.go | 70 +++------------------------ pkg/provider/ssrsub.go | 21 ++++++++ pkg/provider/sssub.go | 21 ++++++++ pkg/provider/surge.go | 10 ++-- pkg/provider/vmesssub.go | 21 ++++++++ 7 files changed, 236 insertions(+), 83 deletions(-) create mode 100644 pkg/provider/ssrsub.go create mode 100644 pkg/provider/sssub.go create mode 100644 pkg/provider/vmesssub.go diff --git a/api/router.go b/api/router.go index f8bb35c..2290182 100644 --- a/api/router.go +++ b/api/router.go @@ -13,7 +13,7 @@ import ( "github.com/zu1k/proxypool/pkg/provider" ) -const version = "v0.3.4" +const version = "v0.3.5" var router *gin.Engine @@ -75,41 +75,112 @@ func setupRouter() { text = cache.GetString("clashproxies") if text == "" { proxies := cache.GetProxies("proxies") - clash := provider.Clash{Proxies: proxies} + clash := provider.Clash{ + provider.Base{ + Proxies: &proxies, + }, + } text = clash.Provide() cache.SetString("clashproxies", text) } } else if proxyTypes == "all" { proxies := cache.GetProxies("allproxies") clash := provider.Clash{ - Proxies: proxies, - Types: proxyTypes, - Country: proxyCountry, - NotCountry: proxyNotCountry, + provider.Base{ + Proxies: &proxies, + Types: proxyTypes, + Country: proxyCountry, + NotCountry: proxyNotCountry, + }, } text = clash.Provide() } else { proxies := cache.GetProxies("proxies") clash := provider.Clash{ - Proxies: proxies, - Types: proxyTypes, - Country: proxyCountry, - NotCountry: proxyNotCountry, + provider.Base{ + Proxies: &proxies, + Types: proxyTypes, + Country: proxyCountry, + NotCountry: proxyNotCountry, + }, } text = clash.Provide() } c.String(200, text) }) router.GET("/surge/proxies", func(c *gin.Context) { - text := cache.GetString("surgeproxies") - if text == "" { - proxies := cache.GetProxies("proxies") - surge := provider.Surge{Proxies: proxies} + proxyTypes := c.DefaultQuery("type", "") + proxyCountry := c.DefaultQuery("c", "") + proxyNotCountry := c.DefaultQuery("nc", "") + text := "" + if proxyTypes == "" && proxyCountry == "" && proxyNotCountry == "" { + text = cache.GetString("surgeproxies") + if text == "" { + proxies := cache.GetProxies("proxies") + surge := provider.Surge{ + provider.Base{ + Proxies: &proxies, + }, + } + text = surge.Provide() + cache.SetString("surgeproxies", text) + } + } else if proxyTypes == "all" { + proxies := cache.GetProxies("allproxies") + surge := provider.Surge{ + provider.Base{ + Proxies: &proxies, + Types: proxyTypes, + Country: proxyCountry, + NotCountry: proxyNotCountry, + }, + } + text = surge.Provide() + } else { + proxies := cache.GetProxies("proxies") + surge := provider.Surge{ + provider.Base{ + Proxies: &proxies, + Types: proxyTypes, + Country: proxyCountry, + NotCountry: proxyNotCountry, + }, + } text = surge.Provide() - cache.SetString("surgeproxies", text) } c.String(200, text) }) + + router.GET("/ss/sub", func(c *gin.Context) { + proxies := cache.GetProxies("proxies") + ssSub := provider.SSSub{ + provider.Base{ + Proxies: &proxies, + Types: "ss", + }, + } + c.String(200, ssSub.Provide()) + }) + router.GET("/ssr/sub", func(c *gin.Context) { + proxies := cache.GetProxies("proxies") + ssrSub := provider.SSRSub{ + provider.Base{ + Proxies: &proxies, + Types: "ssr", + }, + } + c.String(200, ssrSub.Provide()) + }) + router.GET("/vmess/sub", func(c *gin.Context) { + proxies := cache.GetProxies("proxies") + vmessSub := provider.VmessSub{ + provider.Base{ + Proxies: &proxies, + Types: "vmess", + }, + } + c.String(200, vmessSub.Provide()) + }) } func Run() { diff --git a/pkg/provider/base.go b/pkg/provider/base.go index 4e9ae37..e570420 100644 --- a/pkg/provider/base.go +++ b/pkg/provider/base.go @@ -1,5 +1,11 @@ package provider +import ( + "strings" + + "github.com/zu1k/proxypool/pkg/proxy" +) + type Provider interface { Provide() string } @@ -12,3 +18,72 @@ func checkInList(list []string, item string) bool { } return false } + +type Base struct { + Proxies *proxy.ProxyList `yaml:"proxies"` + Types string `yaml:"type"` + Country string `yaml:"country"` + NotCountry string `yaml:"not_country"` +} + +func (b Base) preFilter() { + proxies := make(proxy.ProxyList, 0) + + needFilterType := true + needFilterCountry := true + needFilterNotCountry := true + if b.Types == "" || b.Types == "all" { + needFilterType = false + } + if b.Country == "" || b.Country == "all" { + needFilterCountry = false + } + if b.NotCountry == "" { + needFilterNotCountry = false + } + types := strings.Split(b.Types, ",") + countries := strings.Split(b.Country, ",") + notCountries := strings.Split(b.NotCountry, ",") + + bProxies := *b.Proxies + for _, p := range bProxies { + if needFilterType { + typeOk := false + for _, t := range types { + if p.TypeName() == t { + typeOk = true + break + } + } + if !typeOk { + goto exclude + } + } + + if needFilterNotCountry { + for _, c := range notCountries { + if strings.Contains(p.BaseInfo().Name, c) { + goto exclude + } + } + } + + if needFilterCountry { + countryOk := false + for _, c := range countries { + if strings.Contains(p.BaseInfo().Name, c) { + countryOk = true + break + } + } + if !countryOk { + goto exclude + } + } + + proxies = append(proxies, p) + exclude: + } + + b.Proxies = &proxies +} diff --git a/pkg/provider/clash.go b/pkg/provider/clash.go index e08b607..9466a97 100644 --- a/pkg/provider/clash.go +++ b/pkg/provider/clash.go @@ -7,15 +7,12 @@ import ( ) type Clash struct { - Proxies proxy.ProxyList `yaml:"proxies"` - Types string `yaml:"type"` - Country string `yaml:"country"` - NotCountry string `yaml:"not_country"` + Base } func (c Clash) CleanProxies() (proxies proxy.ProxyList) { proxies = make(proxy.ProxyList, 0) - for _, p := range c.Proxies { + for _, p := range *c.Proxies { if checkClashSupport(p) { proxies = append(proxies, p) } @@ -24,68 +21,15 @@ func (c Clash) CleanProxies() (proxies proxy.ProxyList) { } func (c Clash) Provide() string { + c.preFilter() + var resultBuilder strings.Builder resultBuilder.WriteString("proxies:\n") - - needFilterType := true - needFilterCountry := true - needFilterNotCountry := true - if c.Types == "" || c.Types == "all" { - needFilterType = false - } - if c.Country == "" || c.Country == "all" { - needFilterCountry = false - } - if c.NotCountry == "" { - needFilterNotCountry = false - } - types := strings.Split(c.Types, ",") - countries := strings.Split(c.Country, ",") - notCountries := strings.Split(c.NotCountry, ",") - - for _, p := range c.Proxies { - if !checkClashSupport(p) { - continue + for _, p := range *c.Proxies { + if checkClashSupport(p) { + resultBuilder.WriteString(p.ToClash() + "\n") } - - if needFilterType { - typeOk := false - for _, t := range types { - if p.TypeName() == t { - typeOk = true - break - } - } - if !typeOk { - goto exclude - } - } - - if needFilterNotCountry { - for _, c := range notCountries { - if strings.Contains(p.BaseInfo().Name, c) { - goto exclude - } - } - } - - if needFilterCountry { - countryOk := false - for _, c := range countries { - if strings.Contains(p.BaseInfo().Name, c) { - countryOk = true - break - } - } - if !countryOk { - goto exclude - } - } - - resultBuilder.WriteString(p.ToClash() + "\n") - exclude: } - return resultBuilder.String() } diff --git a/pkg/provider/ssrsub.go b/pkg/provider/ssrsub.go new file mode 100644 index 0000000..77ac933 --- /dev/null +++ b/pkg/provider/ssrsub.go @@ -0,0 +1,21 @@ +package provider + +import ( + "strings" + + "github.com/zu1k/proxypool/pkg/tool" +) + +type SSRSub struct { + Base +} + +func (sub SSRSub) Provide() string { + sub.Types = "ssr" + sub.preFilter() + var resultBuilder strings.Builder + for _, p := range *sub.Proxies { + resultBuilder.WriteString(p.Link() + "\n") + } + return tool.Base64EncodeString(resultBuilder.String()) +} diff --git a/pkg/provider/sssub.go b/pkg/provider/sssub.go new file mode 100644 index 0000000..ba1d1d8 --- /dev/null +++ b/pkg/provider/sssub.go @@ -0,0 +1,21 @@ +package provider + +import ( + "strings" + + "github.com/zu1k/proxypool/pkg/tool" +) + +type SSSub struct { + Base +} + +func (sub SSSub) Provide() string { + sub.Types = "ss" + sub.preFilter() + var resultBuilder strings.Builder + for _, p := range *sub.Proxies { + resultBuilder.WriteString(p.Link() + "\n") + } + return tool.Base64EncodeString(resultBuilder.String()) +} diff --git a/pkg/provider/surge.go b/pkg/provider/surge.go index 425fd3a..a4d0c72 100644 --- a/pkg/provider/surge.go +++ b/pkg/provider/surge.go @@ -7,18 +7,18 @@ import ( ) type Surge struct { - Proxies proxy.ProxyList `yaml:"proxies"` + Base } func (s Surge) Provide() string { - var resultBuilder strings.Builder + s.preFilter() - for _, p := range s.Proxies { + var resultBuilder strings.Builder + for _, p := range *s.Proxies { if checkSurgeSupport(p) { - resultBuilder.WriteString(p.ToSurge() + "\n") + resultBuilder.WriteString(p.ToClash() + "\n") } } - return resultBuilder.String() } diff --git a/pkg/provider/vmesssub.go b/pkg/provider/vmesssub.go new file mode 100644 index 0000000..e4a1fa7 --- /dev/null +++ b/pkg/provider/vmesssub.go @@ -0,0 +1,21 @@ +package provider + +import ( + "strings" + + "github.com/zu1k/proxypool/pkg/tool" +) + +type VmessSub struct { + Base +} + +func (sub VmessSub) Provide() string { + sub.Types = "vmess" + sub.preFilter() + var resultBuilder strings.Builder + for _, p := range *sub.Proxies { + resultBuilder.WriteString(p.Link() + "\n") + } + return tool.Base64EncodeString(resultBuilder.String()) +}