From 02a4f590f2aa3515dc33b4a6a3b8773b34fa34df Mon Sep 17 00:00:00 2001 From: Fedor Lyanguzov Date: Sat, 28 Dec 2024 18:50:30 +0300 Subject: [PATCH] work around relative imports --- tests/test_app.py | 3 ++- vpn_manager/__init__.py | 4 ++- vpn_manager/__main__.py | 54 ----------------------------------------- vpn_manager/peers.py | 48 ++++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 56 deletions(-) create mode 100644 vpn_manager/peers.py diff --git a/tests/test_app.py b/tests/test_app.py index 6e7523d..4918262 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -1,4 +1,4 @@ -from vpn_manager import * +from vpn_manager.peers import * def test_format_static_peer(): sp = Peer('PUB', "PRV", '10.0.0.1/32', '12345', '127.0.0.1') @@ -15,3 +15,4 @@ def test_true(): == '[Interface]\nPrivateKey = PRV\nAddress = 10.0.0.1/32\nListenPort = PORT\nDNS = 1.1.1.1\nPostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE\nPostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE' assert format_interface(Peer('PUB', 'PRV', '10.0.0.1/32', None), '1.1.1.1', forward=True) \ == '[Interface]\nPrivateKey = PRV\nAddress = 10.0.0.1/32\nDNS = 1.1.1.1\nPostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE\nPostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE' + diff --git a/vpn_manager/__init__.py b/vpn_manager/__init__.py index a86c6bb..a03af42 100644 --- a/vpn_manager/__init__.py +++ b/vpn_manager/__init__.py @@ -1 +1,3 @@ -from .__main__ import * +from . import peers + +__all__ = ["peers"] diff --git a/vpn_manager/__main__.py b/vpn_manager/__main__.py index 9470d5b..31564bb 100644 --- a/vpn_manager/__main__.py +++ b/vpn_manager/__main__.py @@ -1,57 +1,3 @@ -from dataclasses import dataclass -from textwrap import dedent - - -@dataclass -class Peer: - public_key: str - private_key: str - address_cidr: str - port: str - endpoint: str = None - - -def format_static_peer(static_peer, routes, keepalive=30): - return f""" -[Peer] -PublicKey = {static_peer.public_key} -AllowedIPs = {routes} -Endpoint = {static_peer.endpoint}:{static_peer.port} -PersistentKeepAlive = {keepalive} -""".strip() - - -def format_interface(peer, dns, forward=False): - if forward: - forward = dedent( - """\ - 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 - """ - ) - # wg0 always? - # eth0 always? - else: - forward = "" - if peer.port: - port = f"ListenPort = {peer.port}\n" - else: - port = "" - dns = f"DNS = {dns}\n" - return f""" -[Interface] -PrivateKey = {peer.private_key} -Address = {peer.address_cidr} -{port}\ -{dns}\ -{forward}\ -""".strip() - - -def remove_empty_lines(text): - return "\n".join([s for s in text.splitlines() if s.strip()]) - - def main(): print("run") diff --git a/vpn_manager/peers.py b/vpn_manager/peers.py new file mode 100644 index 0000000..06d6302 --- /dev/null +++ b/vpn_manager/peers.py @@ -0,0 +1,48 @@ +from dataclasses import dataclass +from textwrap import dedent + + +@dataclass +class Peer: + public_key: str + private_key: str + address_cidr: str + port: str + endpoint: str = None + + +def format_static_peer(static_peer, routes, keepalive=30): + return f""" +[Peer] +PublicKey = {static_peer.public_key} +AllowedIPs = {routes} +Endpoint = {static_peer.endpoint}:{static_peer.port} +PersistentKeepAlive = {keepalive} +""".strip() + + +def format_interface(peer, dns, forward=False): + if forward: + forward = dedent( + """\ + 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 + """ + ) + # wg0 always? + # eth0 always? + else: + forward = "" + if peer.port: + port = f"ListenPort = {peer.port}\n" + else: + port = "" + dns = f"DNS = {dns}\n" + return f""" +[Interface] +PrivateKey = {peer.private_key} +Address = {peer.address_cidr} +{port}\ +{dns}\ +{forward}\ +""".strip()