diff --git a/create.sh b/create.sh index b0d350b..3735cc4 100755 --- a/create.sh +++ b/create.sh @@ -1,10 +1,14 @@ #!/bin/bash +echo 'Detecting Public IP address...' +export WG_PUBLICIP=$(curl ident.me) + export WG_MYPRIK=$(wg genkey) export WG_MYPUBK=$(echo $WG_MYPRIK | wg pubkey) -export WG_PUBLICIP=$(curl ident.me) python3 tool_create.py python3 tool_generate.py chmod +x start.sh +chmod +x stop.sh +chmod +x restart.sh diff --git a/quick_create_client.sh b/quick_create_client.sh new file mode 100644 index 0000000..8691550 --- /dev/null +++ b/quick_create_client.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +export WG_MYPRIK=$(wg genkey) +export WG_MYPUBK=$(echo $WG_MYPRIK | wg pubkey) + +python3 tool_quick_client.py diff --git a/tool_common.py b/tool_common.py index 0c33515..411da41 100644 --- a/tool_common.py +++ b/tool_common.py @@ -56,7 +56,7 @@ def save_config(config, filename=None): f.write(content) except Exception: logger.error("Unable to save config: {}".format(traceback.format_exc())) - logger.info("Config: {}".format(content)) + logger.info("Config:\n{}".format(content)) def json_to_base64(content): diff --git a/tool_create.py b/tool_create.py index e03fa88..9925187 100755 --- a/tool_create.py +++ b/tool_create.py @@ -4,16 +4,14 @@ import getpass from tool_common import load_config, save_config, SimpleLogger, json_to_base64 -logger = SimpleLogger() - config = load_config() if config: - logger.warn("Valid config found. Creation of server is skipped.") + print("Valid config found. Creation of server is skipped.") exit(0) else: - logger.info("No config found. Start creating interactively.") + print("No config found. Start creating interactively.") -print("===== Choose Role =====") +print("====== Choose Role ======") op_mode = input("What will this node act as? (C)lient [S]erver [M]ixed: ").strip().lower() if not op_mode: @@ -172,22 +170,26 @@ save_config(config) if op_mode in ("s", "m"): + if ifip.endswith(".1"): + suggest_allowed = "{}.0/24".format('.'.join(ifip.split('.')[:-1])) + else: + suggest_allowed = ifip + print("===== Quick Import =====") for info in udp2raw_config["server"]: - target_config = { - "udp2raw": { - "client": [{ - "remote": "{}:{}".format(wg_public_ip, info["port"]), - "password": info["password"], - "port": "", - "speeder": info["speeder"] - }] + target_quick_config = { + "udp2raw_client": { + "remote": "{}:{}".format(wg_public_ip, info["port"]), + "password": "", + "port": "29100", + "speeder": info["speeder"] }, - "pubkey": wg_pubk + "server_pubkey": wg_pubk, + "suggest_allowed": suggest_allowed } - print("Connect to this server via tunnel at port {}: (credential included) \n".format(info["port"])) - print(" {}\n".format(json_to_base64(target_config))) + print("Connect to this server via tunnel at port {}: (credential excluded) \n".format(info["port"])) + print("#QCS#{}\n".format(json_to_base64(target_quick_config))) # Configure Peer @@ -207,17 +209,15 @@ while True: continue break - print(">>> Choose from following udp2raw clients <<<") if config["udp2raw"]["client"]: + print(">>> Choose from following udp2raw clients <<<") for index, client_info in enumerate(config["udp2raw"]["client"]): print("[{}] UDP2Raw Tunnel to Remote {}".format(index + 1, client_info["remote"])) - else: - print(" no client ") - peer_endpoint = input("Enter Wireguard Peer Endpoint (ID from tunnel list, keep empty on server side): ").strip() - if peer_endpoint: + peer_endpoint = input("Enter Wireguard Peer Endpoint (ID from list, default to 1): ").strip() or "1" peer_keepalive = input("Enter Wireguard Peer Keep Alive seconds (default to 30): ").strip() or "30" else: + peer_endpoint = "" peer_keepalive = "30" config["peers"].append({ diff --git a/tool_generate.py b/tool_generate.py index 20e5249..8e10a95 100644 --- a/tool_generate.py +++ b/tool_generate.py @@ -95,8 +95,6 @@ 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: @@ -105,8 +103,6 @@ set -e ./stop.sh && ./start.sh ''') -os.system("chmod +x restart.sh") - logger.info('''[Done] Config generated. Before you run start.sh, besure to: 1. Disable SSH Server password login. diff --git a/tool_quick_client.py b/tool_quick_client.py new file mode 100644 index 0000000..3bd1885 --- /dev/null +++ b/tool_quick_client.py @@ -0,0 +1,104 @@ +# -*- coding: utf-8 -*- +import os +import getpass +from tool_common import load_config, save_config, base64_to_json + + +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...") + +ifname = input("Input new wireguard interface name (wg0):").strip() or "wg0" +listen_port = input("Input new wireguard listen port (51820): ").strip() or "51820" +while True: + ifip = input("Input wireguard interface ip (Example: 10.0.0.1)\n> ").strip() + if not ifip: + print("You MUST set a valid wireguard interface IP. Try Again.") + 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"]: + peer_allowed = input("Enter Wireguard Peer AllowedIPs (CIDR, Example: 10.0.0.0/24, default to {})\n> ".format(paste_config["suggest_allowed"])).strip() + if not peer_allowed: + peer_allowed = paste_config["suggest_allowed"] +else: + while True: + peer_allowed = input("Enter Wireguard Peer AllowedIPs (CIDR, Example: 10.0.0.0/24)\n> ").strip() + if not peer_allowed: + print("Peer allowed ips required. Try Again.") + continue + break + + +peer_keepalive = input("Enter Wireguard Peer Keep Alive seconds (default to 30): ").strip() or "30" + + +# 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": [{ + "client": [paste_config["udp2raw_client"]], + "server": [] + }] +} + +print("Saving config...") +save_config(config) + +print(''' + +====== Your Wireguard Public Key ====== + +{} + +====== Your WireGuard IP Address ====== + +{} + +======================================= + +'''.format(os.getenv("WG_MYPUBK"), ifip))