Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /mcp /vpn-killswitch /wg_manager.py
| #!/usr/bin/env python3 | |
| """ | |
| WireGuard Manager – Konfiguration + Rotation + Healthcheck. | |
| Usage: | |
| python wg_manager.py status – Anzeigen | |
| python wg_manager.py rotate – Interface bouncen | |
| python wg_manager.py newpeer <name> – Neuen Peer generieren | |
| python wg_manager.py up – Interface starten | |
| python wg_manager.py down – Interface stoppen | |
| """ | |
| import json | |
| import os | |
| import subprocess | |
| import sys | |
| import time | |
| WG_DIR = os.environ.get("KS_WG_DIR", "/etc/wireguard") | |
| WG_IFACE = os.environ.get("KS_WG_IFACE", "wg0") | |
| def _run(cmd: list[str], timeout: int = 30) -> dict: | |
| try: | |
| r = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout) | |
| return {"ok": r.returncode == 0, "stdout": r.stdout, "stderr": r.stderr, "code": r.returncode} | |
| except subprocess.TimeoutExpired: | |
| return {"ok": False, "stdout": "", "stderr": "Timeout", "code": -1} | |
| except FileNotFoundError: | |
| return {"ok": False, "stdout": "", "stderr": f"Command not found: {cmd[0]}", "code": -1} | |
| except Exception as e: | |
| return {"ok": False, "stdout": "", "stderr": str(e), "code": -1} | |
| def status() -> dict: | |
| result = _run(["wg", "show", WG_IFACE]) | |
| if not result["ok"]: | |
| return {"status": "down", "interface": WG_IFACE} | |
| peers = [] | |
| for line in result["stdout"].split("\n"): | |
| if line.strip().startswith("peer:"): | |
| peers.append({"pubkey": line.split()[1]}) | |
| elif "endpoint:" in line and peers: | |
| peers[-1]["endpoint"] = line.split()[-1] | |
| elif "latest handshake:" in line and peers: | |
| peers[-1]["handshake"] = " ".join(line.split()[2:]) | |
| elif "transfer:" in line and peers: | |
| peers[-1]["transfer"] = " ".join(line.split()[1:]) | |
| # Get public IP to verify tunnel | |
| ip_result = _run(["curl", "-s", "--max-time", "5", "https://ifconfig.me"]) | |
| public_ip = ip_result["stdout"].strip() if ip_result["ok"] else "unknown" | |
| return { | |
| "status": "up", | |
| "interface": WG_IFACE, | |
| "public_ip": public_ip, | |
| "peers": peers, | |
| "peer_count": len(peers), | |
| } | |
| def up() -> dict: | |
| cfg = f"{WG_DIR}/{WG_IFACE}.conf" | |
| if not os.path.exists(cfg): | |
| return {"ok": False, "error": f"Config not found: {cfg}"} | |
| result = _run(["wg-quick", "up", WG_IFACE], timeout=30) | |
| time.sleep(2) | |
| return {**result, "message": f"WireGuard {WG_IFACE} up"} | |
| def down() -> dict: | |
| result = _run(["wg-quick", "down", WG_IFACE], timeout=15) | |
| return {**result, "message": f"WireGuard {WG_IFACE} down"} | |
| def rotate() -> dict: | |
| down() | |
| time.sleep(2) | |
| return up() | |
| def new_peer(name: str) -> dict: | |
| """Generate a new WireGuard keypair and print config.""" | |
| priv = _run(["wg", "genkey"]) | |
| if not priv["ok"]: | |
| return priv | |
| privkey = priv["stdout"].strip() | |
| pub = _run(["wg", "pubkey"], timeout=5) | |
| pub.stdin = privkey | |
| # Can't do pipe this way, use subprocess differently | |
| pub_result = subprocess.run( | |
| ["wg", "pubkey"], | |
| input=privkey.encode(), | |
| capture_output=True, | |
| text=True, | |
| timeout=5, | |
| ) | |
| pubkey = pub_result.stdout.strip() | |
| return { | |
| "ok": True, | |
| "name": name, | |
| "private_key": privkey, | |
| "public_key": pubkey, | |
| "config_example": f""" | |
| [Peer] | |
| # {name} | |
| PublicKey = {pubkey} | |
| AllowedIPs = 10.0.0.{hash(name) % 255}/32 | |
| """, | |
| } | |
| def main(): | |
| if len(sys.argv) < 2: | |
| print(__doc__) | |
| sys.exit(1) | |
| cmd = sys.argv[1] | |
| if cmd == "status": | |
| print(json.dumps(status(), indent=2)) | |
| elif cmd == "up": | |
| print(json.dumps(up(), indent=2)) | |
| elif cmd == "down": | |
| print(json.dumps(down(), indent=2)) | |
| elif cmd == "rotate": | |
| print(json.dumps(rotate(), indent=2)) | |
| elif cmd == "newpeer": | |
| name = sys.argv[2] if len(sys.argv) > 2 else f"peer-{int(time.time())}" | |
| print(json.dumps(new_peer(name), indent=2)) | |
| else: | |
| print(f"Unknown command: {cmd}") | |
| print(__doc__) | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 4.11 kB
- Xet hash:
- eb7f6d1ceb86ef248576b228eb6149060c9be52acfa9cfb779dd44370b85ce78
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.