-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanswers.go
382 lines (367 loc) · 8.77 KB
/
answers.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package main
import (
"encoding/base32"
"encoding/binary"
"encoding/json"
"fmt"
"sort"
"strings"
"time"
pb "github.com/usher2/u2byckdump/msg"
)
const encodeCorc = "abcdefghjkmnpqrstvwxyz0123456789"
const PRINT_LIMIT = 5
const MAX_TIMESTAMP int64 = 1<<63 - 1
const (
DTIME72 = 72 * 60 * 60
DTIME36 = 36 * 60 * 60
)
const (
_ = iota
OFFSET_CONTENT
OFFSET_IP4
OFFSET_URL
OFFSET_DOMAIN
)
type TPagination struct {
Tag, Count int
}
type TReason struct {
Id int32
Ip []string
Url []string
Domain []string
}
func Uint64ToBase32(i uint64) string {
b32 := base32.NewEncoding(encodeCorc).WithPadding(base32.NoPadding)
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, i)
return b32.EncodeToString(b)
}
func Base32ToUint64(s string) (uint64, error) {
b32 := base32.NewEncoding(encodeCorc).WithPadding(base32.NoPadding)
b, err := b32.DecodeString(s)
if err == nil {
return binary.LittleEndian.Uint64(b), nil
}
return 0, err
}
func printUpToDate(t int64) string {
var r rune
d := time.Now().Unix() - t
switch {
case d > DTIME72:
r = 0x2b55
case d > DTIME36:
r = 0x000026a0
default:
r = 0x2705
}
return fmt.Sprintf("\n%c _Данные синхронизированы:_ %s\n", r, time.Unix(t, 0).In(time.FixedZone("UTC+3", 3*60*60)).Format(time.RFC3339))
}
func constructContentResult(a []*pb.Content, o TPagination) (res string, pages []TPagination) {
var oldest int64 = MAX_TIMESTAMP
if len(a) == 0 {
return
}
for _, packet := range a {
content := TContent{}
err := json.Unmarshal(packet.Pack, &content)
if err != nil {
Error.Printf("Упс!!! %s\n", err)
continue
}
if packet.RegistryUpdateTime < oldest {
oldest = packet.RegistryUpdateTime
}
bt := ""
switch packet.BlockType {
case TBLOCK_URL:
bt = "\U000026d4 (url) "
case TBLOCK_HTTPS:
bt = "\U0001f4db (https) "
case TBLOCK_DOMAIN:
bt = "\U0001f6ab (domain) "
case TBLOCK_IP:
bt = "\u274c (ip) "
}
res += fmt.Sprintf("%s /n\\_%s %s %s\n", bt, Uint64ToBase32(content.U2Hash), Sanitize(content.Decision), Sanitize(content.DecisionInfo))
res += fmt.Sprintf("внесено: %s\n", time.Unix(content.IncludeTime, 0).Format("2006-01-02"))
res += "\n"
cnt := 0
for i, d := range content.Domain {
if o.Tag == OFFSET_DOMAIN && i < o.Count {
continue
}
if cnt >= PRINT_LIMIT {
break
}
res += fmt.Sprintf(" domain: %s\n", Sanitize(d.Domain))
cnt++
}
l := len(content.Domain)
if l > PRINT_LIMIT {
offset := 0
if o.Tag == OFFSET_DOMAIN {
switch {
case l <= PRINT_LIMIT:
offset = 0
case o.Count > l-(l%PRINT_LIMIT):
offset = l - (l % PRINT_LIMIT)
default:
offset = o.Count
}
}
res += fmt.Sprintf(" \u2195 результаты с *%d* по *%d* из *%d*\n", offset+1, offset+cnt, l)
pages = append(pages, TPagination{OFFSET_DOMAIN, l})
}
if cnt > 0 {
res += "\n"
}
cnt = 0
for i, u := range content.Url {
if o.Tag == OFFSET_URL && i < o.Count {
continue
}
if cnt >= PRINT_LIMIT {
break
}
res += fmt.Sprintf(" url: %s\n", Sanitize(u.Url))
cnt++
}
l = len(content.Url)
if l > PRINT_LIMIT {
offset := 0
if o.Tag == OFFSET_URL {
switch {
case l <= PRINT_LIMIT:
offset = 0
case o.Count > l-(l%PRINT_LIMIT):
offset = l - (l % PRINT_LIMIT)
default:
offset = o.Count
}
}
res += fmt.Sprintf(" \u2195 результаты с *%d* по *%d* из *%d*\n", offset+1, offset+cnt, l)
pages = append(pages, TPagination{OFFSET_URL, l})
}
if cnt > 0 {
res += "\n"
}
cnt = 0
for i, ip := range content.Ip4 {
if o.Tag == OFFSET_IP4 && i < o.Count {
continue
}
if cnt >= PRINT_LIMIT {
break
}
res += fmt.Sprintf(" IP: %s\n", int2Ip4(ip.Ip4))
cnt++
}
l = len(content.Ip4)
if l > PRINT_LIMIT {
offset := 0
if o.Tag == OFFSET_IP4 {
switch {
case l <= PRINT_LIMIT:
offset = 0
case o.Count > l-(l%PRINT_LIMIT):
offset = l - (l % PRINT_LIMIT)
default:
offset = o.Count
}
}
res += fmt.Sprintf(" \u2195 результаты с *%d* по *%d* из *%d*\n", offset+1, offset+cnt, l)
pages = append(pages, TPagination{OFFSET_IP4, l})
}
break
}
res += printUpToDate(oldest)
return
}
func constructResult(a []*pb.Content, o TPagination) (res string, pages []TPagination) {
var mass string
var oldest int64 = MAX_TIMESTAMP
var ra []TReason
if len(a) == 0 {
return
}
sort.Slice(a, func(i, j int) bool {
return a[i].Id < a[j].Id
})
ra = make([]TReason, 1)
ra[0].Id = a[0].Id
if a[0].Ip4 != 0 {
ra[0].Ip = append(ra[0].Ip, int2Ip4(a[0].Ip4))
}
if a[0].Domain != "" {
ra[0].Domain = append(ra[0].Domain, PrintedDomain(a[0].Domain))
}
if a[0].Url != "" {
ra[0].Url = append(ra[0].Url, PrintedDomain(a[0].Url))
}
for i := 0; i < len(a)-1; i++ {
if a[i].Id == a[i+1].Id {
if a[i+1].Ip4 != 0 {
ra[i].Ip = append(ra[i].Ip, int2Ip4(a[i+1].Ip4))
}
if a[i+1].Domain != "" {
ra[i].Domain = append(ra[i].Domain, PrintedDomain(a[i+1].Domain))
}
if a[i+1].Url != "" {
ra[i].Url = append(ra[i].Url, a[i+1].Url)
}
a = append(a[:i], a[i+1:]...)
i--
} else {
ra = append(ra, TReason{})
ra[i+1].Id = a[i+1].Id
if a[i+1].Ip4 != 0 {
ra[i+1].Ip = append(ra[i+1].Ip, int2Ip4(a[i+1].Ip4))
}
if a[i+1].Domain != "" {
ra[i+1].Domain = append(ra[i+1].Domain, PrintedDomain(a[i+1].Domain))
}
if a[i+1].Url != "" {
ra[i+1].Url = append(ra[i+1].Url, a[i+1].Url)
}
}
}
sort.Slice(a, func(j, i int) bool {
switch {
case a[i].BlockType == TBLOCK_URL && a[j].BlockType != TBLOCK_URL:
return true
case a[i].BlockType == TBLOCK_HTTPS &&
(a[j].BlockType != TBLOCK_URL &&
a[j].BlockType != TBLOCK_HTTPS):
return true
case a[i].BlockType == TBLOCK_DOMAIN &&
(a[j].BlockType != TBLOCK_URL &&
a[j].BlockType != TBLOCK_HTTPS &&
a[j].BlockType != TBLOCK_DOMAIN):
return true
default:
return false
}
})
offset := 0
if o.Tag == OFFSET_CONTENT {
switch {
case len(a) <= PRINT_LIMIT:
offset = 0
case o.Count > len(a)-(len(a)%PRINT_LIMIT):
offset = len(a) - (len(a) % PRINT_LIMIT)
default:
offset = o.Count
}
}
var cnt, cbu, cbh, cbd, cbi int
for i, packet := range a {
if o.Tag == OFFSET_CONTENT && i < offset {
continue
}
content := TContent{}
err := json.Unmarshal(packet.Pack, &content)
if err != nil {
Error.Printf("Упс!!! %s\n", err)
continue
}
if packet.RegistryUpdateTime < oldest {
oldest = packet.RegistryUpdateTime
}
var req TReason
for _, req = range ra {
if req.Id == packet.Id {
break
}
}
if cnt < PRINT_LIMIT {
bt := ""
switch packet.BlockType {
case TBLOCK_URL:
bt = "\U000026d4 "
cbu++
case TBLOCK_HTTPS:
bt = "\U0001f4db "
cbh++
case TBLOCK_DOMAIN:
bt = "\U0001f6ab "
cbd++
case TBLOCK_IP:
bt = "\u274c "
cbi++
}
_l := len([]rune(content.Decision))
if _l > 127 {
_l = 127
}
res += fmt.Sprintf("%s /n\\_%s %s...\n", bt, Uint64ToBase32(content.U2Hash), Sanitize(string([]rune(content.Decision)[:_l])))
if len(req.Ip) != 0 {
for _, ip := range req.Ip {
res += fmt.Sprintf(" _как ip_ %s\n", ip)
}
}
if len(req.Domain) != 0 {
for _, domain := range req.Domain {
res += fmt.Sprintf(" _как domain_ %s\n", Sanitize(PrintedDomain(domain)))
}
}
if len(req.Url) != 0 {
for _, u := range req.Url {
res += fmt.Sprintf(" _как url_ %s\n", Sanitize(PrintedDomain(u)))
}
}
res += "\n"
}
cnt++
}
if mass != "" {
res = mass + res
}
if len(a) > PRINT_LIMIT {
pages = append(pages, TPagination{OFFSET_CONTENT, len(a)})
//rest := cnt - PRINT_LIMIT
res += fmt.Sprintf("\u2195 результаты с *%d* по *%d* из *%d*\n", offset+1, offset+PRINT_LIMIT, len(a))
/*if cbu > 0 && cbu < rest {
res += fmt.Sprintf(" url=%d", cbu)
} else if cbu > 0 {
res += fmt.Sprintf(" url=%d", rest)
}
if cbh > 0 && cbu+cbh < rest {
res += fmt.Sprintf(" https=%d", cbu)
} else if cbh > 0 {
res += fmt.Sprintf(" https=%d", rest-cbu)
}
if cbd > 0 && cbd+cbu+cbh < rest {
res += fmt.Sprintf(" domain=%d", cbu)
} else if cbd > 0 {
res += fmt.Sprintf(" domain=%d", rest-cbh-cbu)
}
if cbm > 0 && cbm+cbd+cbu+cbh < rest {
res += fmt.Sprintf(" wildcard=%d", cbu)
} else if cbm > 0 {
res += fmt.Sprintf(" wildcard=%d", rest-cbd-cbh-cbu)
}
if cbi > 0 && cbm+cbd+cbu+cbh < rest {
res += fmt.Sprintf(" ip=%d", rest-cbm-cbd-cbh-cbu)
}*/
res += "\n"
}
var abt []string
if cbu > 0 {
abt = append(abt, "url: \U000026d4")
}
if cbh > 0 {
abt = append(abt, "https: \U0001f4db")
}
if cbd > 0 {
abt = append(abt, "domain: \U0001f6ab")
}
if cbi > 0 {
abt = append(abt, "ip: \u274c")
}
res += "*типы блокировки:* " + strings.Join(abt, " | ")
res += printUpToDate(oldest)
return
}