mirror of
https://github.com/Horizontal42/netsleuth.git
synced 2026-11-10 23:52:04 +03:00
Merge 146e62e558 into 8c99ac78b7
This commit is contained in:
commit
5ba16c6c0f
1 changed files with 29 additions and 13 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
from urllib.parse import urlsplit
|
from urllib.parse import urlsplit
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
|
|
@ -28,10 +29,35 @@ def classify_portal_response(
|
||||||
if redirect_host and redirect_host != requested_host:
|
if redirect_host and redirect_host != requested_host:
|
||||||
return "portal", f"redirected to a different host ({redirect_host})"
|
return "portal", f"redirected to a different host ({redirect_host})"
|
||||||
if status == 200 and body:
|
if status == 200 and body:
|
||||||
return "portal", f"200 with a non-empty body instead of the expected {expected_status}"
|
return (
|
||||||
|
"portal",
|
||||||
|
f"200 with a non-empty body instead of the expected {expected_status}",
|
||||||
|
)
|
||||||
return "suspect", f"unexpected status {status}"
|
return "suspect", f"unexpected status {status}"
|
||||||
|
|
||||||
|
|
||||||
|
async def _check_url(
|
||||||
|
client: httpx.AsyncClient,
|
||||||
|
url: str,
|
||||||
|
expected_status: int,
|
||||||
|
timeout: float,
|
||||||
|
) -> PortalCheck:
|
||||||
|
try:
|
||||||
|
response = await client.get(url, follow_redirects=False, timeout=timeout)
|
||||||
|
verdict, evidence = classify_portal_response(
|
||||||
|
url,
|
||||||
|
response.status_code,
|
||||||
|
response.text,
|
||||||
|
response.headers.get("location"),
|
||||||
|
expected_status,
|
||||||
|
)
|
||||||
|
return PortalCheck(
|
||||||
|
url=url, status=response.status_code, verdict=verdict, evidence=evidence
|
||||||
|
)
|
||||||
|
except httpx.HTTPError as exc:
|
||||||
|
return PortalCheck(url=url, status=None, verdict="error", evidence=str(exc))
|
||||||
|
|
||||||
|
|
||||||
async def check_captive_portal(
|
async def check_captive_portal(
|
||||||
client: httpx.AsyncClient,
|
client: httpx.AsyncClient,
|
||||||
urls: list[str],
|
urls: list[str],
|
||||||
|
|
@ -39,18 +65,8 @@ async def check_captive_portal(
|
||||||
expected_status: int = 204,
|
expected_status: int = 204,
|
||||||
timeout: float,
|
timeout: float,
|
||||||
) -> CaptivePortal:
|
) -> CaptivePortal:
|
||||||
checks: list[PortalCheck] = []
|
coroutines = [_check_url(client, url, expected_status, timeout) for url in urls]
|
||||||
for url in urls:
|
checks = list(await asyncio.gather(*coroutines))
|
||||||
try:
|
|
||||||
response = await client.get(url, follow_redirects=False, timeout=timeout)
|
|
||||||
verdict, evidence = classify_portal_response(
|
|
||||||
url, response.status_code, response.text, response.headers.get("location"), expected_status
|
|
||||||
)
|
|
||||||
except httpx.HTTPError as exc:
|
|
||||||
verdict, evidence = "error", str(exc)
|
|
||||||
checks.append(PortalCheck(url=url, status=None, verdict=verdict, evidence=evidence))
|
|
||||||
continue
|
|
||||||
checks.append(PortalCheck(url=url, status=response.status_code, verdict=verdict, evidence=evidence))
|
|
||||||
|
|
||||||
portal_checks = [c for c in checks if c.verdict == "portal"]
|
portal_checks = [c for c in checks if c.verdict == "portal"]
|
||||||
if portal_checks:
|
if portal_checks:
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue