block: try to unify the mirror block modules
This commit is contained in:
parent
db0233691c
commit
360c786610
4 changed files with 114 additions and 109 deletions
|
@ -1,11 +1,7 @@
|
|||
from fnmatch import fnmatch
|
||||
from typing import Tuple, List
|
||||
from typing import Any
|
||||
|
||||
import requests
|
||||
|
||||
from app.extensions import db
|
||||
from app.models.activity import Activity
|
||||
from app.models.mirrors import Proxy
|
||||
from app.terraform import BaseAutomation
|
||||
|
||||
|
||||
|
@ -24,32 +20,10 @@ class BlockRoskomsvobodaAutomation(BaseAutomation):
|
|||
description = "Import Russian blocklist from RosKomSvoboda"
|
||||
frequency = 90
|
||||
|
||||
def automate(self, full: bool = False) -> Tuple[bool, str]:
|
||||
activities = []
|
||||
proxies: List[Proxy] = Proxy.query.filter(
|
||||
Proxy.deprecated.is_(None),
|
||||
Proxy.destroyed.is_(None)
|
||||
).all()
|
||||
patterns = requests.get("https://reestr.rublacklist.net/api/v2/domains/json").json()
|
||||
for pattern in patterns:
|
||||
for proxy in proxies:
|
||||
if proxy.url is None:
|
||||
# Not ready yet
|
||||
continue
|
||||
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
|
||||
proxy.deprecate(reason="roskomsvoboda")
|
||||
activities.append(Activity(
|
||||
activity_type="block",
|
||||
text=(f"Proxy {proxy.url} for {proxy.origin.domain_name} detected blocked "
|
||||
"according to RosKomSvoboda. Rotation scheduled.")
|
||||
))
|
||||
for activity in activities:
|
||||
db.session.add(activity)
|
||||
db.session.commit()
|
||||
for activity in activities:
|
||||
activity.notify()
|
||||
return True, ""
|
||||
_data: Any
|
||||
|
||||
def fetch(self):
|
||||
self._data = requests.get("https://reestr.rublacklist.net/api/v2/domains/json").json()
|
||||
|
||||
def parse(self):
|
||||
self.patterns.extend(["https://" + pattern for pattern in self._data])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue