init work on multiple

This commit is contained in:
fl0ppy-d1sk 2024-05-06 17:47:05 +02:00
parent e9b3e60c77
commit 032837e184
No known key found for this signature in database
GPG key ID: 93EE47CC3D061500
4 changed files with 430 additions and 9 deletions

View file

@ -1,8 +1,10 @@
from wtforms import Form
from wtforms.fields import Field, StringField, BooleanField, SelectField, PasswordField
from wtforms.fields import Field, StringField, BooleanField, SelectField, PasswordField, FormField
from wtforms.validators import Regexp
from wtforms.widgets import CheckboxInput
from re import search
class BWBooleanField(Field):
widget = CheckboxInput()
@ -31,9 +33,16 @@ class BWBooleanField(Field):
return str(self.raw_data[0])
return "yes"
def number_from_setting_name(setting):
res = search(r"_([0-9]+)$", setting)
if res:
return res.group(1)
return "0"
def settings_to_form(settings):
class SettingsForm(Form):
pass
bw_multiple_forms = {}
for setting, data in settings.items():
field_type = None
field_data = dict(
@ -70,13 +79,43 @@ def settings_to_form(settings):
else:
print(f"unsupported type {data['type']}")
continue
if "multiple" not in data:
setattr(
SettingsForm,
setting,
field_type(
**field_data
)
)
else:
class BWMultipleForm(Form):
pass
multiple_key = f"{data['multiple']}-{number_from_setting_name(setting)}"
if multiple_key not in bw_multiple_forms:
bw_multiple_forms[multiple_key] = BWMultipleForm
setattr(
bw_multiple_forms[multiple_key],
setting,
field_type(
**field_data
)
)
for multiple, form in bw_multiple_forms.items():
setattr(
SettingsForm,
setting,
field_type(
**field_data
)
multiple,
FormField(form)
)
return SettingsForm
def compute_form(client_form, request_form, settings):
for key, value in request_form.items():
real_key = key
res = search(r"_([0-9]+)$", setting)
if res:
real_key = "_".join(key.split("_")[:-1])
if real_key in settings:
setattr(
client_form,
key,
)

114
limit.json Normal file
View file

@ -0,0 +1,114 @@
{
"id": "limit",
"name": "Limit",
"description": "Limit maximum number of requests and connections.",
"version": "1.0",
"stream": "partial",
"settings": {
"USE_LIMIT_REQ": {
"context": "multisite",
"default": "yes",
"help": "Activate limit requests feature.",
"id": "use-limit-req",
"label": "Activate limit requests",
"regex": "^(yes|no)$",
"type": "check"
},
"LIMIT_REQ_URL": {
"context": "multisite",
"default": "/",
"help": "URL (PCRE regex) where the limit request will be applied or special value / for all requests.",
"id": "limit-req-url",
"label": "Limit request URL",
"regex": "^.+$",
"type": "text",
"multiple": "limit-req"
},
"LIMIT_REQ_RATE": {
"context": "multisite",
"default": "2r/s",
"help": "Rate to apply to the URL (s for second, m for minute, h for hour and d for day).",
"id": "limit-req-rate",
"label": "Limit request Rate",
"regex": "^\\d+r/[smhd]$",
"type": "text",
"multiple": "limit-req"
},
"LIMIT_REQ_URL_1": {
"context": "multisite",
"default": "/",
"help": "URL (PCRE regex) where the limit request will be applied or special value / for all requests.",
"id": "limit-req-url",
"label": "Limit request URL",
"regex": "^.+$",
"type": "text",
"multiple": "limit-req"
},
"LIMIT_REQ_RATE_1": {
"context": "multisite",
"default": "2r/s",
"help": "Rate to apply to the URL (s for second, m for minute, h for hour and d for day).",
"id": "limit-req-rate",
"label": "Limit request Rate",
"regex": "^\\d+r/[smhd]$",
"type": "text",
"multiple": "limit-req"
},
"LIMIT_REQ_URL_2": {
"context": "multisite",
"default": "/",
"help": "URL (PCRE regex) where the limit request will be applied or special value / for all requests.",
"id": "limit-req-url",
"label": "Limit request URL",
"regex": "^.+$",
"type": "text",
"multiple": "limit-req"
},
"LIMIT_REQ_RATE_2": {
"context": "multisite",
"default": "2r/s",
"help": "Rate to apply to the URL (s for second, m for minute, h for hour and d for day).",
"id": "limit-req-rate",
"label": "Limit request Rate",
"regex": "^\\d+r/[smhd]$",
"type": "text",
"multiple": "limit-req"
},
"USE_LIMIT_CONN": {
"context": "multisite",
"default": "yes",
"help": "Activate limit connections feature.",
"id": "use-limit-conn",
"label": "Activate limit connections",
"regex": "^(yes|no)$",
"type": "check"
},
"LIMIT_CONN_MAX_HTTP1": {
"context": "multisite",
"default": "10",
"help": "Maximum number of connections per IP when using HTTP/1.X protocol.",
"id": "limit-conn-max-http1",
"label": "Maximum number of HTTP/1.X connections",
"regex": "^\\d+$",
"type": "text"
},
"LIMIT_CONN_MAX_HTTP2": {
"context": "multisite",
"default": "100",
"help": "Maximum number of streams per IP when using HTTP/2 protocol.",
"id": "limit-conn-max-http2",
"label": "Maximum number of HTTP/2 streams",
"regex": "^\\d+$",
"type": "text"
},
"LIMIT_CONN_MAX_STREAM": {
"context": "multisite",
"default": "10",
"help": "Maximum number of connections per IP when using stream.",
"id": "limit-conn-max-stream",
"label": "Maximum number of stream connections",
"regex": "^\\d+$",
"type": "text"
}
}
}

View file

@ -6,9 +6,11 @@ from forms import settings_to_form
app = Flask(__name__)
@app.route("/global", methods=['GET', 'POST'])
def login():
with open("settings.json") as f:
settings = loads(f.read())
def global_settings():
# with open("settings.json") as f:
# settings = loads(f.read())
with open("limit.json") as f:
settings = loads(f.read())["settings"]
form = settings_to_form(settings)(request.form)
if request.method == "POST" and form.validate():
for field in form:

266
reverseproxy.json Normal file
View file

@ -0,0 +1,266 @@
{
"id": "reverseproxy",
"name": "Reverse proxy",
"description": "Manage reverse proxy configurations.",
"version": "1.0",
"stream": "partial",
"settings": {
"USE_REVERSE_PROXY": {
"context": "multisite",
"default": "no",
"help": "Activate reverse proxy mode.",
"id": "use-reverse-proxy",
"label": "Use reverse proxy",
"regex": "^(yes|no)$",
"type": "check"
},
"REVERSE_PROXY_INTERCEPT_ERRORS": {
"context": "multisite",
"default": "yes",
"help": "Intercept and rewrite errors.",
"id": "reverse-proxy-intercept-errors",
"label": "Intercept errors",
"regex": "^(yes|no)$",
"type": "check"
},
"REVERSE_PROXY_HOST": {
"context": "multisite",
"default": "",
"help": "Full URL of the proxied resource (proxy_pass).",
"id": "reverse-proxy-host",
"label": "Reverse proxy host",
"regex": "^.*$",
"type": "text",
"multiple": "reverse-proxy"
},
"REVERSE_PROXY_URL": {
"context": "multisite",
"default": "",
"help": "Location URL that will be proxied.",
"id": "reverse-proxy-url",
"label": "Reverse proxy url",
"regex": "^.*$",
"type": "text",
"multiple": "reverse-proxy"
},
"REVERSE_PROXY_WS": {
"context": "multisite",
"default": "no",
"help": "Enable websocket on the proxied resource.",
"id": "reverse-proxy-ws",
"label": "Reverse proxy WS",
"regex": "^(yes|no)$",
"type": "check",
"multiple": "reverse-proxy"
},
"REVERSE_PROXY_HEADERS": {
"context": "multisite",
"default": "",
"help": "List of HTTP headers to send to proxied resource separated with semicolons (values for proxy_set_header directive).",
"id": "reverse-proxy-headers",
"label": "Reverse proxy headers",
"regex": "^(?![; ])(;? ?([\\w\\-]+)(?!.*\\2 ) [^;]+)*$",
"type": "text",
"multiple": "reverse-proxy"
},
"REVERSE_PROXY_HEADERS_CLIENT": {
"context": "multisite",
"default": "",
"help": "List of HTTP headers to send to client separated with semicolons (values for add_header directive).",
"id": "reverse-proxy-headers-client",
"label": "Reverse proxy headers-client",
"regex": "^(?![; ])(;? ?([\\w\\-]+)(?!.*\\2 ) [^;]+)*$",
"type": "text",
"multiple": "reverse-proxy"
},
"REVERSE_PROXY_BUFFERING": {
"context": "multisite",
"default": "yes",
"help": "Enable or disable buffering of responses from proxied resource.",
"id": "reverse-proxy-buffering",
"label": "Reverse proxy buffering",
"regex": "^(yes|no)$",
"type": "check",
"multiple": "reverse-proxy"
},
"REVERSE_PROXY_KEEPALIVE": {
"context": "multisite",
"default": "no",
"help": "Enable or disable keepalive connections with the proxied resource.",
"id": "reverse-proxy-keepalive",
"label": "Reverse proxy keepalive",
"regex": "^(yes|no)$",
"type": "check",
"multiple": "reverse-proxy"
},
"REVERSE_PROXY_AUTH_REQUEST": {
"context": "multisite",
"default": "",
"help": "Enable authentication using an external provider (value of auth_request directive).",
"id": "reverse-proxy-auth-request",
"label": "Reverse proxy auth request",
"regex": "^(\\/[\\w\\].~:\\/?#\\[@!$\\&'\\(\\)*+,;=\\-]*|off)?$",
"type": "text",
"multiple": "reverse-proxy"
},
"REVERSE_PROXY_AUTH_REQUEST_SIGNIN_URL": {
"context": "multisite",
"default": "",
"help": "Redirect clients to sign-in URL when using REVERSE_PROXY_AUTH_REQUEST (used when auth_request call returned 401).",
"id": "reverse-proxy-auth-request-signin-url",
"label": "Auth request signin URL",
"regex": "^(https?:\\/\\/[\\-\\w@:%.+~#=]+[\\-\\w\\(\\)!@:%+.~#?&\\/=$]*)?$",
"type": "text",
"multiple": "reverse-proxy"
},
"REVERSE_PROXY_AUTH_REQUEST_SET": {
"context": "multisite",
"default": "",
"help": "List of variables to set from the authentication provider, separated with semicolons (values of auth_request_set directives).",
"id": "reverse-proxy-auth-request-set",
"label": "Reverse proxy auth request set",
"regex": "^(?! ;)(;? ?(\\$[a-z_\\-]+)(?!.*\\2 ) [^;]+)*$",
"type": "text",
"multiple": "reverse-proxy"
},
"USE_PROXY_CACHE": {
"context": "multisite",
"default": "no",
"help": "Enable or disable caching of the proxied resources.",
"id": "use-proxy-cache",
"label": "Reverse proxy cache",
"regex": "^(yes|no)$",
"type": "check"
},
"PROXY_CACHE_PATH_LEVELS": {
"context": "global",
"default": "1:2",
"help": "Hierarchy levels of the cache.",
"id": "proxy-cache-path-levels",
"label": "Hierarchy levels",
"regex": "^(:?[12]){1,3}$",
"type": "text"
},
"PROXY_CACHE_PATH_ZONE_SIZE": {
"context": "global",
"default": "10m",
"help": "Maximum size of cached metadata when caching proxied resources.",
"id": "proxy-cache-path-zone-size",
"label": "Reverse proxy cache zone size",
"regex": "^\\d+[kKmMgG]?$",
"type": "text"
},
"PROXY_CACHE_PATH_PARAMS": {
"context": "global",
"default": "max_size=100m",
"help": "Additional parameters to add to the proxy_cache directive.",
"id": "proxy-cache-path-params",
"label": "Reverse proxy cache params",
"regex": "^.*$",
"type": "text"
},
"PROXY_CACHE_METHODS": {
"context": "multisite",
"default": "GET HEAD",
"help": "HTTP methods that should trigger a cache operation.",
"id": "proxy-cache-methods",
"label": "Reverse proxy cache methods",
"regex": "^(?! )( ?(GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH)(?!.*\\2))+$",
"type": "text"
},
"PROXY_CACHE_MIN_USES": {
"context": "multisite",
"default": "2",
"help": "The minimum number of requests before a response is cached.",
"id": "proxy-cache-min-uses",
"label": "Reverse proxy cache minimum uses",
"regex": "^[1-9]\\d*$",
"type": "text"
},
"PROXY_CACHE_KEY": {
"context": "multisite",
"default": "$scheme$host$request_uri",
"help": "The key used to uniquely identify a cached response.",
"id": "proxy-cache-key",
"label": "Reverse proxy cache key",
"regex": "^(?! )( ?(\\$[a-z_]+)(?!.*\\2))+$",
"type": "text"
},
"PROXY_CACHE_VALID": {
"context": "multisite",
"default": "200=24h 301=1h 302=24h",
"help": "Define the caching time depending on the HTTP status code (list of status=time), separated with spaces.",
"id": "proxy-cache-valid",
"label": "Reverse proxy cache valid",
"regex": "^(?! )( ?([1-5]\\d{2})(?!.*\\2=)=\\d+(ms?|[shdwMy]))*$",
"type": "text"
},
"PROXY_NO_CACHE": {
"context": "multisite",
"default": "$http_pragma $http_authorization",
"help": "Conditions to disable caching of responses.",
"id": "proxy-no-cache",
"label": "Reverse proxy no cache",
"regex": "^.*$",
"type": "text"
},
"PROXY_CACHE_BYPASS": {
"context": "multisite",
"default": "0",
"help": "Conditions to bypass caching of responses.",
"id": "proxy-cache-bypass",
"label": "Reverse proxy bypass",
"regex": "^.*$",
"type": "text"
},
"REVERSE_PROXY_CONNECT_TIMEOUT": {
"context": "multisite",
"default": "60s",
"help": "Timeout when connecting to the proxied resource.",
"id": "reverse-proxy-connect-timeout",
"label": "Reverse proxy connect timeout",
"regex": "^\\d+(ms?|[shdwMy])$",
"type": "text",
"multiple": "reverse-proxy"
},
"REVERSE_PROXY_READ_TIMEOUT": {
"context": "multisite",
"default": "60s",
"help": "Timeout when reading from the proxied resource.",
"id": "reverse-proxy-read-timeout",
"label": "Reverse proxy read timeout",
"regex": "^\\d+(ms?|[shdwMy])$",
"type": "text",
"multiple": "reverse-proxy"
},
"REVERSE_PROXY_SEND_TIMEOUT": {
"context": "multisite",
"default": "60s",
"help": "Timeout when sending to the proxied resource.",
"id": "reverse-proxy-send-timeout",
"label": "Reverse proxy send timeout",
"regex": "^\\d+(ms?|[shdwMy])$",
"type": "text",
"multiple": "reverse-proxy"
},
"REVERSE_PROXY_INCLUDES": {
"context": "multisite",
"default": "",
"help": "Additional configuration to include in the location block, separated with spaces.",
"id": "reverse-proxy-includes",
"label": "Reverse proxy includes",
"regex": "^(?! )( ?(\\w+)(?!.*\\b\\2\\b))*$",
"type": "text",
"multiple": "reverse-proxy"
},
"REVERSE_PROXY_CUSTOM_HOST": {
"context": "multisite",
"default": "",
"help": "Override Host header sent to upstream server.",
"id": "reverse-proxy-custom-host",
"label": "Reverse proxy custom host",
"regex": "^.*$",
"type": "text"
}
}
}