2022-06-18 12:36:54 +01:00
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
import logging
|
|
|
|
|
from abc import abstractmethod
|
|
|
|
|
from fnmatch import fnmatch
|
|
|
|
|
from typing import Tuple, List
|
|
|
|
|
|
|
|
|
|
from app.extensions import db
|
|
|
|
|
from app.models.activity import Activity
|
|
|
|
|
from app.models.mirrors import Proxy
|
|
|
|
|
from app.terraform import BaseAutomation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BlockMirrorAutomation(BaseAutomation):
|
|
|
|
|
|
|
|
|
|
patterns: List[str]
|
|
|
|
|
|
2022-06-18 13:17:36 +01:00
|
|
|
|
def __init__(self) -> None:
|
2022-06-18 12:36:54 +01:00
|
|
|
|
"""
|
|
|
|
|
Constructor method.
|
|
|
|
|
"""
|
|
|
|
|
self.patterns = list()
|
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
|
|
def automate(self, full: bool = False) -> Tuple[bool, str]:
|
|
|
|
|
self.fetch()
|
2022-06-18 13:35:25 +01:00
|
|
|
|
logging.debug("Fetch complete")
|
2022-06-18 12:36:54 +01:00
|
|
|
|
self.parse()
|
2022-06-18 13:35:25 +01:00
|
|
|
|
logging.debug("Parse complete")
|
2022-06-18 12:36:54 +01:00
|
|
|
|
rotated = list()
|
2022-06-18 13:35:25 +01:00
|
|
|
|
for proxy in active_proxies():
|
|
|
|
|
if proxy.url is None:
|
|
|
|
|
# Not ready yet
|
|
|
|
|
continue
|
|
|
|
|
logging.debug("Testing active proxy %s", proxy.url)
|
|
|
|
|
for pattern in self.patterns:
|
2022-06-18 12:36:54 +01:00
|
|
|
|
if fnmatch(proxy.url, pattern):
|
|
|
|
|
logging.debug("Found %s blocked", proxy.url)
|
|
|
|
|
if not proxy.origin.auto_rotation:
|
|
|
|
|
logging.debug("Proxy auto-rotation forbidden for origin")
|
|
|
|
|
continue
|
|
|
|
|
if proxy.added > datetime.utcnow() - timedelta(hours=3):
|
|
|
|
|
logging.debug("Not rotating a proxy less than 3 hours old")
|
|
|
|
|
continue
|
|
|
|
|
if proxy.deprecate(reason=self.short_name):
|
|
|
|
|
logging.info("Rotated %s", proxy.url)
|
|
|
|
|
rotated.append((proxy.url, proxy.origin.domain_name))
|
|
|
|
|
else:
|
|
|
|
|
logging.debug("Not rotating a proxy that is already deprecated")
|
|
|
|
|
if rotated:
|
|
|
|
|
activity = Activity(
|
|
|
|
|
activity_type="block",
|
2022-06-18 13:17:36 +01:00
|
|
|
|
text=(f"[{self.short_name}] ♻ Rotated {len(rotated)} proxies️️: \n"
|
|
|
|
|
+ "\n".join([f"* {proxy_domain} ({origin_domain})" for proxy_domain, origin_domain in rotated]))
|
2022-06-18 12:36:54 +01:00
|
|
|
|
)
|
|
|
|
|
db.session.add(activity)
|
|
|
|
|
activity.notify()
|
2022-06-18 12:48:09 +01:00
|
|
|
|
db.session.commit()
|
2022-06-18 12:36:54 +01:00
|
|
|
|
return True, ""
|
|
|
|
|
|
|
|
|
|
@abstractmethod
|
2022-06-18 13:17:36 +01:00
|
|
|
|
def fetch(self) -> None:
|
2022-06-18 12:36:54 +01:00
|
|
|
|
"""
|
|
|
|
|
Fetch the blocklist data. It is the responsibility of the automation task
|
|
|
|
|
to persist this within the object for the parse step.
|
|
|
|
|
|
|
|
|
|
:return: None
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
@abstractmethod
|
2022-06-18 13:17:36 +01:00
|
|
|
|
def parse(self) -> None:
|
2022-06-18 12:36:54 +01:00
|
|
|
|
"""
|
|
|
|
|
Parse the blocklist data.
|
|
|
|
|
|
|
|
|
|
:return: None
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def active_proxies() -> List[Proxy]:
|
2022-06-18 13:17:36 +01:00
|
|
|
|
return Proxy.query.filter( # type: ignore[no-any-return]
|
2022-06-18 12:36:54 +01:00
|
|
|
|
Proxy.deprecated.is_(None),
|
|
|
|
|
Proxy.destroyed.is_(None)
|
|
|
|
|
).all()
|