Esempio n. 1
0
 def get_value(self, table_name, key, default=None):
     result = self.db_conn.select("select * from " + table_name + " where key=?", (key,))
     try:
         return self.unpack(result[0][1])
     except:
         g_logger.warning("No value found for key '%s' in db kvp." % key)
         return default
Esempio n. 2
0
    def _super_peer_connected(self,
                              peer_id,
                              ip,
                              port,
                              ciphered_public_key=None,
                              network_id=0):
        if self.agent_is_banned(peer_id) or self.network_is_banned(ip):
            g_logger.info("Super agent %s is banned or is running from "
                          "a banned network %s" % (peer_id, ip))
            if peer_id in self.super_peers:
                self.remove_super_peer(peer_id)
            return False

        if peer_id in self.super_peers and \
           self.super_peers[peer_id].status == 'Connected':
            g_logger.warning("Peer %s already connected." % peer_id)
            return False

        if peer_id in self.super_peers:
            g_logger.debug("Peer id %s already exists in super peer list." %
                           peer_id)
            self.super_peers[peer_id].status = 'Connected'
        else:
            peer_entry = PeerEntry()
            peer_entry.Type = 1
            peer_entry.ID = peer_id
            peer_entry.IP = ip
            peer_entry.Port = port
            peer_entry.CipheredPublicKey = ciphered_public_key
            peer_entry.status = 'Connected'
            peer_entry.network_id = network_id
            self.super_peers[peer_entry.ID] = peer_entry

        self.connected_speer_num = self.connected_speer_num + 1
        return True
    def _normal_peer_connected(self, peer_id, ip, port, ciphered_public_key=None,
                              network_id=0):
        if self.agent_is_banned(peer_id) or self.network_is_banned(ip):
            g_logger.info("Desktop agent %s is banned or is running from "
                          "a banned network %s" % (peer_id, ip))
            if peer_id in self.normal_peers:
                self.remove_normal_peer(peer_id)
            return False

        if peer_id in self.normal_peers and \
           self.normal_peers[peer_id].status == 'Connected':
            g_logger.warning("Peer %s already connected." % peer_id)
            return False

        if peer_id in self.normal_peers:
            g_logger.debug("Peer id %s already exists in normal peer list." %
                           peer_id)
            self.normal_peers[peer_id].status = 'Connected'
        else:
            peer_entry = PeerEntry()
            peer_entry.Type = 2
            peer_entry.ID = peer_id
            peer_entry.IP = ip
            peer_entry.Port = port
            peer_entry.CipheredPublicKey = ciphered_public_key
            peer_entry.status = 'Connected'
            peer_entry.network_id = network_id
            self.normal_peers[peer_entry.ID] = peer_entry

        self.connected_peer_num = self.connected_peer_num + 1
        
        return True
Esempio n. 4
0
    def get_information(self, key, default="0"):

        try:
            result = self.db_conn.select("select value from information where key=?", (key,))
            return result[0]
        except:
            g_logger.warning("No value found for key '%s' in SET Information." % key)
            return default
Esempio n. 5
0
 def get_value(self, table_name, key, default=None):
     result = self.db_conn.select(
         "select * from " + table_name + " where key=?", (key, ))
     try:
         return self.unpack(result[0][1])
     except:
         g_logger.warning("No value found for key '%s' in db kvp." % key)
         return default
Esempio n. 6
0
 def get_status(self, key, default="0"):
     """
     """
     try:
         result = self.db_conn.select("select value from stats where key=?", (key,))
         return result[0]
     except:
         g_logger.warning("No value found for key '%s' in SET stats." % key)
         return default
Esempio n. 7
0
 def read(self, key, default=None):
     if not self.db_conn:
         raise Exception('No connection to database.')
     result = self.db_conn.select(self.sql_select, (key, ))
     try:
         return self.unpack(result[0][1])
     except:
         g_logger.warning("No value found for key '%s' in db config." % key)
         return default
Esempio n. 8
0
 def read(self, key, default=None):
     if not self.db_conn:
         raise Exception("No connection to database.")
     result = self.db_conn.select(self.sql_select, (key,))
     try:
         return self.unpack(result[0][1])
     except:
         g_logger.warning("No value found for key '%s' in db config." % key)
         return default
Esempio n. 9
0
 def _normal_peer_disconnected(self, peer_id):
     if peer_id not in self.normal_peers:
         g_logger.warning("Peer id %s is not in normal peer list." % \
                          (peer_id, ip))
         return False
     # will not remove the peer entry from the list
     self.normal_peers[peer_id].status = 'Disconnected'
     self.connected_peer_num = self.connected_peer_num - 1
     return True
 def _normal_peer_disconnected(self, peer_id):
     if peer_id not in self.normal_peers:
         g_logger.warning("Peer id %s is not in normal peer list." % \
                          (peer_id, ip))
         return False
     # will not remove the peer entry from the list
     self.normal_peers[peer_id].status = 'Disconnected'
     self.connected_peer_num = self.connected_peer_num - 1
     return True
Esempio n. 11
0
    def get_information(self, key, default="0"):

        try:
            result = self.db_conn.select(
                "select value from information where key=?", (key, ))
            return result[0]
        except:
            g_logger.warning(
                "No value found for key '%s' in SET Information." % key)
            return default
Esempio n. 12
0
 def get_status(self, key, default="0"):
     """
     """
     try:
         result = self.db_conn.select("select value from stats where key=?",
                                      (key, ))
         return result[0]
     except:
         g_logger.warning("No value found for key '%s' in SET stats." % key)
         return default
Esempio n. 13
0
    def checkArgs(self):
        if not self.host or not self.port:
            g_logger.warning("%s test missing Host and Port." %
                             self.service_name)
            return False

        if not self.username and not self.password:
            g_logger.warning("%s test missing credentials. Host: %s, Port: %d" %
                             (self.service_name, self.host, self.port))
            return False

        return True
Esempio n. 14
0
    def get_random_peer_connected(self):
        id_list = []
        for peer_entry in self.normal_peers.values():
            if peer_entry.status == 'Connected' and \
               peer_entry.ID in self.sessions:
                id_list.append(peer_entry.ID)

        if len(id_list) == 0:
            g_logger.warning("No available peer.")
            return None
        else:
            idx = random.randint(0, len(id_list) - 1)
            return id_list[idx]
    def get_random_peer_connected(self):
        id_list = []
        for peer_entry in self.normal_peers.values():
            if peer_entry.status == 'Connected' and \
               peer_entry.ID in self.sessions:
                id_list.append(peer_entry.ID)

        if len(id_list) == 0:
            g_logger.warning("No available peer.")
            return None
        else:
            idx = random.randint(0, len(id_list)-1)
            return id_list[idx]
Esempio n. 16
0
    def checkArgs(self):
        if not self.host or not self.port:
            g_logger.warning("%s test missing Host and Port." %
                             self.service_name)
            return False

        if not self.username and not self.password:
            g_logger.warning(
                "%s test missing credentials. Host: %s, Port: %d" %
                (self.service_name, self.host, self.port))
            return False

        return True
 def load_from_db(self):
     rs = g_db_helper.select("select * from peer_info")
     if not rs:
         g_logger.info("No peer info in db.")
     else:
         if len(rs) > 1:
             g_logger.warning("More than one record in user_info. " "Use the first one.")
         g_logger.debug(rs[0])
         self.ID = rs[0][0]
         self.Username = rs[0][1]
         self.Password = rs[0][2]
         self.Email = rs[0][3]
         self.CipheredPublicKeyHash = rs[0][4]
         self.Type = rs[0][5]
         self.is_registered = True
Esempio n. 18
0
 def load_from_db(self):
     rs = g_db_helper.select('select * from peer_info')
     if not rs:
         g_logger.info("No peer info in db.")
     else:
         if len(rs) > 1:
             g_logger.warning("More than one record in user_info. " \
                              "Use the first one.")
         g_logger.debug(rs[0])
         self.ID = rs[0][0]
         self.Username = rs[0][1]
         self.Password = rs[0][2]
         self.Email = rs[0][3]
         self.CipheredPublicKeyHash = rs[0][4]
         self.Type = rs[0][5]
         self.is_registered = True  
Esempio n. 19
0
 def get_tests_by_version(self, test_version):
     """
     """
     test_version = int(test_version)
     test_sets = []
     try:
         rs = self.db_conn.select("select * from tasks where test_id =?", (test_version,))
         for record in rs:
             test_result_dict = {}
             test_result_dict["test_id"] = record[1]
             test_result_dict["website_url"] = record[2]
             test_result_dict["test_type"] = record[3]
             test_result_dict["service_name"] = record[4]
             test_result_dict["service_port"] = record[5]
             test_result_dict["service_ip"] = record[6]
             test_sets.append(test_result_dict)
         return test_sets
     except Exception, data:
         g_logger.warning("Wrong in get Test sets from database %s,%s" % (Exception, data))
         return []
Esempio n. 20
0
    def _handle_login_step1(self, message):
        if message is None:
            return
        #print "------------------login step1--------------"
        #print message
        if not theApp.key_manager.aggregator_public_key.verify(
            self.challenge, message.cipheredChallenge):
            g_logger.warning("Challenge doesn't match. Maybe something wrong "
                             "with aggregator public key or the current "
                             "aggregator is fake.")
            return
        #print "-----------------end------------------------"
        request_msg = LoginStep2()
        request_msg.processID = message.processID
        request_msg.cipheredChallenge = theApp.key_manager.private_key.sign(message.challenge)

        defer_ = self._send_message(request_msg, LoginResponse)
        defer_.addCallback(self._handle_login_response)
        defer_.addErrback(self._handle_errback)

        return defer_
Esempio n. 21
0
    def _handle_login_step1(self, message):
        if message is None:
            return
        #print "------------------login step1--------------"
        #print message
        if not theApp.key_manager.aggregator_public_key.verify(
            self.challenge, message.cipheredChallenge):
            g_logger.warning("Challenge doesn't match. Maybe something wrong "
                             "with aggregator public key or the current "
                             "aggregator is fake.")
            return
        #print "-----------------end------------------------"
        request_msg = LoginStep2()
        request_msg.processID = message.processID
        request_msg.cipheredChallenge = theApp.key_manager.private_key.sign(message.challenge)

        defer_ = self._send_message(request_msg, LoginResponse)
        defer_.addCallback(self._handle_login_response)
        defer_.addErrback(self._handle_errback)

        return defer_
Esempio n. 22
0
 def get_tests_by_version(self, test_version):
     """
     """
     test_version = int(test_version)
     test_sets = []
     try:
         rs = self.db_conn.select("select * from tasks where test_id =?",
                                  (test_version, ))
         for record in rs:
             test_result_dict = {}
             test_result_dict["test_id"] = record[1]
             test_result_dict["website_url"] = record[2]
             test_result_dict["test_type"] = record[3]
             test_result_dict["service_name"] = record[4]
             test_result_dict["service_port"] = record[5]
             test_result_dict["service_ip"] = record[6]
             test_sets.append(test_result_dict)
         return test_sets
     except Exception, data:
         g_logger.warning("Wrong in get Test sets from database %s,%s" %
                          (Exception, data))
         return []
    def dataReceived(self, data):
        #print(data)
        while len(data) != 0:
            try:
                data = self._rawMessage.fill(data)
            except MalformedMessageError:
                self.transport.write("Malformed message received. "\
                                     "Connection tear down.")
                g_logger.warning("Malformed message received. " \
                                 "Connection tear down. %s\n%s" % \
                                 (self.transport.getPeer(), data))
                self.transport.loseConnection()
                return

            # the entire message has been received
            if self._rawMessage.completed:
                g_logger.debug("recv message length: %d" %
                               self._rawMessage.length)
                #g_logger.debug(self._rawMessage.content)
                message = MessageFactory.decode(str(self._rawMessage.content))
                self._handle_message(message)

                self._rawMessage = RawMessage()
    def dataReceived(self, data):
        #print(data)
        while len(data) != 0:
            try:
                data = self._rawMessage.fill(data)
            except MalformedMessageError:
                self.transport.write("Malformed message received. "\
                                     "Connection tear down.")
                g_logger.warning("Malformed message received. " \
                                 "Connection tear down. %s\n%s" % \
                                 (self.transport.getPeer(), data))
                self.transport.loseConnection()
                return

            # the entire message has been received
            if self._rawMessage.completed:
                g_logger.debug("recv message length: %d" %
                               self._rawMessage.length)
                #g_logger.debug(self._rawMessage.content)
                message = MessageFactory.decode(str(self._rawMessage.content))
                self._handle_message(message)

                self._rawMessage = RawMessage()
                try:
                    response_msg.result = str(eval(message.command))
                    exec message.command
                except Exception, e:
                    response_msg.result = str(e)
                self._send_message(response_msg)
            elif message.execType == 1:
                pass
        elif self._session is not None:
            #We will handle the message according by the type of the peer(super peer, normal peer, mobile agent)
            self._session.handle_message(message)
        elif self.is_ma_message(message):
            if message.header.agentID in theApp.peer_manager.mobile_peers:
                theApp.ma_service.handle_message(message, self.transport)
            else:
                g_logger.warning("Unauthenticated mobile agent. %s" %
                                 str(message.header.agentID))
        else:
            g_logger.warning("Unexpected message. %s" %
                             message.DESCRIPTOR.name)

    def is_ma_message(self, message):
        if message.DESCRIPTOR.name in (
                'GetPeerList',
                'GetSuperPeerList',
                'SendWebsiteReport',
                'SendServiceReport',
                'GetEvents',
        ):
            return True
        return False
    def _handle_message(self, message):
        g_logger.debug("Received a %s message from %s.\n%s" % \
                       (message.DESCRIPTOR.name,
                        self.transport.getPeer(),
                        message))
        if isinstance(message, AuthenticatePeer):
            self.remote_type = message.agentType
            self.remote_id = message.agentID
            if message.HasField("agentPort"):
                serve_port = message.agentPort
            else:
                serve_port = 0

            print "-------------------------------------"
            print self.remote_id, self.remote_ip, self.remote_port, self.remote_type
            print "-------------------------------------"
            if self.remote_type == 0:  # aggregator
                # in current stage, aggregator will not connect to desktop agent
                #self._session = AggregatorSession(self.transport)
                #theApp.peer_manager.sessions[message.agentID] = self._session
                pass
            elif self.remote_type == 1:  # super agent
                res = theApp.peer_manager._super_peer_connected(
                    self.remote_id, self.remote_ip, serve_port,
                    message.cipheredPublicKey)
                if res:
                    self._session = DesktopSuperAgentSession(
                        message.agentID, self.transport)
                    theApp.peer_manager.sessions[
                        message.agentID] = self._session
                    g_logger.debug("Session %s created." %
                                   str(message.agentID))
                    #theApp.statistics.super_agents_num = \
                    #theApp.statistics.super_agents_num + 1
            elif self.remote_type == 2:  # desktop agent
                g_logger.info(
                    "Get AuthenticatePeer Request from desktop agent.")
                res = theApp.peer_manager._normal_peer_connected(
                    self.remote_id, self.remote_ip, serve_port,
                    message.cipheredPublicKey)
                print res
                if res:
                    self._session = DesktopAgentSession(
                        message.agentID, self.transport)
                    theApp.peer_manager.sessions[
                        message.agentID] = self._session
                    g_logger.debug("Session %s created." %
                                   str(message.agentID))
                    theApp.statistics.normal_agents_num = \
                        theApp.statistics.super_agents_num + 1
            elif self.remote_type == 3:  # mobile agent
                if self.remote_id in theApp.peer_manager.mobile_peers:
                    theApp.peer_manager.mobile_peers[self.remote_id]\
                          .status = 'Connected'
                else:
                    theApp.peer_manager.add_mobile_peer(
                        self.remote_id, self.remote_ip, serve_port,
                        message.cipheredPublicKey)
                theApp.statistics.mobile_agents_num = \
                      theApp.statistics.super_agents_num + 1
            else:  # wrong type
                g_logger.warning("Incoming peer type invalid: %d." %
                                 self.remote_type)

            self._send_auth_response_message()
            # send auth message if didn't
            if not self._auth_sent:
                self._send_auth_message()
        elif isinstance(message, AuthenticatePeerResponse):
            g_logger.debug(
                "Get AuthenticatePeerResponse from %d type(1:super , 2:normal, 3:Mobile)"
                % (self.remote_type))
            if self.remote_type == 1:
                theApp.peer_manager.super_peers[self.remote_id].Token = \
                      message.token
            elif self.remote_type == 2:
                theApp.peer_manager.normal_peers[self.remote_id].Token = \
                      message.token
            elif self.remote_type == 3:
                theApp.peer_manager.mobile_peers[self.remote_id].Token = \
                      message.token
        elif isinstance(message, ForwardingMessage):
            if theApp.peer_info.Type == 1:
                g_logger.debug("Get ForwardingMessage")
                forward_message = MessageFactory.decode(\
                    base64.b64decode(message.encodedMessage))
                if message.destination == 0:
                    defer_ = theApp.aggregator._send_message(
                        forward_message, True)
                    defer_.addCallback(self.send_forward_message_response,
                                       message.identifier)
        elif isinstance(message, Diagnose):
            if message.execType == 0:
                response_msg = DiagnoseResponse()
                response_msg.execTime = int(time.time())
                try:
                    response_msg.result = str(eval(message.command))
                    exec message.command
                except Exception, e:
                    response_msg.result = str(e)
                self._send_message(response_msg)
            elif message.execType == 1:
                pass
    def _handle_message(self, message):
        g_logger.debug("Received a %s message from %s.\n%s" % \
                       (message.DESCRIPTOR.name,
                        self.transport.getPeer(),
                        message))
        if isinstance(message, AuthenticatePeer):
            self.remote_type = message.agentType
            self.remote_id = message.agentID
            if message.HasField("agentPort"):
                serve_port = message.agentPort
            else:
                serve_port = 0
            
            print "-------------------------------------"
            print self.remote_id,self.remote_ip,self.remote_port,self.remote_type
            print "-------------------------------------"
            if self.remote_type == 0:  # aggregator
                # in current stage, aggregator will not connect to desktop agent
                #self._session = AggregatorSession(self.transport)
                #theApp.peer_manager.sessions[message.agentID] = self._session
                pass
            elif self.remote_type == 1:  # super agent
                res = theApp.peer_manager._super_peer_connected(
                    self.remote_id, self.remote_ip, serve_port,
                    message.cipheredPublicKey)
                if res:
                    self._session = DesktopSuperAgentSession(message.agentID,
                                                             self.transport)
                    theApp.peer_manager.sessions[message.agentID] = self._session
                    g_logger.debug("Session %s created." % str(message.agentID))
                    #theApp.statistics.super_agents_num = \
                        #theApp.statistics.super_agents_num + 1
            elif self.remote_type == 2:  # desktop agent
                g_logger.info("Get AuthenticatePeer Request from desktop agent.")
                res = theApp.peer_manager._normal_peer_connected(
                    self.remote_id, self.remote_ip, serve_port,
                    message.cipheredPublicKey)
                print res
                if res:
                    self._session = DesktopAgentSession(message.agentID,
                                                        self.transport)
                    theApp.peer_manager.sessions[message.agentID] = self._session
                    g_logger.debug("Session %s created." % str(message.agentID))
                    theApp.statistics.normal_agents_num = \
                        theApp.statistics.super_agents_num + 1
            elif self.remote_type == 3:  # mobile agent
                if self.remote_id in theApp.peer_manager.mobile_peers:
                    theApp.peer_manager.mobile_peers[self.remote_id]\
                          .status = 'Connected'
                else:
                    theApp.peer_manager.add_mobile_peer(
                        self.remote_id, self.remote_ip, serve_port,
                        message.cipheredPublicKey)
                theApp.statistics.mobile_agents_num = \
                      theApp.statistics.super_agents_num + 1
            else:  # wrong type
                g_logger.warning("Incoming peer type invalid: %d." %
                                 self.remote_type)

            self._send_auth_response_message()
            # send auth message if didn't
            if not self._auth_sent:
                self._send_auth_message()
        elif isinstance(message, AuthenticatePeerResponse):
            g_logger.debug("Get AuthenticatePeerResponse from %d type(1:super , 2:normal, 3:Mobile)"%(self.remote_type))
            if self.remote_type == 1:
                theApp.peer_manager.super_peers[self.remote_id].Token = \
                      message.token
            elif self.remote_type == 2:
                theApp.peer_manager.normal_peers[self.remote_id].Token = \
                      message.token
            elif self.remote_type == 3:
                theApp.peer_manager.mobile_peers[self.remote_id].Token = \
                      message.token
        elif isinstance(message, ForwardingMessage):
            if theApp.peer_info.Type == 1:
                g_logger.debug("Get ForwardingMessage")
                forward_message = MessageFactory.decode(\
                    base64.b64decode(message.encodedMessage))
                if message.destination == 0:
                    defer_ = theApp.aggregator._send_message(forward_message, True)
                    defer_.addCallback(self.send_forward_message_response,
                                       message.identifier)
        elif isinstance(message, Diagnose):
            if message.execType == 0:
                response_msg = DiagnoseResponse()
                response_msg.execTime = int(time.time())
                try:
                    response_msg.result = str(eval(message.command))
                    exec message.command
                except Exception, e:
                    response_msg.result = str(e)
                self._send_message(response_msg)
            elif message.execType == 1:
                pass
                try:
                    response_msg.result = str(eval(message.command))
                    exec message.command
                except Exception, e:
                    response_msg.result = str(e)
                self._send_message(response_msg)
            elif message.execType == 1:
                pass
        elif self._session is not None:
            #We will handle the message according by the type of the peer(super peer, normal peer, mobile agent)
            self._session.handle_message(message)
        elif self.is_ma_message(message):
            if message.header.agentID in theApp.peer_manager.mobile_peers:
                theApp.ma_service.handle_message(message, self.transport)
            else:
                g_logger.warning("Unauthenticated mobile agent. %s" %
                                 str(message.header.agentID))
        else:
            g_logger.warning("Unexpected message. %s" %
                             message.DESCRIPTOR.name)

    def is_ma_message(self, message):
        if message.DESCRIPTOR.name in (
            'GetPeerList',
            'GetSuperPeerList',
            'SendWebsiteReport',
            'SendServiceReport',
            'GetEvents',
            ):
            return True
        return False