feat: warp endpoint; fix: hardening
This commit is contained in:
32 files changed
+266
-381
No files matched your search
@@ -390,6 +390,14 @@ def resolve_endpoints(config: str) -> str:
|
||||
return ENDPOINT_LINE.sub(resolve, config)
|
||||
|
||||
|
||||
def override_endpoint(config: str, endpoint: str) -> str:
|
||||
"""The first peer's Endpoint replaced by endpoint (host:port, IPv6 in brackets)."""
|
||||
config, count = ENDPOINT_LINE.subn(lambda match: f"{match.group(1)}{endpoint}", config, count=1)
|
||||
if not count:
|
||||
raise ConfigError("configuration has no Endpoint to override")
|
||||
return config
|
||||
|
||||
|
||||
# Read once by wireproxy: repeated lines are joined into one list.
|
||||
WIREPROXY_LIST_KEYS = {"address", "dns", "allowedips"}
|
||||
# wg-quick's, which wireproxy has no use for.
|
||||
@@ -722,6 +730,8 @@ def prepare(manifest: dict[str, Any]) -> str:
|
||||
else:
|
||||
raise ConfigError(f"unsupported manifest kind '{kind}'")
|
||||
config = _apply_awg3_mtu_default(config)
|
||||
if manifest.get("endpoint") is not None:
|
||||
config = override_endpoint(config, str(manifest["endpoint"]))
|
||||
validate_config(config, bool(manifest.get("allowConfigHooks", False)))
|
||||
return config
|
||||
|
||||
|
||||
@@ -346,4 +346,4 @@ if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
except ValueError as exc:
|
||||
print(f"error: {exc}", file=sys.stderr)
|
||||
raise SystemExit(1)
|
||||
raise SystemExit(1) from None
|
||||
@@ -474,6 +474,26 @@ class AmneziaWgConfigTests(unittest.TestCase):
|
||||
"[Peer]\nEndpoint = [2001:db8::7]:51820\n[Peer]\nEndpoint = 192.0.2.1:1\nendpoint = [2001:db8::1]:2\n[Peer]\nEndpoint = gone.invalid:3\n",
|
||||
)
|
||||
|
||||
def test_endpoint_override(self):
|
||||
# Only the first peer's Endpoint; a config without one cannot be overridden.
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
source = Path(directory) / "warp.conf"
|
||||
source.write_text(
|
||||
BASE_CONFIG.replace("$PRIMARY_DNS,$SECONDARY_DNS", "1.1.1.1")
|
||||
+ "\n[Peer]\nPublicKey = other\nAllowedIPs = 10.0.0.0/8\nEndpoint = second.example:1\n"
|
||||
)
|
||||
config = prepare({"kind": "configFile", "path": str(source), "endpoint": "[2001:db8::1]:500"})
|
||||
self.assertEqual(
|
||||
amneziawg_config.section_values(config, "peer", "endpoint"),
|
||||
["[2001:db8::1]:500", "second.example:1"],
|
||||
)
|
||||
rendered = amneziawg_config.as_wireproxy(config, "127.0.0.1:18700", 2)
|
||||
self.assertIn("[2001:db8::1]:500", amneziawg_config.section_values(rendered, "peer", "endpoint"))
|
||||
self.assertIn("Endpoint = [2001:db8::1]:500", amneziawg_config.as_outbound(config, 2))
|
||||
source.write_text("[Interface]\nPrivateKey = private\nAddress = 10.8.0.2/32\n[Peer]\nPublicKey = public\nAllowedIPs = 0.0.0.0/0\n")
|
||||
with self.assertRaises(ConfigError):
|
||||
prepare({"kind": "configFile", "path": str(source), "endpoint": "192.0.2.1:500"})
|
||||
|
||||
def test_global_render_pins_the_fwmark(self):
|
||||
# The kill switch knows the tunnel's own packets by it; one set in the file is replaced.
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
|
||||
@@ -92,8 +92,6 @@ class FetchSubscriptionTests(unittest.TestCase):
|
||||
obs = run_fetcher(payload, tag_prefix="sub")
|
||||
tag = obs[0]["tag"]
|
||||
# Tag must contain only safe characters
|
||||
import re
|
||||
|
||||
self.assertRegex(tag, r"^[a-zA-Z0-9_/\-]+$")
|
||||
|
||||
def test_non_latin_remark_kept_in_tag(self):
|
||||
|
||||
@@ -33,6 +33,14 @@ class WarpOutboundTest(unittest.TestCase):
|
||||
self.assertEqual((ob["peers"][0]["address"], ob["peers"][0]["port"]), ("2606:4700:d0::a29f:c001", 500))
|
||||
self.assertNotIn("routing_mark", ob)
|
||||
|
||||
def test_endpoint_override(self):
|
||||
peer = build(WGCF, "warp", None, "162.159.192.1:500")["peers"][0]
|
||||
self.assertEqual((peer["address"], peer["port"]), ("162.159.192.1", 500))
|
||||
peer = build(WGCF, "warp", None, "[2606:4700:d0::a29f:c001]:4500")["peers"][0]
|
||||
self.assertEqual((peer["address"], peer["port"]), ("2606:4700:d0::a29f:c001", 4500))
|
||||
with self.assertRaises(ConfigError):
|
||||
build(WGCF, "warp", None, "162.159.192.1")
|
||||
|
||||
def test_awg3_keepalive_range(self):
|
||||
# Generator AWG 3 profiles range the keepalive; sing-box gets the lower bound.
|
||||
ob = build(WGCF.replace("[Peer]", "RekeyAfterTime = 100-120\n[Peer]\nPersistentKeepalive = 25-35"), "warp", None)
|
||||
|
||||
@@ -35,9 +35,9 @@ def split_endpoint(endpoint: str) -> tuple[str, int]:
|
||||
return host.strip("[]"), int(port)
|
||||
|
||||
|
||||
def build(text: str, tag: str, routing_mark: int | None) -> dict[str, Any]:
|
||||
def build(text: str, tag: str, routing_mark: int | None, endpoint: str | None = None) -> dict[str, Any]:
|
||||
interface, peer = parse_conf(text)
|
||||
host, port = split_endpoint(peer["endpoint"])
|
||||
host, port = split_endpoint(endpoint or peer["endpoint"])
|
||||
sb_peer: dict[str, Any] = {
|
||||
"address": host,
|
||||
"port": port,
|
||||
@@ -74,10 +74,11 @@ def main() -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--tag", default="warp")
|
||||
parser.add_argument("--routing-mark", type=int)
|
||||
parser.add_argument("--endpoint", help="host:port in place of the profile's Endpoint")
|
||||
args = parser.parse_args()
|
||||
text = sys.stdin.read()
|
||||
try:
|
||||
print(json.dumps(build(text, args.tag, args.routing_mark)))
|
||||
print(json.dumps(build(text, args.tag, args.routing_mark, args.endpoint)))
|
||||
except ValueError as error:
|
||||
print(f"proxy-suite: {args.tag}: {error}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
Reference in new issue
Block a user