6 Commits

Author SHA1 Message Date
Fedor Lyanguzov 795e22c043 Add little type annotations 2025-01-09 17:38:11 +03:00
Fedor Lyanguzov 1b37f1b57d neatest way to write multiline f-strings (not fast though) 2024-12-28 19:35:37 +03:00
Fedor Lyanguzov 9e3f16175a add support for pyproject.toml to flake8 2024-12-28 19:17:03 +03:00
Fedor Lyanguzov 02a4f590f2 work around relative imports 2024-12-28 18:50:30 +03:00
Fedor Lyanguzov 8907ed33f8 add interface section formatting 2024-12-28 01:09:24 +03:00
Fedor Lyanguzov 35071d2b6f add static peer formatting for client config 2024-12-27 23:53:58 +03:00
5 changed files with 77 additions and 3 deletions
+7
View File
@@ -0,0 +1,7 @@
alias venv='source .venv/Scripts/activate'
alias check='flake8 vpn_manager'
alias format='black vpn_manager'
alias test='pytest'
venv
+5 -1
View File
@@ -21,8 +21,12 @@ authors = [
test = ["pytest"]
lint = [
"black",
"flake8"
"flake8",
"Flake8-pyproject"
]
[project.scripts]
example = "vpn_manager.__main__:main"
[tool.flake8]
ignore = ["E501"]
+17 -2
View File
@@ -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
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'
+3
View File
@@ -0,0 +1,3 @@
from . import peers
__all__ = ["peers"]
+45
View File
@@ -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()