def runCompress(self, projectPath, bakPath): if not self.runFlag: #终止递归的判断条件 return pList = os.listdir(projectPath) pList.sort() for i in pList: #遍历目录 filePath = os.sep.join([projectPath, i]) fileName = i #获取文件名 if os.path.isdir(filePath): #判断file是否为文件夹 md5 = common.getMd5(fileName) bakPath += os.sep + md5 #计算文件名的md5 os.mkdir(bakPath) if not os.listdir(bakPath): # 判断文件夹是否为空,空则创建flag文件 output = open( bakPath + os.sep + self.sessionDic['flagName'], 'w') output.close() flagFileInfo = "fold:" + md5 + ":" + fileName + "\n" #.flag文件信息 with open( bakPath[0:-17] + os.sep + self.sessionDic['flagName'], 'a') as output: output.write(flagFileInfo) self.runCompress(filePath, bakPath) bakPath = bakPath[0:-17] elif os.path.isfile(filePath): #判断是否为文件,是则进行压缩 with open(filePath, 'rb') as output: text = output.read() md5 = common.getMd5(text) #计算文件内容哈希值 bakPath += os.sep + md5 flagFileInfo = "file:" + md5 + ":" + fileName + "\n" filedeal.compress(filePath, bakPath) #调用压缩函数 bakPath = bakPath[:-17] with open(bakPath + os.sep + self.sessionDic['flagName'], 'a') as output: output.write(flagFileInfo)
def _runWebCheck(self, projectPath, tempPath): pList = os.listdir(projectPath) pList.sort() for i in pList: # 遍历目录 file = os.sep.join([projectPath, i]) fileName = file[file.rfind(os.sep) + 1:] # 获取文件名 if os.path.isdir(file): # 判断file是否为文件夹 md5 = common.getMd5(fileName) #计算文件名的md5 tempPath += os.sep + md5 if not os.path.exists(tempPath): # 判断file是否存在,不存在则创建文件夹 os.mkdir(tempPath) if not os.listdir(tempPath): # 判断文件是否为空,空则创建flag文件 output = open( tempPath + os.sep + self.sessionDic['flagName'], 'a') output.close() flagFileInfo = "fold:" + md5 + ":" + fileName + "\n" #.flag文件信息 with open( tempPath[:-17] + os.sep + self.sessionDic['flagName'], 'a') as output: output.write(flagFileInfo) self._runWebCheck(file, tempPath) tempPath = tempPath[0:-17] elif os.path.isfile(file): #判断是否为文件,是则计算hash with open(file, 'rb') as output: text = output.read() md5 = common.getMd5(text) #计算文件内容哈希值 tempPath = tempPath + os.sep + md5 flagFileInfo = "file:" + md5 + ":" + fileName + "\n" tempPath = tempPath[0:-17] with open(tempPath + os.sep + self.sessionDic['flagName'], 'a') as output: output.write(flagFileInfo)
def working(self): self.heartBeat.start() remoteDic = confdeal.getConfRemote(self.scriptPath) time.sleep(4) try: s = socket.socket() s.bind(("0.0.0.0", remoteDic['localPort'])) s.listen(1) #指定最多允许多少个客户连接到服务器。 except: self.heartBeat.stopRun() print 'Socker init error ! \nPlease check port\nExit!' sys.exit() if not self.bakOrCheck(): print 'No config ! \nWaiting for server\'s command !' # self.test() # 测试 while 1: # 接受指令 try: conn, addr = s.accept() conn.settimeout(5) if addr != remoteDic['remoteHost']: # 判断IP是否合法 print "IP($s)illegal connection !\nAuto reject ..." % ( addr) conn.close() continue messageJson = conn.recv(4096) # 接收服务器消息 message = json.loads(messageJson) command = message['command'] if command == 'start': # 开始监控和备份 slash, bslash = special.getSlash() common.dicStringReplace(message, bslash, slash) if os.path.exists(message['targetPath']): message['projectName'] = message['targetPath'][ message['targetPath'].rfind(os.sep) + 1:] message['sessionName'] = common.getMd5( message['projectName'] + remoteDic['localHost'] + str(remoteDic['localPort'])) message['flagName'] = common.getRandom() self.startMonitor(message, 1) else: print "The monitor path is not exists !" elif command == 'stop': # 停止监控和备份 message['projectName'] = message['targetPath'][ message['targetPath'].rfind(os.sep) + 1:] message['sessionName'] = common.getMd5( message['projectName'] + remoteDic['localHost'] + str(remoteDic['localPort'])) self.stopMonitor(message) elif command == 'getPath' or command == 'getWhitePath': # 遍历返回所有目录信息 infoJson = self.getPath(message) conn.sendall(infoJson) conn.close() # 关闭连接 except: print 'Illegal command !' time.sleep(5)
def _runBakCheck(self, targetPath): if not self.checkFlag: return pList = os.listdir(targetPath) pList.sort() for i in pList: #遍历目录 if not self.checkFlag: return bakFile = os.sep.join([targetPath, i]) if os.path.isdir(bakFile): self._runBakCheck(bakFile) elif os.path.isfile(bakFile): if bakFile[bakFile.rfind(os.sep) + 1:] == self.sessionDic['flagName']: with open(bakFile, 'r') as bakInput: # 读取flag文件信息 Text = bakInput.read() fileType = Text[:4] fileName = Text[5:21] filePath = bakFile[:-16] + fileName if fileType == 'fold': if not os.path.isdir(filePath): self.checkFlag = 0 return elif fileType == 'file': # 如果是文件,则对比内容md5 if not os.path.isfile(filePath): self.checkFlag = 0 return textMd5 = common.getMd5( filedeal.getZipContent('file', filePath)) if textMd5 != fileName: self.checkFlag = 0 return
def test(self): remoteDic = confdeal.getConfRemote(self.scriptPath) message = { 'command': 'start', 'targetPath': '/home/gmfork/Desktop/test', 'mode': 'safe', 'doBak': 1, 'whiteList': ['/home/gmfork/Desktop/test/python/other'], 'blackList': ['php', 'asp'], 'timeDelay': '1', } command = message['command'] # 开始监控和备份 if command == 'start': slash, bslash = special.getSlash() common.dicStringReplace(message, bslash, slash) if os.path.exists(message['targetPath']): message['projectName'] = message['targetPath'][ message['targetPath'].rfind(os.sep) + 1:] message['sessionName'] = common.getMd5( message['projectName'] + remoteDic['localHost'] + str(remoteDic['localPort'])) message['flagName'] = common.getRandom() self.startMonitor(message, 1) else: print "The monitor path is not exists !" # 停止监控和备份 elif command == 'stop': message['projectName'] = message['targetPath'][ message['targetPath'].rfind(os.sep) + 1:] message['sessionName'] = common.getMd5(message['projectName'] + remoteDic['localHost'] + str(remoteDic['localPort'])) self.stopMonitor(message) # 遍历返回所有目录信息 elif command == 'getPath' or command == 'getWhitePath': self.getPath(message)
def getTopFlag(): return common.getMd5('flag@orleven')