2022-05-09 14:11:05 +01:00
|
|
|
from fnmatch import fnmatch
|
|
|
|
from typing import Tuple, List
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
from app.extensions import db
|
2022-05-14 10:35:24 +01:00
|
|
|
from app.models.activity import Activity
|
2022-05-09 14:11:05 +01:00
|
|
|
from app.models.mirrors import Proxy
|
|
|
|
from app.terraform import BaseAutomation
|
|
|
|
|
|
|
|
|
|
|
|
class BlockRoskomsvobodaAutomation(BaseAutomation):
|
2022-06-17 12:42:42 +01:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
2022-05-09 14:11:05 +01:00
|
|
|
short_name = "block_roskomsvoboda"
|
|
|
|
description = "Import Russian blocklist from RosKomSvoboda"
|
2022-05-10 10:37:24 +01:00
|
|
|
frequency = 90
|
2022-05-09 14:11:05 +01:00
|
|
|
|
|
|
|
def automate(self, full: bool = False) -> Tuple[bool, str]:
|
2022-05-14 10:35:24 +01:00
|
|
|
activities = []
|
2022-05-09 14:11:05 +01:00
|
|
|
proxies: List[Proxy] = Proxy.query.filter(
|
2022-05-16 13:29:48 +01:00
|
|
|
Proxy.deprecated.is_(None),
|
|
|
|
Proxy.destroyed.is_(None)
|
2022-05-09 14:11:05 +01:00
|
|
|
).all()
|
|
|
|
patterns = requests.get("https://reestr.rublacklist.net/api/v2/domains/json").json()
|
|
|
|
for pattern in patterns:
|
2022-06-17 12:42:42 +01:00
|
|
|
for proxy in proxies:
|
|
|
|
if proxy.url is None:
|
2022-05-16 14:01:11 +01:00
|
|
|
# Not ready yet
|
|
|
|
continue
|
2022-06-17 12:42:42 +01:00
|
|
|
if fnmatch(proxy.url[len("https://"):], pattern):
|
|
|
|
print(f"Found {proxy.url} blocked")
|
|
|
|
if not proxy.origin.auto_rotation:
|
2022-05-09 14:11:05 +01:00
|
|
|
print("Proxy auto-rotation forbidden for origin")
|
|
|
|
continue
|
2022-06-17 12:42:42 +01:00
|
|
|
proxy.deprecate(reason="roskomsvoboda")
|
2022-05-14 10:35:24 +01:00
|
|
|
activities.append(Activity(
|
|
|
|
activity_type="block",
|
2022-06-17 12:42:42 +01:00
|
|
|
text=(f"Proxy {proxy.url} for {proxy.origin.domain_name} detected blocked "
|
|
|
|
"according to RosKomSvoboda. Rotation scheduled.")
|
2022-05-14 10:35:24 +01:00
|
|
|
))
|
2022-06-17 12:42:42 +01:00
|
|
|
for activity in activities:
|
|
|
|
db.session.add(activity)
|
2022-05-09 14:11:05 +01:00
|
|
|
db.session.commit()
|
2022-06-17 12:42:42 +01:00
|
|
|
for activity in activities:
|
|
|
|
activity.notify()
|
2022-05-09 14:11:05 +01:00
|
|
|
return True, ""
|