ser2netweb 3.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ser2netweb/__init__.py +20 -0
- ser2netweb/__main__.py +6 -0
- ser2netweb/accounts.py +503 -0
- ser2netweb/cli.py +489 -0
- ser2netweb/clock.py +128 -0
- ser2netweb/config.py +4491 -0
- ser2netweb/console.py +181 -0
- ser2netweb/datalock.py +181 -0
- ser2netweb/engine/__init__.py +0 -0
- ser2netweb/engine/access.py +173 -0
- ser2netweb/engine/bindhint.py +115 -0
- ser2netweb/engine/bridge.py +1928 -0
- ser2netweb/engine/cluster.py +1140 -0
- ser2netweb/engine/modbus.py +530 -0
- ser2netweb/engine/modbus_gateway.py +1465 -0
- ser2netweb/engine/mqtt_pub.py +632 -0
- ser2netweb/engine/netguard.py +805 -0
- ser2netweb/engine/netinfo.py +199 -0
- ser2netweb/engine/portdiag.py +414 -0
- ser2netweb/engine/portlist.py +471 -0
- ser2netweb/engine/protocols/__init__.py +28 -0
- ser2netweb/engine/protocols/base.py +117 -0
- ser2netweb/engine/protocols/raw.py +12 -0
- ser2netweb/engine/protocols/rfc2217.py +586 -0
- ser2netweb/engine/protocols/telnet.py +172 -0
- ser2netweb/engine/serial_compat.py +598 -0
- ser2netweb/engine/serial_io.py +210 -0
- ser2netweb/engine/serial_link.py +414 -0
- ser2netweb/engine/sockopts.py +103 -0
- ser2netweb/engine/sparkplug.py +538 -0
- ser2netweb/engine/sparkplug_node.py +550 -0
- ser2netweb/engine/supervisor.py +533 -0
- ser2netweb/engine/tls.py +232 -0
- ser2netweb/envvars.py +57 -0
- ser2netweb/explain.py +117 -0
- ser2netweb/healthcheck.py +78 -0
- ser2netweb/i18n.py +110 -0
- ser2netweb/importer.py +550 -0
- ser2netweb/layout.py +118 -0
- ser2netweb/licences.py +86 -0
- ser2netweb/locale/tr.json +1569 -0
- ser2netweb/logsetup.py +253 -0
- ser2netweb/logtext.py +23 -0
- ser2netweb/notify.py +761 -0
- ser2netweb/optional.py +76 -0
- ser2netweb/reload.py +295 -0
- ser2netweb/runtime.py +762 -0
- ser2netweb/sdnotify.py +85 -0
- ser2netweb/ser2net_import.py +2001 -0
- ser2netweb/signins.py +98 -0
- ser2netweb/standalone.py +154 -0
- ser2netweb/state.py +1457 -0
- ser2netweb/storage.py +130 -0
- ser2netweb/textfold.py +51 -0
- ser2netweb/tlsreload.py +37 -0
- ser2netweb/watchdog.py +201 -0
- ser2netweb/web/__init__.py +0 -0
- ser2netweb/web/api.py +793 -0
- ser2netweb/web/auth.py +699 -0
- ser2netweb/web/certinfo.py +144 -0
- ser2netweb/web/httpguard.py +413 -0
- ser2netweb/web/ldap_auth.py +556 -0
- ser2netweb/web/oidc_auth.py +330 -0
- ser2netweb/web/proxy.py +398 -0
- ser2netweb/web/routes/__init__.py +78 -0
- ser2netweb/web/routes/cluster.py +669 -0
- ser2netweb/web/routes/common.py +359 -0
- ser2netweb/web/routes/console.py +451 -0
- ser2netweb/web/routes/forms.py +853 -0
- ser2netweb/web/routes/login.py +573 -0
- ser2netweb/web/routes/mappings.py +470 -0
- ser2netweb/web/routes/ops.py +374 -0
- ser2netweb/web/routes/settings.py +2135 -0
- ser2netweb/web/routes/system.py +308 -0
- ser2netweb/web/routes/totp.py +282 -0
- ser2netweb/web/selfsigned.py +129 -0
- ser2netweb/web/server.py +483 -0
- ser2netweb/web/static/app.css +327 -0
- ser2netweb/web/static/app.js +1102 -0
- ser2netweb/web/static/htmx.min.js +1 -0
- ser2netweb/web/static/points.js +218 -0
- ser2netweb/web/static/theme.js +7 -0
- ser2netweb/web/static/xterm-addon-fit.js +2 -0
- ser2netweb/web/static/xterm.css +218 -0
- ser2netweb/web/static/xterm.js +2 -0
- ser2netweb/web/templates/_cluster_body.html +109 -0
- ser2netweb/web/templates/_console.html +21 -0
- ser2netweb/web/templates/_footer.html +13 -0
- ser2netweb/web/templates/_login_notice.html +8 -0
- ser2netweb/web/templates/_macros.html +21 -0
- ser2netweb/web/templates/_mapping_form.html +477 -0
- ser2netweb/web/templates/_mapping_log.html +28 -0
- ser2netweb/web/templates/_mapping_traces.html +44 -0
- ser2netweb/web/templates/_mappings_body.html +149 -0
- ser2netweb/web/templates/_modbus_read.html +18 -0
- ser2netweb/web/templates/_ports_table.html +37 -0
- ser2netweb/web/templates/_serial_port_select.html +48 -0
- ser2netweb/web/templates/_shell.html +41 -0
- ser2netweb/web/templates/audit.html +55 -0
- ser2netweb/web/templates/base.html +77 -0
- ser2netweb/web/templates/dashboard.html +72 -0
- ser2netweb/web/templates/licenses.html +18 -0
- ser2netweb/web/templates/login.html +32 -0
- ser2netweb/web/templates/login_totp.html +23 -0
- ser2netweb/web/templates/settings.html +830 -0
- ser2netweb/web/templates/setup.html +43 -0
- ser2netweb/web/templates/totp.html +41 -0
- ser2netweb/web/totp.py +94 -0
- ser2netweb/winservice.py +1793 -0
- ser2netweb-3.0.0.dist-info/METADATA +120 -0
- ser2netweb-3.0.0.dist-info/RECORD +121 -0
- ser2netweb-3.0.0.dist-info/WHEEL +5 -0
- ser2netweb-3.0.0.dist-info/entry_points.txt +2 -0
- ser2netweb-3.0.0.dist-info/licenses/COMMERCIAL_LICENSE.md +94 -0
- ser2netweb-3.0.0.dist-info/licenses/LICENSE +662 -0
- ser2netweb-3.0.0.dist-info/licenses/NOTICE +20 -0
- ser2netweb-3.0.0.dist-info/licenses/THIRD-PARTY-NOTICES.md +105 -0
- ser2netweb-3.0.0.dist-info/licenses/tools/licenses/npm/htmx.org.txt +13 -0
- ser2netweb-3.0.0.dist-info/licenses/tools/licenses/npm/xterm-addon-fit.txt +19 -0
- ser2netweb-3.0.0.dist-info/licenses/tools/licenses/npm/xterm.txt +21 -0
- ser2netweb-3.0.0.dist-info/top_level.txt +1 -0
ser2netweb/web/proxy.py
ADDED
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
"""The web UI behind a reverse proxy (Settings -> Reverse proxy, docs/REVERSE-PROXY.md).
|
|
2
|
+
|
|
3
|
+
* Trusted proxies: X-Forwarded-For (the client's address) and X-Forwarded-Proto (whether
|
|
4
|
+
the browser used HTTPS) are believed only on a connection from one of them -
|
|
5
|
+
admin_ui.trusted_proxies, SER2NETWEB_TRUSTED_PROXIES for one run instead, or uvicorn's
|
|
6
|
+
FORWARDED_ALLOW_IPS when neither is set. None by default: what a client sends must not
|
|
7
|
+
choose the address the sign-in limits, the audit log and all.log see (up to 2.8.1 uvicorn
|
|
8
|
+
believed both from any connection from 127.0.0.1 or ::1). An X-Forwarded-For entry that
|
|
9
|
+
is not an IP address is never taken: the address before it stands.
|
|
10
|
+
* The public URL (admin_ui.public_url, SER2NETWEB_PUBLIC_URL for one run instead): the address
|
|
11
|
+
users open. A page of its origin is this UI's own (forms, the console: auth.cross_site),
|
|
12
|
+
a blank SSO redirect URI is made from it, notifications link to it.
|
|
13
|
+
* Its path, when it has one (https://gw.example.com/ser2netweb): the UI is served under it
|
|
14
|
+
too, behind a proxy that passes the path on, or one that strips it and sends exactly that
|
|
15
|
+
path as X-Forwarded-Prefix. A request without it is served at '/' as before (the LAN,
|
|
16
|
+
cluster peers, the health check). Links, redirects and cookies follow the path a request
|
|
17
|
+
came under (base()).
|
|
18
|
+
* Cookie names (cookie_name): browsers keep cookies per host name, not per port or scheme,
|
|
19
|
+
so the UI's cookies are named after what else tells its address apart - the port (but
|
|
20
|
+
the default 8080), the public URL's path, and https (the __Host- / __Secure- prefixes).
|
|
21
|
+
Instances on other ports or under other paths of one host name then keep their own
|
|
22
|
+
sign-ins, and an http:// address can still set its cookies after an https:// one of the
|
|
23
|
+
same name left Secure ones. The plain names of 2.8 and older (legacy_cookie_name) are
|
|
24
|
+
still read, so nobody is signed out by the change.
|
|
25
|
+
"""
|
|
26
|
+
from __future__ import annotations
|
|
27
|
+
|
|
28
|
+
import functools
|
|
29
|
+
import hashlib
|
|
30
|
+
import ipaddress
|
|
31
|
+
import os
|
|
32
|
+
import re
|
|
33
|
+
from typing import Mapping, Optional
|
|
34
|
+
|
|
35
|
+
from starlette.responses import RedirectResponse
|
|
36
|
+
|
|
37
|
+
from .. import envvars
|
|
38
|
+
from ..config import (
|
|
39
|
+
AdminUI, normalize_public_url, proxy_entry_problem, public_url_problem, split_proxy_entries,
|
|
40
|
+
)
|
|
41
|
+
from ..engine import netguard
|
|
42
|
+
|
|
43
|
+
# (SER2NET_TRUSTED_PROXIES / SER2NET_PUBLIC_URL, their names up to 2.9.x, too: envvars)
|
|
44
|
+
TRUSTED_ENV = "SER2NETWEB_TRUSTED_PROXIES"
|
|
45
|
+
LEGACY_TRUSTED_ENV = "FORWARDED_ALLOW_IPS" # uvicorn's; used when nothing else is set
|
|
46
|
+
PUBLIC_URL_ENV = "SER2NETWEB_PUBLIC_URL"
|
|
47
|
+
|
|
48
|
+
_SCHEMES = {"http": ("http", "ws"), "https": ("https", "wss")}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _env(env: Optional[Mapping[str, str]]) -> Mapping[str, str]:
|
|
52
|
+
return os.environ if env is None else env
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
# --------------------------------------------------------------------------
|
|
56
|
+
# trusted proxies
|
|
57
|
+
# --------------------------------------------------------------------------
|
|
58
|
+
class Trusted:
|
|
59
|
+
"""A set of trusted proxy addresses and networks; '*' (FORWARDED_ALLOW_IPS only) trusts
|
|
60
|
+
every address."""
|
|
61
|
+
|
|
62
|
+
def __init__(self, entries) -> None:
|
|
63
|
+
self.everyone = "*" in entries
|
|
64
|
+
self.networks = [ipaddress.ip_network(e, strict=False) for e in entries
|
|
65
|
+
if e != "*" and proxy_entry_problem(e) is None]
|
|
66
|
+
|
|
67
|
+
def __bool__(self) -> bool:
|
|
68
|
+
return self.everyone or bool(self.networks)
|
|
69
|
+
|
|
70
|
+
def __contains__(self, host) -> bool:
|
|
71
|
+
if self.everyone:
|
|
72
|
+
return True
|
|
73
|
+
ip = _address(host)
|
|
74
|
+
if ip is None:
|
|
75
|
+
return False
|
|
76
|
+
addr = ipaddress.ip_address(ip)
|
|
77
|
+
return any(addr in net for net in self.networks)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
@functools.lru_cache(maxsize=16)
|
|
81
|
+
def _trusted(entries: tuple[str, ...]) -> Trusted:
|
|
82
|
+
return Trusted(entries)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def trusted_entries(admin, env: Optional[Mapping[str, str]] = None) -> tuple[list[str], str]:
|
|
86
|
+
"""(the trusted proxy entries in effect, where they come from): SER2NETWEB_TRUSTED_PROXIES
|
|
87
|
+
(or SER2NET_TRUSTED_PROXIES: the name as it is set), else admin_ui.trusted_proxies, else
|
|
88
|
+
FORWARDED_ALLOW_IPS, else none ([], '')."""
|
|
89
|
+
env = _env(env)
|
|
90
|
+
raw = envvars.text(TRUSTED_ENV, env)
|
|
91
|
+
if raw:
|
|
92
|
+
return split_proxy_entries(raw), envvars.source(TRUSTED_ENV, env)
|
|
93
|
+
if admin.trusted_proxies:
|
|
94
|
+
return list(admin.trusted_proxies), "admin_ui.trusted_proxies"
|
|
95
|
+
raw = (env.get(LEGACY_TRUSTED_ENV) or "").strip()
|
|
96
|
+
if raw:
|
|
97
|
+
return split_proxy_entries(raw), LEGACY_TRUSTED_ENV
|
|
98
|
+
return [], ""
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def trusted(admin) -> Trusted:
|
|
102
|
+
return _trusted(tuple(trusted_entries(admin)[0]))
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _address(text) -> str | None:
|
|
106
|
+
"""The IP address in an X-Forwarded-For entry or a peer address ('1.2.3.4',
|
|
107
|
+
'1.2.3.4:5678', '[2001:db8::1]:443', '::ffff:1.2.3.4' -> '1.2.3.4'), or None."""
|
|
108
|
+
t = str(text or "").strip()
|
|
109
|
+
if t.startswith("["):
|
|
110
|
+
end = t.find("]")
|
|
111
|
+
rest = t[end + 1:] if end > 0 else "x"
|
|
112
|
+
if rest and not (rest[0] == ":" and rest[1:].isdigit()):
|
|
113
|
+
return None
|
|
114
|
+
t = t[1:end]
|
|
115
|
+
elif t.count(":") == 1:
|
|
116
|
+
t, _, port = t.partition(":")
|
|
117
|
+
if not port.isdigit():
|
|
118
|
+
return None
|
|
119
|
+
try:
|
|
120
|
+
ip = ipaddress.ip_address(t) if t.isascii() else None
|
|
121
|
+
except ValueError:
|
|
122
|
+
return None
|
|
123
|
+
if ip is None:
|
|
124
|
+
return None
|
|
125
|
+
if isinstance(ip, ipaddress.IPv6Address) and ip.ipv4_mapped is not None:
|
|
126
|
+
ip = ip.ipv4_mapped
|
|
127
|
+
return ip.compressed
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def forwarded_client(values: list[str], peer: str, proxies: Trusted) -> str:
|
|
131
|
+
"""The client's address behind trusted proxies: the last X-Forwarded-For entry that
|
|
132
|
+
isn't one of them (each proxy adds the address it was reached from at the end), the
|
|
133
|
+
first one when all are; walking back stops at an entry that isn't an IP address, and
|
|
134
|
+
the address after it stands (in the end `peer`, the connection's own)."""
|
|
135
|
+
hops = [h for v in values for h in v.split(",")]
|
|
136
|
+
client = peer
|
|
137
|
+
for hop in reversed(hops):
|
|
138
|
+
ip = _address(hop)
|
|
139
|
+
if ip is None:
|
|
140
|
+
break
|
|
141
|
+
client = ip
|
|
142
|
+
if ip not in proxies:
|
|
143
|
+
break
|
|
144
|
+
return client
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _first(value: str) -> str:
|
|
148
|
+
return value.split(",", 1)[0].strip().lower()
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
# --------------------------------------------------------------------------
|
|
152
|
+
# the public URL and its path
|
|
153
|
+
# --------------------------------------------------------------------------
|
|
154
|
+
@functools.lru_cache(maxsize=8)
|
|
155
|
+
def _usable_url(raw: str) -> str:
|
|
156
|
+
return normalize_public_url(raw) if public_url_problem(raw) is None else ""
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def public_url_from_env(env: Optional[Mapping[str, str]] = None) -> str:
|
|
160
|
+
"""SER2NETWEB_PUBLIC_URL (or SER2NET_PUBLIC_URL) when it is set and usable, else ''."""
|
|
161
|
+
return _usable_url(envvars.text(PUBLIC_URL_ENV, env))
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def public_url(admin, env: Optional[Mapping[str, str]] = None) -> str:
|
|
165
|
+
"""The public URL in effect ('' = none): SER2NETWEB_PUBLIC_URL when it is usable, else
|
|
166
|
+
admin_ui.public_url (both kept as normalize_public_url spells them)."""
|
|
167
|
+
return public_url_from_env(env) or admin.public_url
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def origin_of(url: str) -> str:
|
|
171
|
+
"""'scheme://host[:port]' of a public URL ('' for none)."""
|
|
172
|
+
scheme, sep, rest = url.partition("://")
|
|
173
|
+
return f"{scheme}{sep}{rest.split('/', 1)[0]}" if sep else ""
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def path_of(url: str) -> str:
|
|
177
|
+
"""The path of a public URL without the trailing '/' ('' at the root or for none)."""
|
|
178
|
+
_scheme, sep, rest = url.partition("://")
|
|
179
|
+
return "/" + rest.split("/", 1)[1] if sep and "/" in rest else ""
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def published_origins(admin) -> tuple[str, ...]:
|
|
183
|
+
"""The origins whose pages are this UI's own besides the address a request names:
|
|
184
|
+
the public URL's (auth.cross_site's `also`)."""
|
|
185
|
+
url = public_url(admin)
|
|
186
|
+
return (origin_of(url),) if url else ()
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def start_notes(admin, env: Optional[Mapping[str, str]] = None) -> list[str]:
|
|
190
|
+
"""Lines for the start-up log: what is trusted and published, and settings from the
|
|
191
|
+
environment that can't be used."""
|
|
192
|
+
env = _env(env)
|
|
193
|
+
notes = []
|
|
194
|
+
entries, source = trusted_entries(admin, env)
|
|
195
|
+
bad = [e for e in entries if e != "*" and proxy_entry_problem(e) is not None]
|
|
196
|
+
if bad:
|
|
197
|
+
notes.append(f"WARNING: {source}: {', '.join(repr(e) for e in bad)} left out (not IP "
|
|
198
|
+
"addresses or networks)")
|
|
199
|
+
if "*" in entries:
|
|
200
|
+
notes.append(f"WARNING: {source}='*': X-Forwarded-For and X-Forwarded-Proto are "
|
|
201
|
+
"believed from every client, so anyone who reaches the UI's port can "
|
|
202
|
+
"choose the address the sign-in limits and the logs see. List the "
|
|
203
|
+
"proxies' addresses instead (Settings → Reverse proxy)")
|
|
204
|
+
usable = [e for e in entries if e not in bad]
|
|
205
|
+
if usable:
|
|
206
|
+
notes.append(f"trusted proxies ({source}): {', '.join(usable)} — X-Forwarded-For / "
|
|
207
|
+
"X-Forwarded-Proto are believed from these only")
|
|
208
|
+
if source == LEGACY_TRUSTED_ENV:
|
|
209
|
+
notes.append(f"{LEGACY_TRUSTED_ENV} is read as the list of trusted proxies because "
|
|
210
|
+
f"neither {TRUSTED_ENV} nor Settings → Reverse proxy sets one")
|
|
211
|
+
raw = envvars.text(PUBLIC_URL_ENV, env)
|
|
212
|
+
if raw and (why := public_url_problem(raw)) is not None:
|
|
213
|
+
notes.append(f"WARNING: {envvars.source(PUBLIC_URL_ENV, env)} {raw!r} is not used: {why}")
|
|
214
|
+
return notes
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
# --------------------------------------------------------------------------
|
|
218
|
+
# per request: the path prefix it came under, links, redirects, cookies
|
|
219
|
+
# --------------------------------------------------------------------------
|
|
220
|
+
def _scope(conn) -> dict:
|
|
221
|
+
return conn if isinstance(conn, dict) else conn.scope
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def base(conn) -> str:
|
|
225
|
+
"""The path prefix a request (a Request, WebSocket or ASGI scope) came under: the
|
|
226
|
+
public URL's path, or '' at '/'."""
|
|
227
|
+
s = _scope(conn)
|
|
228
|
+
return s.get("app_root_path", s.get("root_path", "")) or ""
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def route_path(conn) -> str:
|
|
232
|
+
"""A request's path without base(): what the UI's routes are matched against."""
|
|
233
|
+
s = _scope(conn)
|
|
234
|
+
path, prefix = s["path"], base(s)
|
|
235
|
+
if prefix and (path == prefix or path.startswith(prefix + "/")):
|
|
236
|
+
return path[len(prefix):] or "/"
|
|
237
|
+
return path
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def url(conn, path: str) -> str:
|
|
241
|
+
"""The address of the UI's page `path` ('/login') for a request: under its base()."""
|
|
242
|
+
return base(conn) + path
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def redirect(conn, path: str, status_code: int = 303) -> RedirectResponse:
|
|
246
|
+
"""A redirect to the UI's page `path`, under the request's base()."""
|
|
247
|
+
return RedirectResponse(url(conn, path), status_code=status_code)
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def _header(conn, name: bytes) -> str | None:
|
|
251
|
+
"""The first value of a request header (`name` in lower case), or None."""
|
|
252
|
+
for key, value in _scope(conn).get("headers") or ():
|
|
253
|
+
if key == name:
|
|
254
|
+
return value.decode("latin-1")
|
|
255
|
+
return None
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
_FORWARDED_PROTO = re.compile(r'(?:^|[;\s])proto="?([a-z]+)', re.IGNORECASE)
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
def browser_https(conn) -> bool:
|
|
262
|
+
"""Did the browser use HTTPS, as the request shows it? Its scheme is https (uvicorn's
|
|
263
|
+
TLS, or X-Forwarded-Proto from a trusted proxy), or the first X-Forwarded-Proto or
|
|
264
|
+
Forwarded proto= says so, whoever sent it: that only makes cookies stricter (Secure,
|
|
265
|
+
and their names: cookie_name), which a page can't make the browser's requests say.
|
|
266
|
+
auth.is_https adds admin TLS."""
|
|
267
|
+
if _scope(conn).get("scheme") in ("https", "wss"):
|
|
268
|
+
return True
|
|
269
|
+
if _first(_header(conn, b"x-forwarded-proto") or "") == "https":
|
|
270
|
+
return True
|
|
271
|
+
m = _FORWARDED_PROTO.search((_header(conn, b"forwarded") or "").split(",", 1)[0])
|
|
272
|
+
return bool(m and m.group(1).lower() == "https")
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
# The port whose cookies keep the plain names (AdminUI's default): one instance per host
|
|
276
|
+
# name, the usual case, has the same cookies as before.
|
|
277
|
+
_PLAIN_PORT = str(AdminUI.port)
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def _host_port(conn) -> str | None:
|
|
281
|
+
"""The port in a request's Host header as the browser wrote it ('8081'), '' when it
|
|
282
|
+
names none (the scheme's default), None without a Host header."""
|
|
283
|
+
host = _header(conn, b"host")
|
|
284
|
+
if host is None:
|
|
285
|
+
return None
|
|
286
|
+
host = host.strip()
|
|
287
|
+
if host.startswith("["):
|
|
288
|
+
_addr, _sep, rest = host.partition("]")
|
|
289
|
+
port = rest[1:] if rest.startswith(":") else ""
|
|
290
|
+
else:
|
|
291
|
+
port = host.rpartition(":")[2] if host.count(":") == 1 else ""
|
|
292
|
+
return str(int(port)) if port.isascii() and port.isdigit() else ""
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def cookie_tag(conn) -> str:
|
|
296
|
+
"""What the names of the UI's cookies end with for a request: '' at '/' on port 8080
|
|
297
|
+
(or without a Host header), else '_' and 8 hex digits of a hash of the port in the
|
|
298
|
+
address (but 8080) and the path it came under (base())."""
|
|
299
|
+
port = _host_port(conn)
|
|
300
|
+
key = ("" if port is None or port == _PLAIN_PORT else f":{port}") + base(conn)
|
|
301
|
+
return f"_{hashlib.sha256(key.encode()).hexdigest()[:8]}" if key else ""
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def cookie_name(conn, name: str, path: str = "/") -> str:
|
|
305
|
+
"""The name of one of the UI's cookies for a request: `name` and its cookie_tag, with
|
|
306
|
+
the __Host- prefix when the browser used HTTPS (browser_https) - __Secure- for a cookie
|
|
307
|
+
whose Path isn't '/' (cookie_path) - so that a Secure cookie of an https:// address
|
|
308
|
+
never has the name an http:// one of the same host name sets."""
|
|
309
|
+
tagged = name + cookie_tag(conn)
|
|
310
|
+
if not browser_https(conn):
|
|
311
|
+
return tagged
|
|
312
|
+
return ("__Host-" if cookie_path(conn, path) == "/" else "__Secure-") + tagged
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def legacy_cookie_name(conn, name: str, path: str = "/") -> str | None:
|
|
316
|
+
"""The name 2.8 and older gave the cookie (`name` itself), when a request might carry
|
|
317
|
+
one under it: at '/' only (they had no base path), and when it isn't the name the
|
|
318
|
+
cookie has now (cookie_name), else None."""
|
|
319
|
+
if base(conn) or cookie_name(conn, name, path) == name:
|
|
320
|
+
return None
|
|
321
|
+
return name
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def cookie_path(conn, path: str = "/") -> str:
|
|
325
|
+
"""A cookie's Path for a request: `path` under its base()."""
|
|
326
|
+
prefix = base(conn)
|
|
327
|
+
return (prefix + ("" if path == "/" else path)) if prefix else path
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
# --------------------------------------------------------------------------
|
|
331
|
+
# the ASGI layer in front of the app
|
|
332
|
+
# --------------------------------------------------------------------------
|
|
333
|
+
class ProxyMiddleware:
|
|
334
|
+
"""In front of every request and WebSocket: on a connection from a trusted proxy, the
|
|
335
|
+
client address and the scheme from X-Forwarded-For / X-Forwarded-Proto; under the
|
|
336
|
+
public URL's path, root_path. Pure ASGI (it runs for WebSockets too, which
|
|
337
|
+
BaseHTTPMiddleware doesn't); the settings are read for each request, so a change in
|
|
338
|
+
Settings applies at once."""
|
|
339
|
+
|
|
340
|
+
def __init__(self, app, state) -> None:
|
|
341
|
+
self.app = app
|
|
342
|
+
self.state = state
|
|
343
|
+
# an X-Forwarded-For from an address that isn't trusted: noted once an hour
|
|
344
|
+
self._ignored = netguard.LogEvery(lambda msg: state.log(msg), interval=3600)
|
|
345
|
+
|
|
346
|
+
async def __call__(self, scope, receive, send):
|
|
347
|
+
if scope["type"] in ("http", "websocket"):
|
|
348
|
+
admin = self.state.config.admin_ui
|
|
349
|
+
self._forwarded(scope, trusted(admin))
|
|
350
|
+
prefix = path_of(public_url(admin))
|
|
351
|
+
if prefix:
|
|
352
|
+
_under(scope, prefix)
|
|
353
|
+
await self.app(scope, receive, send)
|
|
354
|
+
|
|
355
|
+
def _forwarded(self, scope, proxies: Trusted) -> None:
|
|
356
|
+
client = scope.get("client")
|
|
357
|
+
peer = _address(client[0]) if client else None
|
|
358
|
+
if peer is None:
|
|
359
|
+
return
|
|
360
|
+
xff, proto = [], None
|
|
361
|
+
for name, value in scope.get("headers") or ():
|
|
362
|
+
if name == b"x-forwarded-for":
|
|
363
|
+
xff.append(value.decode("latin-1"))
|
|
364
|
+
elif name == b"x-forwarded-proto":
|
|
365
|
+
proto = value.decode("latin-1")
|
|
366
|
+
if not proxies or peer not in proxies:
|
|
367
|
+
if xff:
|
|
368
|
+
self._ignored(f"X-Forwarded-For from {peer} is ignored: {peer} is not a "
|
|
369
|
+
"trusted proxy, so the sign-in limits, the audit log and "
|
|
370
|
+
f"all.log see {peer} for every client behind it (a reverse "
|
|
371
|
+
"proxy: Settings → Reverse proxy → Trusted proxies)")
|
|
372
|
+
return
|
|
373
|
+
if xff:
|
|
374
|
+
scope["client"] = (forwarded_client(xff, peer, proxies), 0)
|
|
375
|
+
if proto is not None and _first(proto) in ("http", "https", "ws", "wss"):
|
|
376
|
+
plain = _first(proto).replace("ws", "http")
|
|
377
|
+
scope["scheme"] = _SCHEMES[plain][scope["type"] == "websocket"]
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def _under(scope, prefix: str) -> None:
|
|
381
|
+
"""root_path for a request under the public URL's path `prefix`: its path starts with
|
|
382
|
+
it (the proxy passed it on), or X-Forwarded-Prefix is exactly it (the proxy stripped
|
|
383
|
+
it; it is put back, as ASGI wants root_path to be part of the path)."""
|
|
384
|
+
path = scope["path"]
|
|
385
|
+
if path == prefix or path.startswith(prefix + "/"):
|
|
386
|
+
if path == prefix: # its pages are below it: /ser2netweb -> /ser2netweb/
|
|
387
|
+
scope["path"], scope["raw_path"] = prefix + "/", (prefix + "/").encode()
|
|
388
|
+
scope["root_path"] = prefix
|
|
389
|
+
return
|
|
390
|
+
for name, value in scope.get("headers") or ():
|
|
391
|
+
if name == b"x-forwarded-prefix":
|
|
392
|
+
if value.decode("latin-1").strip().rstrip("/") == prefix:
|
|
393
|
+
scope["root_path"] = prefix
|
|
394
|
+
scope["path"] = prefix + path
|
|
395
|
+
raw = scope.get("raw_path")
|
|
396
|
+
if raw is not None:
|
|
397
|
+
scope["raw_path"] = prefix.encode() + raw
|
|
398
|
+
return
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""HTTP routes: pages (login/setup/dashboard/settings) + HTMX/JSON API.
|
|
2
|
+
|
|
3
|
+
Interaction model (HTMX, no SPA):
|
|
4
|
+
- the mappings panel polls GET /api/status every 2s and also refreshes when a
|
|
5
|
+
response sets the `HX-Trigger: refreshMappings` header,
|
|
6
|
+
- add/edit load a server-rendered form fragment into #form-panel,
|
|
7
|
+
- save/delete/start/stop return an empty body + the refresh trigger.
|
|
8
|
+
|
|
9
|
+
One module per area; each exposes ``routes(ctx)``:
|
|
10
|
+
login sign-in, logout, first-run setup, OIDC SSO
|
|
11
|
+
totp two-factor sign-in: the code step, setting it up in Settings
|
|
12
|
+
settings the Settings page and its forms, mappings export/import
|
|
13
|
+
mappings dashboard, mapping table/form/actions, log and console panels
|
|
14
|
+
console the console and hex-view WebSockets, the console's serial BREAK
|
|
15
|
+
cluster LAN cluster view, remote control and remote edit
|
|
16
|
+
system /healthz, /metrics, serial port list
|
|
17
|
+
Shared helpers live in common (Ctx), forms (form parsing) and ops (mapping operations).
|
|
18
|
+
"""
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
from typing import TYPE_CHECKING
|
|
22
|
+
from urllib.parse import parse_qs
|
|
23
|
+
|
|
24
|
+
from starlette.routing import Mount
|
|
25
|
+
from starlette.staticfiles import StaticFiles
|
|
26
|
+
|
|
27
|
+
from . import cluster, console, login, mappings, settings, system, totp
|
|
28
|
+
from .common import Ctx
|
|
29
|
+
from .forms import _build_mapping_dict, _preserve_unmanaged_fields
|
|
30
|
+
|
|
31
|
+
if TYPE_CHECKING:
|
|
32
|
+
from starlette.responses import Response
|
|
33
|
+
from starlette.templating import Jinja2Templates
|
|
34
|
+
from starlette.types import Scope
|
|
35
|
+
|
|
36
|
+
from ...state import AppState
|
|
37
|
+
|
|
38
|
+
__all__ = ["build_routes", "_build_mapping_dict", "_preserve_unmanaged_fields"]
|
|
39
|
+
|
|
40
|
+
# a year: the longest lifetime HTTP caches honour
|
|
41
|
+
_IMMUTABLE = "public, max-age=31536000, immutable"
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class StaticAssets(StaticFiles):
|
|
45
|
+
"""/static. The pages link every asset with ?v=<a hash of the static files>
|
|
46
|
+
(server.asset_version()), so a request carrying the current one may be cached for good:
|
|
47
|
+
the URL changes whenever an asset does. Others are revalidated as before."""
|
|
48
|
+
|
|
49
|
+
def __init__(self, directory: str, version: str) -> None:
|
|
50
|
+
super().__init__(directory=directory)
|
|
51
|
+
self.version = version
|
|
52
|
+
|
|
53
|
+
async def get_response(self, path: str, scope: Scope) -> Response:
|
|
54
|
+
response = await super().get_response(path, scope)
|
|
55
|
+
query = parse_qs(scope.get("query_string", b"").decode("latin-1"))
|
|
56
|
+
if response.status_code in (200, 304) and query.get("v") == [self.version]:
|
|
57
|
+
response.headers["Cache-Control"] = _IMMUTABLE
|
|
58
|
+
return response
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def build_routes(templates: Jinja2Templates, state: AppState, static_dir: str) -> list:
|
|
62
|
+
from ..api import build_api_routes # here: api imports .routes.ops (no import cycle)
|
|
63
|
+
|
|
64
|
+
ctx = Ctx(templates, state)
|
|
65
|
+
return [
|
|
66
|
+
*login.routes(ctx),
|
|
67
|
+
*totp.routes(ctx),
|
|
68
|
+
*settings.routes(ctx),
|
|
69
|
+
# JSON REST API (bearer-token auth, enforced in GuardMiddleware)
|
|
70
|
+
*build_api_routes(state),
|
|
71
|
+
# before mappings: its POST /api/mappings/{mid}/{action} would take .../break
|
|
72
|
+
*console.routes(ctx),
|
|
73
|
+
*mappings.routes(ctx),
|
|
74
|
+
*cluster.routes(ctx),
|
|
75
|
+
*system.routes(ctx),
|
|
76
|
+
Mount("/static", app=StaticAssets(static_dir, str(templates.env.globals["asset_version"])),
|
|
77
|
+
name="static"),
|
|
78
|
+
]
|