2022-05-12 17:03:26 +01:00
|
|
|
import json
|
2022-05-11 15:47:39 +01:00
|
|
|
from datetime import datetime
|
2022-09-26 13:40:59 +01:00
|
|
|
from typing import Optional, Any
|
2022-05-11 15:47:39 +01:00
|
|
|
|
2022-05-12 17:03:26 +01:00
|
|
|
from flask import render_template, url_for, flash, redirect, Blueprint, Response
|
2022-05-16 11:44:03 +01:00
|
|
|
from flask.typing import ResponseReturnValue
|
2022-05-11 15:47:39 +01:00
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from sqlalchemy import exc
|
|
|
|
from wtforms import SelectField, StringField, SubmitField
|
|
|
|
from wtforms.validators import DataRequired
|
|
|
|
|
2022-05-12 17:03:26 +01:00
|
|
|
from app.extensions import db
|
|
|
|
from app.lists.bc2 import mirror_sites
|
|
|
|
from app.lists.bridgelines import bridgelines
|
|
|
|
from app.lists.mirror_mapping import mirror_mapping
|
2022-12-21 19:27:46 +00:00
|
|
|
from app.lists.redirector import redirector_data
|
2022-09-26 13:40:59 +01:00
|
|
|
from app.models.base import MirrorList, Pool
|
2022-05-11 15:47:39 +01:00
|
|
|
from app.portal.util import response_404, view_lifecycle
|
|
|
|
|
|
|
|
bp = Blueprint("list", __name__)
|
|
|
|
|
|
|
|
|
2022-08-25 20:49:20 +01:00
|
|
|
_SECTION_TEMPLATE_VARS = {
|
|
|
|
"section": "list",
|
|
|
|
"help_url": "https://bypass.censorship.guide/user/lists.html"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-12 17:03:26 +01:00
|
|
|
@bp.app_template_filter("provider_name")
|
2022-06-23 11:49:38 +01:00
|
|
|
def list_provider_name(key: str) -> str:
|
|
|
|
return MirrorList.providers_supported.get(key, "Unknown")
|
2022-05-12 17:03:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.app_template_filter("format_name")
|
2022-06-23 11:49:38 +01:00
|
|
|
def list_format_name(key: str) -> str:
|
|
|
|
return MirrorList.formats_supported.get(key, "Unknown")
|
2022-05-12 17:03:26 +01:00
|
|
|
|
|
|
|
|
2022-05-16 17:09:33 +01:00
|
|
|
@bp.app_template_filter("list_encoding_name")
|
2022-06-23 11:49:38 +01:00
|
|
|
def list_encoding_name(key: str) -> str:
|
|
|
|
return MirrorList.encodings_supported.get(key, "Unknown")
|
2022-05-16 17:09:33 +01:00
|
|
|
|
|
|
|
|
2022-05-11 15:47:39 +01:00
|
|
|
@bp.route('/list')
|
2022-05-16 11:44:03 +01:00
|
|
|
def list_list() -> ResponseReturnValue:
|
2022-05-16 13:29:48 +01:00
|
|
|
lists = MirrorList.query.filter(MirrorList.destroyed.is_(None)).all()
|
2022-05-11 15:47:39 +01:00
|
|
|
return render_template("list.html.j2",
|
2022-08-17 13:05:56 +01:00
|
|
|
title="Distribution Lists",
|
|
|
|
item="distribution list",
|
2022-05-11 15:47:39 +01:00
|
|
|
new_link=url_for("portal.list.list_new"),
|
2022-05-12 17:03:26 +01:00
|
|
|
items=lists,
|
2022-08-25 20:49:20 +01:00
|
|
|
**_SECTION_TEMPLATE_VARS
|
2022-05-12 17:03:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-09-26 14:51:11 +01:00
|
|
|
@bp.route('/preview/<format_>/<pool_id>')
|
|
|
|
def list_preview(format_: str, pool_id: int) -> ResponseReturnValue:
|
|
|
|
pool = Pool.query.filter(Pool.id == pool_id).first()
|
|
|
|
if not pool:
|
|
|
|
return response_404(message="Pool not found")
|
2022-05-12 17:03:26 +01:00
|
|
|
if format_ == "bca":
|
2022-09-26 14:51:11 +01:00
|
|
|
return Response(json.dumps(mirror_mapping(pool)), content_type="application/json")
|
2022-05-12 17:03:26 +01:00
|
|
|
if format_ == "bc2":
|
2022-09-26 14:51:11 +01:00
|
|
|
return Response(json.dumps(mirror_sites(pool)), content_type="application/json")
|
2022-05-12 17:03:26 +01:00
|
|
|
if format_ == "bridgelines":
|
2022-09-26 14:51:11 +01:00
|
|
|
return Response(json.dumps(bridgelines(pool)), content_type="application/json")
|
2022-12-21 19:27:46 +00:00
|
|
|
if format_ == "rdr":
|
|
|
|
return Response(json.dumps(redirector_data(pool)), content_type="application/json")
|
2022-05-12 17:03:26 +01:00
|
|
|
return response_404(message="Format not found")
|
2022-05-11 15:47:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/destroy/<list_id>", methods=['GET', 'POST'])
|
2022-05-16 11:44:03 +01:00
|
|
|
def list_destroy(list_id: int) -> ResponseReturnValue:
|
2022-05-16 13:29:48 +01:00
|
|
|
list_ = MirrorList.query.filter(MirrorList.id == list_id, MirrorList.destroyed.is_(None)).first()
|
2022-05-11 15:47:39 +01:00
|
|
|
if list_ 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 mirror list?",
|
2022-05-11 15:47:39 +01:00
|
|
|
message=list_.description,
|
|
|
|
success_view="portal.list.list_list",
|
|
|
|
success_message="This list will no longer be updated and may be deleted depending on the provider.",
|
|
|
|
section="list",
|
|
|
|
resource=list_,
|
|
|
|
action="destroy"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/new", methods=['GET', 'POST'])
|
|
|
|
@bp.route("/new/<group_id>", methods=['GET', 'POST'])
|
2022-05-16 11:44:03 +01:00
|
|
|
def list_new(group_id: Optional[int] = None) -> ResponseReturnValue:
|
2022-05-11 15:47:39 +01:00
|
|
|
form = NewMirrorListForm()
|
2022-05-17 10:12:45 +01:00
|
|
|
form.provider.choices = list(MirrorList.providers_supported.items())
|
|
|
|
form.format.choices = list(MirrorList.formats_supported.items())
|
|
|
|
form.encoding.choices = list(MirrorList.encodings_supported.items())
|
2022-05-11 15:47:39 +01:00
|
|
|
if form.validate_on_submit():
|
|
|
|
list_ = MirrorList()
|
2022-09-26 13:40:59 +01:00
|
|
|
list_.pool_id = form.pool.data
|
2022-05-11 15:47:39 +01:00
|
|
|
list_.provider = form.provider.data
|
|
|
|
list_.format = form.format.data
|
2022-05-17 10:15:00 +01:00
|
|
|
list_.encoding = form.encoding.data
|
2022-05-11 15:47:39 +01:00
|
|
|
list_.description = form.description.data
|
|
|
|
list_.container = form.container.data
|
|
|
|
list_.branch = form.branch.data
|
2022-05-11 16:12:52 +01:00
|
|
|
list_.role = form.role.data
|
2022-05-11 15:47:39 +01:00
|
|
|
list_.filename = form.filename.data
|
|
|
|
list_.created = datetime.utcnow()
|
|
|
|
list_.updated = datetime.utcnow()
|
|
|
|
try:
|
|
|
|
db.session.add(list_)
|
|
|
|
db.session.commit()
|
2022-05-16 13:29:48 +01:00
|
|
|
flash("Created new mirror list.", "success")
|
2022-05-11 15:47:39 +01:00
|
|
|
return redirect(url_for("portal.list.list_list"))
|
2022-06-23 13:42:45 +01:00
|
|
|
except exc.SQLAlchemyError:
|
2022-05-11 15:47:39 +01:00
|
|
|
flash("Failed to create new mirror list.", "danger")
|
|
|
|
return redirect(url_for("portal.list.list_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-11 15:47:39 +01:00
|
|
|
|
|
|
|
|
2022-05-16 11:44:03 +01:00
|
|
|
class NewMirrorListForm(FlaskForm): # type: ignore
|
2022-09-26 13:40:59 +01:00
|
|
|
pool = SelectField('Resource Pool', validators=[DataRequired()])
|
2022-05-11 15:47:39 +01:00
|
|
|
provider = SelectField('Provider', validators=[DataRequired()])
|
|
|
|
format = SelectField('Distribution Method', validators=[DataRequired()])
|
2022-05-16 17:09:33 +01:00
|
|
|
encoding = SelectField('Encoding', validators=[DataRequired()])
|
2022-05-11 15:47:39 +01:00
|
|
|
description = StringField('Description', validators=[DataRequired()])
|
2022-05-11 16:12:52 +01:00
|
|
|
container = StringField('Container', validators=[DataRequired()],
|
|
|
|
description="GitHub Project, GitLab Project or AWS S3 bucket name.")
|
|
|
|
branch = StringField('Git Branch/AWS Region', validators=[DataRequired()],
|
|
|
|
description="For GitHub/GitLab, set this to the desired branch name, e.g. main. For AWS S3, "
|
|
|
|
"set this field to the desired region, e.g. us-east-1.")
|
|
|
|
role = StringField('Role ARN',
|
|
|
|
description="(Optional) ARN for IAM role to assume for interaction with the S3 bucket.")
|
2022-05-11 15:47:39 +01:00
|
|
|
filename = StringField('Filename', validators=[DataRequired()])
|
|
|
|
submit = SubmitField('Save Changes')
|
2022-08-17 13:05:56 +01:00
|
|
|
|
2022-09-26 13:40:59 +01:00
|
|
|
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.pool.choices = [
|
|
|
|
(pool.id, pool.pool_name) for pool in Pool.query.all()
|
|
|
|
]
|
|
|
|
|
2022-08-17 13:28:41 +01:00
|
|
|
|
2022-08-17 13:05:56 +01:00
|
|
|
@bp.route('/edit/<list_id>', methods=['GET', 'POST'])
|
|
|
|
def list_edit(list_id: int) -> ResponseReturnValue:
|
|
|
|
list_: Optional[MirrorList] = MirrorList.query.filter(MirrorList.id == list_id).first()
|
2022-08-17 13:23:42 +01:00
|
|
|
if list_ is None:
|
2022-08-17 13:05:56 +01:00
|
|
|
return Response(render_template("error.html.j2",
|
|
|
|
header="404 Distribution List Not Found",
|
2022-08-25 20:49:20 +01:00
|
|
|
message="The requested distribution list could not be found.",
|
|
|
|
**_SECTION_TEMPLATE_VARS),
|
2022-08-17 13:05:56 +01:00
|
|
|
status=404)
|
|
|
|
form = NewMirrorListForm(
|
2022-09-26 13:58:51 +01:00
|
|
|
pool=list_.pool_id,
|
2022-08-17 13:05:56 +01:00
|
|
|
provider=list_.provider,
|
|
|
|
format=list_.format,
|
|
|
|
encoding=list_.encoding,
|
|
|
|
description=list_.description,
|
|
|
|
container=list_.container,
|
|
|
|
branch=list_.branch,
|
|
|
|
role=list_.role,
|
|
|
|
filename=list_.filename
|
|
|
|
)
|
|
|
|
form.provider.choices = list(MirrorList.providers_supported.items())
|
|
|
|
form.format.choices = list(MirrorList.formats_supported.items())
|
|
|
|
form.encoding.choices = list(MirrorList.encodings_supported.items())
|
|
|
|
if form.validate_on_submit():
|
2022-09-26 13:40:59 +01:00
|
|
|
list_.pool_id = form.pool.data
|
2022-08-17 13:05:56 +01:00
|
|
|
list_.provider = form.provider.data
|
|
|
|
list_.format = form.format.data
|
|
|
|
list_.encoding = form.encoding.data
|
|
|
|
list_.description = form.description.data
|
|
|
|
list_.container = form.container.data
|
|
|
|
list_.branch = form.branch.data
|
|
|
|
list_.role = form.role.data
|
|
|
|
list_.filename = form.filename.data
|
|
|
|
list_.updated = datetime.utcnow()
|
|
|
|
try:
|
|
|
|
db.session.commit()
|
|
|
|
flash("Saved changes to group.", "success")
|
|
|
|
except exc.SQLAlchemyError:
|
|
|
|
flash("An error occurred saving the changes to the distribution list.", "danger")
|
|
|
|
return render_template("distlist.html.j2",
|
2022-08-25 20:49:20 +01:00
|
|
|
list=list_, form=form,
|
|
|
|
**_SECTION_TEMPLATE_VARS)
|