-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathwebui.go
More file actions
193 lines (163 loc) · 4.99 KB
/
webui.go
File metadata and controls
193 lines (163 loc) · 4.99 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
package main
import (
"crypto/subtle"
_ "embed"
"encoding/json"
"net/http"
"os"
"runtime"
"sort"
"strconv"
"sync/atomic"
"time"
)
//go:embed webui.html
var WebUI_Index_HTML []byte
type StatusResponse struct {
ProgramName string `json:"program_name"`
ProgramVersion string `json:"program_version"`
UptimeSeconds int64 `json:"uptime_seconds"`
ClientType string `json:"client_type"`
ClientURL string `json:"client_url"`
LoadedExtensions []string `json:"loaded_extensions"`
CurrentStats Stats `json:"stats"`
Runtime Runtime `json:"runtime"`
}
type Stats struct {
TotalBlockedIPs int `json:"total_blocked_ips"`
TotalBlockedPorts int `json:"total_blocked_ports"`
LastUpdateTimestamp int64 `json:"last_update_timestamp"`
}
type Runtime struct {
GoVersion string `json:"go_version"`
NumGoroutine int `json:"num_goroutine"`
}
type WebUIBlockPeer struct {
IP string `json:"ip"`
Timestamp int64 `json:"timestamp"`
Module string `json:"module"`
Reason string `json:"reason"`
Ports []string `json:"ports"`
ID string `json:"id"`
Client string `json:"client"`
Downloaded int64 `json:"downloaded"`
Uploaded int64 `json:"uploaded"`
}
func WebUI_IsPath(path string) bool {
return path == "/" || path == "/api/status" || path == "/api/peers" || path == "/api/logs"
}
func WebUI_CheckBasicAuth(w http.ResponseWriter, r *http.Request) bool {
if config.WebUIUsername == "" {
return true
}
username, password, ok := r.BasicAuth()
if ok &&
subtle.ConstantTimeCompare([]byte(username), []byte(config.WebUIUsername)) == 1 &&
subtle.ConstantTimeCompare([]byte(password), []byte(config.WebUIPassword)) == 1 {
return true
}
w.Header().Set("WWW-Authenticate", `Basic realm="qBittorrent-ClientBlocker WebUI", charset="UTF-8"`)
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("401: Unauthorized."))
return false
}
func GetWebUIBlockStats() (int, int) {
blockPeerMapMutex.RLock()
defer blockPeerMapMutex.RUnlock()
totalBlockedIPs := len(blockPeerMap)
totalBlockedPorts := 0
for _, peerInfo := range blockPeerMap {
totalBlockedPorts += len(peerInfo.Port)
}
return totalBlockedIPs, totalBlockedPorts
}
func GetWebUIBlockPeers() []WebUIBlockPeer {
blockPeerMapMutex.RLock()
defer blockPeerMapMutex.RUnlock()
peers := make([]WebUIBlockPeer, 0, len(blockPeerMap))
for peerIP, peerInfo := range blockPeerMap {
ports := make([]int, 0, len(peerInfo.Port))
for port := range peerInfo.Port {
ports = append(ports, port)
}
sort.Ints(ports)
portLabels := make([]string, 0, len(ports))
for _, port := range ports {
if port == -1 {
portLabels = append(portLabels, "ALL")
continue
}
portLabels = append(portLabels, strconv.Itoa(port))
}
peers = append(peers, WebUIBlockPeer{
IP: peerIP,
Timestamp: peerInfo.Timestamp,
Module: peerInfo.Module,
Reason: peerInfo.Reason,
Ports: portLabels,
ID: peerInfo.ID,
Client: peerInfo.Client,
Downloaded: peerInfo.Downloaded,
Uploaded: peerInfo.Uploaded,
})
}
sort.Slice(peers, func(i, j int) bool {
if peers[i].Timestamp == peers[j].Timestamp {
return peers[i].IP < peers[j].IP
}
return peers[i].Timestamp > peers[j].Timestamp
})
return peers
}
func WebUI_GetStatus(w http.ResponseWriter, r *http.Request) {
loadedExtensions := []string{}
if config.SyncServerURL != "" {
loadedExtensions = append(loadedExtensions, "SyncServer")
}
if btnConfig != nil {
loadedExtensions = append(loadedExtensions, "BTN")
}
totalBlockedIPs, totalBlockedPorts := GetWebUIBlockStats()
stats := Stats{
TotalBlockedIPs: totalBlockedIPs,
TotalBlockedPorts: totalBlockedPorts,
LastUpdateTimestamp: atomic.LoadInt64(¤tTimestamp),
}
resp := StatusResponse{
ProgramName: programName,
ProgramVersion: programVersion,
UptimeSeconds: time.Now().Unix() - programStartTimestamp,
ClientType: currentClientType,
ClientURL: config.ClientURL,
LoadedExtensions: loadedExtensions,
CurrentStats: stats,
Runtime: Runtime{
GoVersion: runtime.Version(),
NumGoroutine: runtime.NumGoroutine(),
},
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(resp)
}
func WebUI_GetPeers(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(GetWebUIBlockPeers())
}
func WebUI_GetLogs(w http.ResponseWriter, r *http.Request) {
logBufferMutex.Lock()
defer logBufferMutex.Unlock()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(logBuffer)
}
func WebUI_Index(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
// 优先检查当前目录是否有外部 webui.html.
const externalFile = "webui.html"
if _, err := os.Stat(externalFile); err == nil {
if content, err := os.ReadFile(externalFile); err == nil {
w.Write(content)
return
}
}
w.Write(WebUI_Index_HTML)
}