add country filter

This commit is contained in:
zu1k
2020-08-22 16:25:24 +08:00
parent a52e63cf65
commit 7e1dd60837
2 changed files with 38 additions and 15 deletions

View File

@@ -57,8 +57,9 @@ func setupRouter() {
router.GET("/clash/proxies", func(c *gin.Context) {
proxyTypes := c.DefaultQuery("type", "")
proxyCountry := c.DefaultQuery("c", "")
text := ""
if proxyTypes == "" {
if proxyTypes == "" && proxyCountry == "" {
text = cache.GetString("clashproxies")
if text == "" {
proxies := cache.GetProxies("proxies")
@@ -68,11 +69,11 @@ func setupRouter() {
}
} else if proxyTypes == "all" {
proxies := cache.GetProxies("allproxies")
clash := provider.Clash{Proxies: proxies, Types: proxyTypes}
clash := provider.Clash{Proxies: proxies, Types: proxyTypes, Country: proxyCountry}
text = clash.Provide()
} else {
proxies := cache.GetProxies("proxies")
clash := provider.Clash{Proxies: proxies, Types: proxyTypes}
clash := provider.Clash{Proxies: proxies, Types: proxyTypes, Country: proxyCountry}
text = clash.Provide()
}
c.String(200, text)

View File

@@ -9,6 +9,7 @@ import (
type Clash struct {
Proxies proxy.ProxyList `yaml:"proxies"`
Types string `yaml:"type"`
Country string `yaml:"country"`
}
func (c Clash) CleanProxies() (proxies proxy.ProxyList) {
@@ -25,23 +26,44 @@ func (c Clash) Provide() string {
var resultBuilder strings.Builder
resultBuilder.WriteString("proxies:\n")
noNeedFilterType := false
noNeedFilterCountry := false
if c.Types == "" || c.Types == "all" {
for _, p := range c.Proxies {
if checkClashSupport(p) {
resultBuilder.WriteString(p.ToClash() + "\n")
}
noNeedFilterType = true
}
if c.Country == "" || c.Country == "all" {
noNeedFilterCountry = true
}
types := strings.Split(c.Types, ",")
countries := strings.Split(c.Country, ",")
for _, p := range c.Proxies {
if !checkClashSupport(p) {
continue
}
} else {
types := strings.Split(c.Types, ",")
for _, p := range c.Proxies {
if checkClashSupport(p) {
for _, t := range types {
if p.TypeName() == t {
resultBuilder.WriteString(p.ToClash() + "\n")
}
typeOk := false
countryOk := false
if !noNeedFilterType {
for _, t := range types {
if p.TypeName() == t {
typeOk = true
break
}
}
}
if !noNeedFilterCountry {
for _, c := range countries {
if strings.HasPrefix(p.BaseInfo().Name, c) {
countryOk = true
break
}
}
}
if (noNeedFilterType || typeOk) && (noNeedFilterCountry || countryOk) {
resultBuilder.WriteString(p.ToClash() + "\n")
}
}
return resultBuilder.String()