diff --git a/src/whichproxy/match.py b/src/whichproxy/match.py index 0a795d4..8ab4bac 100644 --- a/src/whichproxy/match.py +++ b/src/whichproxy/match.py @@ -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): diff --git a/tests/test_match.py b/tests/test_match.py index 2c311d9..99b6a2d 100644 --- a/tests/test_match.py +++ b/tests/test_match.py @@ -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"