Exemplo n.º 1
0
    def dataReceived(self, data):
        #print(data)
        self.last_data_arrival_time = datetime.now()
        # 定义一个状态机来做协议解析
        cmd, cmd_info = self.do_parse_request(data)
        xtrace("%s [%s] %s %s" %
               (SOCKET_IN, self.client.get_user_no(), cmd, cmd_info))
        # 当发送完文件Hash之后,获取到客户端的回复(客户端是否存在文件),继而发送文件
        if self.transfile_state == kTransFileState.kNeedFileHash:
            self.send_file(self.client.get_kw_filename(), cmd_info)
            return

        result = self.client.process_cmd(cmd, cmd_info)

        # 业务逻辑处理完之后将处理结果发送给客户端
        if result == kProccessState.kUploadFile:

            self.send_info("PAT", self.client.get_response())
        elif result == kProccessState.kCtlFail or result == kProccessState.kCtlSuccess \
                or result == kProccessState.kNeedFileHash:
            return
        else:
            self.reply_client()

        # 根据处理结果做点事情
        # 如果处理结果是认证失败或需关闭与客户端连接
        if result == kProccessState.kAuthFail or result == kProccessState.kEndConnecion:
            self.end_connection()
        # 认证成功
        elif result == kProccessState.kAuthSuccess:
            self.factory.add_client(self.client.get_user_no(), self)
        # 成功写入关键字文件,需要network接口层发送文件
        elif result == kProccessState.kMakeKwSuccess:
            self.send_file(self.client.get_kw_filename())
Exemplo n.º 2
0
 def send_info(self, kind, info):
     xtrace("%s [%s] %s %s" %
            (SOCKET_OUT, self.client.get_user_no(), kind, info))
     buf = info.encode()
     rest_size = len(buf)
     buf = struct.pack(HEAD_FORMAT, kind.encode(), rest_size) + buf
     self.transport.write(buf)
Exemplo n.º 3
0
def send_info(sock, kind, info, user_no=NO_SPECIFIC):
    xtrace("%s [%s] %s %s" % (SOCKET_OUT, user_no, kind, info))
    buf = info.encode()
    rest_size = len(buf)
    buf = struct.pack(HEAD_FORMAT, kind.encode(), rest_size) + buf
    ret = 0
    try:
        ret = sock.send(buf)
    except Exception:
        ret = -1
    return ret > 0
Exemplo n.º 4
0
def reply_client(sock, status, user_no=None):
    if sock is None:
        return False

    if user_no is not None:
        xtrace("%s [%s] %s" % (SOCKET_OUT, user_no, status))

    confirm = status.encode()
    rest_pkt_size = len(confirm)
    buf = struct.pack(HEAD_FORMAT, "RPL".encode(), rest_pkt_size)
    sock.sendall(buf + confirm)
    return True
Exemplo n.º 5
0
def get_warnings(info):
    log_time, file_hash, keywords_info, op, file_name = info.split('\n')
    xtrace(
        "log_time:{lt}, file_hash:{fh}, op:{op}, keywords_info:{im}, file_name:{fn}"
        .format(lt=log_time,
                fh=file_hash,
                op=op,
                im=keywords_info,
                fn=file_name))

    # 解析事件详情
    op_num, op_items = op.split(' ', 1)

    op_details = op_items  # 记录事件详情

    match_text = ""
    print("[op_num] is " + op_num)
    print("[op_details] is " + op_details)
    if op_num == '0':
        # 解析关键字
        real_keywords = keywords_info.split(":")[0:-1]
        print("--------------------------------")
        print("[real_keywords] : " + str(real_keywords))
        for i in real_keywords:
            rank, word, nmatch, location = i.split('-')
            tmp = "(级别:{rk} 关键字:{wd} 重复次数:{nm} 位置:{ln}) ".format(rk=rank,
                                                                 wd=word,
                                                                 nm=nmatch,
                                                                 ln=location)
            print("--------------------------------")
            print("[tmp] is " + tmp)
            match_text += tmp
    else:
        # 当日志类型为 U盘事件时
        # 没有文件哈希,也没有关键字详情,只是单纯记录U盘的插入或者拔出事件
        # 记录U盘详细信息 statu: IN|usb name:可移动磁盘|serial number:2985626131|mount drive:H
        match_text = keywords_info

    ret = {}
    ret["keye_extend"] = op_details
    ret["file_hash"] = file_hash
    ret["err_oprate"] = op_details
    ret["err_code"] = op_num  # 事件编码,可以根据该·事件编码·解析日志详情, 0为用户异常信息, 1为u盘插入
    ret["key_words"] = match_text
    ret["file_name"] = file_name.replace("\\", "/")

    return ret
Exemplo n.º 6
0
def get_reply_info(sock, user_no=NO_SPECIFIC):
    info = None
    reply = None
    message = None
    try:
        buf = sock.recv(HEAD_SIZE)
        print("---**********i****" + str(len(buf)))
        rpl, rest_pkt_size = struct.unpack(HEAD_FORMAT, buf)
        info = sock.recv(rest_pkt_size)
    except Exception as error:
        print('error:', reply, message, ':::::error:', error)
        return 'END', message
    else:
        message = info.decode(errors='ignore')
        reply = rpl.decode()
        xtrace("%s [%s] %s %s" % (SOCKET_IN, user_no, reply, message))
        return reply, message
Exemplo n.º 7
0
def download_file(sock, seq):
    user_no, file_hash, file_name, file_size, file_passwd, file_type, current_time, aes_status, file_path = UPLOAD_QUEUE.get(seq)

    print("file_hash is ", file_hash)
    ori_file_name = file_name
    # 针对涉密文件,我们统一命名
    if file_type != FILE_TYPE.scan_data:
        file_name = pro_local_file_name(file_name)

    # 本地文件统一保存路径
    file_local_path = os.path.join(FILE_KEEP_DIR, file_name)

    rest_size = file_size

    xtrace("Begin Receive [%s %d]" % (file_name, file_size))
    xtrace("%s %s %s bytes passwd: [%s]" % (user_no, file_local_path, file_size, file_passwd))

    # 接收文件
    fp = open(file_local_path, "wb")
    while rest_size:
        buf = sock.recv(MAX_PACKET_SIZE)
        rest_size -= len(buf)
        fp.write(buf)
    fp.close()

    log("UPLOAD %s finished" % ori_file_name)

    # 仅当上传文件为 ‘涉密文件’时才记录此次上传操作
    if file_type == FILE_TYPE.confidential:
        set_file_inf(file_hash, FILE_KEEP_DIR, file_name, file_size, file_passwd)
        log_file_upload(user_no, ori_file_name, file_hash, current_time)
        if aes_status == FIRST_UPLOAD:
            update_scan_data(ori_file_name, file_hash, file_path)
        elif aes_status == SECOND_UPLOAD:
            update_second_scan_data(ori_file_name, file_hash, file_path)

    elif file_type == FILE_TYPE.scan_data:
        print("BEGIN PARSE SCAN_DATA")
        match_dict = {}
        if parse_scan_items(match_dict, file_local_path):
            set_ok_results(user_no, match_dict)
        else:
            set_failed_results(user_no, None)

    # 从任务队列中取消该任务
    xtrace("thread finished")

    # 取消"正在执行"任务记录
    EXECUTING_QUEUE.remove(seq)
    # 删除该任务
    UPLOAD_QUEUE.pop(seq)
    return True
Exemplo n.º 8
0
def commit_scan_files(scan_data, user_no, self_check):
    _user_id = dal_get_userId(user_no)
    if len(_user_id) < 1 or len(_user_id[0]) < 1:
        xtrace("dal_get_userId(%s): " % user_no)
        print("return:", _user_id)
        return False
    else:
        user_id = _user_id[0][0]
        xtrace("%s ==> %s" % (user_no, user_id))

    print("[COMMITTING_FILES]")

    # 即使扫描数据为空,也应该刷新自己的状态位
    # data 为None 说明扫描没有结果
    if scan_data is None:
        log("detect data is None then just update the client scan_status")
        DAL_update_user_scan_status(user_id, 0)
        return False

    for file_name, file_path, scan_time, keywords, context in scan_data:
        xtrace("[COMMITING]:", user_no, file_name, file_path, scan_time,
               keywords, context)
        if self_check == SELF_RESULT:
            insert_local_scan_data(user_id, file_name, file_path, scan_time,
                                   keywords, context)
        elif self_check == SCAN_RESULT:
            insert_sacn_data(user_id, file_name, file_path, scan_time,
                             keywords, context)
        elif self_check == SECOND_RESULT:
            insert_second_scan_data(user_id, file_name, file_path, scan_time,
                                    keywords, context)

    # 设置扫描状态--完成
    log("commit done update the client scan_status")
    DAL_update_user_scan_status(user_id, 0)

    return True