|
|
2 месяцев назад | |
|---|---|---|
| .. | ||
| README.md | 2 месяцев назад | |
| README_es.md | 2 месяцев назад | |
This guide describes how to deploy the SquidStats captive portal with Squid ACLs, the external ACL helper, LDAP/Active Directory authentication, firewall redirection, and a production-safe network layout.
The feature is designed for controlled networks such as guest Wi-Fi or a lab VLAN. It identifies a session by the client's source IP address. It is not suitable for environments where many independent users share one NAT address unless all of them are intentionally treated as one session.
The request flow is:
OK for an active session and ERR otherwise./portal/login.The relevant project components are:
services/captive_portal/session_service.py: session creation, validation, revocation, and expiration.services/captive_portal/helper/captive_portal_helper.py: Squid external_acl_type protocol.services/ldap/ldap_service.py: LDAP/AD end-user authentication.services/squid/captive_portal_config_service.py: managed Squid configuration block.routes/captive_portal_routes.py: public portal routes.routes/admin/captive_portal.py: administrator configuration and active sessions.alembic/versions/014_add_captive_portal.py: database schema.A session is keyed by the source IP address. If ten clients appear to Squid as 192.168.10.20, authenticating one of them authenticates that address for all ten. Use explicit proxy authentication or a network gateway with per-client identity when this is unacceptable.
A normal Squid http_access redirect can redirect HTTP requests. It cannot safely replace arbitrary HTTPS pages with an HTTP login page. Modern browsers also use HSTS, QUIC/HTTP3, certificate validation, and captive portal detection flows that make HTTPS interception fragile.
Recommended production behavior:
CONNECT request, but it still cannot show an arbitrary login page inside the encrypted tunnel.The generated directive uses the Python executable and helper path visible to the SquidStats process. Squid must be able to execute that path and access the same database.
The repository's current docker-compose.yml runs only squidstats_app; it does not run Squid. Therefore, when Squid runs on another host or container, do not point Squid at a helper path that exists only inside the Flask container. Install a copy of the helper and the required application modules on the Squid host, or expose a small local helper service with an audited transport. The simplest reliable arrangement is:
Prepare the following before enabling the feature:
external_acl_type support.014_add_captive_portal.https://portal.example.net.Do not expose the admin panel directly to the guest network. Put it behind a management VLAN, VPN, reverse proxy access policy, or a separate firewall rule.
Copy the example file and set production values:
cd /opt/SquidStats/app
cp example.env .env
chmod 600 .env
At minimum, review these values:
VERSION=2.6.1
FLASK_DEBUG=False
LISTEN_HOST=127.0.0.1
LISTEN_PORT=5000
SECRET_KEY=replace-with-a-long-random-secret
JWT_SECRET_KEY=replace-with-another-long-random-secret
FIRST_PASSWORD=change-before-first-login
DATABASE_TYPE=SQLITE
DATABASE_STRING_CONNECTION=/opt/SquidStats/app/squidstats.db
SQUID_HOST=127.0.0.1
SQUID_PORT=3128
SQUID_CONFIG_PATH=/etc/squid/squid.conf
ACL_FILES_DIR=/etc/squid/squid.d
For a database server, use the project's existing database settings and make sure the helper receives the same environment variables. Never use a database file path that is inside a container but not mounted on the Squid host.
Generate secrets with a password manager or a command such as:
python3 -c 'import secrets; print(secrets.token_urlsafe(48))'
Do not set FLASK_DEBUG=True in production. Do not commit .env.
From the project root:
python3 -m venv /opt/SquidStats/venv
/opt/SquidStats/venv/bin/pip install -r requirements.txt
cd /opt/SquidStats/app
/opt/SquidStats/venv/bin/python manage_db.py upgrade
/opt/SquidStats/venv/bin/python manage_db.py current
The current revision must include 014_add_captive_portal. The application also runs migrations during startup, but running the command explicitly makes deployment failures visible before the service is enabled.
Do not use manage_db.py init on a database that has not been verified. init stamps the database and does not apply missing schema changes.
Open the administrator panel and configure LDAP/AD y Grupos. The following values are required:
SIMPLE or NTLM.The service account needs enough permission to search users. It does not need administrator privileges. The portal first searches the user and then attempts a bind using the submitted password.
Recommended LDAP settings:
DOMAIN\\user or user@domain.Set the public portal URL in Admin > Portal Cautivo, for example:
https://portal.example.net
The URL must be an HTTP or HTTPS URL without embedded credentials, query parameters, or fragments. The portal host must be reachable before authentication. Permit at least:
If SquidStats and Squid run on separate machines, ensure the portal hostname resolves to the SquidStats application or reverse proxy, not to the Squid listener.
A typical reverse proxy should forward the portal routes to 127.0.0.1:5000 and should not expose /admin to the guest network.
The generated managed block contains directives equivalent to:
external_acl_type squidstats_captive_portal ttl=60 negative_ttl=0 %SRC "/opt/SquidStats/venv/bin/python" "/opt/SquidStats/app/services/captive_portal/helper/captive_portal_helper.py"
acl squidstats_captive_portal_valid external squidstats_captive_portal
acl squidstats_captive_portal_domain dstdomain "portal.example.net"
deny_info 302:https://portal.example.net/portal/login?redirect=%s squidstats_captive_portal_valid
http_access allow squidstats_captive_portal_domain
http_access deny !squidstats_captive_portal_valid
The exact Python executable and helper path are generated from the running installation. Keep the captive portal block after Squid's basic safety ACL definitions and after the narrow Cache Manager exception, but before general http_access allow rules.
Before reloading Squid, validate the configuration:
sudo squid -k parse -f /etc/squid/squid.conf
sudo squid -k reconfigure
On a systemd installation:
sudo systemctl reload squid
sudo systemctl status squid --no-pager
The SquidStats admin action also attempts to reload Squid after applying the configuration. Treat the reload result as mandatory: a saved file without a successful Squid reload is not a working deployment.
The Debian service intentionally sets:
Environment=SQUIDSTATS_SQUID_CONFIG_WRITE_MODE=manual
This protects /etc/squid from the web process. In this mode the panel shows a preview but does not write squid.conf. Copy the generated block into the approved Squid configuration workflow, validate it, and reload Squid as root.
For a controlled installation that explicitly permits panel-managed writes, set:
SQUIDSTATS_SQUID_CONFIG_WRITE_MODE=managed
Only use this when the service account has the exact filesystem permissions required and the host has an audited rollback process. Do not grant broad write access to /etc.
If Squid and SquidStats are on the same host, verify the helper directly:
sudo -u squid /opt/SquidStats/venv/bin/python \
/opt/SquidStats/app/services/captive_portal/helper/captive_portal_helper.py
The command waits for input. Type an IP and press Enter. It should return OK or ERR; press Ctrl-D to exit.
For a helper on a separate Squid host:
.env values or use a dedicated environment file.external_acl_type.The helper must not print log messages to stdout. Squid uses stdout for the ACL protocol. Application diagnostics belong on stderr or in the application log.
external_acl_type executes the helper locally inside the Squid environment; it does not make an HTTP request to Flask running on the host. Therefore, the Squid container must have access to:
.env file.If the host application generates the configuration that is later copied into the container, set the paths that exist inside the container before regenerating the block:
SQUIDSTATS_HELPER_PYTHON=/usr/bin/python3
SQUIDSTATS_HELPER_PATH=/opt/squidstats-helper/services/captive_portal/helper/captive_portal_helper.py
These variables only change the paths written to squid.conf; they do not copy files or install dependencies. Mount the code and database at those paths, install the Python dependencies inside the container, and test from there:
docker exec -it squid /usr/bin/python3 \
/opt/squidstats-helper/services/captive_portal/helper/captive_portal_helper.py
Type an IP followed by Enter and confirm that it returns OK or ERR. If the helper uses SQLite, the mounted file must be the same database updated by SquidStats; two SQLite copies will make sessions appear to be missing.
The following is an example for a Linux gateway where:
LAN_IF=br-lan is the guest interface.WAN_IF=eth0 is the upstream interface.LAN_CIDR=192.168.50.0/24 is the guest network.3128 in intercept mode.portal.example.net on TCP 443.Replace every value for your topology. Test from a maintenance session before making rules persistent.
sudo sysctl -w net.ipv4.ip_forward=1
Persist it in /etc/sysctl.d/99-captive-portal.conf:
net.ipv4.ip_forward=1
Apply it:
sudo sysctl --system
The Squid listener must use intercept mode on the port receiving redirected HTTP traffic:
http_port 3128 intercept
Do not use http_port ... intercept for clients that are already configured with an explicit proxy unless the deployment intentionally separates those listeners.
LAN_IF=br-lan
WAN_IF=eth0
LAN_CIDR=192.168.50.0/24
SQUID_PORT=3128
sudo iptables -t nat -A PREROUTING -i "$LAN_IF" -s "$LAN_CIDR" \
-p tcp --dport 80 -j REDIRECT --to-ports "$SQUID_PORT"
Do not redirect traffic destined for the gateway's own portal service. If the portal is hosted on the same machine, use an early ACCEPT rule or route it through a dedicated reverse proxy address. A common layout is to place the portal on a separate host or load balancer so that the client bypasses the interception path naturally.
A restrictive example is:
sudo iptables -A FORWARD -i "$LAN_IF" -o "$WAN_IF" -s "$LAN_CIDR" \
-m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A FORWARD -i "$LAN_IF" -o "$WAN_IF" -s "$LAN_CIDR" \
-j ACCEPT
If the firewall uses a default-deny policy, add explicit rules for DNS, DHCP, the portal, LDAP from the application host, and the Squid listener. Do not allow the guest network to reach the administration interface.
If this host is also the Internet gateway:
sudo iptables -t nat -A POSTROUTING -s "$LAN_CIDR" -o "$WAN_IF" -j MASQUERADE
Do not combine these examples blindly with an existing firewall manager. ufw, firewalld, nftables, Docker, and Kubernetes may install their own chains and ordering rules.
This rule is intentionally not recommended:
# Do not add this for a normal HTTP captive portal:
# iptables -t nat -A PREROUTING -i "$LAN_IF" -p tcp --dport 443 \
# -j REDIRECT --to-ports "$SQUID_PORT"
Redirecting TCP 443 to a plain HTTP intercept listener produces TLS errors or unusable CONNECT requests. Use a dedicated captive portal gateway with a proper walled garden if HTTPS onboarding is required.
On systems using nftables, the equivalent HTTP redirect is conceptually:
table ip nat {
chain prerouting {
type nat hook prerouting priority dstnat; policy accept;
iifname "br-lan" ip saddr 192.168.50.0/24 tcp dport 80 redirect to :3128
}
}
Integrate this with the host's existing nftables ruleset instead of creating a conflicting table. Persist it with the distribution's nftables service.
After LDAP, the portal URL, the helper, and Squid have been tested:
ACL TTL to a low value such as 30-60 seconds.negative_ttl=0 while troubleshooting so a newly authenticated client is recognized immediately.Suggested starting values:
Session TTL: 480 minutes
ACL TTL: 30 seconds
Negative ACL TTL: 0 seconds
Lower the session TTL for public guest networks. Lower the ACL TTL when immediate revocation matters. Higher values reduce helper/database load but delay session changes.
For the Debian package installation:
sudo systemctl daemon-reload
sudo systemctl enable --now squidstats
sudo systemctl status squidstats --no-pager
sudo journalctl -u squidstats -n 100 --no-pager
The service runs as squidstats with ProtectSystem=full and no privilege escalation. This is why the packaged service uses manual Squid configuration mode.
For Docker:
docker compose up -d --build
docker compose logs -f squidstats
Mount the database and .env consistently, and ensure the helper's execution environment is available to Squid. A separate Squid container needs its own helper path and shared database connectivity.
Run these checks in order:
# Database schema
/opt/SquidStats/venv/bin/python manage_db.py current
# Application import
/opt/SquidStats/venv/bin/python -c 'import app; print("app import OK")'
# Squid configuration
sudo squid -k parse -f /etc/squid/squid.conf
# SquidStats service
sudo systemctl status squidstats --no-pager
# Squid service
sudo systemctl status squid --no-pager
# Helper protocol
sudo -u squid /opt/SquidStats/venv/bin/python \
/opt/SquidStats/app/services/captive_portal/helper/captive_portal_helper.py
Functional test:
squid.conf..env and connect to the database.BH or helper errorsexternal_acl_type syntax and quoted paths.OK or ERR line for every input line.http_access deny !squidstats_captive_portal_valid.SQUIDSTATS_SQUID_CONFIG_WRITE_MODE.manual.squid -k parse, and reload Squid.FLASK_DEBUG=False.SECRET_KEY and JWT_SECRET_KEY values./admin to guest clients..env, database files, LDAP credentials, and Squid configuration.To disable the feature safely:
Never remove a live Squid configuration block without validating the resulting file. Keep the last known-good configuration backup available.