-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbl-rkn.py
executable file
·681 lines (601 loc) · 32.5 KB
/
bl-rkn.py
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'Prototype-X'
from datetime import datetime
import argparse
import logging
from logging.handlers import TimedRotatingFileHandler
import smtplib
from email.mime.text import MIMEText
import subprocess
from peewee import fn
import random
from config import Config
from db import Dump, Item, IP, Domain, URL, History, DNSResolver, init_db
from core import Core
from resolver import Resolver
logger = logging.getLogger(__name__)
class Rutoken(object):
def __init__(self, cfg_obj):
self.cfg = cfg_obj
def gen_request(self):
logger.info('Generate request file %s', self.cfg.XMLPathFName())
dt = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
request_xml = '<?xml version="1.0" encoding="windows-1251"?>\n'
request_xml += '<request>\n'
request_xml += '<requestTime>' + dt + '.000+04:00</requestTime>\n'
request_xml += '<operatorName>' + self.cfg.OperatorName() + '</operatorName>\n'
request_xml += '<inn>' + str(self.cfg.inn()) + '</inn>\n'
request_xml += '<ogrn>' + str(self.cfg.ogrn()) + '</ogrn>\n'
request_xml += '<email>' + self.cfg.email() + '</email>\n'
request_xml += '</request>'
with open(self.cfg.XMLPathFName(), 'wb') as f:
f.write(request_xml.encode(encoding='cp1251'))
return True
def sign_request(self):
logger.info('Sign file %s', self.cfg.XMLPathFName())
subprocess.call("sudo " + self.cfg.Path() + "openssl smime -engine pkcs11_gost -sign -in " +
self.cfg.XMLPathFName() + " -out " + self.cfg.P7SPathFName() +
" -outform der -noverify -binary -signer " + self.cfg.PEMPathFName() +
" -inkey " + self.cfg.ID() + " -keyform engine", shell=True)
return True
class Notifier(object):
def __init__(self, cfg):
self.from_address = cfg.MailFrom()
self.to_address = cfg.MailTo()
self.auth = cfg.MailAuth()
self.starttls = cfg.StartTLS()
self.server_address = cfg.MailServer()
self.server_port = cfg.MailPort()
self.subject = cfg.MailSubject()
if self.auth:
self.login = cfg.MailLogin()
self.password = cfg.MailPassword()
def send_mail(self, message, subject=''):
if subject:
self.subject = subject
msg = MIMEText(message)
msg['Subject'] = self.subject
msg['From'] = self.from_address
msg['To'] = self.to_address
if self.auth:
server = smtplib.SMTP(self.server_address, self.server_port)
server.ehlo_or_helo_if_needed()
if self.starttls:
server.starttls()
server.login(self.login, self.password)
server.sendmail(self.from_address, self.to_address, msg.as_string())
server.quit()
else:
server = smtplib.SMTP(self.server_address, self.server_port)
server.connect()
server.ehlo_or_helo_if_needed()
server.sendmail(self.from_address, self.to_address, msg.as_string())
server.quit()
logger.info('Send email from %s to %s', self.from_address, self.to_address)
logger.info('%s', message)
class Reporter(object):
def __init__(self, cfg):
self.cfg = cfg
self.idx_list = [idx.id for idx in History.select(History.id).order_by(History.id.desc())
.limit(self.cfg.DiffCount())]
self.block_type = ['ip', 'default', 'domain', 'domain-mask']
def statistics_show(self, diff=0, stdout=False):
date_time = datetime.fromtimestamp(int(Dump.get(Dump.param == 'lastDumpDate')
.value)).strftime('%Y-%m-%d %H:%M:%S')
message = 'vigruzki.rkn.gov.ru update: ' + date_time + '\n'
url_add_sql = self._url_dedup_sql(diff, 'ignore', 1)
message += '\nURLs added: \n\n'
for url_add in url_add_sql:
message += url_add.url + '\n'
if self.cfg.Resolver():
ip_add_sql = self._ip_dedup_resolv_sql(diff, 'ignore', 1)
else:
ip_add_sql = self._ip_dedup_sql(diff, 'ignore', 1)
message += '\nIPs added: \n\n'
for ip_add in ip_add_sql:
if ip_add.mask < 32:
message += ip_add.ip + '/' + str(ip_add.mask)
else:
message += ip_add.ip + '\n'
domain_add_sql = self._domain_dedup_sql(diff, 'ignore', 1)
message += '\nDOMAINs added: \n\n'
for domain_add in domain_add_sql:
message += domain_add.domain + '\n'
url_del_sql = self._url_dedup_sql(diff, 'ignore', 0)
message += '\nURLs deleted: \n\n'
for url_del in url_del_sql:
message += url_del.url + '\n'
if self.cfg.Resolver():
ip_del_sql = self._ip_dedup_resolv_sql(diff, 'ignore', 0)
else:
ip_del_sql = self._ip_dedup_sql(diff, 'ignore', 0)
message += '\nIPs deleted: \n\n'
for ip_del in ip_del_sql:
if ip_del.mask < 32:
message += ip_del.ip + '/' + str(ip_del.mask)
else:
message += ip_del.ip + '\n'
domain_del_sql = self._domain_dedup_sql(diff, 'ignore', 0)
message += '\nDOMAINs deleted: \n\n'
for domain_del in domain_del_sql:
message += domain_del.domain + '\n'
rb_list = self.idx_list[:diff]
domain_count = Domain.select(fn.Count(fn.Distinct(Domain.domain))) \
.where(~(Domain.add << rb_list) & ((Domain.purge >> None) | (Domain.purge << rb_list))).scalar()
url_count = URL.select(fn.Count(fn.Distinct(URL.url))) \
.where(~(URL.add << rb_list) & ((URL.purge >> None) | (URL.purge << rb_list))).scalar()
if self.cfg.Resolver():
ip_count = DNSResolver.select(fn.Count(fn.Distinct(DNSResolver.ip))) \
.where(~(DNSResolver.add << rb_list) &
((DNSResolver.purge >> None) |
(DNSResolver.purge << rb_list))).scalar()
else:
ip_count = IP.select(fn.Count(fn.Distinct(IP.ip))) \
.where(~(IP.add << rb_list) & ((IP.purge >> None) | (IP.purge << rb_list))).scalar()
id_count = Item.select(fn.Count(fn.Distinct(Item.content_id))) \
.where(~(Item.add << rb_list) & ((Item.purge >> None) | (Item.purge << rb_list))).scalar()
message += '\nURLs count: ' + str(url_count) + '\n'
message += 'IPs count: ' + str(ip_count) + '\n'
message += 'DOMAINs count: ' + str(domain_count) + '\n'
message += 'Item count: ' + str(id_count) + '\n'
logger.info('bl-rkn.py --stat %d', diff)
logger.info('%s', message)
if stdout:
print(message)
return False
else:
return message
def domain_show(self, bt='ignore', diff=None, rollback=None):
if diff is not None:
logger.info('bl-rkn.py --diff %d --domain --bt %s', diff, bt)
# domain_sql = self._domain_diff_sql(diff, bt, 1)
domain_sql = self._domain_dedup_sql(diff, bt, 1)
self._domain_output(domain_sql, '+')
# domain_sql = self._domain_diff_sql(diff, bt, 0)
domain_sql = self._domain_dedup_sql(diff, bt, 0)
self._domain_output(domain_sql, '-')
if rollback is not None:
logger.info('bl-rkn.py --rollback %d --domain --bt %s', rollback, bt)
domain_sql = self.domain_rollback_sql(rollback, bt)
self._domain_output(domain_sql)
def _domain_diff_sql(self, diff, bt, stat):
if stat and bt in self.block_type:
domain_sql = Domain.select(fn.Distinct(Domain.domain)).join(Item) \
.where(Item.blockType == bt, Domain.add == self.idx_list[diff])
return domain_sql
elif not stat and bt in self.block_type:
domain_sql = Domain.select(fn.Distinct(Domain.domain)).join(Item) \
.where(Item.blockType == bt, Domain.purge == self.idx_list[diff])
return domain_sql
elif stat and bt == 'ignore':
domain_sql = Domain.select(fn.Distinct(Domain.domain)).where(Domain.add == self.idx_list[diff])
return domain_sql
elif not stat and bt == 'ignore':
domain_sql = Domain.select(fn.Distinct(Domain.domain)).where(Domain.purge == self.idx_list[diff])
return domain_sql
def _domain_dedup_sql(self, diff, bt, stat):
rb_list_add = self.idx_list[:diff + 1]
rb_list_purge = self.idx_list[:diff]
if stat and bt == 'ignore':
domain_diff_sql = Domain.select(fn.Distinct(Domain.domain)).where(Domain.add == self.idx_list[diff])
domain_dup_sql = Domain.select(fn.Distinct(Domain.domain)) \
.where(~(Domain.add << rb_list_add) & ((Domain.purge >> None) | (Domain.purge << rb_list_add)) &
(Domain.domain << domain_diff_sql))
domain_dedup_sql = Domain.select(fn.Distinct(Domain.domain)).where((Domain.add == self.idx_list[diff]) &
~(Domain.domain << domain_dup_sql))
return domain_dedup_sql
elif not stat and bt == 'ignore':
domain_diff_sql = Domain.select(fn.Distinct(Domain.domain)).where(Domain.purge == self.idx_list[diff])
domain_dup_sql = Domain.select(fn.Distinct(Domain.domain)) \
.where(~(Domain.add << rb_list_purge) & (Domain.purge >> None) &
(Domain.domain << domain_diff_sql))
domain_dedup_sql = Domain.select(fn.Distinct(Domain.domain)).where((Domain.purge == self.idx_list[diff]) &
~(Domain.domain << domain_dup_sql))
return domain_dedup_sql
elif stat and bt in self.block_type:
domain_diff_sql = Domain.select(fn.Distinct(Domain.domain)).join(Item) \
.where(Item.blockType == bt, Domain.add == self.idx_list[diff])
domain_dup_sql = Domain.select(fn.Distinct(Domain.domain)).join(Item) \
.where((Item.blockType == bt) & ~(Domain.add << rb_list_add) &
(Domain.purge >> None) & (Domain.domain << domain_diff_sql))
domain_dedup_sql = Domain.select(fn.Distinct(Domain.domain)).join(Item) \
.where((Item.blockType == bt) & (Domain.add == self.idx_list[diff]) &
~(Domain.domain << domain_dup_sql))
return domain_dedup_sql
elif not stat and bt in self.block_type:
domain_diff_sql = Domain.select(fn.Distinct(Domain.domain)).join(Item) \
.where(Item.blockType == bt, Domain.purge == self.idx_list[diff])
domain_dup_sql = Domain.select(fn.Distinct(Domain.domain)).join(Item) \
.where((Item.blockType == bt) & ~(Domain.add << rb_list_purge) &
(Domain.purge >> None) & (Domain.domain << domain_diff_sql))
domain_dedup_sql = Domain.select(fn.Distinct(Domain.domain)).join(Item) \
.where((Item.blockType == bt) & (Domain.purge == self.idx_list[diff]) &
~(Domain.domain << domain_dup_sql))
return domain_dedup_sql
def domain_rollback_sql(self, rollback, bt):
rb_list = self.idx_list[:rollback]
if bt == 'ignore':
domain_sql = Domain.select(fn.Distinct(Domain.domain)) \
.where(~(Domain.add << rb_list) & ((Domain.purge >> None) | (Domain.purge << rb_list)))
return domain_sql
elif bt in self.block_type:
domain_sql = Domain.select(fn.Distinct(Domain.domain)) \
.join(Item).where((Item.blockType == bt) & ~(Domain.add << rb_list) &
((Domain.purge >> None) | (Domain.purge << rb_list)))
return domain_sql
def ip_show(self, bt='ignore', diff=None, rollback=None):
if diff is not None and not self.cfg.Resolver():
logger.info('Resolver off')
logger.info('bl-rkn.py --diff %d --ip --bt %s', diff, bt)
ip_sql = self._ip_dedup_sql(diff, bt, 1)
self._ip_output(ip_sql, '+')
ip_sql = self._ip_dedup_sql(diff, bt, 0)
self._ip_output(ip_sql, '-')
elif diff is not None and self.cfg.Resolver():
logger.info('Resolver on. Support blockType ignore or default')
logger.info('bl-rkn.py --diff %d --ip --bt %s', diff, bt)
if bt in ['ignore', 'default']:
dns_sql = self._ip_dedup_resolv_sql(diff, bt, 1)
self._ip_output(dns_sql, '+')
if bt in ['ignore', 'default']:
dns_sql = self._ip_dedup_resolv_sql(diff, bt, 0)
self._ip_output(dns_sql, '-')
if rollback is not None and not self.cfg.Resolver():
logger.info('Resolver off')
logger.info('bl-rkn.py --rollback %d --ip --bt %s', rollback, bt)
ip_sql = self.ip_rollback_sql(rollback, bt)
self._ip_output(ip_sql)
elif rollback is not None and self.cfg.Resolver():
logger.info('Resolver on. Support blockType ignore or default')
logger.info('bl-rkn.py --rollback %d --ip --bt %s', rollback, bt)
if bt in ['ignore', 'default']:
dns_sql = self.ip_rollback_resolv_sql(rollback, bt)
self._ip_output(dns_sql)
def _ip_diff_sql(self, diff, bt, stat):
if stat and bt in self.block_type:
ip_sql = IP.select(fn.Distinct(IP.ip), IP.mask).join(Item) \
.where(Item.blockType == bt, IP.add == self.idx_list[diff])
return ip_sql
elif not stat and bt in self.block_type:
ip_sql = IP.select(fn.Distinct(IP.ip), IP.mask).join(Item) \
.where(Item.blockType == bt, IP.purge == self.idx_list[diff])
return ip_sql
elif stat and bt == 'ignore':
ip_sql = IP.select(fn.Distinct(IP.ip), IP.mask).where(IP.add == self.idx_list[diff])
return ip_sql
elif not stat and bt == 'ignore':
ip_sql = IP.select(fn.Distinct(IP.ip), IP.mask).where(IP.purge == self.idx_list[diff])
return ip_sql
def _ip_dedup_resolv_sql(self, diff, bt, stat):
rb_list_add = self.idx_list[:diff + 1]
rb_list_purge = self.idx_list[:diff]
if stat and bt in ['ignore', 'default']:
ip_diff_sql = DNSResolver.select(fn.Distinct(DNSResolver.ip)).where(DNSResolver.add == self.idx_list[diff])
ip_dup_sql = DNSResolver.select(fn.Distinct(DNSResolver.ip)) \
.where(~(DNSResolver.add << rb_list_add) &
((DNSResolver.purge >> None) | (DNSResolver.purge << rb_list_add)) &
(DNSResolver.ip << ip_diff_sql))
ip_dedup_sql = DNSResolver.select(fn.Distinct(DNSResolver.ip), DNSResolver.mask) \
.where((DNSResolver.add == self.idx_list[diff]) & ~(DNSResolver.ip << ip_dup_sql))
return ip_dedup_sql
elif not stat and bt in ['ignore', 'default']:
ip_diff_sql = DNSResolver.select(fn.Distinct(DNSResolver.ip)) \
.where(DNSResolver.purge == self.idx_list[diff])
ip_dup_sql = DNSResolver.select(fn.Distinct(DNSResolver.ip)) \
.where(~(DNSResolver.add << rb_list_purge) &
(DNSResolver.purge >> None) & (DNSResolver.ip << ip_diff_sql))
ip_dedup_sql = DNSResolver.select(fn.Distinct(DNSResolver.ip), DNSResolver.mask) \
.where((DNSResolver.purge == self.idx_list[diff]) & ~(DNSResolver.ip << ip_dup_sql))
return ip_dedup_sql
else:
return None
def _ip_dedup_sql(self, diff, bt, stat):
rb_list_add = self.idx_list[:diff + 1]
rb_list_purge = self.idx_list[:diff]
if stat and bt == 'ignore':
ip_diff_sql = IP.select(fn.Distinct(IP.ip)).where(IP.add == self.idx_list[diff])
ip_dup_sql = IP.select(fn.Distinct(IP.ip)) \
.where(~(IP.add << rb_list_add) & ((IP.purge >> None) | (IP.purge << rb_list_add)) &
(IP.ip << ip_diff_sql))
ip_dedup_sql = IP.select(fn.Distinct(IP.ip), IP.mask).where((IP.add == self.idx_list[diff]) &
~(IP.ip << ip_dup_sql))
return ip_dedup_sql
elif not stat and bt == 'ignore':
ip_diff_sql = IP.select(fn.Distinct(IP.ip)).where(IP.purge == self.idx_list[diff])
ip_dup_sql = IP.select(fn.Distinct(IP.ip)) \
.where(~(IP.add << rb_list_purge) & (IP.purge >> None) &
(IP.ip << ip_diff_sql))
ip_dedup_sql = IP.select(fn.Distinct(IP.ip), IP.mask).where((IP.purge == self.idx_list[diff]) &
~(IP.ip << ip_dup_sql))
return ip_dedup_sql
elif stat and bt in self.block_type:
ip_diff_sql = IP.select(fn.Distinct(IP.ip)).join(Item) \
.where(Item.blockType == bt, IP.add == self.idx_list[diff])
ip_dup_sql = IP.select(fn.Distinct(IP.ip)).join(Item) \
.where((Item.blockType == bt) & ~(IP.add << rb_list_add) &
(IP.purge >> None) & (IP.ip << ip_diff_sql))
ip_dedup_sql = IP.select(fn.Distinct(IP.ip), IP.mask).join(Item) \
.where((Item.blockType == bt) & (IP.add == self.idx_list[diff]) &
~(IP.ip << ip_dup_sql))
return ip_dedup_sql
elif not stat and bt in self.block_type:
ip_diff_sql = IP.select(fn.Distinct(IP.ip)).join(Item) \
.where(Item.blockType == bt, IP.purge == self.idx_list[diff])
ip_dup_sql = IP.select(fn.Distinct(IP.ip)).join(Item) \
.where((Item.blockType == bt) & ~(IP.add << rb_list_purge) &
(IP.purge >> None) & (IP.ip << ip_diff_sql))
ip_dedup_sql = IP.select(fn.Distinct(IP.ip), IP.mask).join(Item) \
.where((Item.blockType == bt) & (IP.purge == self.idx_list[diff]) &
~(IP.ip << ip_dup_sql))
return ip_dedup_sql
def ip_rollback_sql(self, rollback, bt):
rb_list = self.idx_list[:rollback]
if bt == 'ignore':
ip_sql = IP.select(fn.Distinct(IP.ip), IP.mask) \
.where(~(IP.add << rb_list) & ((IP.purge >> None) | (IP.purge << rb_list)))
return ip_sql
elif bt in self.block_type:
ip_sql = IP.select(fn.Distinct(IP.ip), IP.mask) \
.join(Item).where((Item.blockType == bt) & ~(IP.add << rb_list) &
((IP.purge >> None) | (IP.purge << rb_list)))
return ip_sql
def ip_rollback_resolv_sql(self, rollback, bt):
idx_list = [idx.id for idx in History.select(History.id).order_by(History.id.desc())
.limit(self.cfg.DiffCount())]
rb_list = idx_list[:rollback]
if bt in ['ignore', 'default']:
dns_sql = DNSResolver.select(fn.Distinct(DNSResolver.ip)) \
.where(~(DNSResolver.add << rb_list) & (DNSResolver.purge >> None | DNSResolver.purge << rb_list))
return dns_sql
else:
return None
def url_show(self, bt='ignore', diff=None, rollback=None):
if diff is not None:
logger.info('bl-rkn.py --diff %d --url --bt %s', diff, bt)
url_sql = self._url_dedup_sql(diff, bt, 1)
self._url_output(url_sql, '+')
url_sql = self._url_dedup_sql(diff, bt, 0)
self._url_output(url_sql, '-')
if rollback is not None:
logger.info('bl-rkn.py --rollback %d --url --bt %s', rollback, bt)
url_sql = self.url_rollback_sql(rollback, bt)
self._url_output(url_sql)
def _url_diff_sql(self, diff, bt, stat):
if stat and bt in self.block_type:
url_sql = URL.select(fn.Distinct(URL.url)).join(Item) \
.where(Item.blockType == bt, URL.add == self.idx_list[diff])
return url_sql
elif not stat and bt in self.block_type:
url_sql = URL.select(fn.Distinct(URL.url)).join(Item) \
.where(Item.blockType == bt, URL.purge == self.idx_list[diff])
return url_sql
elif stat and bt == 'ignore':
url_sql = URL.select(fn.Distinct(URL.url)).where(URL.add == self.idx_list[diff])
return url_sql
elif not stat and bt == 'ignore':
url_sql = URL.select(fn.Distinct(URL.url)).where(URL.purge == self.idx_list[diff])
return url_sql
def _url_dedup_sql(self, diff, bt, stat):
rb_list_add = self.idx_list[:diff + 1]
rb_list_purge = self.idx_list[:diff]
if stat and bt == 'ignore':
url_diff_sql = URL.select(fn.Distinct(URL.url)).where(URL.add == self.idx_list[diff])
url_dup_sql = URL.select(fn.Distinct(URL.url)) \
.where(~(URL.add << rb_list_add) & ((URL.purge >> None) | (URL.purge << rb_list_add)) &
(URL.url << url_diff_sql))
url_dedup_sql = URL.select(fn.Distinct(URL.url)).where((URL.add == self.idx_list[diff]) &
~(URL.url << url_dup_sql))
return url_dedup_sql
elif not stat and bt == 'ignore':
url_diff_sql = URL.select(fn.Distinct(URL.url)).where(URL.purge == self.idx_list[diff])
url_dup_sql = URL.select(fn.Distinct(URL.url)) \
.where(~(URL.add << rb_list_purge) & (URL.purge >> None) &
(URL.url << url_diff_sql))
url_dedup_sql = URL.select(fn.Distinct(URL.url)).where((URL.purge == self.idx_list[diff]) &
~(URL.url << url_dup_sql))
return url_dedup_sql
elif stat and bt in self.block_type:
url_diff_sql = URL.select(fn.Distinct(URL.url)).join(Item) \
.where(Item.blockType == bt, URL.add == self.idx_list[diff])
url_dup_sql = URL.select(fn.Distinct(URL.url)).join(Item) \
.where((Item.blockType == bt) & ~(URL.add << rb_list_add) &
(URL.purge >> None) & (URL.url << url_diff_sql))
url_dedup_sql = URL.select(fn.Distinct(URL.url)).join(Item) \
.where((Item.blockType == bt) & (URL.add == self.idx_list[diff]) &
~(URL.url << url_dup_sql))
return url_dedup_sql
elif not stat and bt in self.block_type:
url_diff_sql = URL.select(fn.Distinct(URL.url)).join(Item) \
.where(Item.blockType == bt, URL.purge == self.idx_list[diff])
url_dup_sql = URL.select(fn.Distinct(URL.url)).join(Item) \
.where((Item.blockType == bt) & ~(URL.add << rb_list_purge) &
(URL.purge >> None) & (URL.url << url_diff_sql))
url_dedup_sql = URL.select(fn.Distinct(URL.url)).join(Item) \
.where((Item.blockType == bt) & (URL.purge == self.idx_list[diff]) &
~(URL.url << url_dup_sql))
return url_dedup_sql
def url_rollback_sql(self, rollback, bt):
rb_list = self.idx_list[:rollback]
if bt == 'ignore':
url_sql = URL.select(fn.Distinct(URL.url)) \
.where(~(URL.add << rb_list) & ((URL.purge >> None) | (URL.purge << rb_list)))
return url_sql
elif bt in self.block_type:
url_sql = URL.select(fn.Distinct(URL.url)) \
.join(Item).where((Item.blockType == bt) & ~(URL.add << rb_list) &
((URL.purge >> None) | (URL.purge << rb_list)))
return url_sql
@staticmethod
def _url_output(url_sql, prefix=''):
for url_row in url_sql:
logger.info('%s', prefix + url_row.url)
print(prefix + url_row.url)
@staticmethod
def _domain_output(domain_sql, prefix=''):
for domain_row in domain_sql:
logger.info('%s', prefix + domain_row.domain)
print(prefix + domain_row.domain)
@staticmethod
def _ip_output(ip_sql, prefix=''):
for ip_row in ip_sql:
if ip_row.mask < 32:
logger.info('%s', prefix + ip_row.ip + '/' + str(ip_row.mask))
print(prefix + ip_row.ip + '/' + str(ip_row.mask))
else:
logger.info('%s', prefix + ip_row.ip)
print(prefix + ip_row.ip)
@staticmethod
def history_show():
history_sql = History.select()
for history_row in history_sql:
print(history_row.date, history_row.requestCode)
class BlrknCLI(object):
def __init__(self):
self.cfg = Config()
choice_rb_diff = [i for i in range(self.cfg.DiffCount())]
self.parser = argparse.ArgumentParser(add_help=True,
description='Tool for list of restricted websites '
'http://vigruzki.rkn.gov.ru/')
self.group = self.parser.add_mutually_exclusive_group()
self.group.add_argument("--dump", action="store_true", required=False, default=False, help="Get new dump")
self.group.add_argument("--diff", action="store", type=int, choices=choice_rb_diff, required=False,
default=None, help="difference dump")
self.group.add_argument("--rollback", action="store", type=int, choices=choice_rb_diff, required=False,
default=None, help="rollback dump")
self.group.add_argument("--stat", action="store", type=int, choices=choice_rb_diff, required=False,
default=None, help="dump statistics")
self.parser.add_argument("--resolve", action="store_true", required=False, default=None, help="resolve domain")
self.parser.add_argument("--url", action="store_true", required=False, default=False, help="url list show")
self.parser.add_argument("--ip", action="store_true", required=False, default=False, help="ip list show")
self.parser.add_argument("--domain", action="store_true", required=False, default=False,
help="domain list show")
self.parser.add_argument("--history", action="store_true", required=False, default=False,
help="history list show")
self.parser.add_argument('--bt', action='store', default='ignore',
choices=['default', 'ip', 'domain', 'domain-mask'], help='blockType')
self.parser.add_argument("-r", '--reverse', action="store_true", required=False, default=False,
help="if resolver on, set it off for show ip and stat")
self.parser.add_argument("-v", "--version", action='version', version='version 2.1.1', help="show version")
self.args = self.parser.parse_args()
# self._peewee_debug()
self.ip_print = self.args.ip
self.url_print = self.args.url
self.domain_print = self.args.domain
self.history_print = self.args.history
self.block_type = self.args.bt
self.dump = self.args.dump
self.diff = self.args.diff
self.rollback = self.args.rollback
self.stat = self.args.stat
self.resolve = self.args.resolve
self.reverse = self.args.reverse
self._cfg_logging()
logger.info('Starting script.')
self.ctl_transact = init_db(self.cfg)
if self.reverse:
self.cfg.Reverse()
if self.diff is None and self.rollback is None:
self.rollback = 0
if self.ip_print:
self.report = Reporter(self.cfg)
self.report.ip_show(bt=self.block_type, diff=self.diff, rollback=self.rollback)
elif self.url_print:
self.report = Reporter(self.cfg)
self.report.url_show(bt=self.block_type, diff=self.diff, rollback=self.rollback)
elif self.domain_print:
self.report = Reporter(self.cfg)
self.report.domain_show(bt=self.block_type, diff=self.diff, rollback=self.rollback)
elif self.history_print:
self.report = Reporter(self.cfg)
self.report.history_show()
elif self.stat is not None:
self.report = Reporter(self.cfg)
self.report.statistics_show(diff=self.stat, stdout=True)
elif self.resolve:
self._resolve_domain()
elif self.dump:
if self.cfg.Notify():
self.notice = Notifier(self.cfg)
# self._peewee_debug()
# self._parse_dump_only()
self._get_dump()
else:
self.parser.print_help()
logger.info('Script stopped.')
def _get_dump(self):
self.dump = Core(self.ctl_transact, self.cfg)
srv_msg = self.dump.check_service_upd()
if srv_msg:
if self.cfg.Notify():
self.notice.send_mail(srv_msg, subject='vigruzki.rkn.gov.ru service update')
if self.dump.check_new_dump():
if self.cfg.GenRequest():
signer = Rutoken(self.cfg)
signer.gen_request()
signer.sign_request()
if self.dump.send_request():
if self.dump.get_request():
result_bool = self.dump.parse_dump()
if result_bool == 1:
if self.cfg.Resolver():
self.report = Reporter(self.cfg)
dns_resolv = Resolver(self.cfg, self.ctl_transact, self.report, self.dump.code_id)
dns_resolv.query()
dns_resolv.cleaner()
if self.cfg.Notify():
self.report = Reporter(self.cfg)
message = self.report.statistics_show()
self.notice.send_mail(message)
elif result_bool == 2:
if self.cfg.Resolver():
self.report = Reporter(self.cfg)
dns_resolv = Resolver(self.cfg, self.ctl_transact, self.report, self.dump.code_id)
dns_resolv.query()
dns_resolv.cleaner()
if self.cfg.Notify():
self.report = Reporter(self.cfg)
message = self.report.statistics_show()
self.notice.send_mail(message)
elif result_bool == 0:
if self.cfg.Notify():
message = 'Houston, we have a problem'
self.notice.send_mail(message)
logger.info('parse_dump error')
def _resolve_domain(self):
history_last = History.select().where(History.dump == True).order_by(History.id.desc()).get()
self.report = Reporter(self.cfg)
dns_resolv = Resolver(self.cfg, self.ctl_transact, self.report, history_last.id)
if history_last.resolver:
dns_resolv.clear_id(history_last.resolver)
dns_resolv.query()
dns_resolv.cleaner()
def _parse_dump_only(self):
# self.report = Reporter(self.cfg)
# message = self.report.statistics_show()
# self.notice.send_mail(message)
self.dump = Core(self.ctl_transact, self.cfg)
self.dump.code = 'test_' + ''.join(random.SystemRandom().
choice('abcdefgijklmnoprstuvwxyz1234567890') for _ in range(32))
History.create(requestCode=self.dump.code, date=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
self.dump.code_id = History.get(History.requestCode == self.dump.code).id
self.dump.parse_dump()
# dns_resolv = Resolver(self.cfg, self.ctl_transact, self.report, self.dump.code_id)
# dns_resolv.query()
# dns_resolv.cleaner()
def _cfg_logging(self):
"""
Configure logging format, level, rotate.
"""
handler = TimedRotatingFileHandler(self.cfg.LogPathFName(), when=self.cfg.LogRotate(),
interval=self.cfg.LogInterval(), backupCount=self.cfg.LogCount())
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s', handlers=[handler])
@staticmethod
def _peewee_debug():
log = logging.getLogger('peewee')
log.setLevel(logging.DEBUG)
log.addHandler(logging.StreamHandler())
def main():
BlrknCLI()
if __name__ == '__main__':
main()