aboutsummaryrefslogtreecommitdiff
path: root/src/hydrilla/proxy/web_ui/rules.py
blob: 606d33f7b547c64ccd0cb7ba30cfa53eac4aa1c8 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# SPDX-License-Identifier: GPL-3.0-or-later

# Proxy web UI script blocking rule management.
#
# This file is part of Hydrilla&Haketilo.
#
# Copyright (C) 2022 Wojtek Kosior
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
#
# I, Wojtek Kosior, thereby promise not to sue for violation of this
# file's license. Although I request that you do not make use of this
# code in a proprietary program, I am not going to enforce this in
# court.

import typing as t

import flask
import werkzeug

from .. import state as st
from . import _app


bp = flask.Blueprint('rules', __package__)

@bp.route('/rules/add', methods=['GET'])
def add_rule(errors: t.Mapping[str, bool] = {}) -> werkzeug.Response:
    html = flask.render_template('rules/add.html.jinja', **errors)
    return flask.make_response(html, 200)

@bp.route('/rules/add', methods=['POST'])
def add_rule_post() -> werkzeug.Response:
    form_data = flask.request.form

    try:
        new_rule_ref = _app.get_haketilo_state().rule_store().add(
            pattern = form_data['pattern'],
            allow   = form_data['allow'] == 'true'
        )
    except st.RulePatternInvalid:
        return add_rule({'rule_pattern_invalid': True})

    return flask.redirect(flask.url_for('.show_rule', rule_id=new_rule_ref.id))

@bp.route('/rules', methods=['GET'])
def rules(errors: t.Mapping[str, bool] = {}) -> werkzeug.Response:
    store = _app.get_haketilo_state().rule_store()

    html = flask.render_template(
        'rules/index.html.jinja',
        display_infos = store.get_display_infos(),
        **errors
    )
    return flask.make_response(html, 200)

@bp.route('/rules/view/<string:rule_id>')
def show_rule(rule_id: str, errors: t.Mapping[str, bool] = {}) \
    -> werkzeug.Response:
    try:
        store = _app.get_haketilo_state().rule_store()
        display_info = store.get(rule_id).get_display_info()

        html = flask.render_template(
            'rules/show_single.html.jinja',
            display_info = display_info,
            **errors
        )
        return flask.make_response(html, 200)
    except st.MissingItemError:
        flask.abort(404)

@bp.route('/rules/view/<string:rule_id>', methods=['POST'])
def alter_rule(rule_id: str) -> werkzeug.Response:
    form_data = flask.request.form
    action = form_data['action']

    try:
        rule_ref = _app.get_haketilo_state().rule_store().get(rule_id)

        if action == 'remove_rule':
            rule_ref.remove()
            return flask.redirect(flask.url_for('.rules'))
        elif action == 'update_rule_data':
            allow_param = form_data.get('allow')
            rule_ref.update(
                pattern = form_data.get('pattern'),
                allow   = None if allow_param is None else allow_param == 'true'
            )
        else:
            raise ValueError()
    except st.RulePatternInvalid:
        return show_rule(rule_id, {'rule_pattern_invalid': True})
    except st.MissingItemError:
        flask.abort(404)

    return flask.redirect(flask.url_for('.show_rule', rule_id=rule_id))

@bp.route('/rules/viewbypattern')
def show_pattern_rule() -> werkzeug.Response:
    pattern = flask.request.args['pattern']

    try:
        store = _app.get_haketilo_state().rule_store()
        rule_ref = store.get_by_pattern(pattern)
    except st.MissingItemError:
        html = flask.render_template('rules/add.html.jinja', pattern=pattern)
        return flask.make_response(html, 200)

    return flask.redirect(flask.url_for('.show_rule', rule_id=rule_ref.id))