From 4550c284748ec550acb4d3483f59b0c461f964d3 Mon Sep 17 00:00:00 2001 From: spinach Date: Fri, 13 Dec 2024 14:35:14 -0500 Subject: [PATCH] base --- wg-gen | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100755 wg-gen diff --git a/wg-gen b/wg-gen new file mode 100755 index 0000000..064cfe5 --- /dev/null +++ b/wg-gen @@ -0,0 +1,132 @@ +#!/usr/bin/env python3 + +import subprocess +import argparse + +## a simple utility to generate a pair of mirrored config files + +# peers +class Peer: + def __init__(self): + self.pubkey = -1 + self.psk = -1 + self.ip = -1 + self.name = "" + + def __repr__(self): + name = self.name if self.name != "" else "peer" + haspsk = True if (self.psk != -1) else False + return f"" + + def __str__(self): + name = self.name if self.name != "" else "peer" + haspsk = True if (self.psk != -1) else False + return f"{name} ({self.ip}): {self.pubkey} psk: {haspsk}" + + def update_ip(self, ip): + self.ip = ip + + def update_pubkey(self, pubkey): + self.pubkey = pubkey + + def update_psk(self, psk): + self.psk = psk + + def update_name(self, name): + self.name = name + +# parser +## Returns +def read_config(configfile): + + config = open(configfile, "r") + + peers = [] + currentPeer = -1 + lastIP = -1 + # skip the interface section + # TODO modify to parse config for base IP/allowed + config = config.readlines()[4:] + + for line in config: + # skip linebreaks and comments + if line == "\n": + continue + + line = line.strip() + + + if line == "[Peer]": + peers.append(Peer()) + currentPeer += 1 + + if line[0] == "#": + name = line[1:] + peers[currentPeer].update_name(name.strip()) + + if "PublicKey" in line: + pubkey = line.split("=", 1) + peers[currentPeer].update_pubkey(pubkey[1].strip()) + + if "PresharedKey" in line: + psk = line.split("=", 1) + peers[currentPeer].update_psk(psk[1].strip()) + + if "AllowedIPs" in line: + ip = line.split("=", 1) + peers[currentPeer].update_ip(ip[1].strip()) + peerip = ip[1].split(".") + peerip = int(peerip[3][0]) + lastIP = peerip if peerip > lastIP else lastIP + + for peer in peers: + print(peer) + + return lastIP + +parser = argparse.ArgumentParser() + +parser.add_argument("-i", type=int, help="digit to use for peer ip") +parser.add_argument("-n", required=True, help="name of device") + +args = parser.parse_args() + +# globals set from args + +serverfile = open("backup.conf", "a") +lastIP = read_config("backup.conf") + 1 +# exit() + +ALLOWED_IP = "192.168.2.1/32" +DOMAIN = "lindoship.com:51820" +PORT = 51280 +SERVER_KEY = "X3tEqq9iC6ZD3r4COyTLXWoHw4f8HwlR+WtSH4z4AW4=" + +preKey = subprocess.run(["wg", "genpsk"], capture_output=True, universal_newlines=True) + +privKeyA = subprocess.run(["wg", "genkey"], capture_output=True, universal_newlines=True) +pubKeyA = subprocess.run(["wg", "pubkey"], input=privKeyA.stdout, text=True, capture_output=True) + + +peerfile = open("%s.conf" % args.n, "w") + +peerfile.write("[Interface]\n") +peerfile.write("ListenPort = %d\n" % PORT) +peerfile.write("Address = 192.168.2.%d/32\n" % lastIP) +peerfile.write("PrivateKey = %s\n\n" % privKeyA.stdout.strip()) +peerfile.write("[Peer]\n") +peerfile.write("PublicKey = %s\n" % SERVER_KEY) +peerfile.write("PresharedKey = %s\n" % preKey.stdout.strip()) +peerfile.write("AllowedIPs = %s\n" % ALLOWED_IP) +peerfile.write("Endpoint = %s\n" % DOMAIN) + + +# SERVER +serverfile.write("\n[Peer]\n") +serverfile.write("# %s\n" % args.n) +serverfile.write("PublicKey = %s\n" % pubKeyA.stdout.strip()) +serverfile.write("PresharedKey = %s\n" % preKey.stdout.strip()) +serverfile.write("AllowedIPs = 192.168.2.%d/32\n" % lastIP) + + +