예제 #1
0
    def addUser(self, description):
        uid = 1
        while self.userExists(uid):
            uid += 1

        if 'type' in list(description.keys()):
            del description['type']

        logger.log(logging.DEBUG, "add user \"%s\": %s" % (uid, description))

        user = UserDescription(uid, description)
        self.users[uid] = user

        for path in [self.userMessageBox(uid), self.userReceiptBox(uid)]:
            try:
                os.mkdir(path)
            except:
                logging.exception("Cannot create directory " + path)
                sys.exit(1)

        path = ""
        try:
            path = os.path.join(MBOXES_PATH, str(uid), DESC_FILENAME)
            logger.log(logging.DEBUG, "add user description " + path)
            self.saveOnFile(path, json.dumps(description))
        except:
            logging.exception("Cannot create description file " + path)
            sys.exit(1)

        return user
예제 #2
0
    def processCreate(self, data, client):
        logger.log(logging.DEBUG, "%s" % json.dumps(data))

        if 'uuid' not in list(data.keys()):
            logger.log(
                logging.ERROR,
                "No \"uuid\" field in \"create\" message: " + json.dumps(data))
            client.sendResult({"error": "wrong message format"})
            return

        if not set(data.keys()).issuperset(set({'secdata', 'signature'})):
            logger.log(logging.ERROR,
                       "Badly formatted \"send\" message: " + json.dumps(data))
            client.sendResult({"error": "wrong message format"})

        uuid = data['uuid']
        if not isinstance(uuid, int):
            logger.log(
                logging.ERROR,
                "No valid \"uuid\" field in \"create\" message: " +
                json.dumps(data))
            client.sendResult({"error": "wrong message format"})
            return

        if self.registry.userExists(uuid):
            logger.log(logging.ERROR,
                       "User already exists: " + json.dumps(data))
            client.sendResult({"error": "uuid already exists"})
            return

        me = self.registry.addUser(data)

        client.sendResult({"result": me.id})
예제 #3
0
    def print_patterns(self):
        if not self._size_to_patterns:
            logger.warning('No patterns were found')
            return

        if os.path.exists(self.OUTPUT_DIR):
            shutil.rmtree(self.OUTPUT_DIR)

        with multiprocessing.pool.ThreadPool(processes=multiprocessing.cpu_count()) as thread_pool:
            for size, patterns in self._size_to_patterns.items():
                if not patterns:
                    continue

                logger.log(logger.WARNING, f'Exporting patterns of size {size}', show_pid=True)

                same_size_dir = os.path.join(self.OUTPUT_DIR, str(size))
                os.makedirs(same_size_dir, exist_ok=True)

                fn = functools.partial(self._print_pattern, same_size_dir)
                thread_pool.map(fn, patterns)

                self._generate_contents(
                    same_size_dir,
                    f'Size {size} contents',
                    [{'name': f'Pattern #{p.id}', 'url': f'{p.id}/details.html'}
                     for p in sorted(patterns, key=lambda p: p.id)],
                    styles='../../styles.css', has_upper_contents=True)

            self._generate_contents(
                self.OUTPUT_DIR,
                'Contents',
                [{'name': f'Size {size}', 'url': f'{size}/contents.html'}
                 for size in sorted(self._size_to_patterns.keys())])

        logger.warning('Done patterns output')
예제 #4
0
    def _get_most_freq_group_and_freq_for_fragments(self, ext_fragments: set,
                                                    is_giant):
        start = time.time()
        groups: Set[FrozenSet[Fragment]] = Fragment.create_groups(
            ext_fragments)
        logger.log(logger.INFO,
                   f'Groups for {len(ext_fragments)} fragments created',
                   start_time=start,
                   show_pid=True)

        freq_group: Set[Fragment] = set()
        freq = self.MIN_FREQUENCY - 1

        for curr_group in groups:
            overlapped_fragments: list = self.get_graph_overlapped_fragments(
                curr_group)
            curr_freq = len(curr_group) - len(overlapped_fragments)

            if curr_freq > freq:
                curr_group = set(curr_group)
                if is_giant and self._is_giant_extension(curr_group):
                    for fragment in overlapped_fragments:
                        curr_group.remove(fragment)

                freq_group = curr_group
                freq = curr_freq

        return freq_group, freq
    def get_hash(self):
        logger.log(logger.DEBUG, f'Getting vector hash, data size = {len(self.data)}')

        result = 0
        for key in sorted(self.data.keys()):
            result = normalize(result * 31 + self.data[key])

        logger.log(logger.DEBUG, f'Hash calculated, value = {result}')
        return result
    def _remove_duplicates(group):
        if len(group) < Pattern.MIN_FREQUENCY:
            return group

        lst = list(group)  # todo escape convertions
        vb_utils.filter_list(lst, condition=lambda i, j: lst[i].is_equal(lst[j]))
        logger.log(logger.INFO, f'Remove duplicates: affected {len(group) - len(lst)} items', show_pid=True)

        return set(lst)
예제 #7
0
    def uncapsulate_secure_message(self, message):
        logger.log(logging.DEBUG, "SECURE MESSAGE RECEIVED: %r" % message)

        # Check all payload fields
        if not set({'payload', 'cipher_spec', 'mac'}).issubset(
                set(message.keys())):
            logger.log(
                logging.DEBUG, "ERROR: INCOMPLETE FIELDS IN SECURE "
                "MESSAGE: %r" % message)
            return {'type': 'error', 'error': 'Invalid secure message format'}

        assert message['cipher_spec'] == self.cipher_spec

        payload = json.loads(
            base64.b64decode(message['payload'].encode()).decode())

        # Derive AES key and decipher payload
        self.number_of_hash_derivations = payload['secdata']['index']

        self.peer_pub_value = deserialize_key(payload['secdata']['dhpubvalue'])
        self.peer_salt = base64.b64decode(payload['secdata']['salt'].encode())

        aes_key = derive_key_from_ecdh(
            self.priv_value,
            self.peer_pub_value,
            self.peer_salt,
            self.salt,
            self.cipher_suite['aes']['key_size'],
            self.cipher_suite['sha']['size'],
            self.number_of_hash_derivations,
        )

        # Verify MAC to make sure of message integrity
        if not verify_mac(aes_key, message['payload'].encode() + self.prev_mac,
                          base64.b64decode(message['mac'].encode()),
                          self.cipher_suite['sha']['size']):
            return {'type': 'error', 'error': "Invalid MAC; dropping message"}

        self.prev_mac = message['mac'].encode()

        # Decipher payload
        aes_cipher, aes_iv = generate_aes_cipher(
            aes_key, self.cipher_suite['aes']['mode'],
            base64.b64decode(payload['secdata']['iv'].encode()))

        return_payload = payload['message']

        # Decipher message, if present
        if 'message' in payload:
            decryptor = aes_cipher.decryptor()
            return_payload = decryptor.update(
                base64.b64decode(
                    payload['message'].encode())) + decryptor.finalize()
            return_payload = json.loads(return_payload.decode())

        return return_payload
예제 #8
0
    def handle(self):
        data, sock = self.request
        if data == b'MAX-REMOTE':
            logger.log(type='udp', action='handshake',
                       content=self.client_address)
        else:
            logger.log(type='udp', action='data',
                       content=repr(data))

        sock.sendto(config.server_name, self.client_address)
예제 #9
0
 def close(self):
     """Shuts down and closes this client's socket.
     Will log error if called on a client with closed socket.
     Never fails.
     """
     logger.log(logging.INFO, "Client.close(%s)" % self)
     try:
         self.socket.close()
     except:
         logging.exception("Client.close(%s)" % self)
예제 #10
0
def log_servo_error(servo, state, errno, only_log_error_servo=True):
    if only_log_error_servo and errno == 0:
        return
    logger.info('伺服: {},  状态: {}'.format(servo, state))
    if errno != 0:
        error = ServoError(errno)
        logger.info('报警代码: <span style="color: red">{}</span>'.format(hex(error.errno)))
        logger.info('报警说明: <span style="color: red">{}</span>'.format(error.description))
        logger.info('报警处理:      ')
        for i, h in enumerate(error.handle):
            logger.info('------>{}. <span style="color: red">{}</span>'.format(i + 1, h))
    logger.log('-' * 50)
예제 #11
0
    def extend(self, iteration=1):
        logger.warning(
            f'Extending pattern with fragments cnt = {len(self.fragments)}')

        start_time = time.time()
        label_to_fragment_to_ext_list: Dict[str,
                                            Dict[Fragment,
                                                 Set[Tuple[Fragment]]]] = {}
        for fragment in self.fragments:
            label_to_ext_list: Dict[
                str, Set[Tuple]] = fragment.get_label_to_ext_list()
            for label, exts in label_to_ext_list.items():
                d = label_to_fragment_to_ext_list.setdefault(label, {})
                d[fragment] = exts

        label_to_fragment_to_ext_list = {
            k: v
            for k, v in label_to_fragment_to_ext_list.items()
            if len(v) >= self.MIN_FREQUENCY
        }
        logger.warning(
            f'Dict label_to_fragment_to_ext_list with '
            f'{len(label_to_fragment_to_ext_list.items())} items was constructed',
            start_time=start_time)

        freq_group, freq = self._get_most_freq_group_and_freq(
            label_to_fragment_to_ext_list)

        if freq >= Pattern.MIN_FREQUENCY:
            extended_pattern = Pattern(freq_group, freq)

            new_nodes = []
            for ix in range(len(self.repr.nodes),
                            len(extended_pattern.repr.nodes)):
                new_nodes.append(extended_pattern.repr.nodes[ix])

            old_nodes_s = '\n' + '\n'.join(
                [f'\t{node}' for node in self.repr.nodes]) + '\n'
            new_nodes_s = '\n' + '\n'.join([f'\t{node}'
                                            for node in new_nodes]) + '\n'

            logger.info(f'Pattern with old nodes: {old_nodes_s}'
                        f'was extended with new nodes: {new_nodes_s}'
                        f'new size={extended_pattern.size}, '
                        f'fragments cnt={len(extended_pattern.fragments)}, '
                        f'iteration = {iteration}')

            return extended_pattern.extend(iteration=iteration + 1)
        else:
            logger.log(logger.WARNING, f'Done extend() for a pattern')
            return self
예제 #12
0
    def processAll(self, data, client):
        logger.log(logging.DEBUG, "%s" % json.dumps(data))

        user = -1
        if 'id' in list(data.keys()):
            user = int(data['id'])

        if user < 0:
            logger.log(
                logging.ERROR, "No valid \"id\" field in \"all\" message: " +
                json.dumps(data))
            client.sendResult({"error": "wrong message format"})
            return

        if not self.registry.userExists(user):
            logger.log(
                logging.ERROR,
                "Unknown source id for \"all\" message: " + json.dumps(data))
            client.sendResult({"error": "wrong parameters"})
            return

        if self.registry.getUser(client.secure.uuid).id != user:
            logger.log(
                logging.ERROR,
                "Source id different from client id for \"all\" message: " +
                json.dumps(data))
            client.sendResult({"error": "wrong parameters"})
            return

        client.sendResult({
            "result": [
                self.registry.userAllMessages(user),
                self.registry.userSentMessages(user)
            ]
        })
예제 #13
0
    def processResource(self, data, client):
        logger.log(logging.DEBUG, "%s" % json.dumps(data))

        if not set({'ids'}).issubset(set(data.keys())):
            logger.log(
                logging.ERROR,
                "Badly formated \"status\" message: " + json.dumps(data))
            client.sendResult({"error": "wrong message format"})

        result = []
        for user in data['ids']:
            result += [self.get_user_resources(user)]

        client.sendResult({"result": result})
예제 #14
0
    def processReceipt(self, data, client):
        logger.log(logging.DEBUG, "%s" % json.dumps(data))

        if not set({'id', 'msg', 'receipt'}).issubset(set(data.keys())):
            logger.log(
                logging.ERROR,
                "Badly formated \"receipt\" message: " + json.dumps(data))
            client.sendResult({"error": "wrong request format"})

        fromId = int(data["id"])
        msg = str(data['msg'])
        receipt = str(data['receipt'])

        if self.registry.getUser(client.secure.uuid).id != fromId:
            logger.log(
                logging.ERROR,
                "Source id different from client id for \"receipt\" request: "
                + json.dumps(data))
            return

        if not self.registry.messageWasRed(str(fromId), msg):
            logger.log(
                logging.ERROR,
                "Unknown, or not yet red, message for \"receipt\" request " +
                json.dumps(data))
            client.sendResult({"error": "wrong parameters"})
            return

        self.registry.storeReceipt(fromId, msg, receipt)
예제 #15
0
    def processList(self, data, client):
        logger.log(logging.DEBUG, "%s" % json.dumps(data))

        user = 0  # 0 means all users
        userStr = "all users"
        if 'id' in list(data.keys()):
            user = int(data['id'])
            userStr = "user%d" % user

        logger.log(logging.DEBUG, "List %s" % userStr)

        userList = self.registry.listUsers(user)

        client.sendResult({"result": userList})
예제 #16
0
    def ssh_public(self, task):
        '''
        统一任务分配
        :param task: 任务指令
        :return:
        '''
        while True:
            if task == 'cmd':
                command = input("请输入执行命令(q:返回):\n>>> ").strip()
                # 输入命令为空,一直输入
                while not command:
                    command = input(">>> ").strip()
                if command == "q": break
            elif task == 'trans':
                local = input("请输入上传文件的路径(q:返回):\n>>> ").strip()
                # 输入命令为空,一直输入
                while not local:
                    local = input(">>> ").strip()
                if local == 'q': break
                # 当输入文件不存在,继续输入
                while not os.path.exists(local):
                    print("输入文件不存在。")
                    local = input("请输入上传文件的路径:\n>>> ").strip()
                print("\033[32m远程路径格式:(路径+文件名)\033[0m默认上传到用户宿主目录……")
                remote = input("文件到远程的路径(q:返回)\n>>> ").strip()
                if remote == 'q': break

            t_objs = []
            for i in range(len(self.host_list)):
                host = self.host_list[i]
                ip, port, user, passwd = host['ip'], host['port'], host[
                    'user'], host['password']
                if task == 'cmd':
                    log(ip, "执行命令:%s" % command)  # 写入日志
                    t = threading.Thread(target=ssh_conn,
                                         args=(ip, port, user, passwd,
                                               command))
                elif task == 'trans':
                    log(ip, "上传文件:%s到%s" % (local, remote))  # 写入日志
                    t = threading.Thread(target=ssh_trans,
                                         args=(ip, port, user, passwd, local,
                                               remote))
                t.start()
                t_objs.append(t)

            print("\033[5;32;1m任务执行中,请等待……\033[0m")
            for t in t_objs:
                t.join()  # t.wait() 等待
예제 #17
0
    def parseReqs(self, data):
        """Parse a chunk of data from this client.
        Return any complete requests in a list.
        Leave incomplete requests in the buffer.
        This is called whenever data is available from client socket."""

        if len(self.bufin) + len(data) > MAX_BUFSIZE:
            logger.log(
                logging.ERROR,
                "Client (%s) buffer exceeds MAX BUFSIZE. %d > %d" %
                (self, len(self.bufin) + len(data), MAX_BUFSIZE))
            self.bufin = ""

        self.bufin += data
        reqs = self.bufin.split(TERMINATOR)
        self.bufin = reqs[-1]
        return reqs[:-1]
예제 #18
0
    def readMsgFile(self, uid, msg):
        path = self.userMessageBox(uid)

        if msg.startswith('_'):
            path = os.path.join(path, msg)
        else:
            try:
                f = os.path.join(path, msg)
                path = os.path.join(path, "_" + msg)
                logger.log(logging.DEBUG,
                           "Marking message " + msg + " as read")
                os.rename(f, path)
            except:
                logging.exception("Cannot rename message file to " + path)
                path = os.path.join(self.userMessageBox(str(uid)), msg)

        return self.readFromFile(path)
예제 #19
0
def login():
    '''
    用户登录
    :return: 用户登录状态
    '''
    print("\033[32m*\033[0m" * 30)
    print("\033[32m*\t\t简易主机管理系统\033[0m")
    print("\033[32m*\033[0m" * 30)
    username = input("请输入用户名:\n>>> ").strip()
    password = input("请输入密码:\n>>> ").strip()
    if username == "admin" and password == "admin":
        print("\033[32;1m你已成功登录主机管理系统……\033[0m")
        log("Manage_platform", "登录成功")
        return True
    else:
        print("用户名或密码错误……")
        return False
예제 #20
0
    def list_all_messages(self):
        payload = {'type': 'all'}

        if self.debug:
            while True:
                try:
                    payload['id'] = int(input(colored("User ID: ", 'blue')))
                    break
                except ValueError:
                    print(colored("ERROR: Invalid User ID", 'red'))
        else:
            payload['id'] = self.user_id

        print(
            colored(
                str.format('\nGetting all messages for user {:d} ...\n',
                           payload['id']), 'yellow'))

        data = self.send_payload(
            self.secure.encapsulate_secure_message(payload))
        data = self.secure.uncapsulate_secure_message(data)

        if 'error' in data:
            print(colored("ERROR: " + data['error'], 'red'))
        else:
            if 'result' not in data or len(data['result']) != 2:
                print(colored("An error has occurred, aborting.", 'red'))
                logger.log(
                    logging.DEBUG, "ERROR: INCOMPLETE FIELDS IN "
                    "ALL MESSAGE: %r" % data)
                return

            if data['result'][0]:
                print(colored("All received messages: ", 'green'))
                for message in data['result'][0]:
                    print(colored("\t" + message, 'green'))
            else:
                print(colored("No received messages", 'green'))

            if data['result'][1]:
                print(colored("\n\nAll sent messages: ", 'green'))
                for message in data['result'][1]:
                    print(colored("\t" + message, 'green'))
            else:
                print(colored("No sent messages", 'green'))
예제 #21
0
    def storeReceipt(self, uid, msg, receipt):
        pattern = re.compile("_?([0-9]+)_([0-9])")
        m = pattern.match(msg)

        if not m:
            logger.log(
                logging.ERROR, "Internal error, wrong message file name (" +
                msg + ") format!")
            sys.exit(2)

        path = self.userReceiptBox(
            os.path.join(m.group(1),
                         "_%s_%s_%d" % (uid, m.group(2), time.time() * 1000)))

        try:
            self.saveOnFile(path, receipt)
        except:
            logging.exception("Cannot create receipt file " + path)
예제 #22
0
    def userMessages(self, path, pattern):
        logger.log(logging.DEBUG,
                   "Look for files at " + path + " with pattern " + pattern)

        messageList = []
        if not os.path.exists(path):
            return []

        try:
            for filename in os.listdir(path):
                logger.log(logging.DEBUG, "\tFound file " + filename)
                if re.match(pattern, filename):
                    messageList.append(filename)
        except:
            logging.exception("Error while listing messages in directory " +
                              path)

        return messageList
예제 #23
0
    def listUsers(self, uid):
        if uid == 0:
            logger.log(logging.DEBUG, "Looking for all connected users")
        else:
            logger.log(logging.DEBUG, "Looking for \"%d\"" % uid)

        if uid != 0:
            user = self.getUser(uid)

            if user is not None:
                return [user]
            return None

        userList = []
        for k in list(self.users.keys()):
            userList.append(self.users[k])

        return userList
예제 #24
0
    def processRecv(self, data, client):
        logger.log(logging.DEBUG, "%s" % json.dumps(data))

        if not set({'id', 'msg'}).issubset(set(data.keys())):
            logger.log(logging.ERROR,
                       "Badly formated \"recv\" message: " + json.dumps(data))
            client.sendResult({"error": "wrong message format"})

        fromId = int(data['id'])
        msg = str(data['msg'])

        if not self.registry.userExists(fromId):
            logger.log(
                logging.ERROR,
                "Unknown source id for \"recv\" message: " + json.dumps(data))
            client.sendResult({"error": "wrong parameters"})
            return

        if self.registry.getUser(client.secure.uuid).id != fromId:
            logger.log(
                logging.ERROR,
                "Source id different from client id for \"recv\" message: " +
                json.dumps(data))
            client.sendResult({"error": "wrong parameters"})
            return

        if not self.registry.messageExists(fromId, msg):
            logger.log(
                logging.ERROR,
                "Unknown source msg for \"recv\" message: " + json.dumps(data))
            client.sendResult({"error": "wrong parameters"})
            return

        # Read message
        response = self.registry.recvMessage(fromId, msg)
        sender_id = int(response[0])

        client.sendResult({
            "result": response,
            "resources": {
                'result': [self.get_user_resources(sender_id)]
            }
        })
예제 #25
0
    def uncapsulate_init_message(self, payload):
        logger.log(logging.DEBUG, "INIT MESSAGE RECEIVED: %r" % payload)

        # Check all payload fields
        if not set({'payload', 'cipher_spec', 'signature', 'certificate'
                    }).issubset(set(payload.keys())):
            logger.log(
                logging.DEBUG, "ERROR: INCOMPLETE FIELDS IN INIT "
                "MESSAGE: %r" % payload)
            return {'type': 'error', 'error': 'Invalid secure message format'}

        sent_payload = json.loads(
            base64.b64decode(payload['payload'].encode()).decode())

        self.uuid = sent_payload['uuid']
        self.cipher_spec = payload['cipher_spec'] \
            if payload['cipher_spec'] is not None \
            else json.loads(base64.b64decode(self.registry.getUser(
            self.uuid).description['secdata'].encode()).decode())['cipher_spec']
        self.cipher_suite = get_cipher_suite(self.cipher_spec)

        # Verify signature and certificate validity to authenticate client
        peer_certificate = deserialize_certificate(payload['certificate'])
        if not self.certs.validate_cert(peer_certificate):
            logger.log(logging.DEBUG, "Invalid certificate; "
                       "dropping message")
            self.uuid = None
            self.cipher_spec = None
            self.cipher_suite = None
            return {'type': 'error', 'error': 'Invalid server certificate'}

        try:
            rsa_verify(peer_certificate.get_pubkey().to_cryptography_key(),
                       base64.b64decode(payload['signature'].encode()),
                       payload['payload'].encode(),
                       self.cipher_suite['rsa']['sign']['cc']['sha'],
                       self.cipher_suite['rsa']['sign']['cc']['padding'])
        except InvalidSignature:
            logger.log(logging.DEBUG, "Invalid signature; " "dropping message")
            self.uuid = None
            self.cipher_spec = None
            self.cipher_suite = None
            return {'type': 'error', 'error': 'Invalid message signature'}

        self.peer_pub_value = deserialize_key(
            sent_payload['secdata']['dhpubvalue'])
        self.peer_salt = base64.b64decode(
            sent_payload['secdata']['salt'].encode())
        self.number_of_hash_derivations = sent_payload['secdata']['index']
        self.nonce = base64.b64decode(sent_payload['nonce'].encode())

        return {'type': 'init', 'uuid': self.uuid}
예제 #26
0
 def get_servo_debug_msg(self, only_log_error_servo=True):
     if self.ui.main_ui.window.log_window.isHidden():
         self.ui.main_ui.window.log_window.show()
         self.ui.main_ui.logAction.setText('Close-Log')
     try:
         if self.xarm and self.xarm.connected:
             ret = self.xarm.get_servo_debug_msg()
             if ret[0] in [0, x2_config.UX2_ERR_CODE, x2_config.UX2_WAR_CODE]:
                 logger.log('=' * 50)
                 for i in range(1, 8):
                     servo = '机械臂第{}轴'.format(i)
                     log_servo_error(servo, ret[i * 2 - 1], ret[i * 2], only_log_error_servo=only_log_error_servo)
                 log_servo_error('机械爪', ret[15], ret[16], only_log_error_servo=only_log_error_servo)
                 logger.log('=' * 50)
             elif ret[0] == 3:
                 logger.debug('cmd: get_servo_debug_msg, ret: 通信错误')
         else:
             logger.debug('cmd: get_servo_debug_msg, ret: xArm is not connected')
     except Exception as e:
         print(e)
def _get_freq_group(fr):
    max_freq = 0
    freq_group = None

    label_to_ext_list = fr.get_label_to_ext_list()
    for label, ext_list in label_to_ext_list.items():
        ext_fragments = set()
        for ext in ext_list:
            ext_fr = Fragment.create_extended(fr, ext)
            ext_fragments.add(ext_fr)

            groups = Fragment.create_groups(ext_fragments)
            for num, group in enumerate(groups):
                freq = len(group)
                if freq > max_freq:
                    max_freq = freq
                    freq_group = group

                logger.log(logger.DEBUG, f'Elements in group #{num + 1} -> {len(group)}')

    return freq_group
예제 #28
0
    def _group_same_hash_fragments(cls, fragments: set):
        groups: Set[FrozenSet[Fragment]] = set()
        while len(fragments) > 0:
            fragment = next(iter(fragments))
            fragments.remove(fragment)

            group: Set[Fragment] = set()
            group.add(fragment)

            for fr in copy.copy(fragments):
                if fragment.vector.data == fr.vector.data:
                    group.add(fr)
                    fragments.remove(fr)

            # TODO: in the source there are also genesis fragments' groups checks, why?
            if len(group) >= Pattern.MIN_FREQUENCY:
                group: Set[Fragment] = cls._remove_duplicates(group)
                if len(group) >= Pattern.MIN_FREQUENCY:
                    logger.log(logger.INFO, f'A new group has been created, len = {len(group)}', show_pid=True)
                    groups.add(frozenset(group))
        return groups
예제 #29
0
    def processSend(self, data, client):
        logger.log(logging.DEBUG, "%s" % json.dumps(data))

        if not set(data.keys()).issuperset(set({'src', 'dst', 'msg', 'copy'})):
            logger.log(logging.ERROR,
                       "Badly formatted \"send\" message: " + json.dumps(data))
            client.sendResult({"error": "wrong message format"})

        srcId = int(data['src'])
        dstId = int(data['dst'])
        msg = str(data['msg'])
        copy = str(data['copy'])

        if not self.registry.userExists(srcId):
            logger.log(
                logging.ERROR,
                "Unknown source id for \"send\" message: " + json.dumps(data))
            client.sendResult({"error": "wrong parameters"})
            return

        if self.registry.getUser(client.secure.uuid).id != srcId:
            logger.log(
                logging.ERROR,
                "Source id different from client id for \"send\" message: " +
                json.dumps(data))
            client.sendResult({"error": "wrong parameters"})
            return

        if not self.registry.userExists(dstId):
            logger.log(
                logging.ERROR,
                "Unknown destination id for \"send\" message: " +
                json.dumps(data))
            client.sendResult({"error": "wrong parameters"})
            return

        # Save message and copy
        response = self.registry.sendMessage(srcId, dstId, msg, copy)

        client.sendResult({"result": response})
예제 #30
0
    def handleRequest(self, s, request, client):
        """Handle a request from a client socket.
        """
        try:
            logging.info("HANDLING message from %s: %r" %
                         (client, repr(request)))

            try:
                req = request
            except:
                logging.exception("Invalid message from client")
                return

            if not isinstance(req, dict):
                logger.log(logging.ERROR, "Invalid message format from client")
                return

            if 'type' not in req:
                logger.log(logging.ERROR, "Message has no TYPE field")
                return

            if req['type'] in self.messageTypes:
                self.messageTypes[req['type']](req, client)
            else:
                logger.log(
                    logging.ERROR, "Invalid message type: " +
                    str(req['type']) + " Should be one of: " +
                    str(list(self.messageTypes.keys())))
                client.sendResult({"error": "unknown request"})

        except Exception as e:
            logging.exception("Could not handle request")
예제 #31
0
    def recvMessage(self, uid, msg):
        uid = str(uid)
        msg = str(msg)

        result = []
        pattern = "_?([0-9]+)_[0-9]+"

        matches = re.match(pattern, msg)
        if not matches:
            logger.log(logging.ERROR,
                       "Internal error, wrong message file name format!")
            sys.exit(2)

        result.extend(matches.group(1))

        try:
            result.append(self.readMsgFile(uid, msg))
        except:
            logging.exception("Cannot read message " + msg + " from user " +
                              uid)

        return result