feat: create new onion services via api
This commit is contained in:
parent
192dacf760
commit
24cac76f70
10 changed files with 631 additions and 280 deletions
|
@ -1,10 +1,9 @@
|
|||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from flask import flash, redirect, url_for, render_template, Response, Blueprint
|
||||
from flask import flash, redirect, render_template, Response, Blueprint
|
||||
from flask.typing import ResponseReturnValue
|
||||
from flask_wtf import FlaskForm
|
||||
from flask_wtf.file import FileRequired
|
||||
from sqlalchemy import exc
|
||||
from wtforms import StringField, SelectField, SubmitField
|
||||
from flask_wtf.file import FileField
|
||||
|
@ -18,21 +17,6 @@ from app.portal.util import response_404, view_lifecycle
|
|||
bp = Blueprint("onion", __name__)
|
||||
|
||||
|
||||
class NewOnionForm(FlaskForm): # type: ignore
|
||||
domain_name = StringField('Domain Name', validators=[DataRequired()])
|
||||
description = StringField('Description', validators=[DataRequired()])
|
||||
onion_private_key = FileField('Onion Private Key', validators=[FileRequired()])
|
||||
onion_public_key = FileField('Onion Public Key',
|
||||
description="The onion hostname will be automatically calculated from the public key.",
|
||||
validators=[FileRequired()])
|
||||
tls_private_key = FileField('TLS Private Key (PEM format)',
|
||||
description=("If no TLS key and certificate are provided, a self-signed certificate "
|
||||
"will be generated."))
|
||||
tls_public_key = FileField('TLS Certificate (PEM format)')
|
||||
group = SelectField('Group', validators=[DataRequired()])
|
||||
submit = SubmitField('Save Changes')
|
||||
|
||||
|
||||
class EditOnionForm(FlaskForm): # type: ignore
|
||||
domain_name = StringField('Domain Name', validators=[DataRequired()])
|
||||
description = StringField('Description', validators=[DataRequired()])
|
||||
|
@ -47,36 +31,7 @@ class EditOnionForm(FlaskForm): # type: ignore
|
|||
@bp.route("/new", methods=['GET', 'POST'])
|
||||
@bp.route("/new/<group_id>", methods=['GET', 'POST'])
|
||||
def onion_new(group_id: Optional[int] = None) -> ResponseReturnValue:
|
||||
form = NewOnionForm()
|
||||
form.group.choices = [(x.id, x.group_name) for x in Group.query.all()]
|
||||
if form.validate_on_submit():
|
||||
onion = Onion()
|
||||
onion.group_id = form.group.data
|
||||
onion.domain_name = form.domain_name.data
|
||||
for at in [
|
||||
"onion_private_key",
|
||||
"onion_public_key",
|
||||
"tls_private_key",
|
||||
"tls_public_key"
|
||||
]:
|
||||
if form.__getattribute__(at).data is None:
|
||||
flash(f"Failed to create new onion. {at} was not provided.", "danger")
|
||||
return redirect(url_for("portal.onion.onion_list"))
|
||||
onion.__setattr__(at, form.__getattribute__(at).data.read())
|
||||
onion.description = form.description.data
|
||||
onion.created = datetime.utcnow()
|
||||
onion.updated = datetime.utcnow()
|
||||
try:
|
||||
db.session.add(onion)
|
||||
db.session.commit()
|
||||
flash(f"Created new onion {onion.onion_name}.", "success")
|
||||
return redirect(url_for("portal.onion.onion_edit", onion_id=onion.id))
|
||||
except exc.SQLAlchemyError:
|
||||
flash("Failed to create new onion.", "danger")
|
||||
return redirect(url_for("portal.onion.onion_list"))
|
||||
if group_id:
|
||||
form.group.data = group_id
|
||||
return render_template("new.html.j2", section="onion", form=form)
|
||||
return redirect("/ui/web/onions/new")
|
||||
|
||||
|
||||
@bp.route('/edit/<onion_id>', methods=['GET', 'POST'])
|
||||
|
@ -116,18 +71,12 @@ def onion_edit(onion_id: int) -> ResponseReturnValue:
|
|||
|
||||
@bp.route("/list")
|
||||
def onion_list() -> ResponseReturnValue:
|
||||
onions = Onion.query.order_by(Onion.domain_name).all()
|
||||
return render_template("list.html.j2",
|
||||
section="onion",
|
||||
title="Onion Services",
|
||||
item="onion service",
|
||||
new_link=url_for("portal.onion.onion_new"),
|
||||
items=onions)
|
||||
return redirect("/ui/web/onions")
|
||||
|
||||
|
||||
@bp.route("/destroy/<onion_id>", methods=['GET', 'POST'])
|
||||
def onion_destroy(onion_id: int) -> ResponseReturnValue:
|
||||
onion = Onion.query.filter(Onion.id == onion_id, Onion.destroyed.is_(None)).first()
|
||||
def onion_destroy(onion_id: str) -> ResponseReturnValue:
|
||||
onion = Onion.query.filter(Onion.id == int(onion_id), Onion.destroyed.is_(None)).first()
|
||||
if onion is None:
|
||||
return response_404("The requested onion service could not be found.")
|
||||
return view_lifecycle(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue