Esempio n. 1
0
    def check(self):
        station_dump = get_station_dump()
        traffic = dict()

        for sta in station_dump['band2g'] + station_dump['band5g']:
            mac = sta['MAC']
            traffic[mac] = sta['rxBytes'] + sta['txBytes']

        for sta in self.client_traffic.keys():
            if sta not in traffic:
                del self.client_traffic[sta]

        for sta, bytes in traffic.items():
            if sta not in self.client_traffic:
                self.client_traffic[sta] = {
                    'bytes': bytes,
                    'history': deque(maxlen=self.window_size),
                    'triggered': False
                }
            else:
                self.client_traffic[sta]['history'].append(
                    bytes - self.client_traffic[sta]['bytes'])
                self.client_traffic[sta]['bytes'] = bytes

        stations = []

        for sta, stats in self.client_traffic.items():
            if len(stats['history']) < self.window_size:
                continue

            if not stats['triggered']:
                if all(
                    [t > settings.TRAFFIC_THRESHOLD
                     for t in stats['history']]):
                    stations.append(sta)
                    stats['triggered'] = True
            else:
                if all(
                    [t < settings.TRAFFIC_THRESHOLD
                     for t in stats['history']]):
                    stats['triggered'] = False

        if len(stations) == 0:
            return

        logger.debug("Detected massive traffic from clients: %s" %
                     (' '.join(stations)))

        msg = json.dumps({'action': 'triggerAlgorithm', 'clients': stations})
        try:
            conn = socket.create_connection(
                (self.host, settings.PUBLIC_TCP_PORT),
                settings.CONNECTION_TIMEOUT_SEC * 1000)
            conn.sendall(msg)
            conn.close()
        except:
            logger.exception("Failed to send msg.")
  def check(self):
    station_dump = get_station_dump()
    traffic = dict()

    for sta in station_dump['band2g'] + station_dump['band5g']:
      mac = sta['MAC']
      traffic[mac] = sta['rxBytes'] + sta['txBytes']

    for sta in self.client_traffic.keys():
      if sta not in traffic:
        del self.client_traffic[sta]

    for sta, bytes in traffic.items():
      if sta not in self.client_traffic:
        self.client_traffic[sta] = {'bytes': bytes, 'history': deque(maxlen=self.window_size), 'triggered': False}
      else:
        self.client_traffic[sta]['history'].append(bytes - self.client_traffic[sta]['bytes'])
        self.client_traffic[sta]['bytes'] = bytes


    stations = []

    for sta, stats in self.client_traffic.items():
      if len(stats['history']) < self.window_size:
        continue

      if not stats['triggered']:
        if all([t > settings.TRAFFIC_THRESHOLD for t in stats['history']]):
          stations.append(sta)
          stats['triggered'] = True
      else:
        if all([t < settings.TRAFFIC_THRESHOLD for t in stats['history']]):
          stats['triggered'] = False

    if len(stations) == 0:
      return

    logger.debug("Detected massive traffic from clients: %s" % (' '.join(stations)))

    msg = json.dumps({'action': 'triggerAlgorithm', 'clients': stations})
    try:
      conn = socket.create_connection((self.host, settings.PUBLIC_TCP_PORT), settings.CONNECTION_TIMEOUT_SEC*1000)
      conn.sendall(msg)
      conn.close()
    except:
      logger.exception("Failed to send msg.")
    def send_heartbeat(self):
        heartbeart = dict()
        heartbeart["apStatus"] = get_ap_status()
        heartbeart["apScan"] = get_ap_scan()
        heartbeart["stationDump"] = get_station_dump()
        logger.debug("Sending heartbeat: %s" % (json.dumps(heartbeart)))

        headers = {"Content-type": "application/json"}

        try:
            conn = httplib.HTTPConnection(self.host, strict=True)
            conn.request("POST", self.path, json.dumps(heartbeart), headers)
            respose = conn.getresponse()
            if respose.status != httplib.OK:
                raise Exception(respose.reason)
            logger.debug("Successfully sent heartbeat to %s%s", self.host, self.path)
        except:
            logger.exception("Failed to send heartbeat.")
Esempio n. 4
0
    def handle(self):
        station_dump = get_station_dump()
        stations = station_dump['band2g'] + station_dump['band5g']

        for sta in stations:
            if sta['MAC'] not in self.request['clients']:
                logger.debug("Skipping %s: not in targets." % (sta['MAC']))
                continue

            if 'IP' not in sta:
                logger.debug("Skipping %s: no IP address." % (sta['MAC']))
                continue

            logger.debug("Forwarding to %s" % (sta['MAC']))
            try:
                conn = socket.create_connection(
                    (sta['IP'], settings.PUBLIC_TCP_PORT),
                    settings.CONNECTION_TIMEOUT_SEC * 1000)
                conn.sendall(json.dumps(self.request))
                conn.close()
            except:
                logger.exception("Failed to forward message to client.")
Esempio n. 5
0
    def handle(self):
        if self.request['action'] == 'startJamming':
            jamming_channel = self.request['jammingChannel']
            logger.debug("Jamming channel %d" % (jamming_channel))
            utils.set_channel(jamming_channel)
            stations = [
                sta for sta in get_station_dump()['band2g'] if 'IP' in sta
            ]
            if len(stations) == 0:
                logger.debug("Not stations, can not jam.")
            else:
                client_ip = stations[0]['IP']
                subprocess.Popen('iperf -c %s -u -b 72M -t 1000000' %
                                 (client_ip),
                                 shell=True)
        elif self.request['action'] == 'stopJamming':
            logger.debug("Stop Jamming")
            try:
                subprocess.check_call('pgrep -f "iperf" | xargs kill -9',
                                      shell=True)
            except:
                pass

        self.send_reply()