wg-ops/wgop_quick_client.py

115 lines
3.1 KiB
Python
Raw Normal View History

2021-07-26 21:32:23 +08:00
# -*- coding: utf-8 -*-
import os
2021-08-23 01:31:21 +08:00
from wgop_common import load_config, save_config, base64_to_json
from wgop_common import UConfigController
2021-07-26 21:32:23 +08:00
config = load_config()
if config:
print("Valid config found. Creation of server is skipped.")
exit(0)
print("No valid config found, creating a default one...")
2021-07-31 03:06:58 +08:00
ifname = input("Input new WireGuard interface name (wg0): ").strip() or "wg0"
listen_port = input("Input new WireGuard listen port (51820): ").strip() or "51820"
2021-07-26 21:32:23 +08:00
while True:
2021-07-31 03:06:58 +08:00
ifip = input("Input WireGuard interface ip (Example: 10.0.0.1)\n> ").strip()
2021-07-26 21:32:23 +08:00
if not ifip:
2021-07-31 03:06:58 +08:00
print("You MUST set a valid WireGuard interface IP. Try Again.")
2021-07-26 21:32:23 +08:00
continue
break
ucontrol = UConfigController()
2021-07-26 21:32:23 +08:00
paste_config = {}
while True:
paste_temp = input("Paste Quick Setup: ").strip()
if not paste_temp.startswith("#QCS#"):
print("Config not valid. Try again.")
continue
paste_config = base64_to_json(paste_temp.replace("#QCS#", ""))
print("Config imported. Server: {} with public key: {}{}".format(
paste_config["remote"], paste_config["pubkey"],
" and speeders enabled" if paste_config["ratio"] else ""))
2021-07-26 21:32:23 +08:00
break
if paste_config["ratio"]:
speeder_info = ucontrol.new_client_speeder(None, paste_config["ratio"])
else:
speeder_info = None
2021-07-26 21:32:23 +08:00
is_enable_balance = input("Enable Load Balance? [y/N]: ").strip()
if is_enable_balance and is_enable_balance.lower() in ('y', 'yes'):
balance_count = input("Enter Balance Underlay Connection counts (default to 10): ").strip() or "10"
balance_count = int(balance_count)
2021-07-26 21:32:23 +08:00
if balance_count > 1:
balancer_info = ucontrol.new_demuxer(None, balance_count)
else:
print("[WARN] Only one target, skipped balancer creation.")
balancer_info = None
else:
balancer_info = None
ucontrol.add_client(paste_config["remote"], paste_config["password"], None, speeder_info, balancer_info, no_hash=True)
if paste_config["allowed"]:
peer_allowed = input("Enter WireGuard Peer AllowedIPs (CIDR, Example: 10.0.0.0/24, default to {})\n> ".format(paste_config["allowed"])).strip()
2021-07-26 21:32:23 +08:00
if not peer_allowed:
peer_allowed = paste_config["allowed"]
2021-07-26 21:32:23 +08:00
else:
while True:
2021-07-31 03:06:58 +08:00
peer_allowed = input("Enter WireGuard Peer AllowedIPs (CIDR, Example: 10.0.0.0/24)\n> ").strip()
2021-07-26 21:32:23 +08:00
if not peer_allowed:
print("Peer allowed ips required. Try Again.")
continue
break
2021-07-31 03:06:58 +08:00
peer_keepalive = input("Enter WireGuard Peer Keep Alive seconds (default to 30): ").strip() or "30"
2021-07-26 21:32:23 +08:00
# Generate Config
config = {
"version": 1,
"mode": "c",
"prikey": os.getenv("WG_MYPRIK"),
"pubkey": os.getenv("WG_MYPUBK"),
"mtu": "1000",
"interface": ifname,
"ip": ifip,
"listen": listen_port,
"peers": [{
"pubkey": paste_config["pubkey"],
2021-07-26 21:32:23 +08:00
"allowed": peer_allowed,
"endpoint": "1",
"keepalive": peer_keepalive
}],
"udp2raw": ucontrol.udp2raw_config
2021-07-26 21:32:23 +08:00
}
print("Saving config...")
save_config(config)
print('''
2021-07-31 03:06:58 +08:00
====== Your WireGuard Public Key ======
2021-07-26 21:32:23 +08:00
{}
====== Your WireGuard IP Address ======
{}
=======================================
'''.format(os.getenv("WG_MYPUBK"), ifip))