forked from Sansui233/proxypool
-
Notifications
You must be signed in to change notification settings - Fork 6
/
task.go
214 lines (192 loc) · 6.42 KB
/
task.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package app
import (
"fmt"
"sync"
"time"
"github.com/One-Piecs/proxypool/pkg/geoIp"
"github.com/One-Piecs/proxypool/config"
"github.com/One-Piecs/proxypool/internal/cache"
"github.com/One-Piecs/proxypool/internal/database"
"github.com/One-Piecs/proxypool/log"
"github.com/One-Piecs/proxypool/pkg/healthcheck"
"github.com/One-Piecs/proxypool/pkg/provider"
"github.com/One-Piecs/proxypool/pkg/proxy"
)
var location, _ = time.LoadLocation("PRC")
func CrawlGo() {
wg := &sync.WaitGroup{}
pc := make(chan proxy.Proxy)
for _, g := range Getters {
wg.Add(1)
go g.Get2ChanWG(pc, wg)
}
proxies := cache.GetProxies("allproxies")
dbProxies := database.GetAllProxies()
// Show last time result when launch
if proxies == nil && dbProxies != nil {
cache.SetProxies("proxies", dbProxies)
cache.LastCrawlTime = "抓取中,已载入上次数据库数据"
log.Infoln("Database loaded count: %d", len(dbProxies))
}
if dbProxies != nil {
proxies = dbProxies.UniqAppendProxyList2(proxies)
}
if proxies == nil {
proxies = make(proxy.ProxyList, 0)
}
go func() {
wg.Wait()
close(pc)
}() // Note: 为何并发?可以一边抓取一边读取而非抓完再读
// 接收新增 proxy, 去重
mp := proxies.ToProxyMap()
for p := range pc { // Note: pc关闭后不能发送数据可以读取剩余数据
if p != nil {
mp.UniqAppendProxy(p)
}
}
proxies = mp.ToProxyList()
// proxies.NameClear()
proxies = proxies.Derive()
log.Infoln("CrawlGo unique proxy count: %d", len(proxies))
// Clean Clash unsupported proxy because health check depends on clash
proxies = provider.Clash{
Base: provider.Base{
Proxies: &proxies,
},
}.CleanProxies()
log.Infoln("CrawlGo clash supported proxy count: %d", len(proxies))
cache.SetProxies("allproxies", proxies)
cache.AllProxiesCount = proxies.Len()
log.Infoln("AllProxiesCount: %d", cache.AllProxiesCount)
cache.SSProxiesCount = proxies.TypeLen("ss")
log.Infoln("SSProxiesCount: %d", cache.SSProxiesCount)
cache.SSRProxiesCount = proxies.TypeLen("ssr")
log.Infoln("SSRProxiesCount: %d", cache.SSRProxiesCount)
cache.VmessProxiesCount = proxies.TypeLen("vmess")
log.Infoln("VmessProxiesCount: %d", cache.VmessProxiesCount)
cache.TrojanProxiesCount = proxies.TypeLen("trojan")
log.Infoln("TrojanProxiesCount: %d", cache.TrojanProxiesCount)
cache.LastCrawlTime = time.Now().In(location).Format("2006-01-02 15:04:05")
// 节点可用性检测,使用batchsize不能降低内存占用,只是为了看性能
log.Infoln("Now proceed proxy health check...")
/*
b := 1000
round := len(proxies) / b
okproxies := make(proxy.ProxyList, 0)
for i := 0; i < round; i++ {
okproxies = append(okproxies, healthcheck.CleanBadProxiesWithWorkpool(proxies[i*b:(i+1)*b])...)
log.Infoln("\tChecking round: %d", i)
}
okproxies = append(okproxies, healthcheck.CleanBadProxiesWithWorkpool(proxies[round*b:])...)
proxies = okproxies
*/
proxies = healthcheck.CleanBadProxiesWithWorkpool(proxies)
log.Infoln("CrawlGo clash usable proxy count: %d", len(proxies))
// 重命名节点名称为类似US_01的格式,并按国家排序
proxies.NameClear()
proxies.NameAddCountry().Sort()
log.Infoln("Proxy rename DONE!")
/*
// 中转检测并命名
healthcheck.RelayCheck(proxies)
for i, _ := range proxies {
if s, ok := healthcheck.ProxyStats.Find(proxies[i]); ok {
if s.Relay == true {
_, c, e := geoIp.GeoIpDB.Find(s.OutIp)
if e == nil {
proxies[i].SetName(fmt.Sprintf("Relay_%s-%s", proxies[i].BaseInfo().Name, c))
}
} else if s.Pool == true {
proxies[i].SetName(fmt.Sprintf("Pool_%s", proxies[i].BaseInfo().Name))
}
}
}
*/
// 中转检测并命名
healthcheck.RelayCheckWorkpool(proxies)
for i := range proxies {
if s, ok := healthcheck.ProxyStats.Find(proxies[i]); ok {
if s.Relay {
_, c, e := geoIp.GeoIpDB.Find(s.OutIp)
if e == nil {
// proxies[i].SetName(fmt.Sprintf("Relay_%s-%s", proxies[i].BaseInfo().Name, c))
if proxies[i].BaseInfo().Name == "🇨🇳 CN" {
// proxies[i].SetCountry(fmt.Sprintf("%s-%s", proxies[i].BaseInfo().Name, c))
// proxies[i].SetName(fmt.Sprintf("Relay_%s-%s", proxies[i].BaseInfo().Name, c))
proxies[i].SetCountry(c)
proxies[i].SetName(fmt.Sprintf("Relay %s", c))
} else {
proxies[i].SetCountry(c)
proxies[i].SetName(c)
}
}
// }
// else if s.Pool {
// proxies[i].SetName(fmt.Sprintf("Pool_%s", proxies[i].BaseInfo().Name))
}
}
}
// 检测是否支持ChatGPT
healthcheck.CheckWorkpool(proxies)
for i := range proxies {
if s, ok := healthcheck.ProxyStats.Find(proxies[i]); ok {
if s.ChatGPT {
proxies[i].SetName(fmt.Sprintf("ChatGPT %s", proxies[i].BaseInfo().Name))
}
}
}
proxies.Sort().NameAddIndex()
// 可用节点存储
cache.SetProxies("proxies", proxies)
cache.UsefullProxiesCount = proxies.Len()
cache.UsefullSSRProxiesCount = proxies.TypeLen("ssr")
cache.UsefullSSProxiesCount = proxies.TypeLen("ss")
cache.UsefullVmessProxiesCount = proxies.TypeLen("vmess")
cache.UsefullTrojanProxiesCount = proxies.TypeLen("trojan")
database.SaveProxyList(proxies)
// database.SaveBlockProxyList(healthcheck.ProxyInvalidStats)
database.ClearOldItems()
log.Infoln("Usablility checking done. Open %s to check", config.Config().Domain+":"+config.Config().Port)
// 测速
speedTestNew(proxies)
cache.SetString("clashproxies", provider.Clash{
Base: provider.Base{
Proxies: &proxies,
},
}.Provide()) // update static string provider
cache.SetString("surgeproxies", provider.Surge{
Base: provider.Base{
Proxies: &proxies,
},
}.Provide())
cache.SetString("loonproxies", provider.Loon{
Base: provider.Base{
Proxies: &proxies,
},
}.Provide())
}
// Speed test for new proxies
func speedTestNew(proxies proxy.ProxyList) {
if config.Config().SpeedTest {
cache.IsSpeedTest = "已开启"
if config.Config().Timeout > 0 {
healthcheck.SpeedTimeout = time.Second * time.Duration(config.Config().Timeout)
}
healthcheck.SpeedTestNewWithWorkpool(proxies, config.Config().Connection)
} else {
cache.IsSpeedTest = "未开启"
}
}
// Speed test for all proxies in proxy.ProxyList
func SpeedTest(proxies proxy.ProxyList) {
if config.Config().SpeedTest {
cache.IsSpeedTest = "已开启"
if config.Config().Timeout > 0 {
healthcheck.SpeedTimeout = time.Second * time.Duration(config.Config().Timeout)
}
healthcheck.SpeedTestAllWithWorkpool(proxies, config.Config().Connection)
} else {
cache.IsSpeedTest = "未开启"
}
}