247 lines
10 KiB
Python
247 lines
10 KiB
Python
from datetime import datetime, timedelta, timezone
|
|
|
|
from flask import Blueprint, render_template, Response, flash, redirect, url_for, request
|
|
from sqlalchemy import exc, desc, or_
|
|
|
|
from app.extensions import db
|
|
from app.models.bridges import BridgeConf, Bridge
|
|
from app.models.alarms import Alarm
|
|
from app import Origin, Proxy
|
|
from app.models.base import Group, MirrorList
|
|
from app.portal.forms import LifecycleForm, NewBridgeConfForm, EditBridgeConfForm, NewMirrorListForm
|
|
from app.portal.group import bp as group
|
|
from app.portal.origin import bp as origin
|
|
from app.portal.proxy import bp as proxy
|
|
from app.portal.util import response_404, view_lifecycle
|
|
|
|
portal = Blueprint("portal", __name__, template_folder="templates", static_folder="static")
|
|
|
|
portal.register_blueprint(group, url_prefix="/group")
|
|
portal.register_blueprint(origin, url_prefix="/origin")
|
|
portal.register_blueprint(proxy, url_prefix="/proxy")
|
|
|
|
|
|
@portal.app_template_filter("mirror_expiry")
|
|
def calculate_mirror_expiry(s: datetime) -> str:
|
|
expiry = s + timedelta(days=3)
|
|
countdown = expiry - datetime.utcnow()
|
|
if countdown.days == 0:
|
|
return f"{countdown.seconds // 3600} hours"
|
|
return f"{countdown.days} days"
|
|
|
|
|
|
@portal.app_template_filter("format_datetime")
|
|
def format_datetime(s: datetime) -> str:
|
|
if s is None:
|
|
return "Unknown"
|
|
return s.strftime("%a, %d %b %Y %H:%M:%S")
|
|
|
|
|
|
@portal.route("/")
|
|
def portal_home():
|
|
groups = Group.query.order_by(Group.group_name).all()
|
|
now = datetime.now(timezone.utc)
|
|
proxies = Proxy.query.filter(Proxy.destroyed == None).all()
|
|
last24 = len(Proxy.query.filter(Proxy.deprecated > (now - timedelta(days=1))).all())
|
|
last72 = len(Proxy.query.filter(Proxy.deprecated > (now - timedelta(days=3))).all())
|
|
lastweek = len(Proxy.query.filter(Proxy.deprecated > (now - timedelta(days=7))).all())
|
|
return render_template("home.html.j2", section="home", groups=groups, last24=last24, last72=last72,
|
|
lastweek=lastweek, proxies=proxies)
|
|
|
|
|
|
@portal.route("/search")
|
|
def search():
|
|
query = request.args.get("query")
|
|
proxies = Proxy.query.filter(or_(Proxy.url.contains(query)), Proxy.destroyed == None).all()
|
|
origins = Origin.query.filter(or_(Origin.description.contains(query), Origin.domain_name.contains(query))).all()
|
|
return render_template("search.html.j2", section="home", proxies=proxies, origins=origins)
|
|
|
|
|
|
@portal.route('/alarms')
|
|
def view_alarms():
|
|
three_days_ago = datetime.now(timezone.utc) - timedelta(days=3)
|
|
alarms = Alarm.query.filter(Alarm.last_updated >= three_days_ago).order_by(
|
|
Alarm.alarm_state, desc(Alarm.state_changed)).all()
|
|
return render_template("alarms.html.j2", section="alarm", alarms=alarms)
|
|
|
|
|
|
@portal.route('/lists')
|
|
def view_mirror_lists():
|
|
mirrorlists = MirrorList.query.filter(MirrorList.destroyed == None).all()
|
|
return render_template("list.html.j2",
|
|
section="list",
|
|
title="Mirror Lists",
|
|
item="mirror list",
|
|
new_link=url_for("portal.new_mirror_list"),
|
|
items=mirrorlists)
|
|
|
|
|
|
@portal.route("/list/destroy/<list_id>")
|
|
def destroy_mirror_list(list_id):
|
|
return "not implemented"
|
|
|
|
|
|
@portal.route("/list/new", methods=['GET', 'POST'])
|
|
@portal.route("/list/new/<group_id>", methods=['GET', 'POST'])
|
|
def new_mirror_list(group_id=None):
|
|
form = NewMirrorListForm()
|
|
form.provider.choices = [
|
|
("github", "GitHub"),
|
|
("gitlab", "GitLab"),
|
|
("s3", "AWS S3"),
|
|
]
|
|
form.format.choices = [
|
|
("bc2", "Bypass Censorship v2"),
|
|
("bc3", "Bypass Censorship v3"),
|
|
("bca", "Bypass Censorship Analytics"),
|
|
("bridgelines", "Tor Bridge Lines")
|
|
]
|
|
form.container.description = "GitHub Project, GitLab Project or AWS S3 bucket name."
|
|
form.branch.description = "Ignored for AWS S3."
|
|
if form.validate_on_submit():
|
|
mirror_list = MirrorList()
|
|
mirror_list.provider = form.provider.data
|
|
mirror_list.format = form.format.data
|
|
mirror_list.description = form.description.data
|
|
mirror_list.container = form.container.data
|
|
mirror_list.branch = form.branch.data
|
|
mirror_list.filename = form.filename.data
|
|
mirror_list.created = datetime.utcnow()
|
|
mirror_list.updated = datetime.utcnow()
|
|
try:
|
|
db.session.add(mirror_list)
|
|
db.session.commit()
|
|
flash(f"Created new mirror list.", "success")
|
|
return redirect(url_for("portal.view_mirror_lists"))
|
|
except exc.SQLAlchemyError as e:
|
|
print(e)
|
|
flash("Failed to create new mirror list.", "danger")
|
|
return redirect(url_for("portal.view_mirror_lists"))
|
|
if group_id:
|
|
form.group.data = group_id
|
|
return render_template("new.html.j2", section="list", form=form)
|
|
|
|
|
|
@portal.route("/bridgeconfs")
|
|
def view_bridgeconfs():
|
|
bridgeconfs = BridgeConf.query.filter(BridgeConf.destroyed == None).all()
|
|
return render_template("list.html.j2",
|
|
section="bridgeconf",
|
|
title="Tor Bridge Configurations",
|
|
item="bridge configuration",
|
|
items=bridgeconfs,
|
|
new_link=url_for("portal.new_bridgeconf"))
|
|
|
|
|
|
@portal.route("/bridgeconf/new", methods=['GET', 'POST'])
|
|
@portal.route("/bridgeconf/new/<group_id>", methods=['GET', 'POST'])
|
|
def new_bridgeconf(group_id=None):
|
|
form = NewBridgeConfForm()
|
|
form.group.choices = [(x.id, x.group_name) for x in Group.query.all()]
|
|
form.provider.choices = [
|
|
("aws", "AWS Lightsail"),
|
|
("hcloud", "Hetzner Cloud"),
|
|
("ovh", "OVH Public Cloud"),
|
|
("gandi", "GandiCloud VPS")
|
|
]
|
|
form.method.choices = [
|
|
("any", "Any (BridgeDB)"),
|
|
("email", "E-Mail (BridgeDB)"),
|
|
("moat", "Moat (BridgeDB)"),
|
|
("https", "HTTPS (BridgeDB)"),
|
|
("none", "None (Private)")
|
|
]
|
|
if form.validate_on_submit():
|
|
bridge_conf = BridgeConf()
|
|
bridge_conf.group_id = form.group.data
|
|
bridge_conf.provider = form.provider.data
|
|
bridge_conf.method = form.method.data
|
|
bridge_conf.description = form.description.data
|
|
bridge_conf.number = form.number.data
|
|
bridge_conf.created = datetime.utcnow()
|
|
bridge_conf.updated = datetime.utcnow()
|
|
try:
|
|
db.session.add(bridge_conf)
|
|
db.session.commit()
|
|
flash(f"Created new bridge configuration {bridge_conf.id}.", "success")
|
|
return redirect(url_for("portal.view_bridgeconfs"))
|
|
except exc.SQLAlchemyError as e:
|
|
print(e)
|
|
flash("Failed to create new bridge configuration.", "danger")
|
|
return redirect(url_for("portal.view_bridgeconfs"))
|
|
if group_id:
|
|
form.group.data = group_id
|
|
return render_template("new.html.j2", section="bridgeconf", form=form)
|
|
|
|
|
|
@portal.route("/bridges")
|
|
def view_bridges():
|
|
bridges = Bridge.query.filter(Bridge.destroyed == None).all()
|
|
return render_template("list.html.j2",
|
|
section="bridge",
|
|
title="Tor Bridges",
|
|
item="bridge",
|
|
items=bridges)
|
|
|
|
|
|
@portal.route('/bridgeconf/edit/<bridgeconf_id>', methods=['GET', 'POST'])
|
|
def edit_bridgeconf(bridgeconf_id):
|
|
bridgeconf = BridgeConf.query.filter(BridgeConf.id == bridgeconf_id).first()
|
|
if bridgeconf is None:
|
|
return Response(render_template("error.html.j2",
|
|
section="bridge",
|
|
header="404 Bridge Configuration Not Found",
|
|
message="The requested bridge configuration could not be found."),
|
|
status=404)
|
|
form = EditBridgeConfForm(description=bridgeconf.description,
|
|
number=bridgeconf.number)
|
|
if form.validate_on_submit():
|
|
bridgeconf.description = form.description.data
|
|
bridgeconf.number = form.number.data
|
|
bridgeconf.updated = datetime.utcnow()
|
|
try:
|
|
db.session.commit()
|
|
flash("Saved changes to bridge configuration.", "success")
|
|
except exc.SQLAlchemyError:
|
|
flash("An error occurred saving the changes to the bridge configuration.", "danger")
|
|
return render_template("bridgeconf.html.j2",
|
|
section="bridgeconf",
|
|
bridgeconf=bridgeconf, form=form)
|
|
|
|
|
|
@portal.route("/bridge/block/<bridge_id>", methods=['GET', 'POST'])
|
|
def blocked_bridge(bridge_id):
|
|
bridge: Bridge = Bridge.query.filter(Bridge.id == bridge_id, Bridge.destroyed == None).first()
|
|
if bridge is None:
|
|
return Response(render_template("error.html.j2",
|
|
header="404 Proxy Not Found",
|
|
message="The requested bridge could not be found."))
|
|
form = LifecycleForm()
|
|
if form.validate_on_submit():
|
|
bridge.deprecate(reason="manual")
|
|
db.session.commit()
|
|
flash("Bridge will be shortly replaced.", "success")
|
|
return redirect(url_for("portal.edit_bridgeconf", bridgeconf_id=bridge.conf_id))
|
|
return render_template("lifecycle.html.j2",
|
|
header=f"Mark bridge {bridge.hashed_fingerprint} as blocked?",
|
|
message=bridge.hashed_fingerprint,
|
|
section="bridge",
|
|
form=form)
|
|
|
|
|
|
@portal.route("/bridgeconf/destroy/<bridgeconf_id>", methods=['GET', 'POST'])
|
|
def destroy_bridgeconf(bridgeconf_id: int):
|
|
bridgeconf = BridgeConf.query.filter(BridgeConf.id == bridgeconf_id, BridgeConf.destroyed == None).first()
|
|
if bridgeconf is None:
|
|
return response_404("The requested bridge configuration could not be found.")
|
|
return view_lifecycle(
|
|
header=f"Destroy bridge configuration?",
|
|
message=bridgeconf.description,
|
|
success_view="portal.view_bridgeconfs",
|
|
success_message="All bridges from the destroyed configuration will shortly be destroyed at their providers.",
|
|
section="bridgeconf",
|
|
resource=bridgeconf,
|
|
action="destroy"
|
|
)
|
|
|
|
|