2024-12-06 16:08:48 +00:00
|
|
|
from datetime import datetime, timezone
|
|
|
|
from typing import List, Optional
|
2022-05-04 14:03:04 +01:00
|
|
|
|
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
|
2022-05-04 14:03:04 +01:00
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from sqlalchemy import exc
|
2024-12-06 16:08:48 +00:00
|
|
|
from wtforms import IntegerField, SelectField, StringField, SubmitField
|
2022-05-04 14:03:04 +01:00
|
|
|
from wtforms.validators import DataRequired, NumberRange
|
|
|
|
|
|
|
|
from app.extensions import db
|
2023-01-26 15:42:25 +00:00
|
|
|
from app.models.base import Pool
|
2023-02-26 15:06:40 +00:00
|
|
|
from app.models.bridges import BridgeConf, ProviderAllocation
|
2022-05-04 14:03:04 +01:00
|
|
|
from app.portal.util import response_404, view_lifecycle
|
|
|
|
|
|
|
|
bp = Blueprint("bridgeconf", __name__)
|
|
|
|
|
|
|
|
|
2022-08-25 20:49:20 +01:00
|
|
|
_SECTION_TEMPLATE_VARS = {
|
|
|
|
"section": "bridgeconf",
|
2024-12-06 18:15:47 +00:00
|
|
|
"help_url": "https://bypass.censorship.guide/user/bridges.html",
|
2022-08-25 20:49:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-16 11:44:03 +01:00
|
|
|
class NewBridgeConfForm(FlaskForm): # type: ignore
|
2024-12-06 18:15:47 +00:00
|
|
|
method = SelectField("Distribution Method", validators=[DataRequired()])
|
|
|
|
description = StringField("Description")
|
|
|
|
pool = SelectField("Pool", validators=[DataRequired()])
|
|
|
|
target_number = IntegerField(
|
|
|
|
"Target Number",
|
|
|
|
description="The number of active bridges to deploy (excluding deprecated bridges).",
|
|
|
|
validators=[NumberRange(1, message="One or more bridges must be created.")],
|
|
|
|
)
|
|
|
|
max_number = IntegerField(
|
|
|
|
"Maximum Number",
|
|
|
|
description="The maximum number of bridges to deploy (including deprecated bridges).",
|
|
|
|
validators=[
|
|
|
|
NumberRange(
|
|
|
|
1, message="Must be at least 1, ideally greater than target number."
|
|
|
|
)
|
|
|
|
],
|
|
|
|
)
|
|
|
|
expiry_hours = IntegerField(
|
|
|
|
"Expiry Timer (hours)",
|
|
|
|
description=(
|
|
|
|
"The number of hours to wait after a bridge is deprecated before its "
|
|
|
|
"destruction."
|
|
|
|
),
|
|
|
|
)
|
|
|
|
provider_allocation = SelectField(
|
|
|
|
"Provider Allocation Method",
|
|
|
|
description="How to allocate new bridges to providers.",
|
|
|
|
choices=[
|
|
|
|
("COST", "Use cheapest provider first"),
|
|
|
|
("RANDOM", "Use providers randomly"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
submit = SubmitField("Save Changes")
|
2022-05-04 14:03:04 +01:00
|
|
|
|
|
|
|
|
2022-05-16 11:44:03 +01:00
|
|
|
class EditBridgeConfForm(FlaskForm): # type: ignore
|
2024-12-06 18:15:47 +00:00
|
|
|
description = StringField("Description")
|
|
|
|
target_number = IntegerField(
|
|
|
|
"Target Number",
|
|
|
|
description="The number of active bridges to deploy (excluding deprecated bridges).",
|
|
|
|
validators=[NumberRange(1, message="One or more bridges must be created.")],
|
|
|
|
)
|
|
|
|
max_number = IntegerField(
|
|
|
|
"Maximum Number",
|
|
|
|
description="The maximum number of bridges to deploy (including deprecated bridges).",
|
|
|
|
validators=[
|
|
|
|
NumberRange(
|
|
|
|
1, message="Must be at least 1, ideally greater than target number."
|
|
|
|
)
|
|
|
|
],
|
|
|
|
)
|
|
|
|
expiry_hours = IntegerField(
|
|
|
|
"Expiry Timer (hours)",
|
|
|
|
description=(
|
|
|
|
"The number of hours to wait after a bridge is deprecated before its "
|
|
|
|
"destruction."
|
|
|
|
),
|
|
|
|
)
|
|
|
|
provider_allocation = SelectField(
|
|
|
|
"Provider Allocation Method",
|
|
|
|
description="How to allocate new bridges to providers.",
|
|
|
|
choices=[
|
|
|
|
("COST", "Use cheapest provider first"),
|
|
|
|
("RANDOM", "Use providers randomly"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
submit = SubmitField("Save Changes")
|
2022-05-04 14:03:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/list")
|
2022-05-16 11:44:03 +01:00
|
|
|
def bridgeconf_list() -> ResponseReturnValue:
|
2024-12-06 18:15:47 +00:00
|
|
|
bridgeconfs: List[BridgeConf] = BridgeConf.query.filter(
|
|
|
|
BridgeConf.destroyed.is_(None)
|
|
|
|
).all()
|
|
|
|
return render_template(
|
|
|
|
"list.html.j2",
|
|
|
|
title="Tor Bridge Configurations",
|
|
|
|
item="bridge configuration",
|
|
|
|
items=bridgeconfs,
|
|
|
|
new_link=url_for("portal.bridgeconf.bridgeconf_new"),
|
|
|
|
**_SECTION_TEMPLATE_VARS,
|
|
|
|
)
|
2022-05-04 14:03:04 +01:00
|
|
|
|
|
|
|
|
2024-12-06 18:15:47 +00:00
|
|
|
@bp.route("/new", methods=["GET", "POST"])
|
|
|
|
@bp.route("/new/<group_id>", methods=["GET", "POST"])
|
2022-05-16 11:44:03 +01:00
|
|
|
def bridgeconf_new(group_id: Optional[int] = None) -> ResponseReturnValue:
|
2022-05-04 14:03:04 +01:00
|
|
|
form = NewBridgeConfForm()
|
2024-12-06 18:15:47 +00:00
|
|
|
form.pool.choices = [
|
|
|
|
(x.id, x.pool_name) for x in Pool.query.filter(Pool.destroyed.is_(None)).all()
|
|
|
|
]
|
2022-05-04 14:03:04 +01:00
|
|
|
form.method.choices = [
|
|
|
|
("any", "Any (BridgeDB)"),
|
|
|
|
("email", "E-Mail (BridgeDB)"),
|
|
|
|
("moat", "Moat (BridgeDB)"),
|
2024-11-09 13:07:40 +00:00
|
|
|
("settings", "Settings (BridgeDB)"),
|
2022-05-04 14:03:04 +01:00
|
|
|
("https", "HTTPS (BridgeDB)"),
|
2024-12-06 18:15:47 +00:00
|
|
|
("none", "None (Private)"),
|
2022-05-04 14:03:04 +01:00
|
|
|
]
|
|
|
|
if form.validate_on_submit():
|
2023-01-26 15:42:25 +00:00
|
|
|
bridgeconf = BridgeConf()
|
|
|
|
bridgeconf.pool_id = form.pool.data
|
|
|
|
bridgeconf.method = form.method.data
|
|
|
|
bridgeconf.description = form.description.data
|
|
|
|
bridgeconf.target_number = form.target_number.data
|
|
|
|
bridgeconf.max_number = form.max_number.data
|
|
|
|
bridgeconf.expiry_hours = form.expiry_hours.data
|
2024-12-06 18:15:47 +00:00
|
|
|
bridgeconf.provider_allocation = ProviderAllocation[
|
|
|
|
form.provider_allocation.data
|
|
|
|
]
|
2024-12-06 16:08:48 +00:00
|
|
|
bridgeconf.added = datetime.now(tz=timezone.utc)
|
|
|
|
bridgeconf.updated = datetime.now(tz=timezone.utc)
|
2022-05-04 14:03:04 +01:00
|
|
|
try:
|
2023-01-26 15:42:25 +00:00
|
|
|
db.session.add(bridgeconf)
|
2022-05-04 14:03:04 +01:00
|
|
|
db.session.commit()
|
2023-01-26 15:42:25 +00:00
|
|
|
flash(f"Created new bridge configuration {bridgeconf.id}.", "success")
|
2022-05-04 14:03:04 +01:00
|
|
|
return redirect(url_for("portal.bridgeconf.bridgeconf_list"))
|
2022-06-23 13:42:45 +01:00
|
|
|
except exc.SQLAlchemyError:
|
2022-05-04 14:03:04 +01:00
|
|
|
flash("Failed to create new bridge configuration.", "danger")
|
|
|
|
return redirect(url_for("portal.bridgeconf.bridgeconf_list"))
|
|
|
|
if group_id:
|
|
|
|
form.group.data = group_id
|
2024-12-06 18:15:47 +00:00
|
|
|
return render_template("new.html.j2", form=form, **_SECTION_TEMPLATE_VARS)
|
2022-05-04 14:03:04 +01:00
|
|
|
|
|
|
|
|
2024-12-06 18:15:47 +00:00
|
|
|
@bp.route("/edit/<bridgeconf_id>", methods=["GET", "POST"])
|
2022-05-16 11:44:03 +01:00
|
|
|
def bridgeconf_edit(bridgeconf_id: int) -> ResponseReturnValue:
|
2022-05-04 14:03:04 +01:00
|
|
|
bridgeconf = BridgeConf.query.filter(BridgeConf.id == bridgeconf_id).first()
|
|
|
|
if bridgeconf is None:
|
2024-12-06 18:15:47 +00:00
|
|
|
return Response(
|
|
|
|
render_template(
|
|
|
|
"error.html.j2",
|
|
|
|
header="404 Bridge Configuration Not Found",
|
|
|
|
message="The requested bridge configuration could not be found.",
|
|
|
|
**_SECTION_TEMPLATE_VARS,
|
|
|
|
),
|
|
|
|
status=404,
|
|
|
|
)
|
|
|
|
form = EditBridgeConfForm(
|
|
|
|
description=bridgeconf.description,
|
|
|
|
target_number=bridgeconf.target_number,
|
|
|
|
max_number=bridgeconf.max_number,
|
|
|
|
expiry_hours=bridgeconf.expiry_hours,
|
|
|
|
provider_allocation=bridgeconf.provider_allocation.name,
|
|
|
|
)
|
2022-05-04 14:03:04 +01:00
|
|
|
if form.validate_on_submit():
|
|
|
|
bridgeconf.description = form.description.data
|
2023-01-26 15:42:25 +00:00
|
|
|
bridgeconf.target_number = form.target_number.data
|
|
|
|
bridgeconf.max_number = form.max_number.data
|
|
|
|
bridgeconf.expiry_hours = form.expiry_hours.data
|
2024-12-06 18:15:47 +00:00
|
|
|
bridgeconf.provider_allocation = ProviderAllocation[
|
|
|
|
form.provider_allocation.data
|
|
|
|
]
|
2024-12-06 16:08:48 +00:00
|
|
|
bridgeconf.updated = datetime.now(tz=timezone.utc)
|
2022-05-04 14:03:04 +01:00
|
|
|
try:
|
|
|
|
db.session.commit()
|
|
|
|
flash("Saved changes to bridge configuration.", "success")
|
|
|
|
except exc.SQLAlchemyError:
|
2024-12-06 18:15:47 +00:00
|
|
|
flash(
|
|
|
|
"An error occurred saving the changes to the bridge configuration.",
|
|
|
|
"danger",
|
|
|
|
)
|
|
|
|
return render_template(
|
|
|
|
"bridgeconf.html.j2", bridgeconf=bridgeconf, form=form, **_SECTION_TEMPLATE_VARS
|
|
|
|
)
|
2022-05-04 14:03:04 +01:00
|
|
|
|
|
|
|
|
2024-12-06 18:15:47 +00:00
|
|
|
@bp.route("/destroy/<bridgeconf_id>", methods=["GET", "POST"])
|
2022-05-16 11:44:03 +01:00
|
|
|
def bridgeconf_destroy(bridgeconf_id: int) -> ResponseReturnValue:
|
2024-12-06 18:15:47 +00:00
|
|
|
bridgeconf = BridgeConf.query.filter(
|
|
|
|
BridgeConf.id == bridgeconf_id, BridgeConf.destroyed.is_(None)
|
|
|
|
).first()
|
2022-05-04 14:03:04 +01:00
|
|
|
if bridgeconf is None:
|
|
|
|
return response_404("The requested bridge configuration could not be found.")
|
|
|
|
return view_lifecycle(
|
2022-05-16 13:29:48 +01:00
|
|
|
header="Destroy bridge configuration?",
|
2022-05-04 14:03:04 +01:00
|
|
|
message=bridgeconf.description,
|
|
|
|
success_view="portal.bridgeconf.bridgeconf_list",
|
|
|
|
success_message="All bridges from the destroyed configuration will shortly be destroyed at their providers.",
|
2023-01-21 14:49:57 +00:00
|
|
|
section="bridgeconf",
|
2022-05-04 14:03:04 +01:00
|
|
|
resource=bridgeconf,
|
2024-12-06 18:15:47 +00:00
|
|
|
action="destroy",
|
2022-05-04 14:03:04 +01:00
|
|
|
)
|