forked from Sansui233/proxypool
-
Notifications
You must be signed in to change notification settings - Fork 6
/
clash.go
76 lines (65 loc) · 1.44 KB
/
clash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package getter
import (
"io/ioutil"
"sync"
"time"
"github.com/One-Piecs/proxypool/log"
"github.com/One-Piecs/proxypool/pkg/proxy"
"github.com/One-Piecs/proxypool/pkg/tool"
"gopkg.in/yaml.v3"
)
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", len(nodes), c.Url)
for _, node := range nodes {
pc <- node
}
}
func (c *Clash) Get2ChanWG(pc chan proxy.Proxy, wg *sync.WaitGroup) {
defer wg.Done()
start := time.Now()
nodes := c.Get()
log.Infoln("STATISTIC: Clash\tcost=%v\tcount=%d\turl=%s", time.Since(start), 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
}