majuna/app/portal/bridge.py

90 lines
2.8 KiB
Python
Raw Normal View History

2022-05-16 11:44:03 +01:00
from typing import Optional
2024-12-06 18:15:47 +00:00
from flask import Blueprint, Response, flash, redirect, render_template, url_for
2022-05-16 11:44:03 +01:00
from flask.typing import ResponseReturnValue
from app.extensions import db
from app.models.bridges import Bridge
from app.portal.util import LifecycleForm
bp = Blueprint("bridge", __name__)
_SECTION_TEMPLATE_VARS = {
"section": "bridge",
2024-12-06 18:15:47 +00:00
"help_url": "https://bypass.censorship.guide/user/bridges.html",
}
@bp.route("/list")
2022-05-16 11:44:03 +01:00
def bridge_list() -> ResponseReturnValue:
2022-05-16 13:29:48 +01:00
bridges = Bridge.query.filter(Bridge.destroyed.is_(None)).all()
2024-12-06 18:15:47 +00:00
return render_template(
"list.html.j2",
title="Tor Bridges",
item="bridge",
items=bridges,
**_SECTION_TEMPLATE_VARS,
)
2024-12-06 18:15:47 +00:00
@bp.route("/block/<bridge_id>", methods=["GET", "POST"])
2022-05-16 11:44:03 +01:00
def bridge_blocked(bridge_id: int) -> ResponseReturnValue:
2024-12-06 18:15:47 +00:00
bridge: Optional[Bridge] = Bridge.query.filter(
Bridge.id == bridge_id, Bridge.destroyed.is_(None)
).first()
if bridge is None:
2024-12-06 18:15:47 +00:00
return Response(
render_template(
"error.html.j2",
header="404 Proxy Not Found",
message="The requested bridge could not be found.",
**_SECTION_TEMPLATE_VARS,
)
)
form = LifecycleForm()
if form.validate_on_submit():
bridge.deprecate(reason="manual")
db.session.commit()
flash("Bridge will be shortly replaced.", "success")
2024-12-06 18:15:47 +00:00
return redirect(
url_for("portal.bridgeconf.bridgeconf_edit", bridgeconf_id=bridge.conf_id)
)
return render_template(
"lifecycle.html.j2",
header=f"Mark bridge {bridge.hashed_fingerprint} as blocked?",
message=bridge.hashed_fingerprint,
form=form,
**_SECTION_TEMPLATE_VARS,
)
2024-12-06 18:15:47 +00:00
@bp.route("/expire/<bridge_id>", methods=["GET", "POST"])
def bridge_expire(bridge_id: int) -> ResponseReturnValue:
2024-12-06 18:15:47 +00:00
bridge: Optional[Bridge] = Bridge.query.filter(
Bridge.id == bridge_id, Bridge.destroyed.is_(None)
).first()
if bridge is None:
2024-12-06 18:15:47 +00:00
return Response(
render_template(
"error.html.j2",
header="404 Proxy Not Found",
message="The requested bridge could not be found.",
**_SECTION_TEMPLATE_VARS,
)
)
form = LifecycleForm()
if form.validate_on_submit():
bridge.destroy()
db.session.commit()
flash("Bridge will be shortly destroyed.", "success")
2024-12-06 18:15:47 +00:00
return redirect(
url_for("portal.bridgeconf.bridgeconf_edit", bridgeconf_id=bridge.conf_id)
)
return render_template(
"lifecycle.html.j2",
header=f"Destroy bridge {bridge.hashed_fingerprint}?",
message=bridge.hashed_fingerprint,
form=form,
**_SECTION_TEMPLATE_VARS,
)