diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2020-09-26 00:48:24 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2020-09-26 00:49:34 +0200 |
commit | 84712686a77d39f955673f75d33ca0291ed0c6e6 (patch) | |
tree | bb6093faee6d14d38c6fe7493081bcee66bd8038 /examples | |
parent | adce2272dc75d91b0dd3a9a5c502de99d828eca2 (diff) |
Centralized EventName validation and moved code parts.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/py-flow-info/flow-info.py | 105 |
1 files changed, 45 insertions, 60 deletions
diff --git a/examples/py-flow-info/flow-info.py b/examples/py-flow-info/flow-info.py index 77eb70c15..638c76581 100755 --- a/examples/py-flow-info/flow-info.py +++ b/examples/py-flow-info/flow-info.py @@ -12,66 +12,51 @@ def parse_json_str(json_str): j = nDPIsrvd.JsonParseBytes(json_str[0]) - if 'flow_event_name' in j: - event = j['flow_event_name'].lower() - if event == 'new': - event_str = 'New flow' - elif event == 'end': - event_str = 'End flow' - elif event == 'idle': - event_str = 'Idle flow' - elif event == 'detected': - event_str = 'Detected' - elif event == 'detection-update': - event_str = 'Update' - elif event == 'guessed': - event_str = 'Guessed' - elif event == 'not-detected': - event_str = 'Not detected' - else: - raise RuntimeError('unknown flow event name: {}'.format(event)) - - ndpi_proto_categ = '' - ndpi_frisk = '' - - if 'ndpi' in j: - if 'proto' in j['ndpi']: - ndpi_proto_categ += '[' + str(j['ndpi']['proto']) + ']' - - if 'category' in j['ndpi']: - ndpi_proto_categ += '[' + str(j['ndpi']['category']) + ']' - - if 'flow_risk' in j['ndpi']: - cnt = 0 - for key in j['ndpi']['flow_risk']: - ndpi_frisk += str(j['ndpi']['flow_risk'][key]) + ', ' - cnt += 1 - ndpi_frisk = '{}: {}'.format( - TermColor.WARNING + TermColor.BOLD + 'RISK' + TermColor.END if cnt < 2 - else TermColor.FAIL + TermColor.BOLD + TermColor.BLINK + 'RISK' + TermColor.END, - ndpi_frisk[:-2]) - - if j['l3_proto'] == 'ip4': - print('{:>14}: [{:.>6}] [{}][{:.>5}] [{:.>15}]{} -> [{:.>15}]{} {}'.format(event_str, - j['flow_id'], j['l3_proto'], j['l4_proto'], - j['src_ip'].lower(), - '[{:.>5}]'.format(j['src_port']) if 'src_port' in j else '', - j['dst_ip'].lower(), - '[{:.>5}]'.format(j['dst_port']) if 'dst_port' in j else '', - ndpi_proto_categ)) - elif j['l3_proto'] == 'ip6': - print('{:>14}: [{:.>6}] [{}][{:.>5}] [{:.>39}]{} -> [{:.>39}]{} {}'.format(event_str, - j['flow_id'], j['l3_proto'], j['l4_proto'], - j['src_ip'].lower(), - '[{:.>5}]'.format(j['src_port']) if 'src_port' in j else '', - j['dst_ip'].lower(), - '[{:.>5}]'.format(j['dst_port']) if 'dst_port' in j else '', - ndpi_proto_categ)) - else: - raise RuntimeError('unsupported l3 protocol: {}'.format(j['l3_proto'])) - - if len(ndpi_frisk) > 0: - print('{:>16}{}'.format('', ndpi_frisk)) + event_str = validateEventName(j) + if event_str is None: + raise RuntimeError('unknown flow event name: {}'.format(event)) + + ndpi_proto_categ = '' + ndpi_frisk = '' + + if 'ndpi' in j: + if 'proto' in j['ndpi']: + ndpi_proto_categ += '[' + str(j['ndpi']['proto']) + ']' + + if 'category' in j['ndpi']: + ndpi_proto_categ += '[' + str(j['ndpi']['category']) + ']' + + if 'flow_risk' in j['ndpi']: + cnt = 0 + for key in j['ndpi']['flow_risk']: + ndpi_frisk += str(j['ndpi']['flow_risk'][key]) + ', ' + cnt += 1 + ndpi_frisk = '{}: {}'.format( + TermColor.WARNING + TermColor.BOLD + 'RISK' + TermColor.END if cnt < 2 + else TermColor.FAIL + TermColor.BOLD + TermColor.BLINK + 'RISK' + TermColor.END, + ndpi_frisk[:-2]) + + if j['l3_proto'] == 'ip4': + print('{:>14}: [{:.>6}] [{}][{:.>5}] [{:.>15}]{} -> [{:.>15}]{} {}'.format(event_str, + j['flow_id'], j['l3_proto'], j['l4_proto'], + j['src_ip'].lower(), + '[{:.>5}]'.format(j['src_port']) if 'src_port' in j else '', + j['dst_ip'].lower(), + '[{:.>5}]'.format(j['dst_port']) if 'dst_port' in j else '', + ndpi_proto_categ)) + elif j['l3_proto'] == 'ip6': + print('{:>14}: [{:.>6}] [{}][{:.>5}] [{:.>39}]{} -> [{:.>39}]{} {}'.format(event_str, + j['flow_id'], j['l3_proto'], j['l4_proto'], + j['src_ip'].lower(), + '[{:.>5}]'.format(j['src_port']) if 'src_port' in j else '', + j['dst_ip'].lower(), + '[{:.>5}]'.format(j['dst_port']) if 'dst_port' in j else '', + ndpi_proto_categ)) + else: + raise RuntimeError('unsupported l3 protocol: {}'.format(j['l3_proto'])) + + if len(ndpi_frisk) > 0: + print('{:>16}{}'.format('', ndpi_frisk)) if __name__ == '__main__': |