Skip to content

Commit

Permalink
add clash getter
Browse files Browse the repository at this point in the history
  • Loading branch information
Sansui233 committed Dec 1, 2020
1 parent 349e881 commit e59ff68
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 8 deletions.
7 changes: 6 additions & 1 deletion config/source.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
options:
url: https://fanqiangdang.com/forum-48-1.html

# clash格式订阅链接
- type: clash
options:
url: https://raw.githubusercontent.com/du5/free/master/file/0909/Clash.yaml

# 某个网站抓取
- type: web-freessrxyz
options:
options:
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ require (
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/sourcemap.v1 v1.0.5 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
gorm.io/driver/postgres v1.0.5
gorm.io/gorm v1.20.7
)
2 changes: 1 addition & 1 deletion internal/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func connect() (err error) {
log.Infoln("database: successfully connected to: %s", DB.Name())
} else {
DB = nil
log.Warnln("database connection Infoln: %s \n\t\tUse cache to store proxies", err.Error())
log.Warnln("database connection info: %s \n\t\tUse cache to store proxies", err.Error())
}
return
}
2 changes: 1 addition & 1 deletion log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Infoln(format string, v ...interface{}) {
}

func Warnln(format string, v ...interface{}) {
log.Errorln(fmt.Sprintf(format, v...))
log.Warnln(fmt.Sprintf(format, v...))
logToFile(WARNING, fmt.Sprintf(format, v...))
}

Expand Down
11 changes: 11 additions & 0 deletions pkg/getter/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ func StringArray2ProxyArray(origin []string) proxy.ProxyList {
return results
}

func ClashProxy2ProxyArray(origin []map[string]interface{}) proxy.ProxyList {
results := make(proxy.ProxyList, 0, len(origin))
for _, pjson := range origin {
p, err := proxy.ParseProxyFromClashProxy(pjson)
if err == nil && p != nil {
results = append(results, p)
}
}
return results
}

func GrepLinksFromString(text string) []string {
results := proxy.GrepSSRLinkFromString(text)
results = append(results, proxy.GrepVmessLinkFromString(text)...)
Expand Down
74 changes: 74 additions & 0 deletions pkg/getter/clash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package getter

import (
"github.com/Sansui233/proxypool/log"
"github.com/Sansui233/proxypool/pkg/proxy"
"github.com/Sansui233/proxypool/pkg/tool"
"gopkg.in/yaml.v3"
"io/ioutil"
"sync"
)

func init() {
Register("clash", NewClashGetter)
}

type Clash struct {
Url string
}

type config struct {
Proxy []map[string]interface{} `json:"proxies" yaml:"proxies"`
}

func (c *Clash) Get() proxy.ProxyList {
resp, err := tool.GetHttpClient().Get(c.Url)
if err != nil {
return nil
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil
}

conf := config{}
err = yaml.Unmarshal(body, &conf)
if err != nil {
return nil
}

return ClashProxy2ProxyArray(conf.Proxy)

}

func (c *Clash) Get2Chan(pc chan proxy.Proxy) {
nodes := c.Get()
log.Infoln("STATISTIC: Clash\tcount=%d\turl=%s\n", len(nodes), c.Url)
for _, node := range nodes {
pc <- node
}
}

func (c *Clash) Get2ChanWG(pc chan proxy.Proxy, wg *sync.WaitGroup) {
defer wg.Done()
nodes := c.Get()
log.Infoln("STATISTIC: Clash\tcount=%d\turl=%s\n", len(nodes), c.Url)
for _, node := range nodes {
pc <- node
}
}

func NewClashGetter(options tool.Options) (getter Getter, err error) {
urlInterface, found := options["url"]
if found {
url, err := AssertTypeStringNotNull(urlInterface)
if err != nil {
return nil, err
}
return &Clash{
Url: url,
}, nil
}
return nil, ErrorUrlNotFound
}
6 changes: 5 additions & 1 deletion pkg/getter/web_fuzz_sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ func (w *WebFuzzSub) Get() proxy.ProxyList {
subUrls := urlRe.FindAllString(text, -1)
result := make(proxy.ProxyList, 0)
for _, url := range subUrls {
result = append(result, (&Subscribe{Url: url}).Get()...)
newResult := (&Subscribe{Url: url}).Get()
if len(newResult) == 0 {
newResult = (&Clash{Url: url}).Get()
}
result = result.UniqAppendProxyList(newResult)
}
return result
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/healthcheck/delaycheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func testDelay(p proxy.Proxy) (delay uint16, err error) {
pmap["port"] = int(pmap["port"].(float64))
if p.TypeName() == "vmess" {
pmap["alterId"] = int(pmap["alterId"].(float64))
if network := pmap["network"].(string); network == "h2" {
return 10, nil // todo 暂无方法测试h2的延迟,clash对于h2的connection会阻塞。但暂时不丢弃
if network, ok := pmap["network"]; ok && network.(string) == "h2" {
return 0, nil // todo 暂无方法测试h2的延迟,clash对于h2的connection会阻塞
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/healthcheck/speedcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ func ProxySpeedTest(p proxy.Proxy) (speedResult float64, err error) {
pmap["port"] = int(pmap["port"].(float64))
if p.TypeName() == "vmess" {
pmap["alterId"] = int(pmap["alterId"].(float64))
if network := pmap["network"].(string); network == "h2" {
return 0, nil // todo 暂无方法测试h2的延迟,clash对于h2的connection会阻塞。但暂时不丢弃
if network, ok := pmap["network"]; ok && network.(string) == "h2" {
return 0, nil // todo 暂无方法测试h2的速度,clash对于h2的connection会阻塞
}
}

Expand Down
40 changes: 40 additions & 0 deletions pkg/proxy/base.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package proxy

import (
"encoding/json"
"errors"
"strings"
)
Expand Down Expand Up @@ -99,3 +100,42 @@ func ParseProxyFromLink(link string) (p Proxy, err error) {
//}
return
}

func ParseProxyFromClashProxy(p map[string]interface{}) (proxy Proxy, err error) {
p["name"] = ""
pjson, err := json.Marshal(p)
if err != nil {
return nil, err
}
switch p["type"].(string) {
case "ss":
var proxy Shadowsocks
err := json.Unmarshal(pjson, &proxy)
if err != nil {
return nil, err
}
return &proxy, nil
case "ssr":
var proxy ShadowsocksR
err := json.Unmarshal(pjson, &proxy)
if err != nil {
return nil, err
}
return &proxy, nil
case "vmess":
var proxy Vmess
err := json.Unmarshal(pjson, &proxy)
if err != nil {
return nil, err
}
return &proxy, nil
case "trojan":
var proxy Trojan
err := json.Unmarshal(pjson, &proxy)
if err != nil {
return nil, err
}
return &proxy, nil
}
return nil, errors.New("clash json parse failed")
}

0 comments on commit e59ff68

Please sign in to comment.