@@ -11,9 +11,14 @@ class OpenvpnParser(BaseParser):
11
11
protocol = 'OpenVPN Status Log'
12
12
version = '1'
13
13
metric = 'static'
14
+ duplicate_cn = False
14
15
# for internal use only
15
16
_server_common_name = 'openvpn-server'
16
17
18
+ def __init__ (self , * args , ** kwargs ):
19
+ self .duplicate_cn = kwargs .pop ('duplicate_cn' , OpenvpnParser .duplicate_cn )
20
+ super ().__init__ (* args , ** kwargs )
21
+
17
22
def to_python (self , data ):
18
23
if not data :
19
24
return None
@@ -40,14 +45,7 @@ def parse(self, data):
40
45
else :
41
46
clients = data .client_list .values ()
42
47
links = data .routing_table .values ()
43
- id_list = []
44
- special_cases = []
45
- for client in clients :
46
- id_ = f'{ client .common_name } ,{ client .real_address .host } '
47
- if id_ in id_list :
48
- special_cases .append (id_ )
49
- continue
50
- id_list .append (id_ )
48
+ special_cases = self ._find_special_cases (clients )
51
49
# add clients in graph as nodes
52
50
for client in clients :
53
51
if client .common_name == 'UNDEF' :
@@ -62,6 +60,7 @@ def parse(self, data):
62
60
),
63
61
'bytes_received' : int (client .bytes_received ),
64
62
'bytes_sent' : int (client .bytes_sent ),
63
+ 'common_name' : client .common_name ,
65
64
}
66
65
local_addresses = [
67
66
str (route .virtual_address )
@@ -70,19 +69,57 @@ def parse(self, data):
70
69
]
71
70
if local_addresses :
72
71
client_properties ['local_addresses' ] = local_addresses
73
- # if there are multiple nodes with the same common name
74
- # and host address, add the port to the node ID
75
- node_id = f'{ client .common_name } ,{ address .host } '
76
- if node_id in special_cases :
77
- node_id = f'{ node_id } :{ address .port } '
72
+ node_id = self .get_node_id (client , special_cases )
78
73
graph .add_node (node_id , ** client_properties )
79
74
# add links in routing table to graph
80
75
for link in links :
81
76
if link .common_name == 'UNDEF' :
82
77
continue
83
- address = link .real_address
84
- target_id = f'{ link .common_name } ,{ address .host } '
85
- if target_id in special_cases :
86
- target_id = f'{ target_id } :{ address .port } '
78
+ target_id = self .get_target_id (link , special_cases )
87
79
graph .add_edge (server , str (target_id ), weight = 1 )
88
80
return graph
81
+
82
+ def get_node_id (self , client , special_cases ):
83
+ """
84
+ when duplicate_cn is True
85
+ if there are multiple nodes with the same common name
86
+ and host address, add the port to the node ID
87
+ when self.duplicate_cn is False:
88
+ just use the common_name as node ID
89
+ """
90
+ if not self .duplicate_cn :
91
+ return client .common_name
92
+ address = client .real_address
93
+ node_id = f'{ client .common_name } ,{ address .host } '
94
+ if node_id in special_cases :
95
+ node_id = f'{ node_id } :{ address .port } '
96
+ return node_id
97
+
98
+ def get_target_id (self , link , special_cases ):
99
+ """
100
+ when duplicate_cn is True
101
+ if there are multiple nodes with the same common name
102
+ and host address, add the port to the target ID
103
+ when self.duplicate_cn is False:
104
+ just use the common_name as target ID
105
+ """
106
+ if not self .duplicate_cn :
107
+ return link .common_name
108
+ address = link .real_address
109
+ target_id = f'{ link .common_name } ,{ address .host } '
110
+ if target_id in special_cases :
111
+ target_id = f'{ target_id } :{ address .port } '
112
+ return target_id
113
+
114
+ def _find_special_cases (self , clients ):
115
+ if not self .duplicate_cn :
116
+ return []
117
+ id_list = []
118
+ special_cases = []
119
+ for client in clients :
120
+ id_ = f'{ client .common_name } ,{ client .real_address .host } '
121
+ if id_ in id_list :
122
+ special_cases .append (id_ )
123
+ continue
124
+ id_list .append (id_ )
125
+ return special_cases
0 commit comments