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
|
||||
|
||||
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
|
||||
|
|
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)
|
||||
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):
|
||||
|
|
|
@ -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": [{
|
||||
target_quick_config = {
|
||||
"udp2raw_client": {
|
||||
"remote": "{}:{}".format(wg_public_ip, info["port"]),
|
||||
"password": info["password"],
|
||||
"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({
|
||||
|
|
|
@ -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.
|
||||
|
|
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