wg-ops/wgop_quick_client.py

107 lines
2.8 KiB
Python
Raw Normal View History

2021-07-26 21:32:23 +08:00
# -*- coding: utf-8 -*-
import os
import getpass
2021-08-23 01:31:21 +08:00
from wgop_common import load_config, save_config, base64_to_json
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
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["udp2raw_client"]["remote"], paste_config["server_pubkey"]))
break
while True:
udp_server_password = getpass.getpass('Tunnel Password: ').strip()
if not udp_server_password:
print("For security reasons, a udp2raw tunnel password is required. Try again.")
continue
if udp_server_password != getpass.getpass('Confirm Tunnel Password: ').strip():
print("Password mismatch. Try again.")
continue
break
paste_config["udp2raw_client"]["password"] = udp_server_password
if paste_config["suggest_allowed"]:
2021-07-31 03:06:58 +08:00
peer_allowed = input("Enter WireGuard Peer AllowedIPs (CIDR, Example: 10.0.0.0/24, default to {})\n> ".format(paste_config["suggest_allowed"])).strip()
2021-07-26 21:32:23 +08:00
if not peer_allowed:
peer_allowed = paste_config["suggest_allowed"]
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["server_pubkey"],
"allowed": peer_allowed,
"endpoint": "1",
"keepalive": peer_keepalive
}],
"udp2raw": {
2021-07-26 21:32:23 +08:00
"client": [paste_config["udp2raw_client"]],
2021-08-22 15:25:24 +08:00
"server": [],
"demuxer": []
}
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))