Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds mtu and table to the config #3

Merged
merged 17 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ Create a standalone client::
they would typically be on different machines and would not interfere with one another. Be aware
of this when generating peer configs on a server node, or on any node that has a pre-existing
wireguard config at the default file location.

Version
-------
This is version 0.2.4
jnhmcknight marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions tests/test_peers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def test_basic_peer():
assert peer.public_key == public_key(peer.private_key)

assert not peer.peers
assert not peer.mtu
assert not peer.table
assert not peer.pre_up
assert not peer.post_up
assert not peer.pre_down
Expand Down
20 changes: 20 additions & 0 deletions wireguard/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
'pre_down',
'post_down',
'save_config',
'mtu',
'table',
]

PEER_KEYS = [
Expand Down Expand Up @@ -67,6 +69,10 @@ def dns(self):
Returns the DNS settings of the given peer for the config file
"""

# do not write empty DNS = entry
if not bool(self._peer.dns):
return None

marneu marked this conversation as resolved.
Show resolved Hide resolved
return value_list_to_comma('DNS', self._peer.dns)

@property
Expand Down Expand Up @@ -165,6 +171,20 @@ def description(self):
"""
return f'# {self._peer.description}'

@property
def mtu(self):
"""
Returns the mtu for this peer
"""
return f'MTU = {self._peer.mtu}'

@property
def table(self):
"""
Returns the table for this peer
"""
return f'Table = {self._peer.table}'

@property
def interface(self):
"""
Expand Down
70 changes: 69 additions & 1 deletion wireguard/peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class Peer: # pylint: disable=too-many-instance-attributes
post_up = None
pre_down = None
post_down = None
_mtu = None
_table = None

_config = None
peers = None
Expand All @@ -82,7 +84,9 @@ def __init__(self,
interface=None,
peers=None,
config_cls=None,
):
mtu=None,
table=None,
):

self.allowed_ips = IPNetworkSet()
self.dns = IPAddressSet()
Expand Down Expand Up @@ -116,6 +120,8 @@ def __init__(self,
self.port = port
self.interface = interface
self.keepalive = keepalive
self.mtu = mtu
self.table = table

if save_config is not None:
self.save_config = save_config
Expand Down Expand Up @@ -283,6 +289,68 @@ def keepalive(self, value):

self._keepalive = value

@property
def mtu(self):
"""
returns the mtu value
WG Default = 1420 (dunno and leave it to automatic for best results)
if you have to fix mtu depending on outer:
ipv6 connections require 1280 as minimum (try 1300,1350,1400)
PPPoE = try 1412 or lower
"""
return self._mtu

@mtu.setter
def mtu(self, value):
"""
Sets the mtu value
"""
if value is not None:
# Check for bool specifically, because bool is a subclass of int
if not isinstance(value, int) or isinstance(value, bool):
raise ValueError('MTU value must be an integer')

if value < 1280 or value > 1420:
raise ValueError('MTU value must be in the range 1280-1420')

self._mtu = value

@property
def table(self):
"""
returns the routing table value
"""
return self._table

@table.setter
def table(self, value):
"""
Sets the routing table value
"""

if value is not None:

try:
# bool is a subclass of int and can be evaluated in the range condition,
# _but_ we want to give the correct error message to the user, since
# setting `Table = True` or `Table = False` would make a WireGuard config
# file fail to parse correctly. We also don't want to risk `True` becoming
# `Table = 1` as that is probably not what the user would have wanted.
if isinstance(value, bool):
raise TypeError('Table must not be a boolean')

if not (0 < value < 253 or 255 < value < (2**31)):
raise ValueError('Table must be in the ranges 1-252, 256-(2°31-1)')

except TypeError as exc:
# special values allowed (auto=default, off=no route created)
# ref: https://git.zx2c4.com/wireguard-tools/about/src/man/wg-quick.8
if value not in ('auto', 'off'):
raise ValueError('Table must be "auto", "off" or an integer value') from exc

self._table = value


def config(self, config_cls=None):
"""
Return the wireguard config file for this peer
Expand Down