mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-22 07:56:18 -05:00
Hotspot captive portal: welcome page with the WROLPi's address
Devices that join the hotspot probe a fixed plain-HTTP URL to check for Internet access. NetworkManager's shared-mode dnsmasq now resolves those probe hostnames (and `wrolpi`) to the hotspot address via an include the Controller writes when the hotspot starts, the probes land on the Controller (port 80), and are redirected to /portal: "This is your WROLPi, its address is 10.42.0.1, open WROLPi / open the Controller". Only the probe hostnames are hijacked so the hotspot keeps forwarding everything else when the WROLPi has an uplink. The include is corrected and the connection re-activated once if NetworkManager picked a non-default subnet. Writing the include is best-effort; the hotspot starts regardless. `hotspot.captive_portal` (controller.yaml, default true) toggles the probe redirects and the include; like the other hotspot settings it applies on the next hotspot start, so saving while the hotspot is up reports restart_required and both Controller UIs say so. The status endpoint gains the hotspot address and portal URL, shown in both UIs. repair.sh only creates the include directory so a disabled setting survives upgrades. Native only: Docker has no hotspot. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
91f92683ba
commit
6bea09600e
17 changed files with 772 additions and 15 deletions
|
|
@ -30,6 +30,7 @@ from controller.api.schemas import (
|
|||
VncActionResponse,
|
||||
VncStatusResponse,
|
||||
)
|
||||
from controller.lib.captive_portal import is_captive_portal_enabled
|
||||
from controller.lib.admin import (
|
||||
block_bluetooth,
|
||||
disable_throttle,
|
||||
|
|
@ -112,6 +113,7 @@ async def hotspot_settings() -> HotspotSettingsResponse:
|
|||
ssid=get_hotspot_ssid(),
|
||||
password=get_hotspot_password(),
|
||||
protocol=get_hotspot_protocol(),
|
||||
captive_portal=is_captive_portal_enabled(),
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -122,11 +124,12 @@ async def hotspot_settings_update(request: HotspotSettingsRequest) -> HotspotSet
|
|||
if is_docker_mode():
|
||||
raise HTTPException(status_code=500, detail="Not available in Docker mode")
|
||||
result = update_hotspot_settings(device=request.device, ssid=request.ssid, password=request.password,
|
||||
protocol=request.protocol)
|
||||
protocol=request.protocol, captive_portal=request.captive_portal)
|
||||
if not result.get("success"):
|
||||
raise HTTPException(status_code=400, detail=result.get("error", "Failed"))
|
||||
return HotspotSettingsResponse(device=result["device"], ssid=result["ssid"], password=result["password"],
|
||||
protocol=result["protocol"])
|
||||
protocol=result["protocol"], captive_portal=result["captive_portal"],
|
||||
restart_required=result.get("restart_required", False))
|
||||
|
||||
|
||||
@router.post("/api/hotspot/start", response_model=HotspotActionResponse)
|
||||
|
|
|
|||
55
controller/api/captive_portal.py
Normal file
55
controller/api/captive_portal.py
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
"""
|
||||
Captive portal routes: the landing page hotspot clients see, and the OS connectivity probes
|
||||
which are redirected to it.
|
||||
|
||||
These live at bare paths (not under /api/) because phones request fixed URLs such as
|
||||
`http://captive.apple.com/hotspot-detect.html`; the hotspot's DNS points those hostnames here.
|
||||
"""
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
from fastapi.responses import RedirectResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
|
||||
from controller.lib.admin import get_hotspot_device, get_hotspot_ssid
|
||||
from controller.lib.captive_portal import (
|
||||
PORTAL_PATH,
|
||||
PROBE_PATHS,
|
||||
get_hotspot_ip,
|
||||
is_captive_portal_enabled,
|
||||
)
|
||||
from controller.lib.config import is_docker_mode
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
templates = Jinja2Templates(directory=str(Path(__file__).parent.parent / "templates"))
|
||||
|
||||
# Phones must re-probe every time; a cached redirect would show a stale portal.
|
||||
NO_STORE = {"Cache-Control": "no-store"}
|
||||
|
||||
|
||||
@router.get(PORTAL_PATH, include_in_schema=False)
|
||||
async def portal(request: Request):
|
||||
"""The landing page: this WROLPi's address and links to WROLPi and the Controller."""
|
||||
if is_docker_mode():
|
||||
raise HTTPException(status_code=404)
|
||||
ip = get_hotspot_ip(get_hotspot_device())
|
||||
if not ip:
|
||||
# Hotspot is down; a page with the wrong address is worse than none.
|
||||
raise HTTPException(status_code=404)
|
||||
context = {"ip": ip, "ssid": get_hotspot_ssid()}
|
||||
return templates.TemplateResponse(request, "portal.html", context, headers=NO_STORE)
|
||||
|
||||
|
||||
async def probe():
|
||||
"""A connectivity probe. Anything but the expected reply makes the phone show the portal."""
|
||||
if not is_captive_portal_enabled():
|
||||
raise HTTPException(status_code=404)
|
||||
return RedirectResponse(url=PORTAL_PATH, status_code=302, headers=NO_STORE)
|
||||
|
||||
|
||||
for _path in PROBE_PATHS:
|
||||
router.add_api_route(_path, probe, methods=["GET", "HEAD"], include_in_schema=False)
|
||||
|
|
@ -156,6 +156,9 @@ class HotspotStatusResponse(BaseModel):
|
|||
reason: Optional[str] = Field(default=None, description="Reason if unavailable")
|
||||
ssid: Optional[str] = Field(default=None, description="Hotspot SSID when enabled")
|
||||
device: Optional[str] = Field(default=None, description="WiFi device name")
|
||||
ip: Optional[str] = Field(default=None, description="Hotspot IPv4 address when enabled")
|
||||
captive_portal: bool = Field(default=False, description="Whether the captive portal is enabled")
|
||||
portal_url: Optional[str] = Field(default=None, description="Landing page URL when the hotspot is up")
|
||||
|
||||
|
||||
class HotspotDevicesResponse(BaseModel):
|
||||
|
|
@ -178,6 +181,8 @@ class HotspotSettingsRequest(BaseModel):
|
|||
ssid: Optional[str] = Field(default=None, description="Hotspot SSID")
|
||||
password: Optional[str] = Field(default=None, description="Hotspot password (8 to 63 characters)")
|
||||
protocol: Optional[str] = Field(default=None, description="Hotspot protocol (wpa2 or wpa3)")
|
||||
captive_portal: Optional[bool] = Field(default=None,
|
||||
description="Show a landing page to devices that join the hotspot")
|
||||
|
||||
|
||||
class HotspotSettingsResponse(BaseModel):
|
||||
|
|
@ -187,6 +192,9 @@ class HotspotSettingsResponse(BaseModel):
|
|||
ssid: str = Field(description="Hotspot SSID")
|
||||
password: str = Field(description="Hotspot password")
|
||||
protocol: str = Field(description="Hotspot protocol (wpa2 or wpa3)")
|
||||
captive_portal: bool = Field(default=True, description="Show a landing page to devices that join the hotspot")
|
||||
restart_required: bool = Field(default=False,
|
||||
description="A saved change only applies after the hotspot is restarted")
|
||||
|
||||
|
||||
class HotspotActionResponse(BaseModel):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue