forked from glblduh/torrenttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobals.go
173 lines (149 loc) · 4.92 KB
/
globals.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
/* Contains global variables and structs of the program */
package main
import (
"log"
"os"
"time"
"github.com/anacrolix/torrent"
)
/* Variables */
var (
/* BitTorrent client */
btEngine btEng
/* Loggers */
// For information
Info = log.New(os.Stderr, "["+time.Now().Format("2006/01/02 15:04:05")+"] [INFO] ", log.Lmsgprefix)
// For non-critical errors
Warn = log.New(os.Stderr, "["+time.Now().Format("2006/01/02 15:04:05")+"] [WARN] ", log.Lmsgprefix)
// For critical errors
Error = log.New(os.Stderr, "["+time.Now().Format("2006/01/02 15:04:05")+"] [ERROR] ", log.Lmsgprefix)
)
/* Structs for non-HTTP handlers */
type (
// BitTorrent client struct
btEng struct {
Client *torrent.Client
ClientConfig *torrent.ClientConfig
Torrents map[string]*torrentHandle
}
// Struct for persistent spec
persistentSpec struct {
Trackers [][]string
InfoHash string
DisplayName string
Webseeds []string
DhtNodes []string
PeerAddrs []string
Sources []string
DisableInitialPieceCheck bool
DisallowDataUpload bool
DisallowDataDownload bool
Files []string
}
// Holds non-native stats for *torrent.Torrent
torrentHandle struct {
/* Main handles */
Torrent *torrent.Torrent
Spec *torrent.TorrentSpec
/* Stats */
DlSpeedBytes int64
DlSpeedReadable string
UlSpeedBytes int64
UlSpeedReadable string
/* Temporary */
LastDlBytes int64
LastUlBytes int64
}
)
/* Structs of HTTP handlers */
type (
// Response of JSON error
jsonErrorRes struct {
Error string `json:"error"`
}
// Expected request body to addTorrent
apiAddTorrentBody struct {
Magnet string `json:"magnet"`
/*
These are optional for manual adding of torrent spec
The first priority is the magnet link rather than these
*/
InfoHash string `json:"infohash"`
DisplayName string `json:"displayname"`
Trackers []string `json:"trackers"`
}
// Expected response from addTorrent
apiAddTorrentRes struct {
Name string `json:"name"`
InfoHash string `json:"infohash"`
TotalPeers int `json:"totalpeers"`
ActivePeers int `json:"activepeers"`
PendingPeers int `json:"pendingpeers"`
HalfOpenPeers int `json:"halfopenpeers"`
Files []apiTorrentFiles `json:"files"`
}
// Struct for files in torrent
apiTorrentFiles struct {
FileName string `json:"filename"`
FileSizeBytes int `json:"filesizebytes"`
FileSizeReadable string `json:"filesize"`
}
// Expected request body to selectFile
apiTorrentSelectFileBody struct {
InfoHash string `json:"infohash"`
AllFiles bool `json:"allfiles"`
Files []string `json:"files"`
}
// Expected response body from selectFile
apiTorrentSelectFileRes struct {
Name string `json:"name"`
InfoHash string `json:"infohash"`
Files []apiTorrentSelectFileResFiles `json:"files"`
}
// Struct for selectFile Files
apiTorrentSelectFileResFiles struct {
FileName string `json:"filename"`
Stream string `json:"stream"`
Download string `json:"download"`
}
// Expected request body to removeTorrent
apiRemoveTorrentBody struct {
InfoHash string `json:"infohash"`
RemoveFiles bool `json:"removefiles"`
}
// Expected response body from removeTorrent
apiRemoveTorrentRes struct {
Name string `json:"name"`
InfoHash string `json:"infohash"`
}
// Expected response body from torrentStats
apiTorrentStasRes struct {
Torrents []apiTorrentStasResTorrents `json:"torrents"`
}
apiTorrentStasResTorrents struct {
Name string `json:"name"`
InfoHash string `json:"infohash"`
TotalPeers int `json:"totalpeers"`
ActivePeers int `json:"activepeers"`
PendingPeers int `json:"pendingpeers"`
HalfOpenPeers int `json:"halfopenpeers"`
Peers []apiTorrentStatsPeersInfo `json:"peers"`
DownloadSpeed string `json:"downloadspeed"`
UploadSpeed string `json:"uploadspeed"`
Progress string `json:"progress"`
Files []apiTorrentStatsTorrentsFiles `json:"files"`
}
apiTorrentStatsTorrentsFiles struct {
FileName string `json:"filename"`
FileSizeBytes int `json:"filesizebytes"`
FileSizeReadable string `json:"filesize"`
DownloadedBytes int `json:"downloadedbytes"`
DownloadedReadable string `json:"downloaded"`
Stream string `json:"stream,omitempty"`
Download string `json:"download,omitempty"`
}
apiTorrentStatsPeersInfo struct {
PeerAddr string `json:"peeraddr"`
PeerClient string `json:"peercli"`
}
)