add sub support

This commit is contained in:
zu1k
2020-09-04 16:40:31 +08:00
parent 9de327fb29
commit 82f6e1dae8
7 changed files with 236 additions and 83 deletions

View File

@@ -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() {

View File

@@ -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
}

View File

@@ -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()
}

21
pkg/provider/ssrsub.go Normal file
View File

@@ -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())
}

21
pkg/provider/sssub.go Normal file
View File

@@ -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())
}

View File

@@ -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()
}

21
pkg/provider/vmesssub.go Normal file
View File

@@ -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())
}