Direkt zum Hauptinhalt

Site2Site Tunnel mit nur einer Öffentlichen statischen ip auf einer Seite

Beschreibung:

Es gibt Situationen da möchte man zwei Netzwerke miteinander verbinden.
hier zu ein kleines Script das zwei Wireguard configs erstellt.

Das Script:

nano make_wg_s2s.py

Inhalt

#!/usr/bin/env python3
import argparse
import base64
import ipaddress
import os
import shutil
import subprocess
import sys
from typing import List, Optional, Tuple

# -------- Key generation helpers --------

def have_cmd(cmd: str) -> bool:
    return shutil.which(cmd) is not None

def gen_keys_with_wg() -> Tuple[str, str]:
    """Generate (private, public) using wg if available."""
    try:
        priv = subprocess.check_output(["wg", "genkey"], text=True).strip()
        pub = subprocess.check_output(["wg", "pubkey"], input=priv, text=True).strip()
        if not (priv and pub):
            raise RuntimeError("wg returned empty key(s)")
        return priv, pub
    except Exception as e:
        raise RuntimeError(f"wg keygen failed: {e}")

def gen_keys_with_pynacl() -> Tuple[str, str]:
    """Generate (private, public) using PyNaCl (or cryptography) as fallback."""
    try:
        # Try PyNaCl first
        from nacl.public import PrivateKey
        sk = PrivateKey.generate()
        priv_b = bytes(sk._private_key)  # 32 bytes
        pub_b = bytes(sk.public_key._public_key)  # 32 bytes
        priv = base64.b64encode(priv_b).decode()
        pub = base64.b64encode(pub_b).decode()
        return priv, pub
    except Exception:
        # Try cryptography as another fallback
        try:
            from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey
            from cryptography.hazmat.primitives import serialization
            sk = X25519PrivateKey.generate()
            priv_b = sk.private_bytes(
                encoding=serialization.Encoding.Raw,
                format=serialization.PrivateFormat.Raw,
                encryption_algorithm=serialization.NoEncryption(),
            )
            pub_b = sk.public_key().public_bytes(
                encoding=serialization.Encoding.Raw,
                format=serialization.PublicFormat.Raw
            )
            priv = base64.b64encode(priv_b).decode()
            pub = base64.b64encode(pub_b).decode()
            return priv, pub
        except Exception as e2:
            raise RuntimeError(
                "Keygen fallback failed. Install 'wireguard-tools' or 'pynacl' or 'cryptography'. "
                f"Details: {e2}"
            )

def gen_keypair() -> Tuple[str, str]:
    if have_cmd("wg"):
        return gen_keys_with_wg()
    return gen_keys_with_pynacl()

# -------- Validation helpers --------

def parse_ip(addr: str, allow_v6: bool) -> ipaddress._BaseAddress:
    ip = ipaddress.ip_address(addr)
    if ip.version == 6 and not allow_v6:
        raise ValueError("IPv6 angegeben, aber --ipv6 nicht gesetzt.")
    return ip

def parse_cidr_list(text: str, allow_v6: bool) -> List[str]:
    nets = []
    for token in [t.strip() for t in text.split(",") if t.strip()]:
        net = ipaddress.ip_network(token, strict=False)
        if net.version == 6 and not allow_v6:
            raise ValueError("IPv6-Netz angegeben, aber --ipv6 nicht gesetzt.")
        nets.append(str(net))
    return nets

def ensure_single_host_cidr(ip: str) -> str:
    """Return host CIDR (/32 or /128) for a single IP."""
    ipobj = ipaddress.ip_address(ip)
    return f"{ip}/{32 if ipobj.version == 4 else 128}"

# -------- Prompt helpers --------

def prompt(msg: str, default: Optional[str]=None, required: bool=True) -> str:
    suffix = f" [{default}]" if default else ""
    while True:
        val = input(f"{msg}{suffix}: ").strip()
        if not val and default is not None:
            return default
        if val or not required:
            return val
        print("Bitte Wert eingeben.")

# -------- Core --------

def build_args() -> argparse.Namespace:
    p = argparse.ArgumentParser(
        description="Erstellt WireGuard Site-to-Site Configs (eine Seite mit Public Endpoint)."
    )
    p.add_argument("--name", help="Projektname (Verzeichnis wird angelegt)")
    p.add_argument("--public-endpoint", help="Domain oder IP der öffentlichen Seite (z.B. vpn.example.com)")
    p.add_argument("--public-port", type=int, help="UDP-Port der öffentlichen Seite (z.B. 51820)")
    p.add_argument("--public-wg-ip4", help="WG-IPv4 der öffentlichen Seite (z.B. 10.10.0.1)")
    p.add_argument("--private-wg-ip4", help="WG-IPv4 der privaten Seite (z.B. 10.10.0.2)")
    p.add_argument("--public-lans", help="Komma-Liste LAN-Netze hinter öffentlicher Seite (z.B. 192.168.10.0/24,192.168.11.0/24)")
    p.add_argument("--private-lans", help="Komma-Liste LAN-Netze hinter privater Seite")
    p.add_argument("--ipv6", action="store_true", help="IPv6 zusätzlich konfigurieren")
    p.add_argument("--public-wg-ip6", help="WG-IPv6 der öffentlichen Seite (z.B. fd00:10:10::1)")
    p.add_argument("--private-wg-ip6", help="WG-IPv6 der privaten Seite (z.B. fd00:10:10::2)")
    return p.parse_args()

def main():
    args = build_args()

    # Collect inputs (CLI or interactive)
    proj = args.name or prompt("Projektname")
    base_dir = os.path.abspath(proj)
    if os.path.exists(base_dir):
        print(f"Projekt '{proj}' gibt’s schon – nichts überschrieben.", file=sys.stderr)
        sys.exit(1)

    public_endpoint = args.public_endpoint or prompt("Public Endpoint (Domain oder IP)")
    public_port = args.public_port or int(prompt("Public UDP-Port", default="51820"))

    # WG IPv4
    public_wg_ip4 = args.public_wg_ip4 or prompt("WG-IPv4 der öffentlichen Seite (z.B. 10.10.0.1)")
    private_wg_ip4 = args.private_wg_ip4 or prompt("WG-IPv4 der privaten Seite (z.B. 10.10.0.2)")

    # Optional LANs
    public_lans = []
    private_lans = []
    if args.public_lans:
        public_lans = parse_cidr_list(args.public_lans, allow_v6=args.ipv6)
    else:
        val = prompt("LAN-Netze hinter öffentlicher Seite (Komma, leer für keine)", required=False)
        if val:
            public_lans = parse_cidr_list(val, allow_v6=args.ipv6)

    if args.private_lans:
        private_lans = parse_cidr_list(args.private_lans, allow_v6=args.ipv6)
    else:
        val = prompt("LAN-Netze hinter privater Seite (Komma, leer für keine)", required=False)
        if val:
            private_lans = parse_cidr_list(val, allow_v6=args.ipv6)

    # IPv6 optional
    v6_enabled = bool(args.ipv6)
    public_wg_ip6 = None
    private_wg_ip6 = None
    if v6_enabled:
        public_wg_ip6 = args.public_wg_ip6 or prompt("WG-IPv6 der öffentlichen Seite (fd00::/8 o.ä.)")
        private_wg_ip6 = args.private_wg_ip6 or prompt("WG-IPv6 der privaten Seite (fd00::/8 o.ä.)")

    # Validate IPs
    try:
        parse_ip(public_wg_ip4, allow_v6=False)
        parse_ip(private_wg_ip4, allow_v6=False)
        if v6_enabled:
            parse_ip(public_wg_ip6, allow_v6=True)
            parse_ip(private_wg_ip6, allow_v6=True)
    except Exception as e:
        print(f"IP-Fehler: {e}", file=sys.stderr)
        sys.exit(2)

    # Prepare dirs
    pub_dir = os.path.join(base_dir, "public")
    prv_dir = os.path.join(base_dir, "private")
    os.makedirs(pub_dir, exist_ok=False)
    os.makedirs(prv_dir, exist_ok=False)

    # Keys
    print("Erzeuge Schlüsselpaare…")
    server_priv, server_pub = gen_keypair()   # öffentliche Seite
    client_priv, client_pub = gen_keypair()   # private Seite

    # Build addresses & allowed IPs
    iface_addrs_public = [f"{public_wg_ip4}/32"]
    iface_addrs_private = [f"{private_wg_ip4}/32"]
    peer_allowed_from_client = [ensure_single_host_cidr(public_wg_ip4)]  # client -> server host
    peer_allowed_from_server = [ensure_single_host_cidr(private_wg_ip4)] # server -> client host

    if v6_enabled:
        iface_addrs_public.append(f"{public_wg_ip6}/128")
        iface_addrs_private.append(f"{private_wg_ip6}/128")
        peer_allowed_from_client.append(ensure_single_host_cidr(public_wg_ip6))
        peer_allowed_from_server.append(ensure_single_host_cidr(private_wg_ip6))

    # Add LANs if any
    if private_lans:
        peer_allowed_from_server.extend(private_lans)
    if public_lans:
        peer_allowed_from_client.extend(public_lans)

    # Remove duplicates while preserving order
    def uniq(seq: List[str]) -> List[str]:
        seen = set()
        out = []
        for x in seq:
            if x not in seen:
                seen.add(x)
                out.append(x)
        return out

    peer_allowed_from_client = uniq(peer_allowed_from_client)
    peer_allowed_from_server = uniq(peer_allowed_from_server)

    # Render configs
    public_conf = f"""# WireGuard - Öffentliche Seite (Server)
# Projekt: {proj}
# Hinweise:
#  - IPv4-Forwarding aktivieren: sysctl -w net.ipv4.ip_forward=1
#  - (Optional) IPv6-Forwarding:  sysctl -w net.ipv6.conf.all.forwarding=1
#  - Firewall-/Routenregeln entsprechend den LANs setzen.

[Interface]
Address = {", ".join(iface_addrs_public)}
ListenPort = {public_port}
PrivateKey = {server_priv}
# SaveConfig = false

# Peer: Private Seite (hinter NAT)
[Peer]
PublicKey = {client_pub}
AllowedIPs = {", ".join(peer_allowed_from_server)}
# Optional sinnvoll: damit Tunnel offen bleibt (bei NAT):
PersistentKeepalive = 25
"""

    private_conf = f"""# WireGuard - Private Seite (Client hinter NAT)
# Projekt: {proj}
# Hinweise:
#  - Diese Seite initiiert die Verbindung zur öffentlichen Seite.
#  - Endpoint muss von hier erreichbar sein.

[Interface]
Address = {", ".join(iface_addrs_private)}
PrivateKey = {client_priv}
# SaveConfig = false

# Peer: Öffentliche Seite
[Peer]
PublicKey = {server_pub}
Endpoint = {public_endpoint}:{public_port}
AllowedIPs = {", ".join(peer_allowed_from_client)}
# Wichtig bei NAT, damit UDP-Mapping frisch bleibt:
PersistentKeepalive = 25
"""

    # Write files
    with open(os.path.join(pub_dir, "wg0.conf"), "w") as f:
        f.write(public_conf)
    with open(os.path.join(prv_dir, "wg0.conf"), "w") as f:
        f.write(private_conf)

    # README
    readme = f"""# {proj} – WireGuard Site-to-Site

Dieses Projekt erzeugt zwei Konfigurationen:

- `public/wg0.conf`  → Öffentliche Seite (ListenPort: {public_port}, Endpoint: {public_endpoint})
- `private/wg0.conf` → Private Seite (hinter NAT; verbindet aktiv zum Endpoint)

## Start (Beispiele)
Auf beiden Hosts (mit root oder sudo):

```bash
wg-quick up wg0

## Tipps
- IPv4 Forwarding:
    sysctl -w net.ipv4.ip_forward=1
- IPv6 Forwarding (falls genutzt):
    sysctl -w net.ipv6.conf.all.forwarding=1
- Routen/Firewall: Sicherstellen, dass jeweils die LANs geroutet/erlaubt sind:
  - Öffentliche Seite erlaubt/kennt: {", ".join(public_lans) if public_lans else "(keine angegeben)"}
  - Private Seite erlaubt/kennt: {", ".join(private_lans) if private_lans else "(keine angegeben)"}
"""

Verwendung:

1) **Interaktiv** (alles wird abgefragt)
python3 make_wg_s2s.py

2) **Parameter gesteuert für ipv4**
python3 make_wg_s2s.py \
  --name firma-tunnel \
  --public-endpoint vpn.example.com \
  --public-port 51820 \
  --public-wg-ip4 10.10.0.1 \
  --private-wg-ip4 10.10.0.2 \
  --public-lans 192.168.10.0/24 \
  --private-lans 192.168.20.0/24

3) **Parameter gesteuert für ipv6**
python3 make_wg_s2s.py \
  --name firma-v6 \
  --public-endpoint v6.example.com \
  --public-port 51820 \
  --public-wg-ip4 10.10.0.1 \
  --private-wg-ip4 10.10.0.2 \
  --ipv6 \
  --public-wg-ip6 fd00:10:10::1 \
  --private-wg-ip6 fd00:10:10::2 \
  --public-lans 192.168.10.0/24,fd00:aaaa::/64 \
  --private-lans 192.168.20.0/24,fd00:bbbb::/64