majuna/app/lists/bc2.py

84 lines
2.7 KiB
Python
Raw Normal View History

# pylint: disable=too-few-public-methods
import builtins
2022-04-27 13:30:49 +01:00
from datetime import datetime
from typing import List, Dict, Union, Any, Optional
2022-03-10 14:26:22 +00:00
2022-04-27 13:30:49 +01:00
from pydantic import BaseModel, Field
2022-03-10 14:26:22 +00:00
from app.models.mirrors import Origin, Proxy
2022-04-27 13:30:49 +01:00
class BC2Alternative(BaseModel):
proto: str
type: str
created_at: datetime
updated_at: datetime
url: str
class BC2Site(BaseModel):
main_domain: str = Field(description="The main domain name of the website, excluding \"www.\" if present.",
examples=["bbc.co.uk", "bbc.com", "guardianproject.info"])
2022-04-27 13:30:49 +01:00
available_alternatives: List[BC2Alternative]
class BypassCensorship2(BaseModel):
2022-05-16 13:29:48 +01:00
version: str = Field(description="Version number of the Bypass Censorship Extension schema in use", )
2022-04-27 13:30:49 +01:00
sites: List[BC2Site]
class Config:
title = "Bypass Censorship Version 2"
2022-03-10 14:26:22 +00:00
2022-04-27 13:35:04 +01:00
def onion_alternative(origin: Origin) -> List[Dict[str, Any]]:
url: Optional[str] = origin.onion()
if url is None:
return []
return [{
"proto": "tor",
"type": "eotk",
"created_at": str(origin.added),
"updated_at": str(origin.updated),
"url": url}
]
def proxy_alternative(proxy: Proxy) -> Dict[str, Any]:
2022-05-16 13:29:48 +01:00
return {
"proto": "https",
"type": "mirror",
"created_at": str(proxy.added),
"updated_at": str(proxy.updated),
"url": proxy.url
}
def main_domain(origin: Origin) -> str:
# Both description and domain_name are required to be not null in the database schema
description: str = origin.description
if description.startswith("proxy:"):
return description[len("proxy:"):].replace("www.", "")
domain_name: str = origin.domain_name
return domain_name.replace("www.", "")
def active_proxies(origin: Origin, provider: str) -> List[Proxy]:
def _filter_fn(proxy: Proxy) -> bool:
return proxy.url is not None and not proxy.deprecated and not proxy.destroyed and proxy.provider == provider
return list(filter(_filter_fn, origin.proxies))
def mirror_sites(provider: str = "cloudfront") -> Dict[
str, Union[str, List[Dict[str, Union[str, List[Dict[str, str]]]]]]]:
return {"version": "2.0", "sites": [{"main_domain": main_domain(origin),
"available_alternatives": onion_alternative(origin) + [
proxy_alternative(proxy) for proxy in
active_proxies(origin, provider)]} for origin in
Origin.query.order_by(Origin.domain_name).all() if
origin.destroyed is None]}
2022-05-16 13:29:48 +01:00
if getattr(builtins, "__sphinx_build__", False):
schema = BypassCensorship2.schema_json()