-
Notifications
You must be signed in to change notification settings - Fork 24
/
objects.py
133 lines (100 loc) · 3 KB
/
objects.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
#Description : Initialization for malpipe modules
#Author : Silas Cutler ([email protected])
#Date : 2017 12 27
#==============================================================================
import time
import threading
import schedule
import feeds
import exporters
import processors
# Threat Status flags
class threatStatus:
ready = 0
start = 1
running = 2
error = 3
finished = 4
# FeedRunner creates a thread for each feed for parsing.
# Input: Feed() object
class FeedRunner(threading.Thread):
def __init__(self, _feedObject):
super(FeedRunner, self).__init__()
self.feed = _feedObject
self.meta = _feedObject.metadata
self.status = threatStatus.ready
interval = self.getInterval()
if interval is None:
self.setDaemon(True)
else:
interval.do( self.signal_start )
#Used for scheduled threads
def signal_start(self):
self.status = threatStatus.start
def getInterval(self):
return self.meta.interval
def getName(self):
return self.feed.get_module_name()
def run(self):
time.sleep(0.5)
print "Starting %s" % self.feed.get_module_name()
try:
while True:
if self.getInterval() is None:
self.status = threatStatus.running
self.feed.run()
self.status = threatStatus.finished
else:
if self.status == threatStatus.start:
self.status = threatStatus.running
self.feed.run()
self.status = threatStatus.ready
# else:
# print "%s waiting...%s" % (self.getName(), self.status)
time.sleep(30)
except Exception, e:
print "[%s] Module error (%s). Restarting." % (self.getName(), e)
self.status = threatStatus.error
print "[%s] Thread Exited" % self.getName()
class Feeds(object):
def __init__(self):
self._feeds = {}
for m in feeds.common.Modules.Feeds:
runner = FeedRunner(m)
self._feeds[m.metadata.module_name] = runner
def __iter__(self):
for elem in self._feeds.keys():
yield (elem, self._feeds[elem])
def __getitem__(self, item):
yield( self, self._feeds[item] )
def print_list(self):
print "[Feeds] %s" % ( ", ".join(self._feeds.keys()) )
def start(self):
for name in enumerate(self._feeds):
tFeed = self._feeds[name[1]]
tFeed.start()
class Processors(object):
def __init__(self):
self._feeds = {}
for m in processors.common.Modules.Processors:
self._feeds[m.metadata.module_name] = None
def __iter__(self):
for elem in self._feeds.keys():
yield (elem, self._feeds[elem])
def __getitem__(self, item):
yield( self, self._feeds[item] )
def print_list(self):
print "[Processors] %s" % ( ", ".join(self._feeds.keys()) )
class Exporters(object):
def __init__(self):
self._feeds = {}
for m in exporters.common.Modules.Exporters:
self._feeds[m.metadata.module_name] = None
def __iter__(self):
for elem in self._feeds.keys():
yield (elem, self._feeds[elem])
def __getitem__(self, item):
yield( self, self._feeds[item] )
def print_list(self):
print "[Exporters] %s" % ( ", ".join(self._feeds.keys()) )