-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathclient_qBittorrent.go
346 lines (313 loc) · 10.5 KB
/
client_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
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
package main
import (
"encoding/json"
"net/url"
"os"
"strconv"
"strings"
)
type qB_TorrentStruct struct {
InfoHash string `json:"hash"`
NumLeechs int64 `json:"num_leechs"`
TotalSize int64 `json:"total_size"`
Tracker string `json:"tracker"`
}
type qB_PeerStruct struct {
IP string `json:"ip"`
Port int `json:"port"`
Client string `json:"client"`
PeerID string `json:"peer_id_client"`
Progress float64 `json:"progress"`
Downloaded int64 `json:"downloaded"`
Uploaded int64 `json:"uploaded"`
DlSpeed int64 `json:"dl_speed"`
UpSpeed int64 `json:"up_speed"`
}
type qB_TorrentPeersStruct struct {
FullUpdate bool `json:"full_update"`
Peers map[string]qB_PeerStruct `json:"peers"`
}
var qB_useNewBanPeersMethod = false
func qB_GetClientConfigPath() string {
var qBConfigFilename string
userHomeDir, err := os.UserHomeDir()
if err != nil {
Log("Debug-GetClientConfigPath", GetLangText("Error-Debug-GetClientConfigPath_GetUserHomeDir"), true, err.Error())
return ""
}
if IsUnix(userHomeDir) {
qBConfigFilename = userHomeDir + "/.config/qBittorrent/qBittorrent.ini"
} else {
userConfigDir, err := os.UserConfigDir()
if err != nil {
Log("Debug-GetClientConfigPath", GetLangText("Error-Debug-GetClientConfigPath_GetUserConfigDir"), true, err.Error())
return ""
}
qBConfigFilename = userConfigDir + "\\qBittorrent\\qBittorrent.ini"
}
return qBConfigFilename
}
func qB_GetClientConfig() []byte {
qBConfigFilename := qB_GetClientConfigPath()
if qBConfigFilename == "" {
return []byte{}
}
_, err := os.Stat(qBConfigFilename)
if err != nil {
if !os.IsNotExist(err) {
// 避免反复猜测默认 qBittorrent 配置文件的失败信息影响 Debug 用户体验.
Log("GetClientConfig", GetLangText("Error-GetClientConfig_LoadConfigMeta"), true, err.Error())
}
return []byte{}
}
Log("GetClientConfig", GetLangText("GetClientConfig_UseConfig"), true, qBConfigFilename)
qBConfigFile, err := os.ReadFile(qBConfigFilename)
if err != nil {
Log("GetClientConfig", GetLangText("Error-GetClientConfig_LoadConfig"), true, err.Error())
return []byte{}
}
return qBConfigFile
}
func qB_SetURL() bool {
qBConfigFile := qB_GetClientConfig()
if len(qBConfigFile) < 1 {
return false
}
qBConfigFileArr := strings.Split(string(qBConfigFile), "\n")
qBWebUIEnabled := false
qBHTTPSEnabled := false
qBAddress := ""
qBPort := 8080
Username := ""
for _, qbConfigLine := range qBConfigFileArr {
qbConfigLineArr := strings.SplitN(qbConfigLine, "=", 2)
if len(qbConfigLineArr) < 2 || qbConfigLineArr[1] == "" {
continue
}
qbConfigLineArr[0] = strings.ToLower(StrTrim(qbConfigLineArr[0]))
qbConfigLineArr[1] = strings.ToLower(StrTrim(qbConfigLineArr[1]))
switch qbConfigLineArr[0] {
case "webui\\enabled":
if qbConfigLineArr[1] == "true" {
qBWebUIEnabled = true
}
case "webui\\https\\enabled":
if qbConfigLineArr[1] == "true" {
qBHTTPSEnabled = true
}
case "webui\\address":
if qbConfigLineArr[1] == "*" || qbConfigLineArr[1] == "0.0.0.0" {
qBAddress = "127.0.0.1"
} else if qbConfigLineArr[1] == "::" || qbConfigLineArr[1] == "::1" {
qBAddress = "[::1]"
} else {
qBAddress = qbConfigLineArr[1]
}
case "webui\\port":
tmpQBPort, err := strconv.Atoi(qbConfigLineArr[1])
if err == nil {
qBPort = tmpQBPort
}
case "webui\\username":
Username = qbConfigLineArr[1]
}
}
if !qBWebUIEnabled || qBAddress == "" {
Log("SetURL", GetLangText("Abandon-SetURL"), true, qBWebUIEnabled, qBAddress)
return false
}
if qBHTTPSEnabled {
config.ClientURL = "https://" + qBAddress
if qBPort != 443 {
config.ClientURL += ":" + strconv.Itoa(qBPort)
}
} else {
config.ClientURL = "http://" + qBAddress
if qBPort != 80 {
config.ClientURL += ":" + strconv.Itoa(qBPort)
}
}
config.ClientURL += "/api"
config.ClientUsername = Username
Log("SetURL", GetLangText("Success-SetURL"), true, qBWebUIEnabled, config.ClientURL, config.ClientUsername)
return true
}
func qB_GetAPIVersion() bool {
if !strings.HasSuffix(config.ClientURL, "/api") {
apiResponseStatusCodeWithSuffix, _, _ := Fetch(config.ClientURL+"/api/v2/app/webapiVersion", false, false, false, nil)
if apiResponseStatusCodeWithSuffix == 200 || apiResponseStatusCodeWithSuffix == 403 {
config.ClientURL += "/api"
Log("qB_GetAPIVersion", GetLangText("ClientQB_Detect-OldClientURL"), true, config.ClientURL)
return true
}
}
apiResponseStatusCode, _, _ := Fetch(config.ClientURL+"/v2/app/webapiVersion", false, false, false, nil)
return (apiResponseStatusCode == 200 || apiResponseStatusCode == 403)
}
func qB_Login() bool {
loginParams := url.Values{}
loginParams.Set("username", config.ClientUsername)
loginParams.Set("password", config.ClientPassword)
_, _, loginResponseBody := Submit(config.ClientURL+"/v2/auth/login", loginParams.Encode(), false, true, nil)
if loginResponseBody == nil {
Log("Login", GetLangText("Error-Login"), true)
return false
}
loginResponseBodyStr := StrTrim(string(loginResponseBody))
if loginResponseBodyStr == "Ok." {
Log("Login", GetLangText("Success-Login"), true)
return true
} else if loginResponseBodyStr == "Fails." {
Log("Login", GetLangText("Failed-Login_BadUsernameOrPassword"), true)
} else {
Log("Login", GetLangText("Failed-Login_Other"), true, loginResponseBodyStr)
}
return false
}
func qB_FetchTorrents() *[]qB_TorrentStruct {
_, _, torrentsResponseBody := Fetch(config.ClientURL+"/v2/torrents/info?filter=active", true, true, false, nil)
if torrentsResponseBody == nil {
Log("FetchTorrents", GetLangText("Error"), true)
return nil
}
var torrentsResult []qB_TorrentStruct
if err := json.Unmarshal(torrentsResponseBody, &torrentsResult); err != nil {
Log("FetchTorrents", GetLangText("Error-Parse"), true, err.Error())
return nil
}
return &torrentsResult
}
func qB_FetchTorrentPeers(infoHash string) *qB_TorrentPeersStruct {
_, _, torrentPeersResponseBody := Fetch(config.ClientURL+"/v2/sync/torrentPeers?rid=0&hash="+infoHash, true, true, false, nil)
if torrentPeersResponseBody == nil {
Log("FetchTorrentPeers", GetLangText("Error"), true)
return nil
}
var torrentPeersResult qB_TorrentPeersStruct
if err := json.Unmarshal(torrentPeersResponseBody, &torrentPeersResult); err != nil {
Log("FetchTorrentPeers", GetLangText("Error-Parse"), true, err.Error())
return nil
}
/*
if config.Debug_CheckTorrent {
Log("Debug-FetchTorrentPeers", "完整更新: %s", false, strconv.FormatBool(torrentPeersResult.FullUpdate))
}
*/
return &torrentPeersResult
}
func qB_SubmitBlockPeer(blockPeerMap map[string]BlockPeerInfoStruct) bool {
banIPPortsStr := ""
if blockPeerMap != nil {
if qB_useNewBanPeersMethod {
for peerIP, peerInfo := range blockPeerMap {
if _, exist := peerInfo.Port[-1]; config.BanAllPort || exist {
for port := 0; port <= 65535; port++ {
if IsIPv6(peerIP) {
banIPPortsStr += "[" + peerIP + "]:" + strconv.Itoa(port) + "|"
} else {
banIPPortsStr += peerIP + ":" + strconv.Itoa(port) + "|"
banIPPortsStr += "[::ffff:" + peerIP + "]:" + strconv.Itoa(port) + "|"
}
}
continue
}
for port, _ := range peerInfo.Port {
banIPPortsStr += peerIP + ":" + strconv.Itoa(port) + "|"
}
}
banIPPortsStr = strings.TrimRight(banIPPortsStr, "|")
} else {
for peerIP := range blockPeerMap {
banIPPortsStr += peerIP + "\n"
if !IsIPv6(peerIP) {
banIPPortsStr += "::ffff:" + peerIP + "\n"
}
}
}
}
Log("Debug-SubmitBlockPeer", "%s", false, banIPPortsStr)
var banResponseBody []byte
if qB_useNewBanPeersMethod && banIPPortsStr != "" {
banIPPortsStr = url.QueryEscape(banIPPortsStr)
_, _, banResponseBody = Submit(config.ClientURL+"/v2/transfer/banPeers", banIPPortsStr, true, true, nil)
} else {
banIPPortsStr = url.QueryEscape("{\"banned_IPs\": \"" + banIPPortsStr + "\"}")
_, _, banResponseBody = Submit(config.ClientURL+"/v2/app/setPreferences", "json="+banIPPortsStr, true, true, nil)
}
if banResponseBody == nil {
Log("SubmitBlockPeer", GetLangText("Error"), true)
return false
}
return true
}
func qB_GetPreferences() map[string]interface{} {
_, _, responseBody := Submit(config.ClientURL+"/v2/app/preferences", "", true, true, nil)
if responseBody == nil {
Log("GetPreferences", GetLangText("Failed-GetQBPreferences"), true)
return nil
}
var preferences map[string]interface{}
if err := json.Unmarshal(responseBody, &preferences); err != nil {
Log("GetPreferences", GetLangText("Error-Parse"), true, err.Error())
return nil
}
return preferences
}
func qB_TestShadowBanAPI() bool {
// 1. Check if enable_shadowban is true;
// enable_shadowban may be not exist in the preferences.
pref := qB_GetPreferences()
if pref == nil {
return false
}
enableShadowBan, exist := pref["shadow_ban_enabled"]
if !exist {
Log("TestShadowBanAPI", GetLangText("Warning-ShadowBanAPINotExist"), true)
return false
}
if bEnableShadowBan, ok := enableShadowBan.(bool); ok {
if !bEnableShadowBan {
return false
}
} else {
Log("TestShadowBanAPI", GetLangText("Failed-UnknownShadowBanAPI"), true)
return false
}
// 2. Check if the API is available;
code, _, _ := Submit(config.ClientURL+"/v2/transfer/shadowbanPeers", "peers=", true, true, nil)
if code != 200 {
Log("TestShadowBanAPI", GetLangText("Warning-ShadowBanAPINotExist"), true)
return false
}
return true
}
func qB_SubmitShadowBanPeer(blockPeerMap map[string]BlockPeerInfoStruct) bool {
shadowBanIPPortsList := []string{}
for peerIP, peerInfo := range blockPeerMap {
for port := range peerInfo.Port {
if port <= 0 || port > 65535 {
port = 1 // Seems qBittorrent will ignore the invalid port number, so we just set it to 1.
}
if IsIPv6(peerIP) {
shadowBanIPPortsList = append(shadowBanIPPortsList, "[" + peerIP + "]:" + strconv.Itoa(port))
} else {
shadowBanIPPortsList = append(shadowBanIPPortsList, peerIP + ":" + strconv.Itoa(port))
shadowBanIPPortsList = append(shadowBanIPPortsList, "[::ffff:" + peerIP + "]:" + strconv.Itoa(port))
}
}
}
banIPPortsStr := strings.Join(shadowBanIPPortsList, "|")
Log("Debug-SubmitShadowBanPeer", "%s", false, banIPPortsStr)
var banResponseBody []byte
if banIPPortsStr != "" {
banIPPortsStr = url.QueryEscape(banIPPortsStr)
_, _, banResponseBody = Submit(config.ClientURL+"/v2/transfer/shadowbanPeers", "peers="+banIPPortsStr, true, true, nil)
} else {
return true
}
if banResponseBody == nil {
Log("SubmitShadowBanPeer", GetLangText("Error"), true)
return false
}
return true
}