This repository has been archived by the owner on Dec 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
/
apihttp.go
174 lines (145 loc) · 3.9 KB
/
apihttp.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
package stoppropaganda
import (
"bytes"
"encoding/json"
"fmt"
"net"
"sort"
"sync"
"github.com/erkexzcx/stoppropaganda/internal/customresolver"
"github.com/valyala/fasthttp"
)
func fasthttpRequestHandler(ctx *fasthttp.RequestCtx) {
switch string(ctx.Path()) {
case "/status":
fasthttpStatusResponseHandler(ctx)
case "/dnscache":
fasthttpDnsCacheResponseHandler(ctx)
case "/downloaded":
fasthttpDownloadedResponseHandler(ctx)
case "/total":
fasthttpTotalResponseHandler(ctx)
}
}
type StatusStruct struct {
DNS map[string]*DNSTargetStatus `json:"DNS"`
Websites map[string]*WebsiteStatus `json:"Websites"`
}
type StatusService struct {
AllStatus StatusStruct
mux sync.Mutex
}
func fasthttpStatusResponseHandler(ctx *fasthttp.RequestCtx) {
statusService := StatusService{
AllStatus: StatusStruct{
DNS: make(map[string]*DNSTargetStatus, len(dnsTargets)),
Websites: make(map[string]*WebsiteStatus, len(websites)),
},
}
wg := sync.WaitGroup{}
wg.Add(len(dnsTargets))
wg.Add(len(websites))
for endpoint, ds := range dnsTargets {
go func(endpoint string, ds *DNSTarget) {
ds.mux.Lock()
dnsStatus := ds.Status
ds.mux.Unlock()
statusService.mux.Lock()
statusService.AllStatus.DNS[endpoint] = &dnsStatus
statusService.mux.Unlock()
wg.Done()
}(endpoint, ds)
}
for endpoint, ws := range websites {
go func(endpoint string, ws *Website) {
ws.statusMux.Lock()
tmpStatus := ws.status
ws.statusMux.Unlock()
statusService.mux.Lock()
statusService.AllStatus.Websites[endpoint] = &tmpStatus
statusService.mux.Unlock()
wg.Done()
}(endpoint, ws)
}
wg.Wait()
statusService.mux.Lock()
content, err := json.MarshalIndent(statusService.AllStatus, "", " ")
statusService.mux.Unlock()
if err != nil {
ctx.SetStatusCode(500)
ctx.WriteString("failed to marshal data")
return
}
ctx.Write(content)
}
func fasthttpDnsCacheResponseHandler(ctx *fasthttp.RequestCtx) {
cache := customresolver.DnsCache
dnsCacheItems := make(map[string][]net.IPAddr)
for k, v := range cache.Items() {
dnsCacheItems[k] = v.Object.([]net.IPAddr)
}
content, err := json.MarshalIndent(dnsCacheItems, "", " ")
if err != nil {
ctx.SetStatusCode(500)
ctx.WriteString("failed to marshal data")
return
}
ctx.Write(content)
}
type DownloadedStat struct {
Endpoint string
Downloaded uint64
}
func (ds *DownloadedStat) FormatMegabytes() string {
return fmt.Sprintf("%9.3f", float64(ds.Downloaded)/(1024.0*1024.0))
}
type DownloadedStats []*DownloadedStat
func (s DownloadedStats) Len() int { return len(s) }
func (s DownloadedStats) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s DownloadedStats) Less(i, j int) bool {
a := s[i].Downloaded
b := s[j].Downloaded
if a == b {
return s[i].Endpoint < s[j].Endpoint
}
return a > b
}
func fasthttpDownloadedResponseHandler(ctx *fasthttp.RequestCtx) {
stats := make([]*DownloadedStat, 0, len(websites))
for endpoint, website := range websites {
website.statusMux.Lock()
downloaded := website.status.Downloaded
website.statusMux.Unlock()
stat := &DownloadedStat{
Endpoint: endpoint,
Downloaded: downloaded,
}
stats = append(stats, stat)
}
sort.Sort(DownloadedStats(stats))
buf := new(bytes.Buffer)
if ctx.URI().QueryArgs().Has("raw") {
for _, stat := range stats {
fmt.Fprintf(buf, "%d\t%s\n", stat.Downloaded, stat.Endpoint)
}
} else {
for _, stat := range stats {
fmt.Fprintf(buf, "%s MB\t%s\n", stat.FormatMegabytes(), stat.Endpoint)
}
}
ctx.Write(buf.Bytes())
}
func fasthttpTotalResponseHandler(ctx *fasthttp.RequestCtx) {
downloaded := uint64(0)
for _, website := range websites {
website.statusMux.Lock()
downloaded += website.status.Downloaded
website.statusMux.Unlock()
}
if ctx.URI().QueryArgs().Has("raw") {
fmt.Fprintf(ctx, "%d", downloaded)
} else {
downloadedGB := float64(downloaded) / float64(1024*1024*1024)
fmt.Fprintf(ctx, "%.3f GB", downloadedGB)
}
}