Skip to content

Commit e75e663

Browse files
committed
Limit maximum number of monitoring sessions in parallel
1 parent b746713 commit e75e663

11 files changed

+26
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pyninja start
6161
- **MONITOR_USERNAME** - Username to authenticate the monitoring page.
6262
- **MONITOR_PASSWORD** - Password to authenticate the monitoring page.
6363
- **MONITOR_SESSION** - Session timeout for the monitoring page.
64+
- **MAX_CONNECTIONS** - Maximum number of monitoring sessions allowed in parallel.
6465
- **SERVICE_MANAGER** - Service manager filepath to handle the service status requests.
6566
- **DATABASE** - FilePath to store the auth database that handles the authentication errors.
6667
- **RATE_LIMIT** - List of dictionaries with `max_requests` and `seconds` to apply as rate limit.

docs/README.html

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ <h2>Environment Variables<a class="headerlink" href="#environment-variables" tit
9898
<li><p><strong>MONITOR_USERNAME</strong> - Username to authenticate the monitoring page.</p></li>
9999
<li><p><strong>MONITOR_PASSWORD</strong> - Password to authenticate the monitoring page.</p></li>
100100
<li><p><strong>MONITOR_SESSION</strong> - Session timeout for the monitoring page.</p></li>
101+
<li><p><strong>MAX_CONNECTIONS</strong> - Maximum number of monitoring sessions allowed in parallel.</p></li>
101102
<li><p><strong>SERVICE_MANAGER</strong> - Service manager filepath to handle the service status requests.</p></li>
102103
<li><p><strong>DATABASE</strong> - FilePath to store the auth database that handles the authentication errors.</p></li>
103104
<li><p><strong>RATE_LIMIT</strong> - List of dictionaries with <code class="docutils literal notranslate"><span class="pre">max_requests</span></code> and <code class="docutils literal notranslate"><span class="pre">seconds</span></code> to apply as rate limit.</p></li>

docs/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pyninja start
6161
- **MONITOR_USERNAME** - Username to authenticate the monitoring page.
6262
- **MONITOR_PASSWORD** - Password to authenticate the monitoring page.
6363
- **MONITOR_SESSION** - Session timeout for the monitoring page.
64+
- **MAX_CONNECTIONS** - Maximum number of monitoring sessions allowed in parallel.
6465
- **SERVICE_MANAGER** - Service manager filepath to handle the service status requests.
6566
- **DATABASE** - FilePath to store the auth database that handles the authentication errors.
6667
- **RATE_LIMIT** - List of dictionaries with `max_requests` and `seconds` to apply as rate limit.

docs/_sources/README.md.txt

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pyninja start
6161
- **MONITOR_USERNAME** - Username to authenticate the monitoring page.
6262
- **MONITOR_PASSWORD** - Password to authenticate the monitoring page.
6363
- **MONITOR_SESSION** - Session timeout for the monitoring page.
64+
- **MAX_CONNECTIONS** - Maximum number of monitoring sessions allowed in parallel.
6465
- **SERVICE_MANAGER** - Service manager filepath to handle the service status requests.
6566
- **DATABASE** - FilePath to store the auth database that handles the authentication errors.
6667
- **RATE_LIMIT** - List of dictionaries with `max_requests` and `seconds` to apply as rate limit.

docs/genindex.html

+2
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ <h2 id="L">L</h2>
300300
<h2 id="M">M</h2>
301301
<table style="width: 100%" class="indextable genindextable"><tr>
302302
<td style="width: 33%; vertical-align: top;"><ul>
303+
<li><a href="index.html#pyninja.models.EnvConfig.max_connections">max_connections (pyninja.models.EnvConfig attribute)</a>
304+
</li>
303305
<li><a href="index.html#pyninja.models.RateLimit.max_requests">max_requests (pyninja.models.RateLimit attribute)</a>
304306
</li>
305307
<li><a href="index.html#pyninja.models.WSSession.model_computed_fields">model_computed_fields (pyninja.models.WSSession attribute)</a>

docs/index.html

+5
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,11 @@ <h1>Models<a class="headerlink" href="#models" title="Permalink to this heading"
931931
<span class="sig-name descname"><span class="pre">monitor_session</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="pre">int</span></em><a class="headerlink" href="#pyninja.models.EnvConfig.monitor_session" title="Permalink to this definition"></a></dt>
932932
<dd></dd></dl>
933933

934+
<dl class="py attribute">
935+
<dt class="sig sig-object py" id="pyninja.models.EnvConfig.max_connections">
936+
<span class="sig-name descname"><span class="pre">max_connections</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="pre">int</span></em><a class="headerlink" href="#pyninja.models.EnvConfig.max_connections" title="Permalink to this definition"></a></dt>
937+
<dd></dd></dl>
938+
934939
<dl class="py attribute">
935940
<dt class="sig sig-object py" id="pyninja.models.EnvConfig.service_manager">
936941
<span class="sig-name descname"><span class="pre">service_manager</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="pre">Union</span><span class="p"><span class="pre">[</span></span><span class="pre">Path</span><span class="p"><span class="pre">,</span></span><span class="w"> </span><a class="reference internal" href="#pyninja.models.ServiceManager" title="pyninja.models.ServiceManager"><span class="pre">ServiceManager</span></a><span class="p"><span class="pre">]</span></span></em><a class="headerlink" href="#pyninja.models.EnvConfig.service_manager" title="Permalink to this definition"></a></dt>

docs/objects.inv

7 Bytes
Binary file not shown.

docs/searchindex.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyninja/models.py

+1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ class EnvConfig(BaseSettings):
193193
monitor_username: str | None = None
194194
monitor_password: str | None = None
195195
monitor_session: PositiveInt = 3_600
196+
max_connections: PositiveInt = 3
196197
service_manager: FilePath | ServiceManager = get_service_manager()
197198
database: str = Field("auth.db", pattern=".*.db$")
198199
rate_limit: RateLimit | List[RateLimit] = []

pyninja/monitor/routes.py

+7
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ async def monitor_endpoint(request: Request, session_token: str = Cookie(None)):
9797
HTMLResponse:
9898
Returns an HTML response templated using Jinja2.
9999
"""
100+
# todo: use reverse-proxy to test this from multiple devices
101+
# Remove the first hostname from client_auth quietly
102+
if len(models.ws_session.client_auth) > models.env.max_connections:
103+
first_key = next(iter(models.ws_session.client_auth))
104+
# Remove the key-value pair associated with the first authenticated user
105+
LOGGER.info("Maximum parallel connections limit reached. Dropping %s", first_key)
106+
models.ws_session.client_auth.pop(first_key, None)
100107
if session_token:
101108
try:
102109
await monitor.authenticator.validate_session(

release_notes.rst

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Release Notes
22
=============
33

4+
v0.0.5 (09/07/2024)
5+
-------------------
6+
- Packs an entirely new UI and authentication mechanism for monitoring tool
7+
- Includes speed, stability and security improvements for monitoring feature
8+
- Adds night mode option for monitoring UI
9+
410
v0.0.4 (09/06/2024)
511
-------------------
612
- Includes an option to monitor system resources via `WebSockets`

0 commit comments

Comments
 (0)