lint: tidying up code in block tasks

This commit is contained in:
Iain Learmonth 2022-06-17 12:42:42 +01:00
parent a0da4d4641
commit ac5a604587
8 changed files with 86 additions and 49 deletions

View file

@ -10,6 +10,16 @@ from app.terraform import BaseAutomation
class BlockRoskomsvobodaAutomation(BaseAutomation):
"""
Automation task to import Russian blocklist from RosKomSvoboda.
This task will import the Russian state register of prohibited sites,
which is part of the enforcement of federal laws of the Russian Federation
No. 139-FZ, No. 187-FZ, No. 398-FZ and a number of others that regulate
the dissemination of information on the Internet.
Where proxies are found to be blocked they will be rotated.
"""
short_name = "block_roskomsvoboda"
description = "Import Russian blocklist from RosKomSvoboda"
frequency = 90
@ -22,27 +32,24 @@ class BlockRoskomsvobodaAutomation(BaseAutomation):
).all()
patterns = requests.get("https://reestr.rublacklist.net/api/v2/domains/json").json()
for pattern in patterns:
for p in proxies:
if p.url is None:
for proxy in proxies:
if proxy.url is None:
# Not ready yet
continue
if fnmatch(p.url[len("https://"):], pattern):
print(f"Found {p.url} blocked")
if not p.origin.auto_rotation:
if fnmatch(proxy.url[len("https://"):], pattern):
print(f"Found {proxy.url} blocked")
if not proxy.origin.auto_rotation:
print("Proxy auto-rotation forbidden for origin")
continue
if p.deprecated:
print("Proxy already marked blocked")
continue
p.deprecate(reason="roskomsvoboda")
proxy.deprecate(reason="roskomsvoboda")
activities.append(Activity(
activity_type="block",
text=(f"Proxy {p.url} for {p.origin.domain_name} detected blocked according to RosKomSvoboda. "
"Rotation scheduled.")
text=(f"Proxy {proxy.url} for {proxy.origin.domain_name} detected blocked "
"according to RosKomSvoboda. Rotation scheduled.")
))
for a in activities:
db.session.add(a)
for activity in activities:
db.session.add(activity)
db.session.commit()
for a in activities:
a.notify()
for activity in activities:
activity.notify()
return True, ""