douyinWEB/scripts/_setup_wg_easy_pve.py
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 22:42:40 -05:00

134 lines
3.9 KiB
Python

# -*- coding: utf-8 -*-
"""Install wg-easy on US VPS and connect PVE Ubuntu as a client (split tunnel 10.8.0.0/24)."""
from __future__ import annotations
import json
import sys
import time
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
VPS = "107.173.30.245"
ROOT_PASS = "Xiao4669805"
ERIC_PASS = "4669805"
WG_UI_PASS = "Xiao4669805@"
WG_HOST = "107.173.30.245"
INSTALL_VPS = r"""
set -euo pipefail
mkdir -p /opt/wg-easy
cat >/opt/wg-easy/docker-compose.yml <<'EOF'
services:
wg-easy:
image: ghcr.io/wg-easy/wg-easy:14
container_name: wg-easy
restart: unless-stopped
environment:
LANG: en
WG_HOST: 107.173.30.245
PASSWORD: "Xiao4669805@"
PORT: "51821"
WG_PORT: "51820"
WG_DEFAULT_ADDRESS: 10.8.0.x
WG_DEFAULT_DNS: ""
WG_ALLOWED_IPS: 10.8.0.0/24
WG_PERSISTENT_KEEPALIVE: "25"
WG_MTU: "1280"
volumes:
- /opt/wg-easy/data:/etc/wireguard
ports:
- "51820:51820/udp"
- "127.0.0.1:51821:51821/tcp"
cap_add:
- NET_ADMIN
- SYS_MODULE
sysctls:
net.ipv4.ip_forward: 1
net.ipv4.conf.all.src_valid_mark: 1
EOF
cd /opt/wg-easy
docker compose pull
docker compose up -d
sleep 4
docker ps --filter name=wg-easy --format '{{.Names}} {{.Status}} {{.Ports}}'
"""
CREATE_PEER = r"""
set -euo pipefail
# login
rm -f /tmp/wg.ck
curl -sS -c /tmp/wg.ck -X POST 'http://127.0.0.1:51821/api/session' \
-H 'Content-Type: application/json' \
-d '{"password":"Xiao4669805@"}' -w '\nhttp:%{http_code}\n'
# list
curl -sS -b /tmp/wg.ck 'http://127.0.0.1:51821/api/wireguard/client' -o /tmp/wg-clients.json
python3 - <<'PY'
import json,urllib.request
from pathlib import Path
clients=json.loads(Path('/tmp/wg-clients.json').read_text() or '[]')
print('existing', [(c.get('name'), c.get('id'), c.get('address')) for c in clients] if isinstance(clients, list) else clients)
PY
# create if missing
python3 - <<'PY'
import json, urllib.request, http.cookiejar
from pathlib import Path
cj=http.cookiejar.MozillaCookieJar('/tmp/wg.ck')
cj.load(ignore_discard=True, ignore_expires=True)
opener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
raw=Path('/tmp/wg-clients.json').read_text() or '[]'
try:
clients=json.loads(raw)
except Exception:
clients=[]
if not isinstance(clients, list):
print('clients parse', raw[:300])
clients=[]
found=next((c for c in clients if c.get('name')=='pve-ubuntu'), None)
if not found:
req=urllib.request.Request(
'http://127.0.0.1:51821/api/wireguard/client',
data=json.dumps({'name':'pve-ubuntu'}).encode(),
headers={'Content-Type':'application/json'},
method='POST',
)
with opener.open(req, timeout=20) as r:
found=json.loads(r.read().decode())
print('created', found.get('id'), found.get('address'))
else:
print('reuse', found.get('id'), found.get('address'))
cid=found.get('id')
req=urllib.request.Request(f'http://127.0.0.1:51821/api/wireguard/client/{cid}/configuration')
with opener.open(req, timeout=20) as r:
cfg=r.read().decode()
Path('/tmp/pve-ubuntu.conf').write_text(cfg)
print('---CONF---')
print(cfg)
print('---END---')
print('ADDRESS', found.get('address'))
PY
"""
def main() -> None:
v = paramiko.SSHClient()
v.set_missing_host_key_policy(paramiko.AutoAddPolicy())
v.connect(VPS, 22, "root", ROOT_PASS, timeout=40, allow_agent=False, look_for_keys=False)
print("=== install wg-easy ===")
_, o, e = v.exec_command(INSTALL_VPS, timeout=180)
print(o.read().decode("utf-8", "replace"))
err = e.read().decode("utf-8", "replace")
if err.strip():
print("STDERR", err[-2000:])
print("=== create peer ===")
_, o, e = v.exec_command(CREATE_PEER, timeout=60)
print(o.read().decode("utf-8", "replace"))
err = e.read().decode("utf-8", "replace")
if err.strip():
print("STDERR", err[-1500:])
v.close()
if __name__ == "__main__":
main()