Skip to content

Commit

Permalink
[feature] Added ZeroTier Parser #106
Browse files Browse the repository at this point in the history
Closes #106
  • Loading branch information
Aryamanz29 authored and nemesifier committed Oct 11, 2023
1 parent 1a10207 commit 850fb38
Show file tree
Hide file tree
Showing 6 changed files with 566 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ The available parsers are:
* ``netdiff.NetJsonParser``: parser for the `NetJSON NetworkGraph`_ format
* ``netdiff.OpenvpnParser``: parser for the `OpenVPN status file <https://community.openvpn.net/openvpn/wiki/Openvpn24ManPage>`_
* ``netdiff.WireguardParser``: parser for the Wireguard VPN (the command to use is ``wg show all dump``)
* ``netdiff.ZeroTierParser``: parser for ZeroTier VPN (the command to use is ``zerotier-cli peers -j`` or
access the peers information through the `ZeroTier Service API <https://docs.zerotier.com/service/v1/#operation/getPeers>`_)

Initialization arguments
~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
1 change: 1 addition & 0 deletions netdiff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
from .parsers.olsr import OlsrParser # noqa
from .parsers.openvpn import OpenvpnParser # noqa
from .parsers.wireguard import WireguardParser # noqa
from .parsers.zerotier import ZeroTierParser # noqa
from .utils import diff # noqa
44 changes: 44 additions & 0 deletions netdiff/parsers/zerotier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from netdiff.parsers.base import BaseParser


class ZeroTierParser(BaseParser):
version = '1'
metric = 'static'
protocol = 'ZeroTier Controller Peers'

def to_python(self, data):
return super().to_python(data)

def parse(self, data):
graph = self._init_graph()
for peer in data:
# In the ZeroTier architecture, a 'LEAF' refers to a device
# that is a member of a ZeroTier virtual network.
# Therefore, we can skip peers with roles other than 'LEAF'
# or with latency -1 (indicating not reachable)
if peer.get('role') != 'LEAF' or peer.get('latency') == -1:
continue
# Similar to zerotier-cli peers command (PATH)
# We only select path that are active, preferred, and not expired
for path in peer.get('paths'):
if (
not path.get('expired')
and path.get('active')
and path.get('preferred')
):
peer_address = peer.get('address')
peer_properties = dict(
label=peer_address,
address=peer_address,
ip_address=path.pop('address'),
role=peer.get('role'),
version=peer.get('version'),
tunneled=peer.get('tunneled'),
isBonded=peer.get('isBonded'),
)
graph.add_node('controller', label='controller')
graph.add_node(peer_address, **peer_properties)
graph.add_edge(
'controller', peer_address, weight=peer.get('latency'), **path
)
return graph
180 changes: 180 additions & 0 deletions tests/static/zt-peers-updated.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
[
{
"address": "3504e2b2e2",
"isBonded": false,
"latency": 9,
"paths": [
{
"active": true,
"address": "192.168.56.1/44221",
"expired": false,
"lastReceive": 1691493323678,
"lastSend": 1691493323677,
"localSocket": 94434869108304,
"preferred": true,
"trustedPathId": 0
},
{
"active": true,
"address": "fd60:8d57:65e9::1/9993",
"expired": false,
"lastReceive": 1691493323678,
"lastSend": 1691493323677,
"localSocket": 94434869123504,
"preferred": false,
"trustedPathId": 0
},
{
"active": true,
"address": "fd60:8d57:65e9::1/9993",
"expired": false,
"lastReceive": 1691493323678,
"lastSend": 1691493323677,
"localSocket": 94434869095936,
"preferred": false,
"trustedPathId": 0
}
],
"role": "LEAF",
"tunneled": false,
"version": "1.10.3",
"versionMajor": 1,
"versionMinor": 10,
"versionRev": 3
},
{
"address": "9a9e1c9f19",
"isBonded": false,
"latency": 0,
"paths": [
{
"active": true,
"address": "192.168.56.6/9993",
"expired": false,
"lastReceive": 1691493318673,
"lastSend": 1691493318672,
"localSocket": 94434869092208,
"preferred": false,
"trustedPathId": 0
},
{
"active": true,
"address": "192.168.56.1/9993",
"expired": false,
"lastReceive": 1691493318673,
"lastSend": 1691493318672,
"localSocket": 94434869109504,
"preferred": false,
"trustedPathId": 0
},
{
"active": true,
"address": "172.168.56.9/9993",
"expired": false,
"lastReceive": 1691493323678,
"lastSend": 1691493323677,
"localSocket": 94434869108304,
"preferred": true,
"trustedPathId": 0
}
],
"role": "LEAF",
"tunneled": false,
"version": "1.10.3",
"versionMajor": 1,
"versionMinor": 10,
"versionRev": 3
},
{
"address": "62f865ae71",
"isBonded": false,
"latency": 297,
"paths": [
{
"active": true,
"address": "50.7.252.138/9993",
"expired": false,
"lastReceive": 1691493263920,
"lastSend": 1691493323677,
"localSocket": 94434869079296,
"preferred": true,
"trustedPathId": 0
}
],
"role": "PLANET",
"tunneled": false,
"version": "-1.-1.-1",
"versionMajor": -1,
"versionMinor": -1,
"versionRev": -1
},
{
"address": "778cde7190",
"isBonded": false,
"latency": 276,
"paths": [
{
"active": true,
"address": "103.195.103.66/9993",
"expired": false,
"lastReceive": 1691493263899,
"lastSend": 1691493323677,
"localSocket": 94434869085344,
"preferred": true,
"trustedPathId": 0
}
],
"role": "PLANET",
"tunneled": false,
"version": "-1.-1.-1",
"versionMajor": -1,
"versionMinor": -1,
"versionRev": -1
},
{
"address": "cafe04eba9",
"isBonded": false,
"latency": 163,
"paths": [
{
"active": true,
"address": "84.17.53.155/9993",
"expired": false,
"lastReceive": 1691493263786,
"lastSend": 1691493327124,
"localSocket": 94434869086304,
"preferred": true,
"trustedPathId": 0
}
],
"role": "PLANET",
"tunneled": false,
"version": "-1.-1.-1",
"versionMajor": -1,
"versionMinor": -1,
"versionRev": -1
},
{
"address": "cafe9efeb9",
"isBonded": false,
"latency": 241,
"paths": [
{
"active": true,
"address": "104.194.8.134/9993",
"expired": false,
"lastReceive": 1691493263864,
"lastSend": 1691493323677,
"localSocket": 94434869086304,
"preferred": true,
"trustedPathId": 0
}
],
"role": "PLANET",
"tunneled": false,
"version": "-1.-1.-1",
"versionMajor": -1,
"versionMinor": -1,
"versionRev": -1
}
]
Loading

0 comments on commit 850fb38

Please sign in to comment.