2022-05-04 14:03:04 +01:00
|
|
|
from datetime import datetime
|
2022-05-16 11:44:03 +01:00
|
|
|
from typing import Optional, List
|
2022-05-04 14:03:04 +01:00
|
|
|
|
|
|
|
from flask import render_template, url_for, flash, redirect, Response, Blueprint
|
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
|
|
|
|
from wtforms import SelectField, StringField, IntegerField, SubmitField
|
|
|
|
from wtforms.validators import DataRequired, NumberRange
|
|
|
|
|
|
|
|
from app.extensions import db
|
|
|
|
from app.models.base import Group
|
|
|
|
from app.models.bridges import BridgeConf
|
|
|
|
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",
|
|
|
|
"help_url": "https://bypass.censorship.guide/user/bridges.html"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-16 11:44:03 +01:00
|
|
|
class NewBridgeConfForm(FlaskForm): # type: ignore
|
2022-05-04 14:03:04 +01:00
|
|
|
provider = SelectField('Provider', validators=[DataRequired()])
|
|
|
|
method = SelectField('Distribution Method', validators=[DataRequired()])
|
|
|
|
description = StringField('Description')
|
|
|
|
group = SelectField('Group', validators=[DataRequired()])
|
|
|
|
number = IntegerField('Number', validators=[NumberRange(1, message="One or more bridges must be created")])
|
|
|
|
submit = SubmitField('Save Changes')
|
|
|
|
|
|
|
|
|
2022-05-16 11:44:03 +01:00
|
|
|
class EditBridgeConfForm(FlaskForm): # type: ignore
|
2022-05-04 14:03:04 +01:00
|
|
|
description = StringField('Description')
|
|
|
|
number = IntegerField('Number', validators=[NumberRange(1, message="One or more bridges must be created")])
|
|
|
|
submit = SubmitField('Save Changes')
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/list")
|
2022-05-16 11:44:03 +01:00
|
|
|
def bridgeconf_list() -> ResponseReturnValue:
|
2022-05-16 13:29:48 +01:00
|
|
|
bridgeconfs: List[BridgeConf] = BridgeConf.query.filter(BridgeConf.destroyed.is_(None)).all()
|
2022-05-04 14:03:04 +01:00
|
|
|
return render_template("list.html.j2",
|
|
|
|
title="Tor Bridge Configurations",
|
|
|
|
item="bridge configuration",
|
|
|
|
items=bridgeconfs,
|
2022-08-25 20:49:20 +01:00
|
|
|
new_link=url_for("portal.bridgeconf.bridgeconf_new"),
|
|
|
|
**_SECTION_TEMPLATE_VARS)
|
2022-05-04 14:03:04 +01: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()
|
|
|
|
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.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
|
2022-08-25 20:49:20 +01:00
|
|
|
return render_template("new.html.j2",
|
|
|
|
form=form,
|
|
|
|
**_SECTION_TEMPLATE_VARS)
|
2022-05-04 14:03:04 +01: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:
|
|
|
|
return Response(render_template("error.html.j2",
|
|
|
|
header="404 Bridge Configuration Not Found",
|
2022-08-25 20:49:20 +01:00
|
|
|
message="The requested bridge configuration could not be found.",
|
|
|
|
**_SECTION_TEMPLATE_VARS),
|
2022-05-04 14:03:04 +01:00
|
|
|
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",
|
2022-08-25 20:49:20 +01:00
|
|
|
bridgeconf=bridgeconf,
|
|
|
|
form=form,
|
|
|
|
**_SECTION_TEMPLATE_VARS)
|
2022-05-04 14:03:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/destroy/<bridgeconf_id>", methods=['GET', 'POST'])
|
2022-05-16 11:44:03 +01:00
|
|
|
def bridgeconf_destroy(bridgeconf_id: int) -> ResponseReturnValue:
|
2022-05-16 13:29:48 +01: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.",
|
|
|
|
resource=bridgeconf,
|
2022-08-25 20:49:20 +01:00
|
|
|
action="destroy",
|
|
|
|
**_SECTION_TEMPLATE_VARS
|
2022-05-04 14:03:04 +01:00
|
|
|
)
|