Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / vpn / tools / generate_sample_mavpn.py
generate_sample_mavpn.py85 lines · 2.7 KiBRaw
| 1 | #!/usr/bin/env python3 |
| 2 | """Generate a demo .mavpn bundle with fictional servers. |
| 3 | |
| 4 | Usage: |
| 5 | python3 vpn/tools/generate_sample_mavpn.py [output.mavpn] |
| 6 | |
| 7 | Defaults to vpn/tools/sample.mavpn. Also used to (re)generate the parser |
| 8 | test fixture at vpn/src/test/resources/mavpn/demo.mavpn: |
| 9 | |
| 10 | python3 vpn/tools/generate_sample_mavpn.py vpn/src/test/resources/mavpn/demo.mavpn |
| 11 | |
| 12 | Every endpoint is under example.invalid and every key is a clearly fake, |
| 13 | EXAMPLE-labelled placeholder derived from a fixed byte pattern. Never use |
| 14 | this bundle to connect anywhere. ZIP timestamps are pinned so the output is |
| 15 | byte-for-byte reproducible. |
| 16 | """ |
| 17 | |
| 18 | import base64 |
| 19 | import json |
| 20 | import sys |
| 21 | import zipfile |
| 22 | |
| 23 | FIXED_DATE = (2020, 1, 1, 0, 0, 0) |
| 24 | |
| 25 | |
| 26 | def example_key(seed: int) -> str: |
| 27 | """A well-formed 44-char WireGuard-shaped key that is obviously fake.""" |
| 28 | raw = bytes((seed + i) % 256 for i in range(32)) |
| 29 | return base64.b64encode(raw).decode() |
| 30 | |
| 31 | |
| 32 | def conf(server_id: str, host: str, seed: int) -> str: |
| 33 | return ( |
| 34 | "# EXAMPLE fake WireGuard config for the .mavpn demo bundle.\n" |
| 35 | "# EXAMPLE keys only - not real, never use to connect.\n" |
| 36 | "[Interface]\n" |
| 37 | f"PrivateKey = {example_key(seed)}\n" |
| 38 | "Address = 10.66.0.2/32\n" |
| 39 | "DNS = 1.1.1.1\n" |
| 40 | "\n" |
| 41 | "[Peer]\n" |
| 42 | f"PublicKey = {example_key(seed + 100)}\n" |
| 43 | "AllowedIPs = 0.0.0.0/0, ::/0\n" |
| 44 | f"Endpoint = {host}:51820\n" |
| 45 | "PersistentKeepalive = 25\n" |
| 46 | ) |
| 47 | |
| 48 | |
| 49 | SERVERS = [ |
| 50 | ("de-fra-1", "DE", "Frankfurt", "fra1.example.invalid", 11), |
| 51 | ("de-ber-1", "DE", "Berlin", "ber1.example.invalid", 22), |
| 52 | ("us-nyc-1", "US", "New York", "nyc1.example.invalid", 33), |
| 53 | ] |
| 54 | |
| 55 | |
| 56 | def main() -> None: |
| 57 | out = sys.argv[1] if len(sys.argv) > 1 else "vpn/tools/sample.mavpn" |
| 58 | manifest = { |
| 59 | "format": "mavpn/1", |
| 60 | "provider": "EXAMPLE Demo VPN (fictional)", |
| 61 | "servers": [ |
| 62 | { |
| 63 | "id": server_id, |
| 64 | "country": country, |
| 65 | "city": city, |
| 66 | "file": f"servers/{server_id}.conf", |
| 67 | "endpoint": f"{host}:51820", |
| 68 | } |
| 69 | for server_id, country, city, host, _ in SERVERS |
| 70 | ], |
| 71 | } |
| 72 | with zipfile.ZipFile(out, "w", zipfile.ZIP_DEFLATED) as bundle: |
| 73 | def write(name: str, text: str) -> None: |
| 74 | info = zipfile.ZipInfo(name, FIXED_DATE) |
| 75 | info.compress_type = zipfile.ZIP_DEFLATED |
| 76 | bundle.writestr(info, text) |
| 77 | |
| 78 | write("manifest.json", json.dumps(manifest, indent=2) + "\n") |
| 79 | for server_id, _, _, host, seed in SERVERS: |
| 80 | write(f"servers/{server_id}.conf", conf(server_id, host, seed)) |
| 81 | print(f"wrote {out}") |
| 82 | |
| 83 | |
| 84 | if __name__ == "__main__": |
| 85 | main() |