-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Closes #208 Closes #284 Co-authored-by: Federico Capoano <[email protected]>
- Loading branch information
1 parent
68681a1
commit 571af14
Showing
23 changed files
with
2,160 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ VPN Backends | |
/backends/openvpn | ||
/backends/wireguard | ||
/backends/vxlan_over_wireguard | ||
/backends/zerotier |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from ...zerotier.converters import ZeroTier as BaseZeroTier | ||
from ..schema import schema | ||
from .base import OpenWrtConverter | ||
|
||
|
||
class ZeroTier(OpenWrtConverter, BaseZeroTier): | ||
_uci_types = ['zerotier'] | ||
_schema = schema['properties']['zerotier']['items'] | ||
|
||
def __intermediate_vpn(self, vpn): | ||
nwid_ifnames = vpn.get('networks', []) | ||
files = self.netjson.get('files', []) | ||
self.netjson['files'] = self.__get_zt_ifname_files(vpn, files) | ||
vpn.update( | ||
{ | ||
'.name': self._get_uci_name(vpn.pop('name')), | ||
'.type': 'zerotier', | ||
'config_path': vpn.get('config_path', '/etc/openwisp/zerotier'), | ||
'copy_config_path': vpn.get('copy_config_path', '1'), | ||
'join': [networks.get('id', '') for networks in nwid_ifnames], | ||
'enabled': not vpn.pop('disabled', False), | ||
} | ||
) | ||
del vpn['networks'] | ||
return super().__intermediate_vpn(vpn, remove=['']) | ||
|
||
def __netjson_vpn(self, vpn): | ||
nwids = vpn.pop('join') | ||
vpn['name'] = vpn.pop('.name') | ||
vpn['networks'] = [{"id": nwid, "ifname": f"owzt{nwid[-6:]}"} for nwid in nwids] | ||
# 'disabled' defaults to False in OpenWRT | ||
vpn['disabled'] = vpn.pop('enabled', '0') == '0' | ||
del vpn['.type'] | ||
return super().__netjson_vpn(vpn) | ||
|
||
def __get_zt_ifname_files(self, vpn, files): | ||
config_path = vpn.get('config_path', '/etc/openwisp/zerotier') | ||
nwid_ifnames = vpn.get('networks', []) | ||
zt_file_contents = '# network_id=interface_name\n' | ||
|
||
for networks in nwid_ifnames: | ||
nwid = networks.get('id', '') | ||
ifname = networks.get('ifname') | ||
zt_file_contents += f"{nwid}={ifname}\n" | ||
|
||
zt_interface_map = { | ||
'path': f"{config_path}/devicemap", | ||
'mode': '0644', | ||
'contents': zt_file_contents, | ||
} | ||
|
||
if not files: | ||
return [zt_interface_map] | ||
updated_files = [] | ||
for file in files: | ||
if file.get('path') == zt_interface_map.get('path'): | ||
zt_interface_map['contents'] += '\n' + file['contents'] | ||
else: | ||
updated_files.append(file) | ||
if zt_interface_map.get('contents'): | ||
updated_files.append(zt_interface_map) | ||
return updated_files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from ..base.converter import BaseConverter | ||
from .schema import schema | ||
|
||
|
||
class ZeroTier(BaseConverter): | ||
netjson_key = 'zerotier' | ||
intermediate_key = 'zerotier' | ||
_schema = schema['definitions']['zerotier_server'] | ||
|
||
def to_intermediate_loop(self, block, result, index=None): | ||
vpn = self.__intermediate_vpn(block) | ||
result.setdefault('zerotier', []) | ||
result['zerotier'].append(vpn) | ||
return result | ||
|
||
def __intermediate_vpn(self, config, remove=None): | ||
return self.sorted_dict(config) | ||
|
||
def to_netjson_loop(self, block, result, index=None): | ||
vpn = self.__netjson_vpn(block) | ||
result.setdefault('zerotier', []) | ||
result['zerotier'].append(vpn) | ||
return result | ||
|
||
def __netjson_vpn(self, vpn): | ||
vpn = self.type_cast(vpn, self._schema) | ||
return vpn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import re | ||
import tarfile | ||
from json import loads | ||
|
||
from ..base.parser import BaseParser | ||
|
||
vpn_pattern = re.compile('^// zerotier controller config:\s', flags=re.MULTILINE) | ||
config_pattern = re.compile('^([^\s]*) ?(.*)$') | ||
config_suffix = '.json' | ||
|
||
|
||
class ZeroTierParser(BaseParser): | ||
def parse_text(self, config): | ||
return {'zerotier': self._get_vpn_config(config)} | ||
|
||
def parse_tar(self, tar): | ||
fileobj = tar.buffer if hasattr(tar, 'buffer') else tar | ||
tar = tarfile.open(fileobj=fileobj) | ||
text = '' | ||
for member in tar.getmembers(): | ||
if not member.name.endswith(config_suffix): | ||
continue | ||
text += '// zerotier controller config: {name}\n\n{contents}\n'.format( | ||
**{ | ||
'name': member.name, | ||
'contents': tar.extractfile(member).read().decode(), | ||
} | ||
) | ||
return self.parse_text(text) | ||
|
||
def _get_vpn_config(self, text): | ||
# Remove comments from the vpn text | ||
text = re.sub(r'\/\*(\*(?!\/)|[^*])*\*\/|\/\/.*', '', text) | ||
# Strip leading and trailing whitespace from the text | ||
text = text.strip() | ||
# Split the text into separate VPN instances | ||
# using two or more newline characters as the delimiter | ||
vpn_instances = re.split(r"\n{2,}", text) | ||
# Parse each JSON object separately | ||
vpn_configs = [ | ||
loads(vpn_instance) | ||
for vpn_instance in vpn_instances | ||
if vpn_instance.strip() | ||
] | ||
return vpn_configs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from ..base.renderer import BaseRenderer | ||
|
||
|
||
class ZeroTierRenderer(BaseRenderer): | ||
""" | ||
ZeroTier Renderer | ||
""" | ||
|
||
def cleanup(self, output): | ||
# remove last newline | ||
if output.endswith('\n\n'): | ||
output = output[0:-1] | ||
return output |
Oops, something went wrong.