diff --git a/.activate b/.activate new file mode 100644 index 0000000..a3607ff --- /dev/null +++ b/.activate @@ -0,0 +1,7 @@ + +alias venv='source .venv/Scripts/activate' +alias check='flake8 vpn_manager' +alias format='black vpn_manager' +alias test='pytest' + +venv diff --git a/pyproject.toml b/pyproject.toml index 3552b50..7f0a731 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,8 +21,12 @@ authors = [ test = ["pytest"] lint = [ "black", - "flake8" + "flake8", + "Flake8-pyproject" ] [project.scripts] example = "vpn_manager.__main__:main" + +[tool.flake8] +ignore = ["E501"] diff --git a/tests/test_app.py b/tests/test_app.py index 725f245..4918262 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -1,3 +1,18 @@ +from vpn_manager.peers import * + +def test_format_static_peer(): + sp = Peer('PUB', "PRV", '10.0.0.1/32', '12345', '127.0.0.1') + peer_section = format_static_peer(sp, '0.0.0.0/0') + assert peer_section == '[Peer]\nPublicKey = PUB\nAllowedIPs = 0.0.0.0/0\nEndpoint = 127.0.0.1:12345\nPersistentKeepAlive = 30' + + def test_true(): - assert True - \ No newline at end of file + assert format_interface(Peer('PUB', 'PRV', '10.0.0.1/32', 'PORT'), '1.1.1.1') \ + == '[Interface]\nPrivateKey = PRV\nAddress = 10.0.0.1/32\nListenPort = PORT\nDNS = 1.1.1.1' + assert format_interface(Peer('PUB', 'PRV', '10.0.0.1/32', None), '1.1.1.1') \ + == '[Interface]\nPrivateKey = PRV\nAddress = 10.0.0.1/32\nDNS = 1.1.1.1' + assert format_interface(Peer('PUB', 'PRV', '10.0.0.1/32', 'PORT'), '1.1.1.1', forward=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 e69de29..a03af42 100644 --- a/vpn_manager/__init__.py +++ b/vpn_manager/__init__.py @@ -0,0 +1,3 @@ +from . import peers + +__all__ = ["peers"] diff --git a/vpn_manager/peers.py b/vpn_manager/peers.py new file mode 100644 index 0000000..cfd2d9c --- /dev/null +++ b/vpn_manager/peers.py @@ -0,0 +1,45 @@ +from dataclasses import dataclass + + +@dataclass +class Peer: + public_key: str + private_key: str + address_cidr: str + port: str + endpoint: str = None + + +def format_static_peer(static_peer: Peer, routes, keepalive=30): + return ( + "[Peer]\n" + f"PublicKey = {static_peer.public_key}\n" + f"AllowedIPs = {routes}\n" + f"Endpoint = {static_peer.endpoint}:{static_peer.port}\n" + f"PersistentKeepAlive = {keepalive}\n" + ).strip() + + +def format_interface(peer: Peer, dns, forward=False): + if forward: + forward = ( + "PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE\n" + "PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE\n" + ) + # wg0 always? + # eth0 always? + else: + forward = "" + if peer.port: + port = f"ListenPort = {peer.port}\n" + else: + port = "" + dns = f"DNS = {dns}\n" + return ( + "[Interface]\n" + f"PrivateKey = {peer.private_key}\n" + f"Address = {peer.address_cidr}\n" + f"{port}" + f"{dns}" + f"{forward}" + ).strip()