forked from Simple-Tracker/qBittorrent-ClientBlocker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qbittorrent.go
129 lines (118 loc) · 3.65 KB
/
qbittorrent.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
package main
import (
"strings"
"strconv"
"encoding/json"
"net/url"
)
type MainDataStruct struct {
FullUpdate bool `json:"full_update"`
Torrents map[string]TorrentStruct `json:"torrents"`
}
type TorrentStruct struct {
NumLeechs int64 `json:"num_leechs"`
TotalSize int64 `json:"total_size"`
}
type PeerStruct struct {
IP string
Port int
Client string
Peer_ID_Client string
Progress float64
Uploaded int64
}
type TorrentPeersStruct struct {
FullUpdate bool `json:"full_update"`
Peers map[string]PeerStruct `json:"peers"`
}
var useNewBanPeersMethod = false
func Login() bool {
if config.QBUsername == "" {
return true
}
loginParams := url.Values {}
loginParams.Set("username", config.QBUsername)
loginParams.Set("password", config.QBPassword)
loginResponseBody := Submit(config.QBURL + "/api/v2/auth/login", loginParams.Encode(), false)
if loginResponseBody == nil {
Log("Login", "登录时发生了错误", true)
return false
}
loginResponseBodyStr := StrTrim(string(loginResponseBody))
if loginResponseBodyStr == "Ok." {
Log("Login", "登录成功", true)
return true
} else if loginResponseBodyStr == "Fails." {
Log("Login", "登录失败: 账号或密码错误", true)
} else {
Log("Login", "登录失败: %s", true, loginResponseBodyStr)
}
return false
}
func FetchMaindata() *MainDataStruct {
maindataResponseBody := Fetch(config.QBURL + "/api/v2/sync/maindata?rid=0", true)
if maindataResponseBody == nil {
Log("FetchMaindata", "发生错误", true)
return nil
}
var mainDataResult MainDataStruct
if err := json.Unmarshal(maindataResponseBody, &mainDataResult); err != nil {
Log("FetchMaindata", "解析时发生了错误: %s", true, err.Error())
return nil
}
//Log("Debug-FetchMaindata", "完整更新: %s", false, strconv.FormatBool(mainDataResult.FullUpdate))
return &mainDataResult
}
func FetchTorrentPeers(infoHash string) *TorrentPeersStruct {
torrentPeersResponseBody := Fetch(config.QBURL + "/api/v2/sync/torrentPeers?rid=0&hash=" + infoHash, true)
if torrentPeersResponseBody == nil {
Log("FetchTorrentPeers", "发生错误", true)
return nil
}
var torrentPeersResult TorrentPeersStruct
if err := json.Unmarshal(torrentPeersResponseBody, &torrentPeersResult); err != nil {
Log("FetchTorrentPeers", "解析时发生了错误: %s", true, err.Error())
return nil
}
/*
if config.Debug_CheckTorrent {
Log("Debug-FetchTorrentPeers", "完整更新: %s", false, strconv.FormatBool(torrentPeersResult.FullUpdate))
}
*/
return &torrentPeersResult
}
func GenBlockPeersStr() string {
ip_ports := ""
if useNewBanPeersMethod {
for peerIP, peerInfo := range blockPeerMap {
if _, exist := peerInfo.Port[-1]; config.BanAllPort || exist {
for port := 0; port <= 65535; port++ {
ip_ports += peerIP + ":" + strconv.Itoa(port) + "|"
}
continue
}
for port, _ := range peerInfo.Port {
ip_ports += peerIP + ":" + strconv.Itoa(port) + "|"
}
}
ip_ports = strings.TrimRight(ip_ports, "|")
} else {
for peerIP := range blockPeerMap {
ip_ports += peerIP + "\n"
}
}
return ip_ports
}
func SubmitBlockPeer(banIPPortsStr string) {
var banResponseBody []byte
if useNewBanPeersMethod && banIPPortsStr != "" {
banIPPortsStr = url.QueryEscape(banIPPortsStr)
banResponseBody = Submit(config.QBURL + "/api/v2/transfer/banPeers", banIPPortsStr, true)
} else {
banIPPortsStr = url.QueryEscape("{\"banned_IPs\": \"" + banIPPortsStr + "\"}")
banResponseBody = Submit(config.QBURL + "/api/v2/app/setPreferences", "json=" + banIPPortsStr, true)
}
if banResponseBody == nil {
Log("SubmitBlockPeer", "发生错误", true)
}
}