2021-07-13 13:59:59 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
2021-07-26 18:01:56 +08:00
|
|
|
import getpass
|
2021-08-23 00:48:45 +08:00
|
|
|
from tool_common import load_config, save_config, json_to_base64, get_sha256
|
|
|
|
from tool_common import WGOP_LB_PBEGIN, WGOP_UC_PBEGIN, WGOP_USPEEDER_C_PBEGIN, WGOP_USPEEDER_S_PBEGIN
|
2021-07-26 18:01:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
config = load_config()
|
|
|
|
if config:
|
2021-07-26 21:32:23 +08:00
|
|
|
print("Valid config found. Creation of server is skipped.")
|
2021-07-13 13:59:59 +08:00
|
|
|
exit(0)
|
2021-07-26 18:01:56 +08:00
|
|
|
else:
|
2021-07-26 21:32:23 +08:00
|
|
|
print("No config found. Start creating interactively.")
|
2021-07-26 18:01:56 +08:00
|
|
|
|
2021-07-26 21:32:23 +08:00
|
|
|
print("====== Choose Role ======")
|
2021-07-13 13:59:59 +08:00
|
|
|
|
|
|
|
op_mode = input("What will this node act as? (C)lient [S]erver [M]ixed: ").strip().lower()
|
|
|
|
if not op_mode:
|
|
|
|
print("Default to client mode.")
|
|
|
|
op_mode = "c"
|
|
|
|
|
|
|
|
if op_mode not in ("c", "s", "m"):
|
|
|
|
print("Invalid node mode. Please try again.")
|
|
|
|
exit(1)
|
|
|
|
|
2021-08-23 00:48:45 +08:00
|
|
|
|
|
|
|
class UConfigController:
|
|
|
|
next_port_speeder_server = WGOP_USPEEDER_S_PBEGIN
|
|
|
|
next_port_speeder_client = WGOP_USPEEDER_C_PBEGIN
|
|
|
|
next_port_balancer = WGOP_LB_PBEGIN
|
|
|
|
next_port_client = WGOP_UC_PBEGIN
|
|
|
|
udp2raw_config = {
|
|
|
|
"server": [],
|
|
|
|
"client": []
|
|
|
|
}
|
|
|
|
|
|
|
|
def add_server(self, port_required, password, speeder_info):
|
|
|
|
self.udp2raw_config["server"].append({
|
|
|
|
"port": port_required,
|
|
|
|
"password": get_sha256(password),
|
|
|
|
"speeder": speeder_info
|
|
|
|
})
|
|
|
|
|
|
|
|
def add_client(self, remote, password, port, speeder_info, demuxer_info):
|
|
|
|
if port is None:
|
|
|
|
port = self.next_port_client
|
|
|
|
if demuxer_info:
|
|
|
|
self.next_port_client += demuxer_info["size"]
|
|
|
|
else:
|
|
|
|
self.next_port_client += 1
|
|
|
|
|
|
|
|
self.udp2raw_config["client"].append({
|
|
|
|
"remote": remote,
|
|
|
|
"password": get_sha256(password),
|
|
|
|
"port": port,
|
|
|
|
"speeder": speeder_info,
|
|
|
|
"demuxer": demuxer_info
|
|
|
|
})
|
|
|
|
|
|
|
|
def new_server_speeder(self, port, ratio):
|
|
|
|
if port is None:
|
|
|
|
port = self.next_port_speeder_server
|
|
|
|
self.next_port_speeder_server += 1
|
|
|
|
|
|
|
|
return {
|
|
|
|
"port": port,
|
|
|
|
"ratio": ratio
|
|
|
|
}
|
|
|
|
|
|
|
|
def new_client_speeder(self, port, ratio):
|
|
|
|
if port is None:
|
|
|
|
port = self.next_port_speeder_client
|
|
|
|
self.next_port_speeder_client += 1
|
|
|
|
|
|
|
|
return {
|
|
|
|
"port": port,
|
|
|
|
"ratio": ratio
|
|
|
|
}
|
|
|
|
|
|
|
|
def new_demuxer(self, port, size):
|
|
|
|
if port is None:
|
|
|
|
port = self.next_port_balancer
|
|
|
|
self.next_port_balancer += 1
|
|
|
|
|
|
|
|
return {
|
|
|
|
"port": port,
|
|
|
|
"size": size
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ucontrol = UConfigController()
|
|
|
|
|
2021-07-13 13:59:59 +08:00
|
|
|
|
|
|
|
if op_mode in ("s", "m"):
|
|
|
|
print("====== Configuring udp2raw server ======")
|
|
|
|
|
|
|
|
while True:
|
2021-08-23 00:48:45 +08:00
|
|
|
print("====== Adding UDP2RAW Server #{} ======".format(len(ucontrol.udp2raw_config["server"]) + 1))
|
2021-07-13 13:59:59 +08:00
|
|
|
|
|
|
|
while True:
|
|
|
|
udp_server_port = input("Please select an Internet-Facing port for incoming udp2raw connection: ").strip()
|
|
|
|
if not udp_server_port:
|
|
|
|
print("A udp2raw listen port is required. Try again.")
|
|
|
|
continue
|
|
|
|
break
|
|
|
|
|
|
|
|
while True:
|
2021-07-26 18:01:56 +08:00
|
|
|
udp_server_password = getpass.getpass('Tunnel Password: ').strip()
|
2021-07-13 13:59:59 +08:00
|
|
|
if not udp_server_password:
|
2021-07-26 18:01:56 +08:00
|
|
|
print("For security reasons, a udp2raw tunnel password is required. Try again.")
|
2021-07-13 13:59:59 +08:00
|
|
|
continue
|
2021-07-26 18:01:56 +08:00
|
|
|
|
|
|
|
if udp_server_password != getpass.getpass('Confirm Tunnel Password: ').strip():
|
|
|
|
print("Password mismatch. Try again.")
|
|
|
|
continue
|
|
|
|
|
2021-07-13 13:59:59 +08:00
|
|
|
break
|
2021-07-26 18:01:56 +08:00
|
|
|
|
2021-07-31 00:41:45 +08:00
|
|
|
is_enable_speeder = input("Enable UDP Speeder for this tunnel? [y/N]: ").strip()
|
|
|
|
if is_enable_speeder and is_enable_speeder.lower() in ('y', 'yes'):
|
2021-07-27 08:22:35 +08:00
|
|
|
speeder_ratio = input("Enter UDP Speeder Ratio (default to 20:10. Use 2:4 for gaming usage): ").strip() or "20:10"
|
2021-08-23 00:48:45 +08:00
|
|
|
speeder_info = ucontrol.new_server_speeder(None, speeder_ratio)
|
2021-07-16 15:19:56 +08:00
|
|
|
else:
|
2021-08-23 00:48:45 +08:00
|
|
|
speeder_info = None
|
2021-07-13 13:59:59 +08:00
|
|
|
|
2021-08-23 00:48:45 +08:00
|
|
|
ucontrol.add_server(udp_server_port, udp_server_password, speeder_info)
|
2021-07-13 13:59:59 +08:00
|
|
|
|
2021-07-26 18:01:56 +08:00
|
|
|
if not input("Add more udp2raw server? (Keep empty to finish): ").strip():
|
2021-07-13 13:59:59 +08:00
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
if op_mode in ("c", "m"):
|
|
|
|
print("====== Configuring udp2raw client ======")
|
|
|
|
|
|
|
|
while True:
|
2021-08-23 00:48:45 +08:00
|
|
|
print("====== Adding UDP2RAW Client {} ======".format(len(ucontrol.udp2raw_config["client"]) + 1))
|
2021-07-13 13:59:59 +08:00
|
|
|
|
|
|
|
while True:
|
|
|
|
udp_server_address = input("Please input remote node internet-facing udp2raw ip:port : ").strip()
|
|
|
|
if not udp_server_address:
|
|
|
|
print("A udp2raw remote server information is required. Try again.")
|
|
|
|
continue
|
|
|
|
break
|
|
|
|
|
|
|
|
while True:
|
2021-07-26 18:01:56 +08:00
|
|
|
udp_server_password = getpass.getpass('Tunnel Password: ').strip()
|
2021-07-13 13:59:59 +08:00
|
|
|
if not udp_server_password:
|
|
|
|
print("A udp2raw tunnel password is required. Try again.")
|
|
|
|
continue
|
2021-07-26 18:01:56 +08:00
|
|
|
|
|
|
|
if udp_server_password != getpass.getpass('Confirm Tunnel Password: ').strip():
|
|
|
|
print("Password mismatch. Try again.")
|
|
|
|
continue
|
|
|
|
|
2021-07-13 13:59:59 +08:00
|
|
|
break
|
|
|
|
|
2021-07-31 00:41:45 +08:00
|
|
|
is_enable_speeder = input("Enable UDP Speeder for this tunnel? [y/N]: ").strip()
|
|
|
|
if is_enable_speeder and is_enable_speeder.lower() in ('y', 'yes'):
|
2021-07-27 08:22:35 +08:00
|
|
|
speeder_ratio = input("Enter UDP Speeder Ratio (default to 20:10. Use 2:4 for gaming usage): ").strip() or "20:10"
|
2021-08-23 00:48:45 +08:00
|
|
|
speeder_info = ucontrol.new_client_speeder(None, speeder_ratio)
|
2021-07-16 15:19:56 +08:00
|
|
|
else:
|
2021-08-23 00:48:45 +08:00
|
|
|
speeder_info = None
|
2021-07-16 15:19:56 +08:00
|
|
|
|
2021-08-04 21:11:43 +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-08-23 00:48:45 +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
|
2021-08-04 21:11:43 +08:00
|
|
|
else:
|
2021-08-23 00:48:45 +08:00
|
|
|
balancer_info = None
|
|
|
|
|
|
|
|
ucontrol.add_client(udp_server_address, udp_server_password, None, speeder_info, balancer_info)
|
2021-07-16 15:19:56 +08:00
|
|
|
|
2021-07-13 13:59:59 +08:00
|
|
|
if not input("Add more udp2raw client? (Keep empty to finish)").strip():
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
wg_prik = os.getenv("WG_MYPRIK")
|
|
|
|
wg_pubk = os.getenv("WG_MYPUBK")
|
|
|
|
wg_mtu = "1000"
|
2021-07-13 15:16:10 +08:00
|
|
|
wg_public_ip = os.getenv("WG_PUBLICIP")
|
|
|
|
|
|
|
|
print('''
|
|
|
|
|
2021-07-31 03:06:58 +08:00
|
|
|
====== Your WireGuard Public Key ======
|
2021-07-13 15:16:10 +08:00
|
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
======= Your Public IP Address ========
|
|
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
=======================================
|
|
|
|
|
|
|
|
'''.format(wg_pubk, wg_public_ip))
|
2021-07-13 13:59:59 +08:00
|
|
|
|
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-13 13:59:59 +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-13 13:59:59 +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-13 13:59:59 +08:00
|
|
|
continue
|
|
|
|
break
|
|
|
|
|
2021-07-26 18:01:56 +08:00
|
|
|
|
|
|
|
print("Saving config...")
|
|
|
|
config = {
|
|
|
|
"version": 1,
|
|
|
|
"mode": op_mode,
|
|
|
|
"pubkey": wg_pubk,
|
|
|
|
"prikey": wg_prik,
|
|
|
|
"mtu": wg_mtu,
|
|
|
|
"interface": ifname,
|
|
|
|
"ip": ifip,
|
|
|
|
"listen": listen_port,
|
|
|
|
"peers": [],
|
2021-08-23 00:48:45 +08:00
|
|
|
"udp2raw": ucontrol.udp2raw_config
|
2021-07-26 18:01:56 +08:00
|
|
|
}
|
|
|
|
save_config(config)
|
|
|
|
|
|
|
|
|
2021-08-23 00:48:45 +08:00
|
|
|
# Display Quick Config
|
2021-07-26 18:01:56 +08:00
|
|
|
if op_mode in ("s", "m"):
|
2021-07-26 21:32:23 +08:00
|
|
|
if ifip.endswith(".1"):
|
|
|
|
suggest_allowed = "{}.0/24".format('.'.join(ifip.split('.')[:-1]))
|
|
|
|
else:
|
|
|
|
suggest_allowed = ifip
|
|
|
|
|
2021-07-26 18:01:56 +08:00
|
|
|
print("===== Quick Import =====")
|
2021-08-23 00:48:45 +08:00
|
|
|
for server_info in ucontrol.udp2raw_config["server"]:
|
|
|
|
speeder_info = server_info["speeder"]
|
|
|
|
|
|
|
|
quick_config = {
|
|
|
|
"pubkey": wg_pubk,
|
|
|
|
"allowed": suggest_allowed,
|
|
|
|
"remote": "{}:{}".format(wg_public_ip, server_info["port"]),
|
|
|
|
"password": server_info["password"],
|
|
|
|
"ratio": speeder_info["ratio"] if speeder_info else ""
|
2021-07-26 18:01:56 +08:00
|
|
|
}
|
|
|
|
|
2021-08-23 00:48:45 +08:00
|
|
|
print("Connect to this server via tunnel at port {}: (credential included) \n".format(server_info["port"]))
|
|
|
|
print("#QCS#{}\n".format(json_to_base64(quick_config)))
|
2021-07-26 18:01:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
# Configure Peer
|
2021-07-13 13:59:59 +08:00
|
|
|
while True:
|
2021-07-26 18:01:56 +08:00
|
|
|
print("====== Adding Peer {} ======".format(len(config["peers"]) + 1))
|
2021-07-13 13:59:59 +08:00
|
|
|
while True:
|
2021-07-31 03:06:58 +08:00
|
|
|
peer_pubk = input("Enter WireGuard Peer Public Key: ").strip()
|
2021-07-13 13:59:59 +08:00
|
|
|
if not peer_pubk:
|
|
|
|
print("A public key is required. Try Again.")
|
|
|
|
continue
|
|
|
|
break
|
|
|
|
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-13 13:59:59 +08:00
|
|
|
if not peer_allowed:
|
|
|
|
print("Peer allowed ips required. Try Again.")
|
|
|
|
continue
|
|
|
|
break
|
|
|
|
|
2021-08-23 00:48:45 +08:00
|
|
|
if ucontrol.udp2raw_config["client"]:
|
2021-07-26 21:32:23 +08:00
|
|
|
print(">>> Choose from following udp2raw clients <<<")
|
2021-08-23 00:48:45 +08:00
|
|
|
for index, client_info in enumerate(ucontrol.udp2raw_config["client"]):
|
|
|
|
speeder_info = client_info["speeder"]
|
|
|
|
balancer_info = client_info["demuxer"]
|
|
|
|
|
|
|
|
print("[{}] {} {} {}".format(index + 1, client_info["remote"],
|
|
|
|
"SpeederRatio: {}".format(speeder_info["ratio"]) if speeder_info else "",
|
|
|
|
"Load-Balanced over {} tunnels".format(balancer_info["size"]) if balancer_info else ""
|
|
|
|
))
|
2021-07-13 13:59:59 +08:00
|
|
|
|
2021-07-31 03:06:58 +08:00
|
|
|
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"
|
2021-07-16 11:56:50 +08:00
|
|
|
else:
|
2021-07-26 21:32:23 +08:00
|
|
|
peer_endpoint = ""
|
2021-07-27 08:14:10 +08:00
|
|
|
peer_keepalive = ""
|
2021-07-13 13:59:59 +08:00
|
|
|
|
2021-07-26 18:01:56 +08:00
|
|
|
config["peers"].append({
|
2021-07-13 13:59:59 +08:00
|
|
|
"pubkey": peer_pubk,
|
|
|
|
"allowed": peer_allowed,
|
|
|
|
"endpoint": peer_endpoint,
|
|
|
|
"keepalive": peer_keepalive
|
|
|
|
})
|
|
|
|
|
2021-07-26 18:01:56 +08:00
|
|
|
print("Saving config...")
|
|
|
|
save_config(config)
|
|
|
|
|
2021-07-13 13:59:59 +08:00
|
|
|
if not input("Add more peers? (Keep empty to finish)").strip():
|
|
|
|
break
|