瀏覽代碼

add country filter

zu1k 5 年之前
父節點
當前提交
7e1dd60837
共有 2 個文件被更改,包括 37 次插入14 次删除
  1. 4 3
      api/router.go
  2. 33 11
      pkg/provider/clash.go

+ 4 - 3
api/router.go

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

+ 33 - 11
pkg/provider/clash.go

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