Skip to content

Commit 40071b0

Browse files
committed
[openvpn] Simplified and improved OpenVPN parser
1 parent 2762ef4 commit 40071b0

File tree

3 files changed

+33
-46
lines changed

3 files changed

+33
-46
lines changed

netdiff/parsers/openvpn.py

+26-39
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,19 @@
11
import networkx
2-
from openvpn_status import ParsingError, parse_status
2+
from openvpn_status import parse_status
33

4-
from ..exceptions import ConversionException, ParserError
54
from .base import BaseParser
65

76

87
class OpenvpnParser(BaseParser):
98
""" OpenVPN status log parser """
10-
protocol = 'openvpn'
11-
server_address = '127.0.0.1' # Testing purposes
9+
protocol = 'OpenVPN Status Log'
1210
version = '1'
13-
metric = '0'
11+
metric = 'static'
12+
# for internal use only
13+
_server_common_name = 'openvpn-server'
1414

1515
def to_python(self, data):
16-
status = parse_status(data)
17-
nodes = []
18-
for i in status.client_list:
19-
node = status.client_list[i]
20-
nodes.append({
21-
'commonName': node.common_name,
22-
'bytesSent': node.bytes_sent,
23-
'bytesReceived': node.bytes_received,
24-
'connectedSince': node.connected_since,
25-
'realAddress': str(node.real_address)
26-
})
27-
28-
return {
29-
'type': 'OpenVPN',
30-
'nodes': nodes,
31-
'updated_at': status.updated_at,
32-
'server': self.server_address
33-
}
16+
return parse_status(data)
3417

3518
def parse(self, data):
3619
"""
@@ -39,20 +22,24 @@ def parse(self, data):
3922
"""
4023
# initialize graph and list of aggregated nodes
4124
graph = networkx.Graph()
42-
if 'type' not in data or data['type'] != 'OpenVPN':
43-
raise ParserError('Parse Error, not a OpenVPN object')
44-
required_keys = ['nodes', 'server']
45-
for key in required_keys:
46-
if key not in data:
47-
raise ParserError('Parse Error, "%s" key not found' % key)
48-
49-
graph.add_node(data['server'])
50-
for link in data['nodes']:
51-
try:
52-
source = data['server']
53-
target = link['realAddress']
54-
except KeyError as e:
55-
raise ParserError('Parse Error, "%s" key not found' % key)
56-
graph.add_node(target)
57-
graph.add_edge(source, target, weight=1)
25+
server = self._server_common_name
26+
# add server (central node) to graph
27+
graph.add_node(server)
28+
# add clients in graph as nodes
29+
for common_name, client in data.client_list.items():
30+
client_properties = {
31+
'real_address': str(client.real_address.host),
32+
'port': client.real_address.port,
33+
'connected_since': client.connected_since.strftime('%Y-%m-%dT%H:%M:%SZ'),
34+
'bytes_received': client.bytes_received,
35+
'bytes_sent': client.bytes_sent
36+
}
37+
if common_name in data.routing_table:
38+
client_properties['local_addresses'] = [
39+
str(data.routing_table.get(common_name).virtual_address)
40+
]
41+
graph.add_node(common_name, **client_properties)
42+
# add links in routing table to graph
43+
for common_name, link in data.routing_table.items():
44+
graph.add_edge(server, common_name, weight=1)
5845
return graph

tests/static/openvpn-2-links.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
OpenVPN CLIENT LIST
22
Updated,Thu Jun 18 08:12:15 2015
33
Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since
4-
foo@example.com,10.10.10.10:49502,334948,1973012,Thu Jun 18 04:23:03 2015
5-
bar@example.com,10.10.10.10:64169,1817262,28981224,Thu Jun 18 04:08:39 2015
4+
node1,87.18.10.87:49502,334948,1973012,Thu Jun 18 04:23:03 2015
5+
node2,93.40.230.50:64169,1817262,28981224,Thu Jun 18 04:08:39 2015
66
ROUTING TABLE
77
Virtual Address,Common Name,Real Address,Last Ref
8-
192.168.255.134,foo@example.com,10.10.10.10:49502,Thu Jun 18 08:12:09 2015
9-
192.168.255.126,bar@example.com,10.10.10.10:64169,Thu Jun 18 08:11:55 2015
8+
192.168.255.134,node1,87.18.10.87:49502,Thu Jun 18 08:12:09 2015
9+
192.168.255.126,node2,93.40.230.50:64169,Thu Jun 18 08:11:55 2015
1010
GLOBAL STATS
1111
Max bcast/mcast queue length,0
1212
END

tests/test_openvpn.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ def test_parse(self):
1616
p = OpenvpnParser(links2)
1717
self.assertIsInstance(p.graph, networkx.Graph)
1818
self.assertEqual(p.version, '1')
19-
self.assertEqual(p.metric, '0')
19+
self.assertEqual(p.metric, 'static')
2020

2121
def test_json_dict(self):
2222
p = OpenvpnParser(links2)
2323
data = p.json(dict=True)
2424
self.assertIsInstance(data, dict)
2525
self.assertEqual(data['type'], 'NetworkGraph')
26-
self.assertEqual(data['protocol'], 'openvpn')
26+
self.assertEqual(data['protocol'], 'OpenVPN Status Log')
2727
self.assertEqual(data['version'], '1')
2828
self.assertEqual(data['revision'], None)
29-
self.assertEqual(data['metric'], '0')
29+
self.assertEqual(data['metric'], 'static')
3030
self.assertIsInstance(data['nodes'], list)
3131
self.assertIsInstance(data['links'], list)
3232
self.assertEqual(len(data['nodes']), 3)

0 commit comments

Comments
 (0)