feat: allow using suite on non-nixos systems
This commit is contained in:
63 files changed
+3572
-523
No files matched your search
@@ -307,6 +307,42 @@ def as_outbound(config: str, fwmark: int) -> str:
|
||||
return _set_interface_value(config, "FwMark", str(fwmark))
|
||||
|
||||
|
||||
# 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.
|
||||
WIREPROXY_DROPPED_KEYS = FORBIDDEN_WG_QUICK_KEYS | {"table", "fwmark"}
|
||||
|
||||
|
||||
def as_wireproxy(config: str, socks_address: str, fwmark: int | None = None) -> str:
|
||||
"""A wireproxy configuration: the profile, served as a SOCKS5 listener on socks_address.
|
||||
|
||||
With fwmark, the tunnel's own packets are marked, to get past TUN and TProxy capture.
|
||||
"""
|
||||
lines: list[str] = []
|
||||
for name, pairs in conf_sections(config):
|
||||
if name not in {"interface", "peer"}:
|
||||
continue
|
||||
if lines:
|
||||
lines.append("")
|
||||
lines.append(f"[{name.capitalize()}]")
|
||||
lists: dict[str, list[str]] = {}
|
||||
for key, value in pairs:
|
||||
if key in WIREPROXY_DROPPED_KEYS:
|
||||
continue
|
||||
if key in WIREPROXY_LIST_KEYS:
|
||||
if key not in lists:
|
||||
lists[key] = []
|
||||
lines.append(key)
|
||||
lists[key].extend(item.strip() for item in value.split(",") if item.strip())
|
||||
else:
|
||||
lines.append(f"{key} = {value}")
|
||||
lines = [f"{line} = {','.join(lists[line])}" if line in lists else line for line in lines]
|
||||
if name == "interface" and fwmark is not None:
|
||||
lines.append(f"fwmark = {fwmark}")
|
||||
lines.extend(["", "[Socks5]", f"BindAddress = {socks_address}"])
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
|
||||
def conf_sections(config: str) -> list[tuple[str, list[tuple[str, str]]]]:
|
||||
"""Each [Section] of a WireGuard .conf, lowercased, with its key = value lines in order.
|
||||
|
||||
@@ -629,6 +665,7 @@ def write_private(path: str, content: str) -> None:
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--manifest")
|
||||
parser.add_argument("--config", metavar="CONFIG", help="render a .conf file, as a configFile manifest")
|
||||
parser.add_argument("--output")
|
||||
parser.add_argument(
|
||||
"--outbound-fwmark",
|
||||
@@ -636,6 +673,11 @@ def main() -> int:
|
||||
metavar="MARK",
|
||||
help="render for an outbound-only interface: no routes or DNS, packets marked MARK",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--wireproxy",
|
||||
metavar="ADDRESS",
|
||||
help="render for wireproxy, as a SOCKS5 listener on ADDRESS (packets marked with --outbound-fwmark)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--inspect",
|
||||
metavar="CONFIG",
|
||||
@@ -644,18 +686,25 @@ def main() -> int:
|
||||
args = parser.parse_args()
|
||||
try:
|
||||
if args.inspect is not None:
|
||||
if args.manifest is not None or args.output is not None:
|
||||
if args.manifest is not None or args.config is not None or args.output is not None:
|
||||
raise ConfigError("--inspect cannot be combined with rendering options")
|
||||
config = _read_limited(args.inspect)
|
||||
print(transport_implementation(config), probe_address(config), rekey_after_time(config))
|
||||
return 0
|
||||
if args.manifest is None or args.output is None:
|
||||
raise ConfigError("--manifest and --output are required when rendering a configuration")
|
||||
manifest = json.loads(_read_limited(args.manifest))
|
||||
if (args.manifest is None) == (args.config is None) or args.output is None:
|
||||
raise ConfigError(
|
||||
"--output and one of --manifest or --config are required when rendering a configuration"
|
||||
)
|
||||
if args.config is not None:
|
||||
manifest = {"kind": "configFile", "path": args.config}
|
||||
else:
|
||||
manifest = json.loads(_read_limited(args.manifest))
|
||||
if not isinstance(manifest, dict):
|
||||
raise ConfigError("manifest must be a JSON object")
|
||||
config = prepare(manifest)
|
||||
if args.outbound_fwmark is not None:
|
||||
if args.wireproxy is not None:
|
||||
config = as_wireproxy(config, args.wireproxy, args.outbound_fwmark)
|
||||
elif args.outbound_fwmark is not None:
|
||||
config = as_outbound(config, args.outbound_fwmark)
|
||||
write_private(args.output, config)
|
||||
except (ConfigError, KeyError, OSError, json.JSONDecodeError) as exc:
|
||||
|
||||
@@ -458,6 +458,28 @@ class AmneziaWgConfigTests(unittest.TestCase):
|
||||
# The peer is untouched.
|
||||
self.assertIn("Endpoint = vpn.example.com:51820", rendered)
|
||||
|
||||
def test_wireproxy_render_joins_lists_and_adds_socks_listener(self):
|
||||
config = BASE_CONFIG.replace("$PRIMARY_DNS,$SECONDARY_DNS", "1.1.1.1").replace(
|
||||
"Address = 10.8.0.2/32",
|
||||
"Address = 10.8.0.2/32\nAddress = fd00::2/128\nTable = off\nPostUp = true\nDNS = 9.9.9.9",
|
||||
) + "AllowedIPs = ::/0\n\n[Peer]\nPublicKey = other\nAllowedIPs = 10.0.0.0/8\n"
|
||||
rendered = amneziawg_config.as_wireproxy(config, "127.0.0.1:18700", 2)
|
||||
sections = amneziawg_config.conf_sections(rendered)
|
||||
self.assertEqual([name for name, _ in sections], ["interface", "peer", "peer", "socks5"])
|
||||
interface = dict(sections[0][1])
|
||||
self.assertEqual(interface["address"], "10.8.0.2/32,fd00::2/128")
|
||||
self.assertEqual(interface["dns"], "9.9.9.9,1.1.1.1")
|
||||
self.assertEqual(interface["fwmark"], "2")
|
||||
self.assertEqual(interface["headerprotectionkey"], "header")
|
||||
self.assertNotIn("table", interface)
|
||||
self.assertNotIn("postup", interface)
|
||||
self.assertEqual(dict(sections[1][1])["allowedips"], "0.0.0.0/0,::/0")
|
||||
self.assertEqual(dict(sections[2][1])["publickey"], "other")
|
||||
self.assertEqual(sections[3][1], [("bindaddress", "127.0.0.1:18700")])
|
||||
# Rootless: no mark.
|
||||
unmarked = amneziawg_config.as_wireproxy(config, "127.0.0.1:18700")
|
||||
self.assertEqual(amneziawg_config.section_values(unmarked, "interface", "fwmark"), [])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user