Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/whichproxy/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ def _curl_match(host: str, tokens: list[str]) -> str | None:
continue
if raw == "*":
return "matched *"
if "/" in raw:
try:
network = ipaddress.ip_network(raw, strict=False)
addr = ipaddress.ip_address(host)
except ValueError:
continue
if addr in network:
return f"CIDR {raw}"
continue
if raw.startswith("."):
suffix = raw.lower().lstrip(".")
if host.endswith("." + suffix):
Expand Down
16 changes: 16 additions & 0 deletions tests/test_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ def test_go_bare_openai_matches_apex_and_subdomain() -> None:
assert sub["go"].route == "DIRECT"


def test_curl_cidr_matches_like_modern_curl() -> None:
inside = _by_model("10.0.0.8", "10.0.0.0/8")
outside = _by_model("11.1.1.1", "10.0.0.0/8")
assert inside["curl"].route == "DIRECT"
assert "CIDR" in inside["curl"].reason
assert outside["curl"].route == "PROXY"


def test_curl_ipv6_cidr_matches_like_modern_curl() -> None:
inside = _by_model("[fd00::1]", "fd00::/8")
outside = _by_model("[2001:db8::1]", "fd00::/8")
assert inside["curl"].route == "DIRECT"
assert "CIDR" in inside["curl"].reason
assert outside["curl"].route == "PROXY"


def test_star_matches_all_curl_and_go_direct() -> None:
results = _by_model("api.openai.com", "*")
assert results["curl"].route == "DIRECT"
Expand Down