Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 795e22c043 | |||
| 1b37f1b57d | |||
| 9e3f16175a | |||
| 02a4f590f2 | |||
| 8907ed33f8 | |||
| 35071d2b6f |
@@ -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
@@ -21,8 +21,12 @@ authors = [
|
|||||||
test = ["pytest"]
|
test = ["pytest"]
|
||||||
lint = [
|
lint = [
|
||||||
"black",
|
"black",
|
||||||
"flake8"
|
"flake8",
|
||||||
|
"Flake8-pyproject"
|
||||||
]
|
]
|
||||||
|
|
||||||
[project.scripts]
|
[project.scripts]
|
||||||
example = "vpn_manager.__main__:main"
|
example = "vpn_manager.__main__:main"
|
||||||
|
|
||||||
|
[tool.flake8]
|
||||||
|
ignore = ["E501"]
|
||||||
|
|||||||
+17
-2
@@ -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():
|
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'
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
from . import peers
|
||||||
|
|
||||||
|
__all__ = ["peers"]
|
||||||
|
|||||||
@@ -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()
|
||||||
Reference in New Issue
Block a user