Skip to content
Snippets Groups Projects
Commit 4d0ecd01 authored by Pavel Kácha's avatar Pavel Kácha
Browse files

LaBrea: More Py3 compat, remove support for IPv6 (LaBrea does not support it at all)

parent cb54d16e
No related branches found
No related tags found
No related merge requests found
...@@ -7,7 +7,6 @@ import re ...@@ -7,7 +7,6 @@ import re
import time import time
import optparse import optparse
import signal import signal
import contextlib
import uuid import uuid
import json import json
import socket import socket
...@@ -28,6 +27,17 @@ try: ...@@ -28,6 +27,17 @@ try:
except ImportError: except ImportError:
from ordereddict import OrderedDict from ordereddict import OrderedDict
try:
from contextlib import nested
except ImportError:
from contextlib import ExitStack, contextmanager
@contextmanager
def nested(*contexts):
with ExitStack() as stack:
for ctx in contexts:
stack.enter_context(ctx)
yield contexts
class WindowContextMgr(object): class WindowContextMgr(object):
...@@ -46,7 +56,7 @@ class WindowContextMgr(object): ...@@ -46,7 +56,7 @@ class WindowContextMgr(object):
def expire_queue(self, queue, window): def expire_queue(self, queue, window):
aggr_events = [] aggr_events = []
ctx_to_del = [] ctx_to_del = []
for ctx, timestamp in queue.iteritems(): for ctx, timestamp in queue.items():
if timestamp >= self.update_timestamp - window: if timestamp >= self.update_timestamp - window:
break break
closed = self.ctx_close(self.contexts[ctx]) closed = self.ctx_close(self.contexts[ctx])
...@@ -304,7 +314,7 @@ class IdeaGen(object): ...@@ -304,7 +314,7 @@ class IdeaGen(object):
def gen_idea(self, src, src_ports, targets, detect_time, event_time, cease_time, count, template): def gen_idea(self, src, src_ports, targets, detect_time, event_time, cease_time, count, template):
tmpl = self.template[template] tmpl = self.template[template]
isource = { isource = {
"IP6" if ":" in src else "IP4": [src], "IP4": [src],
"Proto": tmpl["proto"] "Proto": tmpl["proto"]
} }
if "source_type" in tmpl: if "source_type" in tmpl:
...@@ -318,12 +328,8 @@ class IdeaGen(object): ...@@ -318,12 +328,8 @@ class IdeaGen(object):
itargets = [] itargets = []
for ports, tgt in folded_tgt.items(): for ports, tgt in folded_tgt.items():
itarget = {"Proto": tmpl["proto"]} itarget = {"Proto": tmpl["proto"]}
tgts4 = [ip for ip in tgt if ":" not in ip] if tgt:
tgts6 = [ip for ip in tgt if ":" in ip] itarget["IP4"] = tgt
if tgts4:
itarget["IP4"] = tgts4
if tgts6:
itarget["IP6"] = tgts6
if ports: if ports:
itarget["Port"] = [int(port) for port in ports] itarget["Port"] = [int(port) for port in ports]
itargets.append(itarget) itargets.append(itarget)
...@@ -661,7 +667,7 @@ def main(): ...@@ -661,7 +667,7 @@ def main():
for context in [PingContextMgr, ConnectContextMgr, InboundContextMgr] for context in [PingContextMgr, ConnectContextMgr, InboundContextMgr]
] ]
with contextlib.nested(*files): with nested(*files):
for line_set in izip(*files): for line_set in izip(*files):
for line in line_set: for line in line_set:
if not line: if not line:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment