Esempio n. 1
0
    def _adv_poller(self):
        # give the system a few seconds to start the first time so that any expressions
        # due to nearby units will be rendered properly
        time.sleep(20)
        while True:
            try:
                logging.debug("polling pwngrid-peer for peers ...")

                grid_peers = grid.peers()
                new_peers = {}

                self._closest_peer = None
                for obj in grid_peers:
                    peer = Peer(obj)
                    new_peers[peer.identity()] = peer
                    if self._closest_peer is None:
                        self._closest_peer = peer

                # check who's gone
                to_delete = []
                for ident, peer in self._peers.items():
                    if ident not in new_peers:
                        to_delete.append(ident)

                for ident in to_delete:
                    self._on_lost_peer(self._peers[ident])
                    del self._peers[ident]

                for ident, peer in new_peers.items():
                    # check who's new
                    if ident not in self._peers:
                        self._peers[ident] = peer
                        self._on_new_peer(peer)
                    # update the rest
                    else:
                        self._peers[ident].update(peer)

            except Exception as e:
                logging.warning("error while polling pwngrid-peer: %s" % e)
                logging.debug(e, exc_info=True)

            time.sleep(3)
Esempio n. 2
0
    def _adv_poller(self):
        while True:
            logging.debug("polling pwngrid-peer for peers ...")

            try:
                grid_peers = grid.peers()
                new_peers = {}

                self._closest_peer = None
                for obj in grid_peers:
                    peer = Peer(obj)
                    new_peers[peer.identity()] = peer
                    if self._closest_peer is None:
                        self._closest_peer = peer

                # check who's gone
                to_delete = []
                for ident, peer in self._peers.items():
                    if ident not in new_peers:
                        self._view.on_lost_peer(peer)
                        plugins.on('peer_lost', self, peer)
                        to_delete.append(ident)

                for ident in to_delete:
                    del self._peers[ident]

                for ident, peer in new_peers.items():
                    # check who's new
                    if ident not in self._peers:
                        self._peers[ident] = peer
                        self._view.on_new_peer(peer)
                        plugins.on('peer_detected', self, peer)
                    # update the rest
                    else:
                        self._peers[ident].update(peer)

            except Exception as e:
                logging.exception("error while polling pwngrid-peer")

            time.sleep(1)
Esempio n. 3
0
    def _adv_poller(self):
        while True:
            try:
                logging.debug("polling pwngrid-peer for peers ...")

                grid_peers = grid.peers()
                new_peers = {}

                self._closest_peer = None
                for obj in grid_peers:
                    peer = Peer(obj)
                    new_peers[peer.identity()] = peer
                    if self._closest_peer is None:
                        self._closest_peer = peer

                # check who's gone
                to_delete = []
                for ident, peer in self._peers.items():
                    if ident not in new_peers:
                        to_delete.append(ident)

                for ident in to_delete:
                    self._on_lost_peer(self._peers[ident])
                    del self._peers[ident]

                for ident, peer in new_peers.items():
                    # check who's new
                    if ident not in self._peers:
                        self._peers[ident] = peer
                        self._on_new_peer(peer)
                    # update the rest
                    else:
                        self._peers[ident].update(peer)

            except Exception as e:
                logging.warning("error while polling pwngrid-peer: %s" % e)
                logging.debug(e, exc_info=True)

            time.sleep(3)
Esempio n. 4
0
 def __init__(self, iface, name, version, identity, period=0.3, data={}):
     self._iface = iface
     self._period = period
     self._running = False
     self._stopped = threading.Event()
     self._peers_lock = threading.Lock()
     self._adv_lock = threading.Lock()
     self._new_peer_cb = _dummy_peer_cb
     self._lost_peer_cb = _dummy_peer_cb
     self._peers = {}
     self._frame = None
     self._me = Peer(new_session_id(), 0, 0, {
         'name': name,
         'version': version,
         'identity': identity,
         'face': faces.FRIEND,
         'pwnd_run': 0,
         'pwnd_tot': 0,
         'uptime': 0,
         'epoch': 0,
         'data': data
     })
     self.update()
Esempio n. 5
0
    def _on_advertisement(self, src_session_id, channel, rssi, adv):
        ident = adv['identity']
        with self._peers_lock:
            if ident not in self._peers:
                peer = Peer(src_session_id, channel, rssi, adv)
                core.log("detected unit %s (v%s) on channel %d (%s dBm) [sid:%s pwnd_tot:%d uptime:%d]" % ( \
                    peer.full_name(),
                    peer.version(),
                    channel,
                    rssi,
                    src_session_id,
                    peer.pwnd_total(),
                    peer.uptime()))

                self._peers[ident] = peer
                self._new_peer_cb(peer)
            else:
                self._peers[ident].update(src_session_id, channel, rssi, adv)
Esempio n. 6
0
    def _parse_stats(self):
        self.duration = ''
        self.duration_human = ''
        self.deauthed = 0
        self.associated = 0
        self.handshakes = 0
        self.epochs = 0
        self.train_epochs = 0
        self.peers = 0
        self.last_peer = None
        self.min_reward = 1000
        self.max_reward = -1000
        self.avg_reward = 0

        started_at = None
        stopped_at = None
        cache = {}

        for line in self.last_session:
            parts = line.split(']')
            if len(parts) < 2:
                continue

            try:
                line_timestamp = parts[0].strip('[')
                line = ']'.join(parts[1:])
                stopped_at = self._parse_datetime(line_timestamp)
                if started_at is None:
                    started_at = stopped_at

                if LastSession.DEAUTH_TOKEN in line and line not in cache:
                    self.deauthed += 1
                    cache[line] = 1

                elif LastSession.ASSOC_TOKEN in line and line not in cache:
                    self.associated += 1
                    cache[line] = 1

                elif LastSession.HANDSHAKE_TOKEN in line and line not in cache:
                    self.handshakes += 1
                    cache[line] = 1

                elif LastSession.TRAINING_TOKEN in line:
                    self.train_epochs += 1

                elif LastSession.EPOCH_TOKEN in line:
                    self.epochs += 1
                    m = LastSession.EPOCH_PARSER.findall(line)
                    if m:
                        epoch_num, epoch_data = m[0]
                        m = LastSession.EPOCH_DATA_PARSER.findall(epoch_data)
                        for key, value in m:
                            if key == 'reward':
                                reward = float(value)
                                self.avg_reward += reward
                                if reward < self.min_reward:
                                    self.min_reward = reward

                                elif reward > self.max_reward:
                                    self.max_reward = reward

                elif LastSession.PEER_TOKEN in line:
                    m = self._peer_parser.findall(line)
                    if m:
                        name, pubkey, rssi, sid, pwnd_tot, uptime = m[0]
                        if pubkey not in cache:
                            self.last_peer = Peer({
                                'session_id': sid,
                                'channel': 1,
                                'rssi': int(rssi),
                                'identity': pubkey,
                                'advertisement':{
                                    'name': name,
                                    'pwnd_tot': int(pwnd_tot)
                                }})
                            self.peers += 1
                            cache[pubkey] = self.last_peer
                        else:
                            cache[pubkey].adv['pwnd_tot'] = pwnd_tot
            except Exception as e:
                logging.error("error parsing line '%s': %s" % (line, e))

        if started_at is not None:
            self.duration = stopped_at - started_at
            mins, secs = divmod(self.duration, 60)
            hours, mins = divmod(mins, 60)
        else:
            hours = mins = secs = 0

        self.duration = '%02d:%02d:%02d' % (hours, mins, secs)
        self.duration_human = []
        if hours > 0:
            self.duration_human.append('%d %s' % (hours, self.voice.hhmmss(hours, 'h')))
        if mins > 0:
            self.duration_human.append('%d %s' % (mins, self.voice.hhmmss(mins, 'm')))
        if secs > 0:
            self.duration_human.append('%d %s' % (secs, self.voice.hhmmss(secs, 's')))

        self.duration_human = ', '.join(self.duration_human)
        self.avg_reward /= (self.epochs if self.epochs else 1)
Esempio n. 7
0
    def _parse_stats(self):
        self.duration = ''
        self.duration_human = ''
        self.deauthed = 0
        self.associated = 0
        self.handshakes = 0
        self.epochs = 0
        self.train_epochs = 0
        self.peers = 0
        self.last_peer = None
        self.min_reward = 1000
        self.max_reward = -1000
        self.avg_reward = 0

        started_at = None
        stopped_at = None
        cache = {}

        for line in self.last_session:
            parts = line.split(']')
            if len(parts) < 2:
                continue
            line_timestamp = parts[0].strip('[')
            line = ']'.join(parts[1:])
            stopped_at = self._parse_datetime(line_timestamp)
            if started_at is None:
                started_at = stopped_at

            if SessionParser.DEAUTH_TOKEN in line and line not in cache:
                self.deauthed += 1
                cache[line] = 1

            elif SessionParser.ASSOC_TOKEN in line and line not in cache:
                self.associated += 1
                cache[line] = 1

            elif SessionParser.HANDSHAKE_TOKEN in line and line not in cache:
                self.handshakes += 1
                cache[line] = 1

            elif SessionParser.TRAINING_TOKEN in line:
                self.train_epochs += 1

            elif SessionParser.EPOCH_TOKEN in line:
                self.epochs += 1
                m = SessionParser.EPOCH_PARSER.findall(line)
                if m:
                    epoch_num, epoch_data = m[0]
                    m = SessionParser.EPOCH_DATA_PARSER.findall(epoch_data)
                    for key, value in m:
                        if key == 'reward':
                            reward = float(value)
                            self.avg_reward += reward
                            if reward < self.min_reward:
                                self.min_reward = reward

                            elif reward > self.max_reward:
                                self.max_reward = reward

            elif SessionParser.PEER_TOKEN in line:
                m = self._peer_parser.findall(line)
                if m:
                    name, pubkey, rssi, sid, pwnd_tot, uptime = m[0]
                    if pubkey not in cache:
                        self.last_peer = Peer(
                            sid, 1, int(rssi), {
                                'name': name,
                                'identity': pubkey,
                                'pwnd_tot': int(pwnd_tot)
                            })
                        self.peers += 1
                        cache[pubkey] = self.last_peer
                    else:
                        cache[pubkey].adv['pwnd_tot'] = pwnd_tot

        if started_at is not None:
            self.duration = stopped_at - started_at
            mins, secs = divmod(self.duration, 60)
            hours, mins = divmod(mins, 60)
        else:
            hours = mins = secs = 0

        self.duration = '%02d:%02d:%02d' % (hours, mins, secs)
        self.duration_human = []
        if hours > 0:
            self.duration_human.append('%d hours' % hours)
        if mins > 0:
            self.duration_human.append('%d minutes' % mins)
        if secs > 0:
            self.duration_human.append('%d seconds' % secs)

        self.duration_human = ', '.join(self.duration_human)
        self.avg_reward /= self.epochs