wg-ops/tool_generate.py

117 lines
3.8 KiB
Python
Raw Normal View History

2021-07-13 13:59:59 +08:00
# -*- coding: utf-8 -*-
2021-07-16 11:56:50 +08:00
import os
from tool_common import load_config, SimpleLogger
2021-07-13 13:59:59 +08:00
logger = SimpleLogger()
config = load_config()
if not config:
logger.error("No valid config found.")
exit(1)
2021-07-13 13:59:59 +08:00
2021-07-16 15:19:56 +08:00
if "version" not in config or int(config["version"]) < 1:
logger.warn("[WARN] Legacy version of config found. This may cause issues.")
2021-07-16 15:19:56 +08:00
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-16 15:19:56 +08:00
logger.info("Generating wireguard config...")
2021-07-17 19:51:29 +08:00
with open("local/{}.conf".format(config["interface"]), "w", encoding='utf-8') as f:
2021-07-13 13:59:59 +08:00
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"]:
2021-07-16 15:19:56 +08:00
udp_info = udp_clients[int(info["endpoint"]) - 1]
if udp_info["speeder"]["enable"]:
# WG --> Speeder
f.write("Endpoint = 127.0.0.1:{}\n".format(udp_info["speeder"]["port"]))
else:
# WG --> Tunnel
f.write("Endpoint = 127.0.0.1:{}\n".format(udp_info["port"]))
2021-07-13 13:59:59 +08:00
if info["keepalive"]:
2021-07-16 16:23:04 +08:00
f.write("PersistentKeepalive = {}\n".format(info["keepalive"]))
2021-07-13 13:59:59 +08:00
2021-07-16 11:56:50 +08:00
os.system("chmod 600 {}.conf".format(config["interface"]))
2021-07-13 14:06:48 +08:00
logger.info("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
2021-07-17 19:51:29 +08:00
cp local/{}.conf /etc/wireguard/
tmux new-session -s tunnel -d 'watch -n 1 wg'
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-16 15:19:56 +08:00
if info["speeder"]["enable"]:
# WG --> Speeder --> RawTunnel
speeder = info["speeder"]
f.write('''tmux new-window -t tunnel -d 'bin/speederv2_amd64 -c -l127.0.0.1:{} -r 127.0.0.1:{} -f{} --mode 0' \n'''.format(speeder["port"], info["port"], speeder["ratio"]))
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-16 15:19:56 +08:00
if info["speeder"]["enable"]:
# RawTunnel --> Speeder --> WG
speeder = info["speeder"]
f.write('''tmux new-window -t tunnel -d 'bin/speederv2_amd64 -s -l127.0.0.1:{} -r 127.0.0.1:{} -f{} --mode 0' \n'''.format(speeder["port"], config["listen"], speeder["ratio"]))
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"], speeder["port"], info["password"]))
else:
# RawTunnel --> WG
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"]))
# Enable BBR
f.write("sysctl net.core.default_qdisc=fq\n")
f.write("sysctl net.ipv4.tcp_congestion_control=bbr\n")
2021-07-13 15:16:10 +08:00
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
2021-07-17 19:51:29 +08:00
logger.info("Generating stop script...")
2021-07-17 19:51:29 +08:00
with open("stop.sh", "w", encoding='utf-8') as f:
f.write('''#!/bin/bash
set -e
wg-quick down {}
tmux kill-session -t tunnel
'''.format(config["interface"]))
os.system("chmod +x stop.sh")
logger.info("Generating restart script...")
with open("restart.sh", "w", encoding='utf-8') as f:
f.write('''#!/bin/bash
set -e
./stop.sh && ./start.sh
''')
os.system("chmod +x restart.sh")
2021-07-17 19:51:29 +08:00
logger.info('''[Done] Config generated. Before you run start.sh, besure to:
2021-07-16 11:56:50 +08:00
1. Disable SSH Server password login.
2. Enable UFW (or any other firewall)
Safety First.
''')