def auth(self): # 发送认证消息 #send_info(self.sock, "ATH", "WHO ARE YOU") # 接收用编号 try: # 获取用户命令 text = get_reply_info(self.sock)[1] print("---text----", text) if text is None: return False else: user_no, user_pas = text.split('\n') except Exception as error: error_log("AUTH FAILED") print(error) reply_client(self.sock, "WRONG ARISE") return False # 检查该用户是否有效 if not com_check_name(user_no): reply_client(self.sock, "INVALID USERNAME") error_log("INVALID USERNAME") return False # 检查用户名密码是否正确 if not com_check_passwd(user_no, user_pas): reply_client(self.sock, "WRONG PASSWD") return False return user_no
def auth(self, text): # 接收并解析用户发来的信息,进行身份认证 self.process_state = kProccessState.kAuthFail try: if text is None: return False else: if len(text.split('\n')) == 4: user_no, user_pas, user_mac, self.user_addr = text.split( '\n') else: self.__response = "NEED FOUR ARGUMENTS:userno userpassword usermac userip" return False except Exception as error: error_log("AUTH FAILED") print(error) self.__response = "WRONG ARISE ABOUT SOCKET COMMUNICATION" return False self.__user_no = user_no # 检查该用户是否有效(数据库中是否有该用户) if not check_user_name(user_no): self.__response = "INVALID CLIENT" error_log("INVALID CLIENT") return False # 检查用户名密码是否正确 if not check_passwd(user_no, user_pas): self.__response = "WRONG PASSWD" return False # 检查用户是否已经注册过 user_registed = is_user_registed(user_no) if user_registed: # 判断用户mac信息是否正确 if identify_usermac(user_no, user_mac): log("%s LOGIN" % user_no) self.__response = kOK self.__response += "\n" self.__response += UploadServerConfig # 登录成功,保存用户身份 self.__is_login = True self.process_state = kProccessState.kAuthSuccess self.insert_user_info() return True else: log("[login filed] userNo [%s] invalid usermac [%s]" % (user_no, user_mac)) self.__response = "MAC_DIFF" return False # 当用户尚未注册时,返回登录失败,要求用户提供主机网卡和硬盘序列号信息 elif not user_registed: self.__response = "NEED REGISTER" self.process_state = kProccessState.kNeedRegister return # 用户身份不合法 self.__response = "INVALID" return False
def parse_scan_items(match_dict, file_name): if not isinstance(match_dict, dict): error_log("[MISMATCH ARGS]") return False if file_name is None or not os.path.exists(file_name): error_log("[%s] not exists" % file_name) return False _data = open(file_name, "rb").read().decode(errors='replace') details = [_line.strip() for _line in _data.split('\n') if len(_line.strip()) != 0] if len(details) < 1: error_log("EMPTY SCAN LOG") elif len(details) == 1 and details[0] == 'EMPTY': log("CLIENT IS CLEAN") return True # 开始解析扫描结果 for line in details: # ignore empty line if len(line) < 3: continue try: path, match, key_extend = line.strip().split('|') # print("path:%s match: %s key_extend: %s" % (path, match, key_extend)) match_dict[path] = (match, key_extend) except Exception as error: error_log("ERROR PARSE SCAN_ITEMS") print(error) return True
def run(self): while True: client_fd, (clnt_addr, clnt_port) = self.sock.accept() log("get connection from %s:%d" % (clnt_addr, clnt_port)) # if len(self.client_info) <= MAX_UPLOAD_TASK: # 以IP地址为键,sockfd 为值,保存已经建立连接的客户端 # 客户端报告自己的`任务编号` try: seq = client_fd.recv(8).decode() seq = int(seq) task = UPLOAD_QUEUE.get(seq, 0) except Exception: client_fd.send("-1".encode()) client_fd.close() continue if seq <= 0: # invalid task_id error_log("invalid task_id [%d]" % seq) client_fd.send("-1".encode()) client_fd.close() continue exec_task_count = len(EXECUTING_QUEUE) # 根据`任务编号`没有找到任务,直接断开连接 if not isinstance(task, tuple): log("failed to get upload task for [%d]" % seq) # NO TASK FOUND client_fd.send("-2".encode()) client_fd.close() continue # 返回还需等待的任务总数,0 表示不需要等待,可以上传了 if exec_task_count >= MAX_UPLOAD_TASK: EXECUTING_QUEUE.sort() wait_tasks = seq - EXECUTING_QUEUE[-1] if wait_tasks < 0: wait_tasks = 0 client_fd.send(str(wait_tasks)) else: client_fd.send('0'.encode()) EXECUTING_QUEUE.append(seq) tk = threading.Thread(target=download_file, args=(client_fd, seq)) # 设置该属性后,则不必处理‘线程回收’工作 tk.setDaemon(True) tk.start()
def recv_file(self, file_info): self.process_state = kProccessState.kRecvFile if not os.path.exists(FILE_KEEP_DIR): log("MKDIRS ", FILE_KEEP_DIR) os.makedirs(FILE_KEEP_DIR) # 整合两种文件上传,需要对传入的文件信息进行特别处理 suffix_pos = file_info.rfind(".") if suffix_pos == -1: error_log("invalid filename: %s" % file_info) return if len(file_info.split("\\")) > 1: array_file_info = file_info.split("\\") self.recv_file_name = array_file_info[len(array_file_info) - 1] else: self.recv_file_name = file_info file_suffix = self.recv_file_name[suffix_pos + 1:] self.recv_file_type = MAP_TYPE.get(file_suffix, FILE_TYPE.confidential) # self.recv_file_info = file_info self.recv_file_path = file_info[0:-4] # print("[file_type] : " + str(self.recv_file_type)) self.process_state = kProccessState.kNeedFileHash
def run(self): ''' 登录:ATH 管理员: 人员管理:AUC(add) DUC(del) MUC(alter) SUC(select) RUP(重置密码) 配件管理:APT DPT MPT SPT 维修统计:RMS 维修工: 配件查看:SPT 维修发起:SRO SPT(零件种类和数量) 维修历史:SRH ''' user_no = self.auth() if user_no == False: send_info(self.sock, "CNO", "ATH ERR", user_no) self.end_connection() return False send_info(self.sock, "C*K", "ATH OK", user_no) while not self.disconnected: # 获取用户命令 try: cmd, info = get_reply_info(self.sock, user_no) print("info信息:", info, "cmd信息:", cmd) except Exception as error: self.disconnected = True error_log("RECV INFO ERROR!") print(error) break if "SPT" == cmd: #查询配件信息 send_data = com_get_car_parts() if send_data: send_info(self.sock, "SPT", send_data, user_no) else: send_info(self.sock, "SPT", "null", user_no) elif "SUC" == cmd: #查询用户信息 send_data = com_get_user_info() if send_data: send_info(self.sock, "SUC", send_data, user_no) else: send_info(self.sock, "SUC", "null", user_no) elif "ARH" == cmd: #管理员查询所有工单 send_data = com_admin_repair_info() if send_data: send_info(self.sock, "ARH", send_data, user_no) else: send_info(self.sock, "ARH", "null", user_no) elif "SRH" == cmd: #维修工查询已完成工单 print(user_no) send_data = com_get_repair_info(user_no) if send_data: send_info(self.sock, "SRH", send_data, user_no) else: send_info(self.sock, "SRH", "null", user_no) elif "SRI" == cmd: # 维修工查询正在进行的工单 send_data = com_get_repairing(user_no) if send_data: send_info(self.sock, "SRI", send_data, user_no) else: send_info(self.sock, "SRI", "null", user_no) elif "AUC" == cmd: #添加用户信息 if not com_add_user_info(info): send_info(self.sock, "AUC", "WRONG AUC", user_no) else: send_info(self.sock, "AUC", "AUC OK", user_no) elif "DUC" == cmd: #删除用户信息 if not com_del_user_info(info): send_info(self.sock, "DUC", "WRONG DUC", user_no) else: send_info(self.sock, "DUC", "DUC OK", user_no) elif "MUC" == cmd: #修改用户信息 if not com_update_user_info(info): send_info(self.sock, "MUC", "WRONG MUC", user_no) else: send_info(self.sock, "MUC", "MUC OK", user_no) elif "RUP" == cmd: #重置用户密码 if not com_reset_user_password(info): send_info(self.sock, "RUP", "WRONG RUP", user_no) else: send_info(self.sock, "RUP", "RUP OK", user_no) elif "UUP" == cmd: #修改用户密码 if not com_update_user_password(info, user_no): send_info(self.sock, "UUP", "WRONG UUP", user_no) else: send_info(self.sock, "UUP", "UUP OK", user_no) elif "APT" == cmd: #添加零件信息 if not com_add_car_part(info): send_info(self.sock, "APT", "WRONG APT", user_no) else: send_info(self.sock, "APT", "APT OK", user_no) elif "DPT" == cmd: #删除零件信息 if not com_del_car_part(info): send_info(self.sock, "DPT", "WRONG DPT", user_no) else: send_info(self.sock, "DPT", "DPT OK", user_no) elif "SRO" == cmd: #添加一个维修单 if not com_add_repair_info(info, user_no): send_info(self.sock, "SRO", "WRONG SRO", user_no) else: send_info(self.sock, "SRO", "SRO OK", user_no) elif "CRS" == cmd: #改变维修单状态 if not com_change_repair_status(info): send_info(self.sock, "CRS", "WRONG CRS", user_no) else: send_info(self.sock, "CRS", "CRS OK", user_no) elif "DRH" == cmd: #删除一个维修单记录 if not com_del_repair_info(info): send_info(self.sock, "DRH", "WRONG DRH", user_no) else: send_info(self.sock, "DRH", "DRH OK", user_no) elif "END" == cmd: #客户端下线 # record user log off log("%s %s OFFLINE" % (user_no, self.hostIP)) self.end_connection() break log("CLIENT OFFLINE %s %s " % (user_no, self.hostIP)) try: self.end_connection() except Exception as err: print("[error]", err)