Skip to content
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
19 changes: 11 additions & 8 deletions netdiff/parsers/openvpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,23 @@ def parse(self, data):
# add server (central node) to graph
graph.add_node(server)
# add clients in graph as nodes
for common_name, client in data.client_list.items():
for client in data.client_list.values():
client_properties = {
'real_address': str(client.real_address.host),
'port': client.real_address.port,
'connected_since': client.connected_since.strftime('%Y-%m-%dT%H:%M:%SZ'),
'bytes_received': client.bytes_received,
'bytes_sent': client.bytes_sent
}
if common_name in data.routing_table:
client_properties['local_addresses'] = [
str(data.routing_table.get(common_name).virtual_address)
]
graph.add_node(common_name, **client_properties)
local_addresses = [
str(route.virtual_address)
for route in data.routing_table.values()
if route.real_address == client.real_address
]
if local_addresses:
client_properties['local_addresses'] = local_addresses
graph.add_node(str(client.real_address.host), **client_properties)
# add links in routing table to graph
for common_name, link in data.routing_table.items():
graph.add_edge(server, common_name, weight=1)
for link in data.routing_table.values():
graph.add_edge(server, str(link.real_address.host), weight=1)
return graph
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ networkx<2.0
requests<3.0
six
libcnml<0.10.0
openvpn-status<0.2
openvpn-status>=0.2,<0.3
18 changes: 18 additions & 0 deletions tests/static/openvpn-5-links-tap.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
OpenVPN CLIENT LIST
Updated,Fri Oct 20 12:52:38 2017
Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since
Kali-Matera,2.226.154.66:45641,126345570,264437194,Sun Oct 15 11:03:54 2017
Kali-Oppido,93.40.230.50:37394,3625584,7923472,Fri Oct 20 09:16:31 2017
rosario,95.250.161.57:53084,5742,382894,Fri Oct 20 12:45:30 2017
Syskrack,79.18.21.144:63859,481360,33208728,Fri Oct 20 00:20:32 2017
pomezia,87.3.36.166:60485,395226,33194538,Fri Oct 20 00:20:27 2017
ROUTING TABLE
Virtual Address,Common Name,Real Address,Last Ref
46:8a:69:e7:46:24,Kali-Matera,2.226.154.66:45641,Fri Oct 20 12:52:26 2017
c0:4a:00:2d:05:fc,pomezia,87.3.36.166:60485,Fri Oct 20 12:52:26 2017
aa:c5:99:28:91:a4,rosario,95.250.161.57:53084,Fri Oct 20 12:52:26 2017
c4:6e:1f:ff:02:23,Kali-Oppido,93.40.230.50:37394,Fri Oct 20 12:52:26 2017
9e:c2:f2:55:f4:33,Syskrack,79.18.21.144:63859,Fri Oct 20 12:52:26 2017
GLOBAL STATS
Max bcast/mcast queue length,6
END
16 changes: 15 additions & 1 deletion tests/test_openvpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import networkx
import six
from netdiff import OpenvpnParser
from netdiff.exceptions import ParserError, TopologyRetrievalError
from netdiff.tests import TestCase

CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
links2 = open('{0}/static/openvpn-2-links.txt'.format(CURRENT_DIR)).read()
links2_tap = open('{0}/static/openvpn-5-links-tap.txt'.format(CURRENT_DIR)).read()


class TestOpenvpnParser(TestCase):
Expand All @@ -31,3 +31,17 @@ def test_json_dict(self):
self.assertIsInstance(data['links'], list)
self.assertEqual(len(data['nodes']), 3)
self.assertEqual(len(data['links']), 2)

def test_json_dict_tap(self):
p = OpenvpnParser(links2_tap)
data = p.json(dict=True)
self.assertIsInstance(data, dict)
self.assertEqual(data['type'], 'NetworkGraph')
self.assertEqual(data['protocol'], 'OpenVPN Status Log')
self.assertEqual(data['version'], '1')
self.assertEqual(data['revision'], None)
self.assertEqual(data['metric'], 'static')
self.assertIsInstance(data['nodes'], list)
self.assertIsInstance(data['links'], list)
self.assertEqual(len(data['nodes']), 6)
self.assertEqual(len(data['links']), 5)