-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathclient_Transmission.go
229 lines (197 loc) · 7.05 KB
/
client_Transmission.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
package main
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"time"
)
type Tr_RequestStruct struct {
Method string `json:"method"`
Args interface{} `json:"arguments"`
}
type Tr_ResponseStruct struct {
Result string `json:"result"`
}
type Tr_TorrentsResponseStruct struct {
Result string `json:"result"`
Args Tr_TorrentsStruct `json:"arguments"`
}
type Tr_ArgsStruct struct {
Field []string `json:"fields"`
}
type Tr_ArgTorrentsStruct struct {
IDs []string `json:"ids"`
Field []string `json:"fields"`
}
type Tr_SessionSetStruct struct {
BlocklistEnabled bool `json:"blocklist-enabled"`
BlocklistSize int `json:"blocklist-size"`
BlocklistURL string `json:"blocklist-url"`
}
type Tr_TorrentsStruct struct {
Torrents []Tr_TorrentStruct `json:"torrents"`
}
type Tr_TorrentStruct struct {
InfoHash string `json:"hashString"`
TotalSize int64 `json:"totalSize"`
Private bool `json:"private"`
Peers []Tr_PeerStruct `json:"peers"`
}
type Tr_PeerStruct struct {
IP string `json:"address"`
Port int `json:"port"`
Client string `json:"clientName"`
Progress float64 `json:"progress"`
IsUploading bool `json:"isUploadingTo"`
DlSpeed int64 `json:"rateToClient"`
UpSpeed int64 `json:"rateToPeer"`
}
var Tr_csrfToken = ""
var Tr_ipfilterStr = ""
var Tr_jsonHeader = map[string]string{"Content-Type": "application/json"}
func Tr_InitClient() {
go StartServer()
}
func Tr_ProcessHTTP(w http.ResponseWriter, r *http.Request) bool {
if strings.SplitN(r.RequestURI, "?", 2)[0] == "/ipfilter.dat" {
w.WriteHeader(200)
w.Write([]byte(Tr_ipfilterStr))
return true
}
return false
}
func Tr_SetURL() bool {
return false
}
func Tr_DetectVersion() bool {
detectJSON, err := json.Marshal(Tr_RequestStruct{Method: "session-get", Args: Tr_ArgsStruct{Field: []string{"version"}}})
if err != nil {
Log("DetectVersion", GetLangText("Error-GenJSON"), true, err.Error())
return false
}
detectStatusCode, _, _ := Submit(config.ClientURL, string(detectJSON), false, false, &Tr_jsonHeader)
return (detectStatusCode == 200 || detectStatusCode == 409)
}
func Tr_Login() bool {
// Transmission 通过 Basic Auth 进行认证, 因此实际处理 CSRF 请求以避免 409 响应.
loginJSON, err := json.Marshal(Tr_RequestStruct{Method: "session-get"})
if err != nil {
Log("Login", GetLangText("Error-GenJSON"), true, err.Error())
return false
}
Submit(config.ClientURL, string(loginJSON), false, true, nil)
if Tr_csrfToken == "" {
Log("Login", GetLangText("Error-Login"), true)
return false
}
return true
}
func Tr_SetCSRFToken(csrfToken string) {
Tr_csrfToken = csrfToken
Log("SetCSRFToken", GetLangText("Success-SetCSRFToken"), true, csrfToken)
}
func Tr_FetchTorrents() *Tr_TorrentsStruct {
loginJSON, err := json.Marshal(Tr_RequestStruct{Method: "torrent-get", Args: Tr_ArgsStruct{Field: []string{"hashString", "totalSize", "isPrivate", "peers"}}})
if err != nil {
Log("FetchTorrents", GetLangText("Error-GenJSON"), true, err.Error())
return nil
}
_, _, torrentsResponseBody := Submit(config.ClientURL, string(loginJSON), true, true, &Tr_jsonHeader)
if torrentsResponseBody == nil {
Log("FetchTorrents", GetLangText("Error"), true)
return nil
}
var torrentsResponse Tr_TorrentsResponseStruct
if err := json.Unmarshal(torrentsResponseBody, &torrentsResponse); err != nil {
Log("FetchTorrents", GetLangText("Error-Parse"), true, err.Error())
return nil
}
if torrentsResponse.Result != "success" {
Log("FetchTorrents", GetLangText("Error-Parse"), true, torrentsResponse.Result)
return nil
}
return &torrentsResponse.Args
}
func Tr_RestartTorrentByMap(blockPeerMap map[string]BlockPeerInfoStruct) {
peerInfoHashes := []string{}
for _, peerInfo := range blockPeerMap {
peerInfoHashes = append(peerInfoHashes, peerInfo.InfoHash)
}
if len(peerInfoHashes) <= 0 {
return
}
stopJSON, err := json.Marshal(Tr_RequestStruct{Method: "torrent-stop", Args: Tr_ArgTorrentsStruct{IDs: peerInfoHashes}})
if err != nil {
Log("RestartTorrentByMap", GetLangText("Error-GenJSON"), true, err.Error())
return
}
stopStatusCode, _, _ := Submit(config.ClientURL, string(stopJSON), true, true, &Tr_jsonHeader)
if stopStatusCode != 200 {
Log("RestartTorrentByMap", GetLangText("Error-RestartTorrentByMap_Stop"), true, err.Error())
return
}
Log("RestartTorrentByMap", GetLangText("Debug-RestartTorrentByMap_Wait"), true, config.Interval)
time.Sleep(time.Duration(config.RestartInterval) * time.Second)
startJSON, err := json.Marshal(Tr_RequestStruct{Method: "torrent-start", Args: Tr_ArgTorrentsStruct{IDs: peerInfoHashes}})
if err != nil {
Log("RestartTorrentByMap", GetLangText("Error-GenJSON"), true, err.Error())
return
}
startStatusCode, _, _ := Submit(config.ClientURL, string(startJSON), true, true, &Tr_jsonHeader)
if startStatusCode != 200 {
Log("RestartTorrentByMap", GetLangText("Error-RestartTorrentByMap_Start"), true, err.Error())
return
}
}
func Tr_SubmitBlockPeer(blockPeerMap map[string]BlockPeerInfoStruct) bool {
ipfilterCount, ipfilterStr := GenIPFilter(2, blockPeerMap)
Tr_ipfilterStr = ipfilterStr
blocklistURL := ""
if strings.Contains(config.Listen, ".") {
blocklistURL = "http://" + config.Listen
} else {
blocklistURL = "http://127.0.0.1" + config.Listen
}
blocklistURL += "/ipfilter.dat?t=" + strconv.FormatInt(currentTimestamp, 10)
sessionSetJSON, err := json.Marshal(Tr_RequestStruct{Method: "session-set", Args: Tr_SessionSetStruct{BlocklistEnabled: true, BlocklistSize: ipfilterCount, BlocklistURL: blocklistURL}})
if err != nil {
Log("SubmitBlockPeer", GetLangText("Error-GenJSON"), true, err.Error())
return false
}
_, _, sessionResponseBody := Submit(config.ClientURL, string(sessionSetJSON), true, true, &Tr_jsonHeader)
if sessionResponseBody == nil {
Log("SubmitBlockPeer", GetLangText("Error"), true)
return false
}
var sessionResponse Tr_ResponseStruct
if err := json.Unmarshal(sessionResponseBody, &sessionResponse); err != nil {
Log("SubmitBlockPeer", GetLangText("Error-Parse"), true, err.Error())
return false
}
if sessionResponse.Result != "success" {
Log("SubmitBlockPeer", GetLangText("Error-Parse"), true, sessionResponse.Result)
return false
}
blocklistUpdateJSON, err := json.Marshal(Tr_RequestStruct{Method: "blocklist-update"})
if err != nil {
Log("SubmitBlockPeer", GetLangText("Error-GenJSON"), true, err.Error())
return false
}
_, _, blocklistUpdateResponseBody := Submit(config.ClientURL, string(blocklistUpdateJSON), true, true, &Tr_jsonHeader)
if blocklistUpdateResponseBody == nil {
Log("SubmitBlockPeer", GetLangText("Error"), true)
return false
}
var blocklistUpdateResponse Tr_ResponseStruct
if err := json.Unmarshal(blocklistUpdateResponseBody, &blocklistUpdateResponse); err != nil {
Log("SubmitBlockPeer", GetLangText("Error-Parse"), true, err.Error())
return false
}
if blocklistUpdateResponse.Result != "success" {
Log("SubmitBlockPeer", GetLangText("Error-Parse"), true, blocklistUpdateResponse.Result)
return false
}
Tr_RestartTorrentByMap(blockPeerMap)
return true
}