Skip to content

Commit

Permalink
add country filter
Browse files Browse the repository at this point in the history
  • Loading branch information
zu1k committed Aug 22, 2020
1 parent a52e63c commit 7e1dd60
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
7 changes: 4 additions & 3 deletions api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down
44 changes: 33 additions & 11 deletions pkg/provider/clash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}

typeOk := false
countryOk := false
if !noNeedFilterType {
for _, t := range types {
if p.TypeName() == t {
typeOk = true
break
}
}
}
} 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")
}
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()
Expand Down

0 comments on commit 7e1dd60

Please sign in to comment.