add type filter

This commit is contained in:
zu1k
2020-08-14 13:13:11 +08:00
parent a32b2a79bf
commit 3ca9e86a29
6 changed files with 47 additions and 12 deletions

View File

@@ -19,12 +19,20 @@ func setupRouter() {
router.StaticFile("/clash/config", "assets/clash-config.yaml")
router.StaticFile("/surge/config", "assets/surge.conf")
router.GET("/clash/proxies", func(c *gin.Context) {
text := cache.GetString("clashproxies")
if text == "" {
proxyTypes := c.DefaultQuery("type", "all")
text := ""
if proxyTypes == "all" {
text = cache.GetString("clashproxies")
if text == "" {
proxies := cache.GetProxies()
clash := provider.Clash{Proxies: proxies}
text = clash.Provide()
cache.SetString("clashproxies", text)
}
} else {
proxies := cache.GetProxies()
clash := provider.Clash{Proxies: proxies}
clash := provider.Clash{Proxies: proxies, Types: proxyTypes}
text = clash.Provide()
cache.SetString("clashproxies", text)
}
c.String(200, text)
})

View File

@@ -8,15 +8,29 @@ import (
type Clash struct {
Proxies []proxy.Proxy `yaml:"proxies"`
Types string `yaml:"type"`
}
func (c Clash) Provide() string {
var resultBuilder strings.Builder
resultBuilder.WriteString("proxies:\n")
for _, p := range c.Proxies {
if checkClashSupport(p) {
resultBuilder.WriteString(p.ToClash() + "\n")
if c.Types == "" || c.Types == "all" {
for _, p := range c.Proxies {
if checkClashSupport(p) {
resultBuilder.WriteString(p.ToClash() + "\n")
}
}
} else {
types := strings.Split(c.Types, ",")
for _, p := range c.Proxies {
if checkClashSupport(p) {
for _, t := range types {
if p.Type() == t {
resultBuilder.WriteString(p.ToClash() + "\n")
}
}
}
}
}
@@ -24,18 +38,18 @@ func (c Clash) Provide() string {
}
func checkClashSupport(p proxy.Proxy) bool {
switch p.(type) {
case *proxy.ShadowsocksR:
switch p.Type() {
case "ssr":
ssr := p.(*proxy.ShadowsocksR)
if checkInList(ssrCipherList, ssr.Cipher) && checkInList(ssrProtocolList, ssr.Protocol) && checkInList(ssrObfsList, ssr.Obfs) {
return true
}
case *proxy.Vmess:
case "vmess":
vmess := p.(*proxy.Vmess)
if checkInList(vmessCipherList, vmess.Cipher) {
return true
}
case *proxy.Shadowsocks:
case "ss":
ss := p.(*proxy.Shadowsocks)
if checkInList(ssCipherList, ss.Cipher) {
return true

View File

@@ -14,6 +14,7 @@ type Proxy interface {
ToSurge() string
Identifier() string
SetName(name string)
Type() string
}
func Deduplication(src []Proxy) []Proxy {

View File

@@ -65,6 +65,10 @@ func (ss *Shadowsocks) SetName(name string) {
ss.Name = name
}
func (ss *Shadowsocks) Type() string {
return "ss"
}
func ParseSSLink(link string) (*Shadowsocks, error) {
if !strings.HasPrefix(link, "ss://") {
return nil, ErrorNotSSRLink

View File

@@ -61,6 +61,10 @@ func (ssr *ShadowsocksR) SetName(name string) {
ssr.Name = name
}
func (ssr *ShadowsocksR) Type() string {
return "ssr"
}
func ParseSSRLink(link string) (*ShadowsocksR, error) {
if !strings.HasPrefix(link, "ssr") {
return nil, ErrorNotSSRLink

View File

@@ -86,6 +86,10 @@ func (v *Vmess) SetName(name string) {
v.Name = name
}
func (v *Vmess) Type() string {
return "vmess"
}
type vmessLinkJson struct {
Add string `json:"add"`
V string `json:"v"`