def crack(f_name=None, e=None, subscriber=None, socket=None): print("Começando quebra do arquivo ", f_name) # executa o john cmd = "" if len(subscriber.cfg): cmd = "exec john -i=" + subscriber.cfg + " " + f_name + " --session=" + subscriber.id else: cmd = "exec john " + f_name + " --session=" + subscriber.id print(cmd) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) while True: if e.isSet(): e.clear() p.kill() subscriber.state = "ocioso" print("Recebido comando para parar.") break if p.poll() is not None: subscriber.state = "ocioso" cmd = "john --show " + f_name p = subprocess.run(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT) results = p.stdout.decode("utf-8").splitlines() print(results) erro = p.stderr if erro is not None: print(erro.decode("utf-8")) if len(results) >= 2: print("Arquivo quebrado!!") results = results[:len(results) - 2] results = "\n".join(results) else: print("ERRO: Não foi possível quebrar o arquivo.") results = "" msg = { "action": "done", "f_name": f_name, "id": subscriber.id, "status": p.returncode, "results": results, "data": datetime.now().strftime("%d/%m/%Y %H:%M:%S") } socket.send_json(msg) socket.recv_json() break
def tryConnect(ip=getLocalIp(), port=PORT, timeout=10, dt=0.01): ok = None context = zmq.Context() socket = context.socket(zmq.REQ) socket.connect("tcp://" + str(ip) + ":" + str(port)) socket.send_json({"action": "list"}) # print "send at ", time.time() t = time.time() while t + timeout > time.time(): try: socket.recv_json(zmq.DONTWAIT) ok = (ip, port) except Exception, e: time.sleep(dt) continue
def getFile(filename): context = zmq.Context() socket = context.socket(zmq.REQ) socket.connect("tcp://localhost:1337") socket.send(filename) response = socket.recv_json() return response
def getSongBytes(server): try: socket = ctx.socket(zmq.REQ) ip = server["ip"] port = server["port"] socket.connect("tcp://" + ip + ":" + str(port)) filenames = server["files"] files = {} for f in filenames: print("apending bytes") socket.send_json({ "origin": "client", "type": "download", "data": { "filename": f } }) ans = socket.recv_json() if ans["status"] == "ok": files[f] = ans["data"]["bytes"].encode('iso8859-15') else: print("Error, ", ans) return files except Exception as e: print("Ocurrió un error al obtener la parte de la canción, ", e)
def recv_array(socket, flags=0, copy=True, track=False): """recv a numpy array""" md = socket.recv_json(flags=flags) msg = socket.recv(flags=flags, copy=copy, track=track) buf = buffer(msg) A = np.frombuffer(buf, dtype=md['dtype']) return A.reshape(md['shape'])
def _ipcFun(self, socket): message = socket.recv_json() if message['action'] == 'status': socket.send_json({ 'data': { "server response! PID:" + str(os.getpid()): systemDict } }) elif message['action'] == 'stop': self.flag = False systemDict['main']['target'] = 'off' self._checkProcessEnd() stdLogger.info('check Process completion.') socket.send_json({'data': 'Process stop completion.'}) systemDict['main']['state'] = 'off' elif message['action'] == 'forcestop': self.flag = False socket.send_json({'data': 'Process forcestop completion.'}) #time.sleep(5) systemDict['main']['state'] = 'off' elif message['action'] == 'start': socket.send_json( {'data': "Start to finish,pid:" + str(os.getpid())}) elif message['action'] == 'addlog': stdLogger.info(message['data']) socket.send_json({'data': 'addlog completion.'}) else: socket.send_json({'data': 'unknow parameter'})
def _reader_loop(self, socket): """ Thread function that reads the zmq socket and puts the data into the queue """ queue = self.reader_queue while True: data = socket.recv_json() try: __tag__ = data.get('__tag__') if __tag__ == '__quit__': # means to shutdown the thread # Close the socket just so the confirmation message # goes after the socket is closed. The 'with' # statement of _reader would close it anyways. socket.close() # Put None in the queue to signal clients that are # waiting for data queue.put(None) confirm_to = data.get('confirm_to', None) if confirm_to is not None: # Confirm that the socket was closed with Sender(confirm_to) as sender: confirm_msg = data.get('confirm_msg', None) sender.put(confirm_msg) send_to_namebroker('localhost', {'__tag__': 'unregister', '__name__': self.name}) return if __tag__ == '__ping__': # answer special message without going to the receive, # since the actor may be doing something long lasting # and not reading the queue with Sender(data['reply_to']) as sender: sender.put({'tag': '__pong__'}) # avoid inserting this message in the queue continue if __tag__ == '__address__': # Fill the port info for my address with Sender(data['reply_to']) as sender: sender.put({'tag': 'reply', 'address': self.address(), 'pid': os.getpid()}) continue if __tag__ == '__low_level_ping__': # answer a ping from a straight zmq socket sender = data['reply_to'] with zmq_socket(zmq.PUSH) as s: s.connect(sender) s.send_json({'__tag__': '__pong__'}) continue except Exception: exc = traceback.format_exc() logger.debug('Reader thread for {} got an exception:' .format(self.path)) logger.debug(exc) data = {'__tag__': '__exception__', 'exception': exc} queue.put(data)
def recv_msg(socket, act, **d): msg = socket.recv_json() assert 'act' in msg if msg['act'] != act: raise UnexpectedMessage('Required %s, received %s.' % (act, msg['act'])) return [v(msg[k]) for k, v in d.items()]
def _reader_loop(self, socket): """ Thread function that reads the zmq socket and puts the data into the queue """ queue = self.reader_queue while True: try: data = socket.recv_json() __tag__ = data.get('tag') if __tag__ == '__quit__': # means to shutdown the thread # Close the socket just so the confirmation message # goes after the socket is closed. The 'with' # statement of _reader would close it anyways. socket.close() # Put None in the queue to signal clients that are # waiting for data queue.put(None) confirm_to = data.get('confirm_to', None) if confirm_to is not None: # Confirm that the socket was closed with Sender(confirm_to) as sender: confirm_msg = data.get('confirm_msg', None) sender.put(confirm_msg) self.namebroker_client.unregister(self.name) return if __tag__ == '__ping__': # answer special message without going to the receive, # since the actor may be doing something long lasting # and not reading the queue with Sender(data['reply_to']) as sender: sender.put({'tag': '__pong__'}) # avoid inserting this message in the queue continue if __tag__ == '__address__': # Fill the port info for my address with Sender(data['reply_to']) as sender: sender.put({ 'tag': 'reply', 'address': self.address(), 'pid': os.getpid() }) continue if __tag__ == '__low_level_ping__': # answer a ping from a straight zmq socket sender = data['reply_to'] with zmq_socket(zmq.PUSH) as s: s.connect(sender) s.send_json({'tag': '__pong__'}) continue except Exception: exc = traceback.format_exc() logger.debug('Reader thread for {} got an exception:'.format( self.path)) logger.debug(exc) return queue.put(data)
def recv_array(socket, flags=0, copy=True, track=False): md = socket.recv_json(flags=flags) # Recieves the json file containing dtype and shape of the required array msg = socket.recv(flags=flags, copy=copy, track=track) # msg is the buffer recived which contains the array A = np.frombuffer(msg, dtype=md["dtype"]) # Using numpy and shape known we transform the buffer into the array and reshape # it to required shape and then finally return it. return A.reshape(md['shape'])
def recv_gym(socket, flags=0, copy=True, track=False): md = socket.recv_json(flags=flags) msg = socket.recv(flags=flags, copy=copy, track=track) rew = float(socket.recv_string(flags=flags)) done = socket.recv_string(flags=flags) done = (done == "True") buf = buffer(msg) A = np.frombuffer(buf, dtype=md['dtype']) return (A.reshape(md['shape']), rew, done)
def tryConnect(ip=getLocalIp(), port=PORT, timeout=4, dt=0.01): ok = None context = zmq.Context() socket = context.socket(zmq.REQ) socket.connect("tcp://" + str(ip) + ":" + str(port)) socket.send_json({"action": "list"}) # print "send at ", time.time() t = time.time() while t + timeout > time.time(): try: socket.recv_json(zmq.DONTWAIT) ok = (ip, port) except Exception as e: time.sleep(dt) continue socket.setsockopt(zmq.LINGER, 0) socket.close() context.term() return ok
def keepalive(socket=None, subscriber=None): while True: msg = { "action": "announcement", "state": subscriber.state, "id": subscriber.id, "file": subscriber.file, "cfg": subscriber.cfg } socket.send_json(msg) msg = socket.recv_json() subscriber.cfg = msg["cfg"] time.sleep(10)
def _getIPCMsg(self, flag): context = zmq.Context() socket = context.socket(zmq.REQ) socket.setsockopt(zmq.LINGER, 0) #socket.connect("ipc://"+baseParams['IPCFile']) socket.connect(baseParams['tcpaddr']) socket.send_json(flag) poller = zmq.Poller() poller.register(socket, zmq.POLLIN) if poller.poll(360 * 1000): # 10s timeout in milliseconds return (socket.recv_json()) else: return ({'data': 'The process has no response'})
def join_cluster(self, cluster_cmd_uri): socket = self.ctx.socket(zmq.REQ) socket.connect('tcp://{}'.format(cluster_cmd_uri)) socket.send_json({ 'type': 'JOIN', 'node': self.local_node.details, 'timestamp': time.time() }) resp = socket.recv_json() for node in resp['nodes']: self.add_node(node) socket.close()
def _send_kill_build(socket, build_info: dict) -> dict: """ This sends a command to the launch_binaries daemon running on a remote node to terminate a given binary. :param socket: The zmq socket. :param build_info: A diciontary containing the build_port. :return A kill_status indicating build has been succesfully terminated. """ build_port = build_info["build_port"] request = {"type": "kill_build", "build_port": build_port} socket.send_json(request) kill_status = socket.recv_json() return kill_status
def _(timeout=3 * 1000): if poller.poll(timeout): m = socket.recv_json() socket.close() assert 'act' in m assert 'ack' == m['act'] for k, v in additional_info.items(): if k not in m: return False if v != m[k]: return False return True
def _send_keep_alive(socket, build_info: dict) -> dict: """ This sends a command to the launch_binaries daemon running on a remote node to mark a given binary as still alive, preventing garbage collection. :param socket: The zmq socket. :param build_info: A diciontary containing the build_port. :return a heartbeat indicating build is still alive. """ build_port = build_info["build_port"] request = {"type": "keep_alive", "build_port": build_port} socket.send_json(request) heartbeat = socket.recv_json() return heartbeat
def send_ack_master(): context = zmq.Context() socket = context.socket(zmq.REQ) socket.connect("tcp://%s:%s" % (MASTER_IP, master_ACK_port)) header_data = { "machine": machine_name, ##ID instead don't forget to modify "username": "******", "filename": "vid_1.mp4", "numberofchunks": 15 } header_data_sent_to_master = json.dumps(header_data) socket.send_json(header_data_sent_to_master) replica_list = socket.recv_json() print(replica_list) socket.close() return replica_list
def _send_start_build(socket, controller_address: str) -> dict: """ This sends a command to the launch_binaries daemon running on a remote node to start a binary connected to the given controller address. :param socket: The zmq socket. :param controller_address: The host name or ip address of node running the controller. :return Build info dictionary containing build port. """ request = { "type": "start_build", "controller_address": controller_address } socket.send_json(request) build_info = socket.recv_json() return build_info
def ping(seed, q, time, log): """ Process that make ping to a seed. """ context = zmq.Context() socket = noBlockREQ(context, timeout=time) socket.connect(f"tcp://{seed[0]}:{seed[1]}") status = True log.debug(f"PING to {seed[0]}:{seed[1]}", "Ping") try: socket.send_json(("PING", )) msg = socket.recv_json() log.info(f"Received {msg} from {seed[0]}:{seed[1]} after ping", "Ping") except zmq.error.Again as e: log.debug(f"PING failed -- {e}", "Ping") status = False q.put(status)
def update_ctr(self, ctr_ip): try: LOG.info("Controller IP Address= {0}".format(ctr_ip)) context = zmq.Context() socket = context.socket(zmq.REQ) socket.connect("tcp://{0}:50165".format(ctr_ip)) ip = str(subprocess.check_output('hostname -I', shell=True)) nc = len(ip) - 3 ip = ip[2:nc] host_ip = ip.strip() LOG.info("HOST IP Address= {0}".format(host_ip)) mapping = {"VLAN": "10", "IP": host_ip, "MAC": "00:50:56:b8:11:c9"} socket.send_json(mapping) LOG.info("Received " + socket.recv_json()["reply"] + " event from SDN controller.") except BaseException as err: LOG.error("Passing IP to SDN controller failed! ERROR: {0}".format( str(err)))
def listen(port): print('Listening for calls...') socket = context.socket(zmq.REP) print(port) socket.bind("tcp://*:{}".format(port)) pyAudio = pyaudio.PyAudio() stream = pyAudio.open(format=FORMAT, channels=CHANNELS, rate=RATE, output=True, frames_per_buffer=CHUNK) global BUSY while True: request = socket.recv_json() if request['op'] == 'sendVoiceMessage': print('Voice message recieved. Playing...') for audio in request['audio']: stream.write(audio.encode('UTF-16', 'ignore')) socket.send_string('ok') print('played.') elif request['op'] == 'callRequest': print('Incoming call from: {}'.format(request['from'])) if ACCEPTCALLS: socket.send_string('1') else: socket.send_string('0') print('accepted.' if ACCEPTCALLS else 'rejected.') elif request['op'] == 'startCall': if BUSY: print("I'M IS BUSY IN ANOTHER CALL") else: BUSY = True client = context.socket(zmq.REQ) client.connect("tcp://{}:{}".format(request['ip'], request['port'])) socket.send_string('ok') threading.Thread(target=recordAndSend, args=[client]).start() elif request['op'] == 'activeCallAudio': socket.send_string('ok') stream.write(request['audio'].encode('UTF-16', 'ignore')) else: print('invalid request recieved.') stream.stop_stream() stream.close() pyAudio.terminate()
def run(self, args): """ Wrapper function that starts the launch binary event loop :param args: The args defined upon initializing this script defining binary. """ port = args.listening_port build_index = dict() thread = Thread(target=self._garbage_collector, args=(build_index, )) thread.start() context = zmq.Context() socket = context.socket(zmq.REP) socket.bind("tcp://*:%s" % port) while True: # Wait for next request from client message = socket.recv_json() self._handle_message(message, args, socket, build_index) print("Received request: ", message)
def recv_gym(socket, flags=0, copy=True, track=False): md = socket.recv_json(flags=flags) msg = socket.recv(flags=flags, copy=copy, track=track) rew = float(socket.recv_string(flags=flags)) done = socket.recv_string(flags=flags) done = (done == "True") misc = socket.recv_string(flags=flags) if "array" in misc: # this means somebody made a mistake and sent a numpy array instead of a list misc = misc.replace("array([", "[").replace("])}", "]}") try: misc = ast.literal_eval(misc) except BaseException as e: msg = "Exception while calling literal_eval() on '{}'':\n\n{}".format(misc, traceback.format_exc(e)) raise Exception(msg) buf = buffer(msg) A = np.frombuffer(buf, dtype=md['dtype']) return A.reshape(md['shape']), rew, done, misc
def send_manage_message(endpoint: str, action: dict, timeout: int = 5): """ Sends a 'management' message, following the threatbus-zmq-app protocol to either subscribe or unsubscribe this application to/from Threat Bus. @param endpoint A host:port string to connect to via ZeroMQ @param action The message to send as JSON @param timeout The period after which the connection attempt is aborted """ context = zmq.Context() socket = context.socket(zmq.REQ) socket.setsockopt(zmq.LINGER, 0) socket.connect(f"tcp://{endpoint}") socket.send_json(action) poller = zmq.Poller() poller.register(socket, zmq.POLLIN) reply = None if poller.poll(timeout * 1000): reply = socket.recv_json() socket.close() context.term() return reply
def post(self, experiment_id, user_id): if self.get_cookie('sessionid') not in self.db['user']: self.set_status(500) self.finish() return client_query = {} client_query['QUERY'] = self.get_argument('QUERY') client_query['CLIENT_ID'] = self.get_cookie('sessionid') client_query['CLIENT_RESPONSE'] = {} client_query['CLIENT_RESPONSE']['WORKSITE'] = self.get_argument('CLIENT_RESPONSE_WORKSITE', None) client_query['CLIENT_RESPONSE']['CONCLUSION'] = self.get_argument('CLIENT_RESPONSE_CONCLUSION', None) client_query['QUERY_INDEX'] = int(self.get_argument('QUERY_INDEX', -1)) client_query['QUERY_STATUS'] = self.get_argument('QUERY_STATUS', '') context = zmq.Context() socket = context.socket(zmq.REQ) socket.connect('ipc://%s'%self.totem) socket.send_json(client_query) server_response = socket.recv_json() socket.close() # return self.write(json.dumps(server_response))
def run(self): self.isStopped = False print "Starting nameserver on", getLocalIp(), str(PORT) time.sleep(1) self.canGo = True context = zmq.Context() socket = context.socket(zmq.REP) # socket.bind("tcp://" + config.local_ip + ":" + str(config.name_port)) socket.bind("tcp://*:" + str(PORT)) programs = {} counter = 0 import SimpleHTTPServer, SocketServer WPORT = 8080 import pprint, StringIO class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Content-Type', 'text/html') self.end_headers() self.wfile.write("<html><body>"); self.wfile.write("<h1>Programs</h1><pre>"); s = StringIO.StringIO() pprint.pprint(programs, s) self.wfile.write(s.getvalue()) self.wfile.write("</pre>"); self.wfile.write("</body></html>"); self.wfile.close(); Handler = MyHandler # self.httpd = SocketServer.ThreadingTCPServer(("", WPORT), Handler) print "serving WWW at port", WPORT # self.www = threading.Thread(target=self.httpd.serve_forever).start() while self.canGo: data = {} try: data = socket.recv_json(zmq.DONTWAIT) print data except Exception, e: time.sleep(0.01) continue print 'NS recive at', time.time() try: data['action'] except: data['action'] = 'ping' try: if data['action'] == 'register': try: data['location'] except: data['location'] = 'local' programs[data['name']] = dict(time=time.time(), ip=data['ip'], port=data['port'], location=data['location']) print "registrating ", data['name'], programs[data['name']] socket.send_json(dict(status='ok')) continue if data['action'] == 'list': d = [] for k, v in programs.iteritems(): d.append(dict(name=k, data=v)) socket.send_json(d) print 'send at', time.time() continue if data['action'] == 'ping': socket.send_json(dict(status='ok')) continue except Exception, e: print e socket.send_json(dict(status='fail', text=str(e))) continue
def download_uplaod(process_id, client_server_port): context = zmq.Context() socket = context.socket(zmq.REP) print("process_id", process_id, "inside download upload", " I took port %s" % client_server_port) socket.bind("tcp://*:%s" % client_server_port) while True: message = socket.recv_json() parsed_json = json.loads(message) print(parsed_json["mode"]) print("recieved header from client download or upload operation") if (parsed_json["mode"] == "upload"): print("operation upload") socket.send_string("ACK") print("sent ACK to client") message = socket.recv() p = zlib.decompress(message) data = pickle.loads(p) print("finished recieving file to be uploaded from client") socket.send_string("finished writting file, success") extension_index = len(parsed_json["filename"]) if "." in parsed_json["filename"]: extension_index = parsed_json["filename"].rfind(".") directory = "./" + parsed_json["username"] + "/" + str( parsed_json["filename"])[:extension_index] if not os.path.exists(directory): os.makedirs(directory) with open(directory + "/" + parsed_json["filename"], 'wb') as f: f.write(data) ####will slice here#### ###done and tested####### number_of_chunks = slice_file(directory, parsed_json["filename"], 64 * 1024) ########change to connect############### socket_master_ack = context.socket(zmq.REQ) socket_master_ack.connect("tcp://%s:%s" % (MASTER_IP, master_ACK_port)) header_data = { "machine": machine_name, "username": parsed_json["username"], "filename": parsed_json["filename"], "numberofchunks": number_of_chunks } header_data_sent_to_master = json.dumps(header_data) socket_master_ack.send_json(header_data_sent_to_master) ackAfterUpload = socket_master_ack.recv_string() ''' will send file path(comelete file path) from the client, and replica list, parsed json ''' elif (parsed_json["mode"] == "download"): print("operation download") print(machine_name) chunk_number = parsed_json["chunknumber"] extension_index = len(parsed_json["filename"]) if "." in parsed_json["filename"]: extension_index = parsed_json["filename"].rfind(".") directory = "./" + parsed_json["username"] + "/" + str( parsed_json["filename"])[:extension_index] #print("directory ", directory) #print(os.listdir(directory)) filename = get_chunck_name_by_number(chunk_number, directory, parsed_json["filename"]) file_path = directory + "/" + filename #print("file path" , file_path) with open(file_path, 'rb') as f: chunk_small = f.read(64 * 1024) # chunk_small = f.read() #print(file_path) p = pickle.dumps(chunk_small) z = zlib.compress(p) f.close() socket.send(z)
def rep(host, port): context = zmq.Context() socket = context.socket(zmq.REP) socket.bind('tcp://' '%s:%d' % (host, port)) message = socket.recv_json(1024) print(message)