1028 lines
42 KiB
Python
1028 lines
42 KiB
Python
#!/usr/bin/env python3
|
|
"""Prepare a private AmneziaWG configuration for a proxy-suite profile."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import base64
|
|
import binascii
|
|
import ipaddress
|
|
import json
|
|
import os
|
|
import re
|
|
import socket
|
|
import stat
|
|
import sys
|
|
import tempfile
|
|
import zlib
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
|
|
MAX_INPUT_BYTES = 8 * 1024 * 1024
|
|
MAX_DECOMPRESSED_BYTES = 8 * 1024 * 1024
|
|
AWG3_SAFE_DEFAULT_MTU = 1280
|
|
FORBIDDEN_WG_QUICK_KEYS = {"preup", "postup", "predown", "postdown", "saveconfig"}
|
|
AWG_PROTOCOL_KEYS = {"awg", "amnezia-awg", "amnezia-awg2", "amnezia-awg3"}
|
|
AWG3_INTERFACE_KEYS = {
|
|
"headerprotectionkey",
|
|
"contentpaddingaddition",
|
|
"rekeyaftertime",
|
|
"rekeytimeout",
|
|
"rejectaftertime",
|
|
"keepalivetimeout",
|
|
"maxhandshakeattempts",
|
|
"randomtrailers",
|
|
"disablecookies",
|
|
}
|
|
VPN_INTERFACE_FIELDS = {
|
|
"jc": "Jc",
|
|
"jmin": "Jmin",
|
|
"jmax": "Jmax",
|
|
"s1": "S1",
|
|
"s2": "S2",
|
|
"s3": "S3",
|
|
"s4": "S4",
|
|
"h1": "H1",
|
|
"h2": "H2",
|
|
"h3": "H3",
|
|
"h4": "H4",
|
|
"i1": "I1",
|
|
"i2": "I2",
|
|
"i3": "I3",
|
|
"i4": "I4",
|
|
"i5": "I5",
|
|
"headerprotectionkey": "HeaderProtectionKey",
|
|
"contentpaddingaddition": "ContentPaddingAddition",
|
|
"rekeyaftertime": "RekeyAfterTime",
|
|
"rekeytimeout": "RekeyTimeout",
|
|
"rejectaftertime": "RejectAfterTime",
|
|
"keepalivetimeout": "KeepaliveTimeout",
|
|
"maxhandshakeattempts": "MaxHandshakeAttempts",
|
|
"randomtrailers": "RandomTrailers",
|
|
"disablecookies": "DisableCookies",
|
|
}
|
|
|
|
|
|
class ConfigError(ValueError):
|
|
pass
|
|
|
|
|
|
def _shared_directory(path: str) -> bool:
|
|
"""Whether someone besides this user may add files next to path.
|
|
|
|
Runtime profiles come from a group-writable spool and are read as root: a symlink
|
|
there would hand over any file root can read, and a FIFO would hang the unit.
|
|
"""
|
|
try:
|
|
info = os.stat(os.path.dirname(os.path.abspath(path)))
|
|
except OSError:
|
|
return True
|
|
return info.st_uid != os.geteuid() or bool(info.st_mode & (stat.S_IWGRP | stat.S_IWOTH))
|
|
|
|
|
|
def _read_limited(path: str) -> str:
|
|
"""At most MAX_INPUT_BYTES of path, as UTF-8; in a shared directory, only a regular file."""
|
|
shared = _shared_directory(path)
|
|
fd = os.open(path, os.O_RDONLY | (os.O_NOFOLLOW | os.O_NONBLOCK if shared else 0))
|
|
with os.fdopen(fd, "rb") as handle:
|
|
if shared and not stat.S_ISREG(os.fstat(fd).st_mode):
|
|
raise ConfigError(f"{path} is not a regular file")
|
|
data = handle.read(MAX_INPUT_BYTES + 1)
|
|
if len(data) > MAX_INPUT_BYTES:
|
|
raise ConfigError(f"input exceeds {MAX_INPUT_BYTES} bytes")
|
|
return data.decode("utf-8")
|
|
|
|
|
|
def _decode_json_value(value: Any, label: str) -> dict[str, Any]:
|
|
if isinstance(value, str):
|
|
try:
|
|
value = json.loads(value)
|
|
except json.JSONDecodeError as exc:
|
|
raise ConfigError(f"invalid {label} JSON: {exc}") from exc
|
|
if not isinstance(value, dict):
|
|
raise ConfigError(f"invalid {label}: expected an object")
|
|
return value
|
|
|
|
|
|
def decode_vpn_link(link: str) -> dict[str, Any]:
|
|
link = link.strip()
|
|
if not link.startswith("vpn://"):
|
|
raise ConfigError("expected a self-contained vpn:// link")
|
|
encoded = link[len("vpn://") :]
|
|
if not encoded or re.fullmatch(r"[A-Za-z0-9_-]+={0,2}", encoded) is None:
|
|
raise ConfigError("unsupported vpn:// link; API-backed text keys are not supported")
|
|
try:
|
|
raw = base64.b64decode(
|
|
encoded + "=" * (-len(encoded) % 4), altchars=b"-_", validate=True
|
|
)
|
|
except (binascii.Error, ValueError) as exc:
|
|
raise ConfigError(
|
|
"unsupported vpn:// link; expected embedded base64 data, not an API-backed text key"
|
|
) from exc
|
|
if len(raw) > MAX_INPUT_BYTES:
|
|
raise ConfigError(f"vpn:// payload exceeds {MAX_INPUT_BYTES} bytes")
|
|
|
|
decoded = raw
|
|
if len(raw) >= 5:
|
|
expected_size = int.from_bytes(raw[:4], "big")
|
|
try:
|
|
decompressor = zlib.decompressobj()
|
|
candidate = decompressor.decompress(raw[4:], MAX_DECOMPRESSED_BYTES + 1)
|
|
if len(candidate) > MAX_DECOMPRESSED_BYTES or decompressor.unconsumed_tail:
|
|
raise ConfigError(
|
|
f"decompressed vpn:// payload exceeds {MAX_DECOMPRESSED_BYTES} bytes"
|
|
)
|
|
candidate += decompressor.flush(MAX_DECOMPRESSED_BYTES + 1 - len(candidate))
|
|
if decompressor.eof:
|
|
if len(candidate) > MAX_DECOMPRESSED_BYTES:
|
|
raise ConfigError(
|
|
f"decompressed vpn:// payload exceeds {MAX_DECOMPRESSED_BYTES} bytes"
|
|
)
|
|
if len(candidate) != expected_size:
|
|
raise ConfigError("vpn:// qCompress length header does not match payload")
|
|
decoded = candidate
|
|
except zlib.error:
|
|
pass
|
|
|
|
if len(decoded) > MAX_DECOMPRESSED_BYTES:
|
|
raise ConfigError(f"decoded vpn:// payload exceeds {MAX_DECOMPRESSED_BYTES} bytes")
|
|
# Amnezia clients also export a compact direct form where the payload is
|
|
# simply the URL-safe-base64 encoded .conf text. Normalize it to the
|
|
# same container shape as the JSON export so selection and validation stay
|
|
# identical. This is deliberately limited to a complete AWG config and
|
|
# never attempts to resolve short/API-backed keys.
|
|
try:
|
|
direct_config = decoded.decode("utf-8")
|
|
except UnicodeDecodeError:
|
|
direct_config = ""
|
|
if direct_config.lstrip().startswith("[Interface]") and "[Peer]" in direct_config:
|
|
return {
|
|
"defaultContainer": "amnezia-awg",
|
|
"containers": [
|
|
{
|
|
"container": "amnezia-awg",
|
|
"config": direct_config,
|
|
}
|
|
],
|
|
}
|
|
try:
|
|
result = json.loads(decoded.decode("utf-8"))
|
|
except (UnicodeDecodeError, json.JSONDecodeError) as exc:
|
|
raise ConfigError(
|
|
"unsupported vpn:// link; expected embedded qCompress/base64 JSON, not an API-backed key"
|
|
) from exc
|
|
if not isinstance(result, dict):
|
|
raise ConfigError("invalid vpn:// configuration: expected a JSON object")
|
|
return result
|
|
|
|
|
|
def encode_vpn_link(
|
|
config: str,
|
|
host: str,
|
|
description: str,
|
|
client_public_key: str | None = None,
|
|
) -> str:
|
|
"""A self-contained vpn:// link carrying a client .conf, as AmneziaVPN exports one.
|
|
|
|
The payload is qCompress'd JSON (a 4-byte big-endian length, then zlib), in URL-safe
|
|
base64. Everything but host and the client's public key is read from the config.
|
|
"""
|
|
interface = {key: value for name, pairs in conf_sections(config) if name == "interface" for key, value in pairs}
|
|
peer = {key: value for name, pairs in conf_sections(config) if name == "peer" for key, value in pairs}
|
|
endpoint = peer.get("endpoint", "")
|
|
port = endpoint.rsplit(":", 1)[1] if ":" in endpoint else ""
|
|
dns = [item.strip() for item in interface.get("dns", "").split(",") if item.strip()]
|
|
last: dict[str, Any] = {
|
|
"config": config,
|
|
"hostName": host,
|
|
"port": int(port) if port.isdigit() else port,
|
|
"client_ip": interface.get("address", "").split(",")[0].strip().split("/")[0],
|
|
"client_priv_key": interface.get("privatekey", ""),
|
|
"server_pub_key": peer.get("publickey", ""),
|
|
"psk_key": peer.get("presharedkey", ""),
|
|
"allowed_ips": [item.strip() for item in peer.get("allowedips", "").split(",") if item.strip()],
|
|
"persistent_keep_alive": peer.get("persistentkeepalive", ""),
|
|
}
|
|
if client_public_key:
|
|
last["client_pub_key"] = client_public_key
|
|
if interface.get("mtu"):
|
|
last["mtu"] = interface["mtu"]
|
|
protocol: dict[str, Any] = {}
|
|
for normalized, key in VPN_INTERFACE_FIELDS.items():
|
|
if normalized in interface:
|
|
last[key] = interface[normalized]
|
|
protocol[key] = interface[normalized]
|
|
protocol.update(
|
|
{
|
|
"last_config": json.dumps(last, separators=(",", ":")),
|
|
"port": str(port),
|
|
"transport_proto": "udp",
|
|
"isThirdPartyConfig": True,
|
|
}
|
|
)
|
|
data = {
|
|
"containers": [{"container": "amnezia-awg", "awg": protocol}],
|
|
"defaultContainer": "amnezia-awg",
|
|
"description": description,
|
|
"hostName": host,
|
|
}
|
|
if dns:
|
|
data["dns1"] = dns[0]
|
|
if len(dns) > 1:
|
|
data["dns2"] = dns[1]
|
|
payload = json.dumps(data, separators=(",", ":")).encode("utf-8")
|
|
compressed = len(payload).to_bytes(4, "big") + zlib.compress(payload, 8)
|
|
return "vpn://" + base64.urlsafe_b64encode(compressed).decode("ascii").rstrip("=")
|
|
|
|
|
|
def _candidate_payload(value: Any, key: str) -> tuple[dict[str, Any], str]:
|
|
payload = _decode_json_value(value, f"{key} protocol")
|
|
last = _decode_json_value(payload.get("last_config", payload), f"{key}.last_config")
|
|
config = last.get("config")
|
|
if not isinstance(config, str) or "[Interface]" not in config or "[Peer]" not in config:
|
|
raise ConfigError(f"{key} candidate does not contain an AmneziaWG config")
|
|
return last, config
|
|
|
|
|
|
def extract_vpn_config(data: dict[str, Any], requested_container: str | None = None) -> str:
|
|
containers = data.get("containers")
|
|
if not isinstance(containers, list):
|
|
raise ConfigError("vpn:// configuration has no containers list")
|
|
|
|
candidates: list[dict[str, Any]] = []
|
|
for index, item in enumerate(containers):
|
|
if not isinstance(item, dict):
|
|
continue
|
|
container_id = item.get("container") if isinstance(item.get("container"), str) else ""
|
|
protocols = [(key, item[key]) for key in item if key.lower() in AWG_PROTOCOL_KEYS]
|
|
if container_id.lower() in AWG_PROTOCOL_KEYS and not protocols and (
|
|
"last_config" in item or "config" in item
|
|
):
|
|
protocols = [(container_id, item)]
|
|
for key, value in protocols:
|
|
try:
|
|
last, config = _candidate_payload(value, key)
|
|
except ConfigError:
|
|
continue
|
|
candidates.append(
|
|
{
|
|
"id": container_id or key,
|
|
"key": key,
|
|
"index": index,
|
|
"last": last,
|
|
"config": config,
|
|
}
|
|
)
|
|
|
|
if not candidates:
|
|
raise ConfigError("vpn:// configuration contains no AmneziaWG client config")
|
|
|
|
selected: list[dict[str, Any]]
|
|
if requested_container:
|
|
requested_lower = requested_container.lower()
|
|
selected = [
|
|
candidate
|
|
for candidate in candidates
|
|
if requested_lower
|
|
in {str(candidate["id"]).lower(), str(candidate["key"]).lower()}
|
|
]
|
|
if len(selected) != 1:
|
|
raise ConfigError(
|
|
f"vpnContainer '{requested_container}' matched {len(selected)} AmneziaWG configs"
|
|
)
|
|
else:
|
|
default_container = data.get("defaultContainer")
|
|
default_matches = [
|
|
candidate
|
|
for candidate in candidates
|
|
if isinstance(default_container, str)
|
|
and default_container.lower()
|
|
in {str(candidate["id"]).lower(), str(candidate["key"]).lower()}
|
|
]
|
|
if len(default_matches) == 1:
|
|
selected = default_matches
|
|
elif len(candidates) == 1:
|
|
selected = candidates
|
|
else:
|
|
choices = ", ".join(str(candidate["id"]) for candidate in candidates)
|
|
raise ConfigError(
|
|
f"vpn:// configuration contains multiple AmneziaWG configs ({choices}); set vpnContainer"
|
|
)
|
|
|
|
candidate = selected[0]
|
|
config = candidate["config"]
|
|
dns1 = data.get("dns1") if isinstance(data.get("dns1"), str) else ""
|
|
dns2 = data.get("dns2") if isinstance(data.get("dns2"), str) else ""
|
|
if dns1:
|
|
config = config.replace("$PRIMARY_DNS", dns1)
|
|
if dns2:
|
|
config = config.replace("$SECONDARY_DNS", dns2)
|
|
if "$PRIMARY_DNS" in config or "$SECONDARY_DNS" in config:
|
|
raise ConfigError("vpn:// configuration contains unresolved DNS placeholders")
|
|
|
|
last = candidate["last"]
|
|
normalized_last = {
|
|
re.sub(r"[^a-z0-9]", "", str(key).lower()): value for key, value in last.items()
|
|
}
|
|
for normalized, config_key in VPN_INTERFACE_FIELDS.items():
|
|
value = normalized_last.get(normalized)
|
|
if value not in (None, ""):
|
|
if normalized in {"randomtrailers", "disablecookies"} and isinstance(value, bool):
|
|
value = "on" if value else "off"
|
|
config = _set_interface_value(config, config_key, str(value))
|
|
if last.get("mtu") not in (None, ""):
|
|
config = _set_interface_value(config, "MTU", str(last["mtu"]))
|
|
if last.get("port") not in (None, ""):
|
|
config = _set_interface_value(config, "ListenPort", str(last["port"]))
|
|
return _apply_awg3_mtu_default(config)
|
|
|
|
|
|
def _set_interface_value(config: str, key: str, value: str) -> str:
|
|
lines = config.splitlines()
|
|
interface_index = next(
|
|
(index for index, line in enumerate(lines) if line.strip().lower() == "[interface]"),
|
|
None,
|
|
)
|
|
if interface_index is None:
|
|
raise ConfigError("configuration has no [Interface] section")
|
|
end = next(
|
|
(
|
|
index
|
|
for index in range(interface_index + 1, len(lines))
|
|
if lines[index].strip().startswith("[")
|
|
),
|
|
len(lines),
|
|
)
|
|
pattern = re.compile(rf"^\s*{re.escape(key)}\s*=", re.IGNORECASE)
|
|
for index in range(interface_index + 1, end):
|
|
if pattern.match(lines[index]):
|
|
lines[index] = f"{key} = {value}"
|
|
break
|
|
else:
|
|
lines.insert(end, f"{key} = {value}")
|
|
return "\n".join(lines) + "\n"
|
|
|
|
|
|
def _remove_interface_key(config: str, key: str) -> str:
|
|
pattern = re.compile(rf"^\s*{re.escape(key)}\s*=", re.IGNORECASE)
|
|
lines: list[str] = []
|
|
in_interface = False
|
|
for line in config.splitlines():
|
|
stripped = line.strip()
|
|
if stripped.startswith("["):
|
|
in_interface = stripped.lower() == "[interface]"
|
|
if not (in_interface and pattern.match(line)):
|
|
lines.append(line)
|
|
return "\n".join(lines) + "\n"
|
|
|
|
|
|
def dns_servers(config: str) -> list[str]:
|
|
"""The addresses in [Interface] DNS, in order; search domains left out."""
|
|
servers: list[str] = []
|
|
for value in section_values(config, "interface", "dns"):
|
|
for item in value.split(","):
|
|
try:
|
|
servers.append(str(ipaddress.ip_address(item.strip())))
|
|
except ValueError:
|
|
pass
|
|
return servers
|
|
|
|
|
|
def _host_addresses(config: str) -> str:
|
|
"""Every [Interface] Address narrowed to the host itself (/32, /128): the kernel routes
|
|
its prefix in the main table whatever Table says, and an outbound has no use for it."""
|
|
pattern = re.compile(r"^(\s*Address\s*=\s*)(.*)$", re.IGNORECASE)
|
|
lines: list[str] = []
|
|
in_interface = False
|
|
for line in config.splitlines():
|
|
stripped = line.strip()
|
|
if stripped.startswith("["):
|
|
in_interface = stripped.lower() == "[interface]"
|
|
match = pattern.match(line) if in_interface else None
|
|
if match:
|
|
hosts = []
|
|
for item in match.group(2).split("#", 1)[0].split(","):
|
|
item = item.strip()
|
|
if not item:
|
|
continue
|
|
try:
|
|
interface = ipaddress.ip_interface(item)
|
|
except ValueError as exc:
|
|
raise ConfigError(f"invalid interface Address '{item}'") from exc
|
|
hosts.append(f"{interface.ip}/{interface.max_prefixlen}")
|
|
line = f"{match.group(1)}{', '.join(hosts)}"
|
|
lines.append(line)
|
|
return "\n".join(lines) + "\n"
|
|
|
|
|
|
def _replace_interface_key(config: str, key: str, value: str) -> str:
|
|
"""key set to value, every other line of it gone: wg and awg-quick take the last one,
|
|
so `Table = 1234` then `Table = auto` would keep auto past a rewrite of the first."""
|
|
return _set_interface_value(_remove_interface_key(config, key), key, value)
|
|
|
|
|
|
def _drop_privileged_listen_port(config: str) -> str:
|
|
"""A privileged ListenPort (53, say), bound by root, would take it from the host's own
|
|
services: the port goes, and the tunnel takes any."""
|
|
for value in section_values(config, "interface", "listenport"):
|
|
if not value.strip().isascii() or not value.strip().isdigit() or int(value.strip()) < 1024:
|
|
return _remove_interface_key(config, "ListenPort")
|
|
return config
|
|
|
|
|
|
def as_outbound(config: str, fwmark: int, host_addresses: bool = True) -> str:
|
|
"""Keep the host's routes and resolver: the proxy binds to the interface instead.
|
|
|
|
The mark lets the tunnel's own packets past TUN and TProxy capture. host_addresses
|
|
narrows Address to the host (_host_addresses), for a profile from a shared spool.
|
|
"""
|
|
config = _remove_interface_key(config, "DNS")
|
|
if host_addresses:
|
|
config = _host_addresses(config)
|
|
# Any source port does for an outbound.
|
|
config = _drop_privileged_listen_port(config)
|
|
config = _replace_interface_key(config, "Table", "off")
|
|
return _replace_interface_key(config, "FwMark", str(fwmark))
|
|
|
|
|
|
ENDPOINT_LINE = re.compile(r"^(\s*Endpoint\s*=\s*)(\S+):(\d+)\s*$", re.IGNORECASE | re.MULTILINE)
|
|
|
|
|
|
def resolve_endpoints(config: str) -> str:
|
|
"""Endpoint names looked up here, by this process, as awg would; one that does not resolve is left."""
|
|
|
|
def resolve(match: re.Match[str]) -> str:
|
|
prefix, host, port = match.groups()
|
|
try:
|
|
ipaddress.ip_address(host.strip("[]"))
|
|
return match.group(0)
|
|
except ValueError:
|
|
pass
|
|
try:
|
|
family, *_, address = socket.getaddrinfo(host, int(port), type=socket.SOCK_DGRAM, flags=socket.AI_ADDRCONFIG)[0]
|
|
except (OSError, UnicodeError):
|
|
return match.group(0)
|
|
ip = f"[{address[0]}]" if family == socket.AF_INET6 else address[0]
|
|
return f"{prefix}{ip}:{port}"
|
|
|
|
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.
|
|
WIREPROXY_DROPPED_KEYS = FORBIDDEN_WG_QUICK_KEYS | {"table", "fwmark"}
|
|
|
|
|
|
def as_wireproxy(
|
|
config: str, socks_address: str, fwmark: int | None = None, login: tuple[str, str] | 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}"])
|
|
# Both, or wireproxy takes no login at all: a password alone is none.
|
|
if login is not None:
|
|
user, password = login
|
|
if not re.fullmatch(r"[A-Za-z0-9._-]+", user) or not re.fullmatch(r"[A-Za-z0-9._-]+", password):
|
|
raise ConfigError("the SOCKS login must be letters, digits, dot, dash and underscore")
|
|
lines.extend([f"Username = {user}", f"Password = {password}"])
|
|
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.
|
|
|
|
Keys are lowercased; comments (# or ;) and lines outside a section are dropped.
|
|
"""
|
|
sections: list[tuple[str, list[tuple[str, str]]]] = []
|
|
for line in config.splitlines():
|
|
stripped = line.split("#", 1)[0].split(";", 1)[0].strip()
|
|
if stripped.startswith("[") and stripped.endswith("]"):
|
|
sections.append((stripped[1:-1].strip().lower(), []))
|
|
elif "=" in stripped and sections:
|
|
key, value = (part.strip() for part in stripped.split("=", 1))
|
|
sections[-1][1].append((key.lower(), value))
|
|
return sections
|
|
|
|
|
|
def section_values(config: str, section: str, key: str) -> list[str]:
|
|
"""Every value of `key` in every `section`, in file order."""
|
|
return [
|
|
value
|
|
for name, pairs in conf_sections(config)
|
|
if name == section
|
|
for found, value in pairs
|
|
if found == key
|
|
]
|
|
|
|
|
|
def _interface_keys(config: str) -> set[str]:
|
|
keys: set[str] = set()
|
|
for name, pairs in conf_sections(config):
|
|
if name == "interface":
|
|
keys.update(re.sub(r"[^a-z0-9]", "", key) for key, _ in pairs)
|
|
return keys
|
|
|
|
|
|
def _apply_awg3_mtu_default(config: str) -> str:
|
|
keys = _interface_keys(config)
|
|
if "mtu" in keys or not (AWG3_INTERFACE_KEYS & keys):
|
|
return config
|
|
# Amnezia exports rely on the official client's platform MTU default when
|
|
# they omit MTU. awg-quick would instead choose 1420, which is unsafe once
|
|
# AWG 3 per-packet padding is enabled and commonly black-holes larger TLS
|
|
# handshake/data packets. Match the conservative mobile client default.
|
|
return _set_interface_value(config, "MTU", str(AWG3_SAFE_DEFAULT_MTU))
|
|
|
|
|
|
def _secret_value(inline: Any, path: Any, label: str) -> str:
|
|
values = int(inline is not None) + int(path is not None)
|
|
if values != 1:
|
|
raise ConfigError(f"set exactly one of {label} or {label}File")
|
|
value = str(inline) if inline is not None else _read_limited(str(path)).strip()
|
|
if not value or "\n" in value or "\r" in value:
|
|
raise ConfigError(f"{label} must be one non-empty line")
|
|
return value
|
|
|
|
|
|
OBFUSCATION_UNSIGNED_FIELDS = {"jc", "jmin", "jmax", "s1", "s2", "s3", "s4"}
|
|
OBFUSCATION_RANGE_FIELDS = {
|
|
"h1",
|
|
"h2",
|
|
"h3",
|
|
"h4",
|
|
"contentPaddingAddition",
|
|
"rekeyAfterTime",
|
|
"rekeyTimeout",
|
|
"rejectAfterTime",
|
|
"keepaliveTimeout",
|
|
"maxHandshakeAttempts",
|
|
}
|
|
OBFUSCATION_STRING_FIELDS = {"i1", "i2", "i3", "i4", "i5", "headerProtectionKey"}
|
|
OBFUSCATION_BOOLEAN_FIELDS = {"randomTrailers", "disableCookies"}
|
|
|
|
|
|
def _unique_secret_object(pairs: list[tuple[str, Any]]) -> dict[str, Any]:
|
|
result: dict[str, Any] = {}
|
|
for key, value in pairs:
|
|
if key in result:
|
|
# Even a key can contain secret input; do not echo it.
|
|
raise ConfigError("obfuscationFile contains duplicate JSON keys")
|
|
result[key] = value
|
|
return result
|
|
|
|
|
|
def _reject_secret_constant(value: str) -> Any:
|
|
raise ConfigError("obfuscationFile contains an invalid JSON constant")
|
|
|
|
|
|
def _merge_obfuscation(settings: dict[str, Any]) -> dict[str, Any]:
|
|
public = settings.get("obfuscation", {})
|
|
if not isinstance(public, dict):
|
|
raise ConfigError("obfuscation must be an object")
|
|
path = settings.get("obfuscationFile")
|
|
if path is None:
|
|
return public
|
|
try:
|
|
secret = json.loads(
|
|
_read_limited(str(path)),
|
|
object_pairs_hook=_unique_secret_object,
|
|
parse_constant=_reject_secret_constant,
|
|
)
|
|
except (OSError, UnicodeError, ValueError, RecursionError) as exc:
|
|
# Decoder errors and OS exceptions can include input excerpts or paths.
|
|
message = (
|
|
str(exc) if isinstance(exc, ConfigError)
|
|
else "cannot read or decode obfuscationFile"
|
|
)
|
|
raise ConfigError(message) from None
|
|
if not isinstance(secret, dict):
|
|
raise ConfigError("obfuscationFile must contain a JSON object")
|
|
|
|
merged = dict(public)
|
|
known = (
|
|
OBFUSCATION_UNSIGNED_FIELDS | OBFUSCATION_RANGE_FIELDS
|
|
| OBFUSCATION_STRING_FIELDS | OBFUSCATION_BOOLEAN_FIELDS
|
|
)
|
|
for key, value in secret.items():
|
|
if key not in known:
|
|
raise ConfigError("obfuscationFile contains an unsupported field")
|
|
if value is None:
|
|
continue
|
|
if public.get(key) is not None or (
|
|
key == "headerProtectionKey" and public.get("headerProtectionKeyFile") is not None
|
|
):
|
|
raise ConfigError(f"obfuscationFile field '{key}' has multiple sources")
|
|
unsigned = type(value) is int and value >= 0
|
|
if key in OBFUSCATION_UNSIGNED_FIELDS:
|
|
valid = unsigned
|
|
elif key in OBFUSCATION_RANGE_FIELDS:
|
|
valid = unsigned or (
|
|
isinstance(value, str)
|
|
and re.fullmatch(r"([0-9]+|[0-9]+-[0-9]+|\(off\))", value) is not None
|
|
)
|
|
elif key in OBFUSCATION_BOOLEAN_FIELDS:
|
|
valid = type(value) is bool
|
|
else:
|
|
valid = (
|
|
isinstance(value, str)
|
|
and bool(value.strip())
|
|
and value.splitlines() == [value]
|
|
and "\x00" not in value
|
|
)
|
|
if valid:
|
|
try:
|
|
value.encode("utf-8")
|
|
except UnicodeEncodeError:
|
|
valid = False
|
|
if not valid:
|
|
raise ConfigError(f"obfuscationFile field '{key}' has an invalid value")
|
|
merged[key] = value
|
|
return merged
|
|
|
|
|
|
def render_settings(settings: dict[str, Any]) -> str:
|
|
addresses = settings.get("addresses", [])
|
|
peers = settings.get("peers", [])
|
|
if not isinstance(addresses, list) or not addresses:
|
|
raise ConfigError("declarative settings require at least one address")
|
|
if not isinstance(peers, list) or not peers:
|
|
raise ConfigError("declarative settings require at least one peer")
|
|
|
|
lines = ["[Interface]"]
|
|
private_key = _secret_value(
|
|
settings.get("privateKey"), settings.get("privateKeyFile"), "privateKey"
|
|
)
|
|
lines.append(f"PrivateKey = {private_key}")
|
|
for address in addresses:
|
|
lines.append(f"Address = {address}")
|
|
dns = settings.get("dns", [])
|
|
if dns:
|
|
lines.append(f"DNS = {','.join(str(value) for value in dns)}")
|
|
field_map = {
|
|
"listenPort": "ListenPort",
|
|
"mtu": "MTU",
|
|
"table": "Table",
|
|
}
|
|
for source, target in field_map.items():
|
|
if settings.get(source) is not None:
|
|
lines.append(f"{target} = {settings[source]}")
|
|
|
|
obfuscation = _merge_obfuscation(settings)
|
|
# Option names are the config keys in camelCase; the key comes last, from its secret.
|
|
values = {key.lower(): value for key, value in obfuscation.items()}
|
|
for normalized, target in VPN_INTERFACE_FIELDS.items():
|
|
value = values.get(normalized)
|
|
if normalized == "headerprotectionkey" or value is None:
|
|
continue
|
|
if isinstance(value, bool):
|
|
value = "on" if value else "off"
|
|
lines.append(f"{target} = {value}")
|
|
if obfuscation.get("headerProtectionKey") is not None or obfuscation.get(
|
|
"headerProtectionKeyFile"
|
|
) is not None:
|
|
value = _secret_value(
|
|
obfuscation.get("headerProtectionKey"),
|
|
obfuscation.get("headerProtectionKeyFile"),
|
|
"headerProtectionKey",
|
|
)
|
|
lines.append(f"HeaderProtectionKey = {value}")
|
|
|
|
for peer in peers:
|
|
if not isinstance(peer, dict):
|
|
raise ConfigError("peer must be an object")
|
|
public_key = peer.get("publicKey")
|
|
allowed_ips = peer.get("allowedIPs", [])
|
|
if not public_key or not allowed_ips:
|
|
raise ConfigError("each peer requires publicKey and allowedIPs")
|
|
lines.extend(["", "[Peer]", f"PublicKey = {public_key}"])
|
|
if peer.get("presharedKey") is not None or peer.get("presharedKeyFile") is not None:
|
|
value = _secret_value(
|
|
peer.get("presharedKey"), peer.get("presharedKeyFile"), "presharedKey"
|
|
)
|
|
lines.append(f"PresharedKey = {value}")
|
|
lines.append(f"AllowedIPs = {','.join(str(value) for value in allowed_ips)}")
|
|
if peer.get("endpoint") is not None:
|
|
lines.append(f"Endpoint = {peer['endpoint']}")
|
|
if peer.get("persistentKeepalive") is not None:
|
|
lines.append(f"PersistentKeepalive = {peer['persistentKeepalive']}")
|
|
if peer.get("advancedSecurity") is not None:
|
|
lines.append(
|
|
f"AdvancedSecurity = {'on' if peer['advancedSecurity'] else 'off'}"
|
|
)
|
|
return "\n".join(lines) + "\n"
|
|
|
|
|
|
# What ends a line for str.splitlines(), which the rewrites here split on, but not for
|
|
# awg-quick's `read -r`, plus NUL, which bash drops from what it reads: "PostUp \f= cmd"
|
|
# or "Post\0Up = cmd" would reach awg-quick as a hook no check here saw. A lone CR too;
|
|
# a CRLF line end is fine. No real configuration has any of them.
|
|
_AMBIGUOUS_CHARACTERS = re.compile("[\x00-\x08\x0b\x0c\x0e-\x1f\x7f\x85\u2028\u2029]|\r(?!\n)")
|
|
|
|
|
|
def validate_config(config: str, allow_hooks: bool = False) -> None:
|
|
if _AMBIGUOUS_CHARACTERS.search(config):
|
|
raise ConfigError("configuration contains control characters")
|
|
lowered = config.lower()
|
|
if "[interface]" not in lowered or "[peer]" not in lowered:
|
|
raise ConfigError("configuration requires [Interface] and [Peer] sections")
|
|
# Headers exactly as written, one [Interface]: one the rewrites here miss would let its
|
|
# Table, Address or DNS reach the host untouched.
|
|
interfaces = 0
|
|
for line in config.split("\n"):
|
|
stripped = line.strip()
|
|
if not stripped.split("#", 1)[0].strip().startswith("["):
|
|
continue
|
|
if stripped.lower() not in ("[interface]", "[peer]"):
|
|
raise ConfigError(f"unexpected section header {stripped[:40]!r}: only [Interface] and [Peer], alone on their line")
|
|
interfaces += stripped.lower() == "[interface]"
|
|
if interfaces != 1:
|
|
raise ConfigError("configuration needs exactly one [Interface] section")
|
|
if not allow_hooks:
|
|
# Read as awg-quick reads it: lines end at \n, a comment starts at #, and the
|
|
# key is what precedes the first =, or the whole line without one.
|
|
for line in config.split("\n"):
|
|
key = line.split("#", 1)[0].split("=", 1)[0].strip().lower()
|
|
# A non-ASCII key can fold to one under the shell's case-insensitive match
|
|
# ("SaveConfİg" is SaveConfig to awg-quick's nocasematch); no real key has one.
|
|
if not key.isascii():
|
|
raise ConfigError(f"configuration has a key with non-ASCII characters: {key[:40]!r}")
|
|
if key in FORBIDDEN_WG_QUICK_KEYS:
|
|
raise ConfigError(
|
|
f"configuration contains privileged wg-quick directive '{key}'; "
|
|
"set allowConfigHooks = true only for trusted input"
|
|
)
|
|
|
|
|
|
def probe_address(config: str) -> str:
|
|
"""Return an address covered by a peer route without exposing credentials."""
|
|
networks: list[ipaddress.IPv4Network | ipaddress.IPv6Network] = []
|
|
for value in section_values(config, "peer", "allowedips"):
|
|
for item in value.split(","):
|
|
try:
|
|
networks.append(ipaddress.ip_network(item.strip(), strict=False))
|
|
except ValueError:
|
|
continue
|
|
|
|
preferred = [ipaddress.ip_address("1.1.1.1"), ipaddress.ip_address("2606:4700:4700::1111")]
|
|
for address in preferred:
|
|
if any(address in network for network in networks):
|
|
return str(address)
|
|
for network in networks:
|
|
if network.num_addresses == 1:
|
|
return str(network.network_address)
|
|
return str(network.network_address + 1)
|
|
raise ConfigError("configuration has no usable peer AllowedIPs for a startup handshake probe")
|
|
|
|
|
|
def transport_implementation(config: str) -> str:
|
|
"""Select userspace for the upstream RandomTrailers/ranged-header defect."""
|
|
trailers = section_values(config, "interface", "randomtrailers")
|
|
random_trailers = bool(trailers) and trailers[-1].lower() in {"on", "true", "yes", "1"}
|
|
ranged_handshake_header = any(
|
|
re.fullmatch(r"[0-9]+\s*-\s*[0-9]+", value) is not None
|
|
for key in ("h1", "h2", "h3")
|
|
for value in section_values(config, "interface", key)
|
|
)
|
|
return "userspace" if random_trailers and ranged_handshake_header else "auto"
|
|
|
|
|
|
def rekey_after_time(config: str) -> int:
|
|
"""Longest wait between rekeys, in seconds; 0 when rekeying is off."""
|
|
values = section_values(config, "interface", "rekeyaftertime")
|
|
if not values:
|
|
return 120
|
|
if values[-1] == "(off)":
|
|
return 0
|
|
return int(values[-1].split("-")[-1])
|
|
|
|
|
|
def _normalize_headers(config: str) -> str:
|
|
"""Section headers as awg-quick reads them ("[Peer] # home" opens a Peer section),
|
|
written plainly so the rewrites here see every section it will."""
|
|
lines = []
|
|
for line in config.split("\n"):
|
|
header = line.split("#", 1)[0].strip().lower()
|
|
if header in ("[interface]", "[peer]"):
|
|
line = "[Interface]" if header == "[interface]" else "[Peer]"
|
|
lines.append(line)
|
|
return "\n".join(lines)
|
|
|
|
|
|
def prepare(manifest: dict[str, Any]) -> str:
|
|
kind = manifest.get("kind")
|
|
if kind == "configFile":
|
|
config = _read_limited(str(manifest["path"]))
|
|
elif kind in {"vpnFile", "vpn"}:
|
|
link = _read_limited(str(manifest["path"]))
|
|
config = extract_vpn_config(decode_vpn_link(link), manifest.get("vpnContainer"))
|
|
elif kind == "settings":
|
|
config = render_settings(_decode_json_value(manifest.get("settings"), "settings"))
|
|
else:
|
|
raise ConfigError(f"unsupported manifest kind '{kind}'")
|
|
config = _apply_awg3_mtu_default(_normalize_headers(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
|
|
|
|
|
|
def import_source(text: str, container: str | None = None) -> tuple[str, dict[str, str]]:
|
|
"""A pasted .conf or vpn:// link as a normalized .conf, and what it says about itself.
|
|
|
|
For profiles added at runtime: hooks are refused whatever the source, since whoever may
|
|
add one need not be root. The name is the export's description, else its server's host.
|
|
"""
|
|
text = text.strip()
|
|
name = ""
|
|
if text.startswith("vpn://"):
|
|
data = decode_vpn_link(text)
|
|
config = _normalize_headers(extract_vpn_config(data, container))
|
|
for key in ("description", "name", "hostName"):
|
|
if isinstance(data.get(key), str) and data[key].strip():
|
|
name = data[key].strip()
|
|
break
|
|
elif "[interface]" in text.lower():
|
|
if container:
|
|
raise ConfigError("--container only applies to a vpn:// link")
|
|
config = _apply_awg3_mtu_default(_normalize_headers(text + "\n"))
|
|
else:
|
|
raise ConfigError("expected an AmneziaWG .conf or a vpn:// link")
|
|
validate_config(config)
|
|
endpoints = section_values(config, "peer", "endpoint")
|
|
endpoint = endpoints[0] if endpoints else ""
|
|
if not name and endpoint:
|
|
name = endpoint.rsplit(":", 1)[0].strip("[]")
|
|
return config, {"name": name, "endpoint": endpoint}
|
|
|
|
|
|
def write_private(path: str, content: str) -> None:
|
|
destination = Path(path)
|
|
destination.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
|
|
fd, temporary = tempfile.mkstemp(prefix=f".{destination.name}.", dir=destination.parent)
|
|
try:
|
|
os.fchmod(fd, 0o600)
|
|
with os.fdopen(fd, "w", encoding="utf-8") as handle:
|
|
handle.write(content)
|
|
handle.flush()
|
|
os.fsync(handle.fileno())
|
|
os.replace(temporary, destination)
|
|
except Exception:
|
|
try:
|
|
os.unlink(temporary)
|
|
except FileNotFoundError:
|
|
pass
|
|
raise
|
|
|
|
|
|
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",
|
|
type=int,
|
|
metavar="MARK",
|
|
help="render for an outbound-only interface: no routes or DNS, packets marked MARK",
|
|
)
|
|
parser.add_argument(
|
|
"--dns-out",
|
|
metavar="FILE",
|
|
help="also write the profile's DNS server addresses to FILE, one a line",
|
|
)
|
|
parser.add_argument(
|
|
"--wireproxy",
|
|
metavar="ADDRESS",
|
|
help="render for wireproxy, as a SOCKS5 listener on ADDRESS (packets marked with --outbound-fwmark)",
|
|
)
|
|
parser.add_argument(
|
|
"--fwmark",
|
|
type=int,
|
|
metavar="MARK",
|
|
help="render a global profile whose own packets carry MARK (awg-quick routes it with table MARK)",
|
|
)
|
|
parser.add_argument(
|
|
"--resolve-endpoints",
|
|
action="store_true",
|
|
help="write Endpoint names as the addresses they resolve to here",
|
|
)
|
|
parser.add_argument(
|
|
"--import",
|
|
dest="import_",
|
|
metavar="SOURCE",
|
|
help="normalize a .conf or vpn:// link (a path, or - for stdin), hooks refused",
|
|
)
|
|
parser.add_argument("--container", help="with --import: which container of a vpn:// export")
|
|
parser.add_argument(
|
|
"--describe",
|
|
action="store_true",
|
|
help="with --import: print the name and endpoint it carries as JSON",
|
|
)
|
|
parser.add_argument(
|
|
"--inspect",
|
|
metavar="CONFIG",
|
|
help="print the transport implementation, a probe address and the rekey interval",
|
|
)
|
|
args = parser.parse_args()
|
|
try:
|
|
if args.inspect 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.import_ is not None:
|
|
if args.manifest is not None or args.config is not None:
|
|
raise ConfigError("--import cannot be combined with --manifest or --config")
|
|
if args.output is None and not args.describe:
|
|
raise ConfigError("--import needs --output, --describe or both")
|
|
if args.import_ == "-":
|
|
text = sys.stdin.read(MAX_INPUT_BYTES + 1)
|
|
if len(text) > MAX_INPUT_BYTES:
|
|
raise ConfigError(f"input exceeds {MAX_INPUT_BYTES} bytes")
|
|
else:
|
|
text = _read_limited(args.import_)
|
|
config, described = import_source(text, args.container)
|
|
if args.output is not None:
|
|
write_private(args.output, config)
|
|
if args.describe:
|
|
print(json.dumps(described))
|
|
return 0
|
|
if args.container is not None or args.describe:
|
|
raise ConfigError("--container and --describe only apply to --import")
|
|
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.dns_out is not None:
|
|
# Before the outbound rendering drops them: per-app routing sends the app's DNS here.
|
|
write_private(args.dns_out, "".join(f"{server}\n" for server in dns_servers(config)))
|
|
if args.wireproxy is not None:
|
|
# The listener's login from the environment (wg-tunnel.nix): argv is public.
|
|
hop_password = os.environ.get("HOP_PASSWORD", "")
|
|
login = (os.environ.get("HOP_USER", "hop"), hop_password) if hop_password else None
|
|
config = as_wireproxy(config, args.wireproxy, args.outbound_fwmark, login)
|
|
elif args.outbound_fwmark is not None:
|
|
# A declared profile keeps its prefixes; one from a group-writable spool may not
|
|
# route a range of the host's into its tunnel.
|
|
shared = args.config is not None and _shared_directory(args.config)
|
|
config = as_outbound(config, args.outbound_fwmark, host_addresses=shared)
|
|
elif args.fwmark is not None:
|
|
config = _replace_interface_key(config, "FwMark", str(args.fwmark))
|
|
if args.wireproxy is None and args.config is not None and _shared_directory(args.config):
|
|
# A global profile from the group-writable spool, which root's awg-quick brings up.
|
|
config = _drop_privileged_listen_port(config)
|
|
if args.resolve_endpoints:
|
|
config = resolve_endpoints(config)
|
|
if args.wireproxy is None:
|
|
# What awg-quick runs, checked again after the rewrites above.
|
|
validate_config(config, bool(manifest.get("allowConfigHooks", False)))
|
|
write_private(args.output, config)
|
|
except (ConfigError, KeyError, OSError, UnicodeDecodeError, json.JSONDecodeError) as exc:
|
|
parser.exit(1, f"amneziawg-config: {exc}\n")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|