onions: switch bc2 over to new onion table, drop mirrors table

fixes: #4
This commit is contained in:
Iain Learmonth 2022-05-17 09:44:18 +01:00
parent fce594bbc4
commit 23a8a6b8af
4 changed files with 81 additions and 39 deletions

View file

@ -1,10 +1,10 @@
import builtins
from datetime import datetime
from typing import List, Dict, Union, Any
from typing import List, Dict, Union, Any, Optional
from pydantic import BaseModel, Field
from app.models.mirrors import Origin, Proxy, Mirror
from app.models.mirrors import Origin, Proxy
class BC2Alternative(BaseModel):
@ -16,9 +16,8 @@ class BC2Alternative(BaseModel):
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"])
main_domain: str = Field(description="The main domain name of the website, excluding \"www.\" if present.",
examples=["bbc.co.uk", "bbc.com", "guardianproject.info"])
available_alternatives: List[BC2Alternative]
@ -30,14 +29,18 @@ class BypassCensorship2(BaseModel):
title = "Bypass Censorship Version 2"
def mirror_alternative(mirror: Mirror) -> Dict[str, Any]:
return {
"proto": "tor" if ".onion" in mirror.url else "https",
"type": "eotk" if ".onion" in mirror.url else "mirror",
"created_at": str(mirror.added),
"updated_at": str(mirror.updated),
"url": mirror.url
}
def onion_alternative(origin: Origin) -> List[Dict[str, Any]]:
url: Optional[str] = origin.onion()
if url is None:
return []
else:
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]:
@ -50,15 +53,29 @@ def proxy_alternative(proxy: Proxy) -> Dict[str, Any]:
}
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": x.description[len("proxy:"):].replace("www.", "") if x.description.startswith(
"proxy:") else x.domain_name.replace("www.", ""),
"available_alternatives": [mirror_alternative(a) for a in x.mirrors if not a.deprecated and not a.destroyed] + [
proxy_alternative(a) for a in x.proxies if
a.url is not None and not a.deprecated and not a.destroyed and a.provider == provider]} for x in
Origin.query.order_by(Origin.domain_name).all() if x.destroyed is None]}
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]}
if getattr(builtins, "__sphinx_build__", False):