Output text configs #2

Open
Fedor-Lyanguzov wants to merge 6 commits from output-text-files into main
4 changed files with 61 additions and 8 deletions
Showing only changes of commit 8907ed33f8 - Show all commits
+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
+13 -2
View File
@@ -1,6 +1,17 @@
from vpn_manager import * from vpn_manager import *
def test_format_static_peer(): def test_format_static_peer():
sp = StaticPeer('sample-public-key', '127.0.0.1', '12345') 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') peer_section = format_static_peer(sp, '0.0.0.0/0')
assert peer_section == '[Peer]\nPublicKey = sample-public-key\nAllowedIPs = 0.0.0.0/0\nEndpoint = 127.0.0.1:12345\nPersistentKeepAlive = 30' assert peer_section == '[Peer]\nPublicKey = PUB\nAllowedIPs = 0.0.0.0/0\nEndpoint = 127.0.0.1:12345\nPersistentKeepAlive = 30'
def test_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'
-1
View File
@@ -1,2 +1 @@
from .__main__ import * from .__main__ import *
+40 -4
View File
@@ -1,20 +1,56 @@
from dataclasses import dataclass from dataclasses import dataclass
from textwrap import dedent
@dataclass @dataclass
class StaticPeer: class Peer:
public_key: str public_key: str
endpoint: str private_key: str
address_cidr: str
port: str port: str
endpoint: str = None
def format_static_peer(static_peer, routes, keepalive=30): def format_static_peer(static_peer, routes, keepalive=30):
return f'''[Peer] return f"""
[Peer]
PublicKey = {static_peer.public_key} PublicKey = {static_peer.public_key}
AllowedIPs = {routes} AllowedIPs = {routes}
Endpoint = {static_peer.endpoint}:{static_peer.port} Endpoint = {static_peer.endpoint}:{static_peer.port}
PersistentKeepAlive = {keepalive}''' 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(): def main():
print("run") print("run")