2021-07-13 13:59:59 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import json
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open("config.json") as f:
|
|
|
|
content = f.read()
|
|
|
|
config = json.loads(content)
|
|
|
|
except Exception:
|
|
|
|
print(traceback.format_exc())
|
|
|
|
print("[ERROR] No valid config found.")
|
|
|
|
|
2021-07-13 15:16:10 +08:00
|
|
|
op_mode = config["mode"]
|
2021-07-13 13:59:59 +08:00
|
|
|
udp_clients = config["udp2raw"]["client"]
|
|
|
|
udp_servers = config["udp2raw"]["server"]
|
|
|
|
|
2021-07-13 14:06:48 +08:00
|
|
|
print("Generating wireguard config...")
|
2021-07-13 13:59:59 +08:00
|
|
|
with open("{}.conf".format(config["interface"]), "w", encoding='utf-8') as f:
|
|
|
|
f.write('''[Interface]
|
|
|
|
Address = {}
|
|
|
|
PrivateKey = {}
|
|
|
|
ListenPort = {}
|
|
|
|
MTU = {}
|
|
|
|
'''.format(config["ip"], config["prikey"], config["listen"], config["mtu"]))
|
|
|
|
|
|
|
|
for info in config["peers"]:
|
|
|
|
f.write('''[Peer]
|
|
|
|
PublicKey = {}
|
|
|
|
AllowedIPs = {}
|
|
|
|
'''.format(info["pubkey"], info["allowed"]))
|
|
|
|
if info["endpoint"]:
|
|
|
|
f.write("Endpoint = 127.0.0.1:{}\n".format(udp_clients[int(info["endpoint"]) - 1]["port"]))
|
|
|
|
if info["keepalive"]:
|
|
|
|
f.write("PersistentKeepalive = {}".format(info["keepalive"]))
|
|
|
|
|
2021-07-13 14:06:48 +08:00
|
|
|
|
|
|
|
print("Generating start script...")
|
2021-07-13 13:59:59 +08:00
|
|
|
with open("start.sh", "w", encoding='utf-8') as f:
|
|
|
|
f.write('''#!/bin/bash
|
2021-07-13 14:06:48 +08:00
|
|
|
set -e
|
2021-07-13 14:03:49 +08:00
|
|
|
|
|
|
|
cp {}.conf /etc/wireguard/
|
2021-07-13 13:59:59 +08:00
|
|
|
tmux new-session -s tunnel -d
|
2021-07-13 14:03:49 +08:00
|
|
|
'''.format(config["interface"]))
|
2021-07-13 13:59:59 +08:00
|
|
|
for info in udp_clients:
|
2021-07-13 15:16:10 +08:00
|
|
|
f.write('''tmux new-window -t tunnel -d 'bin/udp2raw_amd64 -c -l127.0.0.1:{} -r{} -k "{}" --raw-mode faketcp -a' \n'''.format(info["port"], info["remote"], info["password"]))
|
2021-07-13 13:59:59 +08:00
|
|
|
|
|
|
|
for info in udp_servers:
|
2021-07-13 15:16:10 +08:00
|
|
|
f.write('''tmux new-window -t tunnel -d 'bin/udp2raw_amd64 -s -l0.0.0.0:{} -r 127.0.0.1:{} -k "{}" --raw-mode faketcp -a' \n'''.format(info["port"], config["listen"], info["password"]))
|
|
|
|
|
|
|
|
if op_mode in ("s", "m"):
|
|
|
|
f.write("sysctl net.ipv4.ip_forward=1\n")
|
2021-07-13 13:59:59 +08:00
|
|
|
|
2021-07-13 14:03:49 +08:00
|
|
|
f.write('''wg-quick up {}
|
|
|
|
tmux attach-session -t tunnel
|
|
|
|
'''.format(config["interface"]))
|
2021-07-13 15:16:10 +08:00
|
|
|
|
|
|
|
print("[OK] Config generated. Be sure to configure and enable UFW (or any other firewall) before start.")
|