-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathclient_BitComet.go
More file actions
476 lines (419 loc) · 11.8 KB
/
Copy pathclient_BitComet.go
File metadata and controls
476 lines (419 loc) · 11.8 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package main
import (
"bytes"
"encoding/json"
"github.com/PuerkitoBio/goquery"
"strconv"
"strings"
)
// BCClient 实现了 BitComet 的客户端接口.
type BCClient struct {
Version int // 1: HTML, 2: JSON (v2.09+)
}
func (c *BCClient) GetClientType() string {
return "BitComet"
}
func (c *BCClient) ConfigPath() string {
return ""
}
func (c *BCClient) SetURL() bool {
return false
}
func (c *BCClient) Login() bool {
return BC_Login()
}
// FetchTorrents 获取所有活动的种子列表.
func (c *BCClient) FetchTorrents() ([]*Torrent, error) {
if c.Version == 2 {
return c.FetchTorrents_v2()
}
torrents := BC_FetchTorrents()
if torrents == nil {
return nil, nil
}
var result []*Torrent
for id, t := range *torrents {
result = append(result, &Torrent{
Hash: strconv.Itoa(id),
TotalSize: t.TotalSize,
Tracker: "Unsupported",
LeechCount: 233,
})
}
return result, nil
}
// FetchTorrentPeers 获取特定种子的 Peer 列表.
func (c *BCClient) FetchTorrentPeers(torrent *Torrent) ([]*Peer, error) {
if c.Version == 2 {
return c.FetchTorrentPeers_v2(torrent)
}
peers := BC_FetchTorrentPeers(torrent.Hash)
if peers == nil {
return nil, nil
}
var result []*Peer
for _, p := range *peers {
result = append(result, &Peer{
IP: p.IP,
Port: p.Port,
Client: p.Client,
DlSpeed: p.DlSpeed,
UpSpeed: p.UpSpeed,
Progress: p.Progress,
Downloaded: p.Downloaded,
Uploaded: p.Uploaded,
ID: "", // BitComet 不提供 PeerID.
})
}
return result, nil
}
func (c *BCClient) SubmitBlockPeer(blockPeerMap map[string]BlockPeerInfoStruct) bool {
if c.Version == 2 {
return c.BC_SubmitBlockPeer_v2(blockPeerMap)
}
return false // BitComet 1.x 暂未通过 WebUI 实现封禁.
}
// Version 2 实现逻辑.
func (c *BCClient) FetchTorrents_v2() ([]*Torrent, error) {
_, _, responseBody := Fetch(config.ClientURL+"/api_v2/task_list/get?state_group=ACTIVE", true, true, false, nil)
if responseBody == nil {
return nil, nil
}
var resp BC_v2_TaskListResponse
if err := json.Unmarshal(responseBody, &resp); err != nil {
Log("FetchTorrents_v2", GetLangText("Error-Parse"), true, err.Error())
return nil, err
}
var result []*Torrent
for _, t := range resp.TaskList {
if strings.ToUpper(t.Type) != "BT" {
continue
}
result = append(result, &Torrent{
Hash: t.TaskID,
TotalSize: t.Size,
Tracker: "BitComet-v2",
LeechCount: int64(t.LeechCount),
})
}
return result, nil
}
func (c *BCClient) FetchTorrentPeers_v2(torrent *Torrent) ([]*Peer, error) {
_, _, responseBody := Fetch(config.ClientURL+"/api/task/peers/get?task_id="+torrent.Hash+"&groups=peers_connected", true, true, false, nil)
if responseBody == nil {
return nil, nil
}
var resp BC_v2_PeerListResponse
if err := json.Unmarshal(responseBody, &resp); err != nil {
Log("FetchTorrentPeers_v2", GetLangText("Error-Parse"), true, err.Error())
return nil, err
}
var result []*Peer
for _, p := range resp.PeerList {
result = append(result, &Peer{
IP: p.IP,
Port: p.Port,
Client: p.Client,
DlSpeed: p.DlSpeed,
UpSpeed: p.UpSpeed,
Progress: p.Progress / 100.0, // API 返回通常是 0-100.
Downloaded: p.Downloaded,
Uploaded: p.Uploaded,
})
}
return result, nil
}
func (c *BCClient) BC_SubmitBlockPeer_v2(blockPeerMap map[string]BlockPeerInfoStruct) bool {
// 按 Torrent 分组 IP 以匹配 ban_ip 接口要求.
taskIPs := make(map[string][]string)
for peerIP, peerInfo := range blockPeerMap {
if peerInfo.InfoHash != "" {
taskIPs[peerInfo.InfoHash] = append(taskIPs[peerInfo.InfoHash], peerIP)
}
}
if len(taskIPs) == 0 {
return true
}
allSuccess := true
for taskID, ips := range taskIPs {
params := BC_v2_BanParams{
TaskID: taskID,
BanTime: "ban_ip_forever",
IPList: ips,
}
postData, _ := json.Marshal(params)
code, _, _ := Submit(config.ClientURL+"/api/task/peers/ban_ip", postData, true, true, &Tr_jsonHeader)
if code != 200 {
allSuccess = false
}
}
return allSuccess
}
type BC_v2_BanParams struct {
TaskID string `json:"task_id"`
BanTime string `json:"ban_time"`
IPList []string `json:"ip_list"`
}
func (c *BCClient) SubmitShadowBanPeer(blockPeerMap map[string]BlockPeerInfoStruct) bool {
return false // 不支持.
}
type BC_TorrentStruct struct {
TotalSize int64
UpSpeed int64
}
type BC_PeerStruct struct {
IP string
Port int
Client string
// PeerID string
Progress float64
Downloaded int64
Uploaded int64
DlSpeed int64
UpSpeed int64
}
// BitComet v2 JSON API 结构体.
type BC_v2_CommonResponse struct {
Result string `json:"result"`
}
type BC_v2_TaskListResponse struct {
TaskList []BC_v2_Task `json:"movie_list"` // 该 API 实际返回的是 movie_list.
}
type BC_v2_Task struct {
TaskID string `json:"task_id"`
Type string `json:"type"`
Size int64 `json:"total_size"`
UpSpeed int64 `json:"upload_speed"`
Status string `json:"state"`
InfoHash string `json:"info_hash"`
Name string `json:"name"`
Progress int `json:"progress"`
LeechCount int `json:"leechers_count"`
}
type BC_v2_PeerListResponse struct {
PeerList []BC_v2_Peer `json:"peers_connected"`
}
type BC_v2_Peer struct {
IP string `json:"address"`
Port int `json:"remoteport"`
Client string `json:"clienttype"`
Progress float64 `json:"progress"`
DlSpeed int64 `json:"downrate"`
UpSpeed int64 `json:"uprate"`
Downloaded int64 `json:"downsize"`
Uploaded int64 `json:"upsize"`
}
func BC_ParseTorrentLink(torrentLinkStr string) int {
torrentIDSplit1 := strings.SplitN(StrTrim(torrentLinkStr), "?id=", 2)
if len(torrentIDSplit1) < 2 {
return -2
}
torrentIDStr := strings.SplitN(torrentIDSplit1[1], "&", 2)[0]
torrentID, err := strconv.Atoi(torrentIDStr)
if err != nil {
return -3
}
return torrentID
}
func BC_ParseSize(sizeStr string) int64 {
sizeStr = StrTrim(sizeStr)
if sizeStr == "" {
return 0
}
sizeStrSplit := strings.SplitN(sizeStr, " ", 2)
if len(sizeStrSplit) < 2 || len(sizeStrSplit[1]) < 2 {
return -1
}
rawSize, err := strconv.ParseFloat(sizeStrSplit[0], 64)
if err != nil {
return -2
}
matched := false
multipler := 1
switch strings.ToUpper(sizeStrSplit[1]) {
case "EB":
multipler *= 1024
fallthrough
case "PB":
multipler *= 1024
fallthrough
case "TB":
multipler *= 1024
fallthrough
case "GB":
multipler *= 1024
fallthrough
case "MB":
multipler *= 1024
fallthrough
case "KB":
multipler *= 1024
fallthrough
case "B":
matched = true
}
if !matched {
return -3
}
return int64(rawSize * float64(multipler))
}
func BC_ParseSpeed(speedStr string) int64 {
speedStr = StrTrim(speedStr)
if speedStr == "" {
return 0
}
speedStrSplit := strings.SplitN(speedStr, "/", 2)
if len(speedStrSplit) < 2 || len(speedStrSplit[1]) != 1 {
return -1
}
return BC_ParseSize(speedStrSplit[0])
}
func BC_ParsePercent(percentStr string) float64 {
percentStr = StrTrim(percentStr)
if len(percentStr) < 2 {
return -1
}
percentStr = percentStr[:(len(percentStr) - 1)]
percent, err := strconv.ParseFloat(percentStr, 64)
if err != nil {
return -2
}
return percent
}
func BC_ParseIP(ipStr string) (string, int) {
ipStr = strings.ToLower(StrTrim(ipStr))
if ipStr == "myself" {
return "", -1
}
lastColonIndex := strings.LastIndex(ipStr, ":")
if lastColonIndex == -1 || len(ipStr) < (lastColonIndex+2) {
return "", -2
}
ipWithoutPortStr := ipStr[:lastColonIndex]
portStr := ipStr[(lastColonIndex + 1):]
port, err := strconv.Atoi(portStr)
if err != nil {
return "", -3
}
return ipWithoutPortStr, port
}
func (c *BCClient) Detect() bool {
// 优先探测 Version 2 (JSON API).
apiResponseStatusCode, _, _ := Fetch(config.ClientURL+"/api_v2/task_list/get", false, false, false, nil)
if apiResponseStatusCode == 200 || apiResponseStatusCode == 401 {
c.Version = 2
Log("DetectClient", "BitComet (Version 2 - JSON API) Detected", true)
return true
}
// 回落探测 Version 1 (HTML).
apiResponseStatusCode, apiResponseHeaders, _ := Fetch(config.ClientURL+"/panel/", false, false, false, nil)
if apiResponseStatusCode == 401 && strings.Contains(apiResponseHeaders.Get("WWW-Authenticate"), "BitComet") {
c.Version = 1
Log("DetectClient", "BitComet (Version 1 - HTML) Detected", true)
return true
}
return false
}
func BC_Login() bool {
apiResponseStatusCode, _, _ := Fetch(config.ClientURL+"/panel/", false, true, false, nil)
return (apiResponseStatusCode == 200)
}
func BC_FetchTorrents() *map[int]BC_TorrentStruct {
_, _, torrentsResponseBody := Fetch(config.ClientURL+"/panel/task_list?group=active", true, true, false, nil)
if torrentsResponseBody == nil {
Log("FetchTorrents", GetLangText("Error"), true)
return nil
}
document, err := goquery.NewDocumentFromReader(bytes.NewReader(torrentsResponseBody))
if err != nil {
Log("FetchTorrents", GetLangText("Error-Parse"), true, err.Error())
return nil
}
torrentsMap := make(map[int]BC_TorrentStruct)
document.Find("table").Last().Find("tbody > tr").Each(func(index int, element *goquery.Selection) {
if index == 0 {
return
}
torrentStatus := ""
torrentID := 0
var torrentSize int64 = -233
var torrentUpSpeed int64 = -233
element.Find("td").EachWithBreak(func(tdIndex int, tdElement *goquery.Selection) bool {
switch tdIndex {
case 0:
if strings.ToUpper(StrTrim(tdElement.Text())) != "BT" {
return false
}
case 1:
href, exists := tdElement.Find("a").Attr("href")
if !exists {
return false
}
torrentID = BC_ParseTorrentLink(href)
case 2:
torrentStatus = strings.ToLower(StrTrim(tdElement.Text()))
case 4:
torrentSize = BC_ParseSize(tdElement.Text())
case 7:
torrentUpSpeed = BC_ParseSpeed(tdElement.Text())
}
return true
})
if torrentStatus == "" || torrentID <= 0 || torrentSize <= 0 || torrentUpSpeed < 0 {
return
}
torrentsMap[torrentID] = BC_TorrentStruct{TotalSize: torrentSize, UpSpeed: torrentUpSpeed}
})
return &torrentsMap
}
func BC_FetchTorrentPeers(infoHash string) *[]BC_PeerStruct {
_, _, torrentPeersResponseBody := Fetch(config.ClientURL+"/panel/task_detail?id="+infoHash+"&show=peers", true, true, false, nil)
if torrentPeersResponseBody == nil {
Log("FetchTorrentPeers", GetLangText("Error"), true)
return nil
}
document, err := goquery.NewDocumentFromReader(bytes.NewReader(torrentPeersResponseBody))
if err != nil {
Log("FetchTorrentPeers", GetLangText("Error-Parse"), true, err.Error())
return nil
}
torrentPeersMap := []BC_PeerStruct{}
document.Find("table").Last().Find("tbody > tr").Each(func(index int, element *goquery.Selection) {
if index == 0 {
return
}
peerIP := ""
peerPort := -233
var peerProgress float64 = -233
var peerDlSpeed int64 = -233
var peerUpSpeed int64 = -233
var peerDownloaded int64 = -233
var peerUploaded int64 = -233
peerClient := ""
element.Find("td").EachWithBreak(func(tdIndex int, tdElement *goquery.Selection) bool {
switch tdIndex {
case 0:
peerIP, peerPort = BC_ParseIP(tdElement.Text())
case 1:
peerProgress = BC_ParsePercent(tdElement.Text())
case 2:
peerDlSpeed = BC_ParseSpeed(tdElement.Text())
case 3:
peerUpSpeed = BC_ParseSpeed(tdElement.Text())
case 4:
peerDownloaded = BC_ParseSize(tdElement.Text())
case 5:
peerUploaded = BC_ParseSize(tdElement.Text())
case 9:
peerClient = tdElement.Text()
}
return true
})
if peerIP == "" || peerPort < 0 || peerProgress < 0 || peerDlSpeed < 0 || peerUpSpeed < 0 || peerDownloaded < 0 || peerUploaded < 0 {
return
}
peerStruct := BC_PeerStruct{IP: peerIP, Port: peerPort, Client: peerClient, Progress: peerProgress, Downloaded: peerDownloaded, Uploaded: peerUploaded, DlSpeed: peerDlSpeed, UpSpeed: peerUpSpeed}
torrentPeersMap = append(torrentPeersMap, peerStruct)
})
return &torrentPeersMap
}