mirror of
https://github.com/Kiritow/wg-ops.git
synced 2024-03-22 13:11:37 +08:00
Add Quick Connect (Paste & Go)
This commit is contained in:
parent
895db3a3b9
commit
34c80589c8
|
@ -1,10 +1,14 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
echo 'Detecting Public IP address...'
|
||||||
|
export WG_PUBLICIP=$(curl ident.me)
|
||||||
|
|
||||||
export WG_MYPRIK=$(wg genkey)
|
export WG_MYPRIK=$(wg genkey)
|
||||||
export WG_MYPUBK=$(echo $WG_MYPRIK | wg pubkey)
|
export WG_MYPUBK=$(echo $WG_MYPRIK | wg pubkey)
|
||||||
export WG_PUBLICIP=$(curl ident.me)
|
|
||||||
|
|
||||||
python3 tool_create.py
|
python3 tool_create.py
|
||||||
python3 tool_generate.py
|
python3 tool_generate.py
|
||||||
|
|
||||||
chmod +x start.sh
|
chmod +x start.sh
|
||||||
|
chmod +x stop.sh
|
||||||
|
chmod +x restart.sh
|
||||||
|
|
6
quick_create_client.sh
Normal file
6
quick_create_client.sh
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
export WG_MYPRIK=$(wg genkey)
|
||||||
|
export WG_MYPUBK=$(echo $WG_MYPRIK | wg pubkey)
|
||||||
|
|
||||||
|
python3 tool_quick_client.py
|
|
@ -56,7 +56,7 @@ def save_config(config, filename=None):
|
||||||
f.write(content)
|
f.write(content)
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.error("Unable to save config: {}".format(traceback.format_exc()))
|
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):
|
def json_to_base64(content):
|
||||||
|
|
|
@ -4,16 +4,14 @@ import getpass
|
||||||
from tool_common import load_config, save_config, SimpleLogger, json_to_base64
|
from tool_common import load_config, save_config, SimpleLogger, json_to_base64
|
||||||
|
|
||||||
|
|
||||||
logger = SimpleLogger()
|
|
||||||
|
|
||||||
config = load_config()
|
config = load_config()
|
||||||
if config:
|
if config:
|
||||||
logger.warn("Valid config found. Creation of server is skipped.")
|
print("Valid config found. Creation of server is skipped.")
|
||||||
exit(0)
|
exit(0)
|
||||||
else:
|
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()
|
op_mode = input("What will this node act as? (C)lient [S]erver [M]ixed: ").strip().lower()
|
||||||
if not op_mode:
|
if not op_mode:
|
||||||
|
@ -172,22 +170,26 @@ save_config(config)
|
||||||
|
|
||||||
|
|
||||||
if op_mode in ("s", "m"):
|
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 =====")
|
print("===== Quick Import =====")
|
||||||
for info in udp2raw_config["server"]:
|
for info in udp2raw_config["server"]:
|
||||||
target_config = {
|
target_quick_config = {
|
||||||
"udp2raw": {
|
"udp2raw_client": {
|
||||||
"client": [{
|
"remote": "{}:{}".format(wg_public_ip, info["port"]),
|
||||||
"remote": "{}:{}".format(wg_public_ip, info["port"]),
|
"password": "",
|
||||||
"password": info["password"],
|
"port": "29100",
|
||||||
"port": "",
|
"speeder": info["speeder"]
|
||||||
"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("Connect to this server via tunnel at port {}: (credential excluded) \n".format(info["port"]))
|
||||||
print(" {}\n".format(json_to_base64(target_config)))
|
print("#QCS#{}\n".format(json_to_base64(target_quick_config)))
|
||||||
|
|
||||||
|
|
||||||
# Configure Peer
|
# Configure Peer
|
||||||
|
@ -207,17 +209,15 @@ while True:
|
||||||
continue
|
continue
|
||||||
break
|
break
|
||||||
|
|
||||||
print(">>> Choose from following udp2raw clients <<<")
|
|
||||||
if config["udp2raw"]["client"]:
|
if config["udp2raw"]["client"]:
|
||||||
|
print(">>> Choose from following udp2raw clients <<<")
|
||||||
for index, client_info in enumerate(config["udp2raw"]["client"]):
|
for index, client_info in enumerate(config["udp2raw"]["client"]):
|
||||||
print("[{}] UDP2Raw Tunnel to Remote {}".format(index + 1, client_info["remote"]))
|
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()
|
peer_endpoint = input("Enter Wireguard Peer Endpoint (ID from list, default to 1): ").strip() or "1"
|
||||||
if peer_endpoint:
|
|
||||||
peer_keepalive = input("Enter Wireguard Peer Keep Alive seconds (default to 30): ").strip() or "30"
|
peer_keepalive = input("Enter Wireguard Peer Keep Alive seconds (default to 30): ").strip() or "30"
|
||||||
else:
|
else:
|
||||||
|
peer_endpoint = ""
|
||||||
peer_keepalive = "30"
|
peer_keepalive = "30"
|
||||||
|
|
||||||
config["peers"].append({
|
config["peers"].append({
|
||||||
|
|
|
@ -95,8 +95,6 @@ wg-quick down {}
|
||||||
tmux kill-session -t tunnel
|
tmux kill-session -t tunnel
|
||||||
'''.format(config["interface"]))
|
'''.format(config["interface"]))
|
||||||
|
|
||||||
os.system("chmod +x stop.sh")
|
|
||||||
|
|
||||||
|
|
||||||
logger.info("Generating restart script...")
|
logger.info("Generating restart script...")
|
||||||
with open("restart.sh", "w", encoding='utf-8') as f:
|
with open("restart.sh", "w", encoding='utf-8') as f:
|
||||||
|
@ -105,8 +103,6 @@ set -e
|
||||||
./stop.sh && ./start.sh
|
./stop.sh && ./start.sh
|
||||||
''')
|
''')
|
||||||
|
|
||||||
os.system("chmod +x restart.sh")
|
|
||||||
|
|
||||||
|
|
||||||
logger.info('''[Done] Config generated. Before you run start.sh, besure to:
|
logger.info('''[Done] Config generated. Before you run start.sh, besure to:
|
||||||
1. Disable SSH Server password login.
|
1. Disable SSH Server password login.
|
||||||
|
|
104
tool_quick_client.py
Normal file
104
tool_quick_client.py
Normal file
|
@ -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))
|
Loading…
Reference in New Issue
Block a user