diff --git a/api/router.go b/api/router.go index a131c1b..546c184 100644 --- a/api/router.go +++ b/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) diff --git a/pkg/provider/clash.go b/pkg/provider/clash.go index 640f297..8383eb0 100644 --- a/pkg/provider/clash.go +++ b/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 } - } 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()