From f5354e8ff877e65ebe9ecb8ddf020c47d65d9421 Mon Sep 17 00:00:00 2001 From: Fedor Lyanguzov Date: Thu, 19 Dec 2024 18:56:45 +0300 Subject: [PATCH] Add old reference code --- .gitignore | 3 +++ make_configs.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 make_configs.py diff --git a/.gitignore b/.gitignore index 82f9275..09875ae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# Folder with result WG configs +result/ + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/make_configs.py b/make_configs.py new file mode 100644 index 0000000..4dbb3a7 --- /dev/null +++ b/make_configs.py @@ -0,0 +1,52 @@ +import subprocess + +def user_cfg(private_key, n): + user_cfg = f''' + [Interface] + PrivateKey = {private_key} + Address = 10.0.0.{n}/32 + DNS = 1.1.1.1 + + [Peer] + PublicKey = {public_key} + AllowedIPs = 0.0.0.0/0, ::/0 + Endpoint = 89.19.211.56:51820 + ''' + return user_cfg + +def server_cfg(peers): + server_cfg = ''' + [Interface] + PrivateKey = {private_key} + Address = 10.0.0.1/32 + PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE + PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE + ListenPort = 51820 + + [Peer] + PublicKey = {public_key} + AllowedIPs = 10.0.0.2/32 + ''' + return '\n\n'.join([server_cfg]+list(map(lambda x: peer_cfg(*x), enumerate(peers, 3)))) + +def peer_cfg(n, peer_public_key): + peer_cfg = f''' + [Peer] + PublicKey = {peer_public_key} + AllowedIPs = 10.0.0.{n}/32 + ''' + return peer_cfg + +peers = [] +for n, user in enumerate(['fedor-walker', 'fedor-phone', 'polina-notebook', 'polina-phone'], 3): + with subprocess.Popen('wg genkey | tee privatekey | wg pubkey > publickey', shell=True): + pass + with open('privatekey') as f: + private_key = f.read().strip() + with open('publickey') as f: + public_key = f.read().strip() + with open(f'{user}.conf', 'w') as f: + f.write(user_cfg(private_key, n)) + peers.append(public_key) +with open(f'server.conf', 'w') as f: + f.write(server_cfg(peers))