Beispiel #1
0
def create_website_event(request):
    if request.method == 'POST':
        form = WebsiteEventForm(request.POST)
        if form.is_valid():
            website = form.cleaned_data['website']
            first_detection = form.cleaned_data['first_detection']
            event_type = form.cleaned_data['event_type']
            location = Location.retrieve_location(form.cleaned_data['location'].split(', ')[0])

            logging.info(location)

            event = Event()
            event.active = True
            if first_detection:
                event.first_detection_utc = first_detection
            else:
                event.first_detection_utc = datetime.datetime.now()
            event.last_detection_utc  = datetime.datetime.now()
            event.target = website
            event.target_type = TargetType.Website
            if event_type:
                # 'censor', 'throttling', 'offline'
                if event_type == 'censor':
                    event.event_type = EventType.Censor
                elif event_type == 'throttling':
                    event.event_type = EventType.Throttling
                else:
                    event.event_type = EventType.Offline
            else:
                event.event_type = EventType.Offline

            logging.info(location)

            if location!=None:
                event.location_ids.append(location.id)
                event.location_names.append(location.fullname)
                event.location_country_names.append(location.country_name)
                event.location_country_codes.append(location.country_code)
                event.lats.append(location.lat)
                event.lons.append(location.lon)
                event.isps.append('')

            event.save()
            NotificationSystem.publishEvent(event)

            return HttpResponse(json.dumps(dict(status='OK',
                       msg='Website event added successfully!',
                       errors=None)))
        else:
            return HttpResponse(json.dumps(dict(status='FAILED',
                    msg='Failed to add event.',
                    errors=form.errors)))
    
    form = WebsiteEventForm()
    return render_to_response('gui/create_website_event.html', locals(), context_instance=RequestContext(request))
Beispiel #2
0
class DecisionSystem:
    def newReport(report):
        # TODO: decision about event creation
        # for now event is created directly from report

        logging.info("Report received in decision system")

        event = Event()
        event.first_detection_utc = datetime.datetime.now()
        event.last_detection_utc  = datetime.datetime.now()

        if isinstance(report, WebsiteReport):
            event.target = report.url
            event.target_type = TargetType.Website
        elif isinstance(report, ServiceReport):
            event.target = report.target
            event.target_type = TargetType.Service
        else:
            raise TypeError, 'report not supported'

        event.active = True

        # TODO: get correct event type
        event.event_type = EventType.Offline

        # TODO: get isp information
        # TODO: save information about target server

        try:
            event.location_ids.append(report.agent_location_id)
            event.location_names.append(report.agent_location_name)
            event.location_country_names.append(report.agent_country_name)
            event.location_country_codes.append(report.agent_country_code)
            event.lats.append(report.agent_lat)
            event.lons.append(report.agent_lon)

            # TODO: ISP!
            event.isps.append('')
            #isps = ListField()

            if len(report.trace):
                trace = [t.get_dict() for t in report.trace]
                trace_json = json.dumps(trace)
                event.latest_traces.append(trace_json)
#
#            # get blocked node from trace
#            if report.trace:
#                ip = report.trace.getBlockedNode().ip
#
#                # TODO: get geolocation information
#                blockedNode = EventBlockedNode()
#                blockedNode.ip = ip
#                blockedNode.city = "Unknown"
#                blockedNode.country = "Unknown"
#                blockedNode.latitude = random.randrange(-90,90,1)
#                blockedNode.longitude = random.randrange(0,180,1)
#                blockedNode.event = event
#                blockedNode.save()
#
#            # associate report to event
#            if isinstance(report, WebsiteReport):
#                eventReport = EventWebsiteReport(event=event, report=report)
#                eventReport.save()
#            elif isinstance(report, ServiceReport):
#                eventReport = EventServiceReport(event=event, report=report)
#                eventReport.save()
#
#            # save event location
#            # TODO: get location from agent ip
#            eventLocation = EventLocation()
#            eventLocation.city = "Unknown"
#            eventLocation.country = "Unknown"
#            eventLocation.latitude = random.randrange(-90,90,1)
#            eventLocation.longitude = random.randrange(0,180,1)
#            eventLocation.event = event
#            eventLocation.save()
#
        except Exception,ex:
            logging.error(ex)

        # save event
        event.save()

        # report event no notification system
        NotificationSystem.publishEvent(event)
#
#            # associate report to event
#            if isinstance(report, WebsiteReport):
#                eventReport = EventWebsiteReport(event=event, report=report)
#                eventReport.save()
#            elif isinstance(report, ServiceReport):
#                eventReport = EventServiceReport(event=event, report=report)
#                eventReport.save()
#
#            # save event location
#            # TODO: get location from agent ip
#            eventLocation = EventLocation()
#            eventLocation.city = "Unknown"
#            eventLocation.country = "Unknown"
#            eventLocation.latitude = random.randrange(-90,90,1)
#            eventLocation.longitude = random.randrange(0,180,1)
#            eventLocation.event = event
#            eventLocation.save()
#
        except Exception,ex:
            logging.error(ex)

        # save event
        event.save()

        # report event no notification system
        NotificationSystem.publishEvent(event)

    newReport = staticmethod(newReport)
    updateReport = staticmethod(updateReport)