Revision 97e42c7d flowspec/models.py
b/flowspec/models.py | ||
---|---|---|
12 | 12 |
from time import sleep |
13 | 13 |
|
14 | 14 |
from flowspy.utils import beanstalkc |
15 |
from flowspy.utils.randomizer import id_generator as id_gen |
|
15 | 16 |
|
16 | 17 |
|
17 | 18 |
FORMAT = '%(asctime)s %(levelname)s: %(message)s' |
... | ... | |
38 | 39 |
("sample", "Sample") |
39 | 40 |
) |
40 | 41 |
|
42 |
ROUTE_STATES = ( |
|
43 |
("ACTIVE", "ACTIVE"), |
|
44 |
("ERROR", "ERROR"), |
|
45 |
("EXPIRED", "EXPIRED"), |
|
46 |
("PENDING", "PENDING"), |
|
47 |
("OUTOFSYNC", "OUTOFSYNC"), |
|
48 |
("INACTIVE", "INACTIVE"), |
|
49 |
) |
|
50 |
|
|
41 | 51 |
|
42 | 52 |
def days_offset(): return datetime.now() + timedelta(days = settings.EXPIRATION_DAYS_OFFSET) |
43 | 53 |
|
44 | 54 |
class MatchPort(models.Model): |
45 |
port = models.CharField(max_length=24) |
|
55 |
port = models.CharField(max_length=24, unique=True)
|
|
46 | 56 |
def __unicode__(self): |
47 | 57 |
return self.port |
48 | 58 |
class Meta: |
... | ... | |
60 | 70 |
action = models.CharField(max_length=60, choices=THEN_CHOICES, verbose_name="Action") |
61 | 71 |
action_value = models.CharField(max_length=255, blank=True, null=True, verbose_name="Action Value") |
62 | 72 |
def __unicode__(self): |
63 |
return "%s: %s" %(self.action, self.action_value) |
|
73 |
ret = "%s:%s" %(self.action, self.action_value) |
|
74 |
return ret.rstrip(":") |
|
64 | 75 |
class Meta: |
65 | 76 |
db_table = u'then_action' |
66 | 77 |
|
... | ... | |
82 | 93 |
then = models.ManyToManyField(ThenAction, verbose_name="Then") |
83 | 94 |
filed = models.DateTimeField(auto_now_add=True) |
84 | 95 |
last_updated = models.DateTimeField(auto_now=True) |
85 |
is_online = models.BooleanField(default=False) |
|
86 |
is_active = models.BooleanField(default=False) |
|
96 |
status = models.CharField(max_length=20, choices=ROUTE_STATES, blank=True, null=True, verbose_name="Status", default="PENDING") |
|
97 |
# is_online = models.BooleanField(default=False) |
|
98 |
# is_active = models.BooleanField(default=False) |
|
87 | 99 |
expires = models.DateField(default=days_offset, blank=True, null=True,) |
88 | 100 |
response = models.CharField(max_length=512, blank=True, null=True) |
89 | 101 |
comments = models.TextField(null=True, blank=True, verbose_name="Comments") |
... | ... | |
93 | 105 |
return self.name |
94 | 106 |
|
95 | 107 |
class Meta: |
96 |
unique_together = (("name", "is_active"),) |
|
97 | 108 |
db_table = u'route' |
98 | 109 |
|
110 |
def save(self, *args, **kwargs): |
|
111 |
if not self.pk: |
|
112 |
hash = id_gen() |
|
113 |
self.name = "%s_%s" %(self.name, hash) |
|
114 |
super(Route, self).save(*args, **kwargs) # Call the "real" save() method. |
|
115 |
|
|
116 |
|
|
99 | 117 |
def clean(self, *args, **kwargs): |
100 | 118 |
from django.core.exceptions import ValidationError |
101 | 119 |
if self.destination: |
... | ... | |
122 | 140 |
# logger.info("Got save job id: %s" %response) |
123 | 141 |
|
124 | 142 |
def commit_add(self, *args, **kwargs): |
125 |
send_message("Adding route %s. Please wait..." %self.name, self.applier) |
|
143 |
peer = self.applier.get_profile().peer.domain_name |
|
144 |
send_message("[%s] Adding route %s. Please wait..." %(self.applier.username, self.name), peer) |
|
126 | 145 |
response = add.delay(self) |
127 | 146 |
logger.info("Got save job id: %s" %response) |
128 | 147 |
|
129 | 148 |
def deactivate(self): |
130 |
self.is_online = False |
|
131 |
self.is_active = False |
|
149 |
self.status = "INACTIVE" |
|
132 | 150 |
self.save() |
133 | 151 |
# def delete(self, *args, **kwargs): |
134 | 152 |
# response = delete.delay(self) |
135 | 153 |
# logger.info("Got delete job id: %s" %response) |
136 | 154 |
|
137 | 155 |
def commit_edit(self, *args, **kwargs): |
138 |
send_message("Editing route %s. Please wait..." %self.name, self.applier) |
|
156 |
peer = self.applier.get_profile().peer.domain_name |
|
157 |
send_message("[%s] Editing route %s. Please wait..." %(self.applier.username, self.name), peer) |
|
139 | 158 |
response = edit.delay(self) |
140 | 159 |
logger.info("Got edit job id: %s" %response) |
141 | 160 |
|
142 | 161 |
def commit_delete(self, *args, **kwargs): |
143 |
send_message("Removing route %s. Please wait..." %self.name, self.applier) |
|
162 |
peer = self.applier.get_profile().peer.domain_name |
|
163 |
send_message("[%s] Removing route %s. Please wait..." %(self.applier.username, self.name), peer) |
|
144 | 164 |
response = delete.delay(self) |
145 | 165 |
logger.info("Got edit job id: %s" %response) |
146 | 166 |
# |
147 | 167 |
# def delete(self, *args, **kwargs): |
148 | 168 |
# response = delete.delay(self) |
149 | 169 |
# logger.info("Got delete job id: %s" %response) |
150 |
def is_synced(self): |
|
151 |
|
|
170 |
|
|
171 |
def is_synced(self): |
|
152 | 172 |
found = False |
153 | 173 |
get_device = PR.Retriever() |
154 | 174 |
device = get_device.fetch_device() |
155 | 175 |
try: |
156 | 176 |
routes = device.routing_options[0].routes |
157 | 177 |
except Exception as e: |
158 |
self.is_online = False
|
|
178 |
self.status = "EXPIRED"
|
|
159 | 179 |
self.save() |
160 | 180 |
logger.error("No routing options on device. Exception: %s" %e) |
161 | 181 |
return False |
... | ... | |
230 | 250 |
logger.info('Protocol fields do not match') |
231 | 251 |
except: |
232 | 252 |
pass |
233 |
if found and not self.is_online:
|
|
253 |
if found and self.status != "ACTIVE":
|
|
234 | 254 |
logger.error('Rule is applied on device but appears as offline') |
235 | 255 |
found = False |
236 | 256 |
|
... | ... | |
252 | 272 |
def get_match(self): |
253 | 273 |
ret = '' |
254 | 274 |
if self.destination: |
255 |
ret = '%s Destination Address:<strong>%s</strong><br/>' %(ret, self.destination)
|
|
275 |
ret = '%s Dst Addr:<strong>%s</strong><br/>' %(ret, self.destination)
|
|
256 | 276 |
if self.fragmenttype: |
257 | 277 |
ret = "%s Fragment Type:<strong>%s</strong><br/>" %(ret, self.fragmenttype) |
258 | 278 |
if self.icmpcode: |
... | ... | |
264 | 284 |
if self.protocol: |
265 | 285 |
ret = "%s Protocol:<strong>%s</strong><br/>" %(ret, self.protocol) |
266 | 286 |
if self.source: |
267 |
ret = "%s Source Address:<strong>%s</strong><br/>" %(ret, self.source)
|
|
287 |
ret = "%s Src Addr:<strong>%s</strong><br/>" %(ret, self.source)
|
|
268 | 288 |
if self.tcpflag: |
269 | 289 |
ret = "%s TCP flag:<strong>%s</strong><br/>" %(ret, self.tcpflag) |
270 | 290 |
if self.port: |
... | ... | |
272 | 292 |
ret = ret + "Port:<strong>%s</strong><br/>" %(port) |
273 | 293 |
if self.destinationport: |
274 | 294 |
for port in self.destinationport.all(): |
275 |
ret = ret + "Destination Port:<strong>%s</strong><br/>" %(port)
|
|
295 |
ret = ret + "Dst Port:<strong>%s</strong><br/>" %(port)
|
|
276 | 296 |
if self.sourceport: |
277 | 297 |
for port in self.sourceport.all(): |
278 |
ret = ret +"Source Port:<strong>%s</strong><br/>" %(port)
|
|
298 |
ret = ret +"Src Port:<strong>%s</strong><br/>" %(port)
|
|
279 | 299 |
if self.dscp: |
280 | 300 |
for dscp in self.dscp.all(): |
281 | 301 |
ret = ret + "%s Port:<strong>%s</strong><br/>" %(ret, dscp) |
... | ... | |
285 | 305 |
get_match.allow_tags = True |
286 | 306 |
|
287 | 307 |
def send_message(msg, user): |
288 |
username = user.username |
|
308 |
# username = user.username |
|
309 |
peer = user |
|
289 | 310 |
b = beanstalkc.Connection() |
290 | 311 |
b.use(settings.POLLS_TUBE) |
291 |
tube_message = json.dumps({'message': str(msg), 'username':username})
|
|
312 |
tube_message = json.dumps({'message': str(msg), 'username':peer})
|
|
292 | 313 |
b.put(tube_message) |
293 | 314 |
b.close() |
Also available in: Unified diff