def downloader_connected_to_proxy(original_data, sharerid, local_reader, local_writer): downloaderid = protocol.get_unique_id() downloaders[downloaderid] = asyncio.Queue() q = downloaders[downloaderid] remote_writer = sharers[sharerid] remote_writer.write(protocol.encode(downloaderid, protocol.CONNECTED)) remote_writer.write(protocol.encode(downloaderid, protocol.PACKET, original_data)) local_connection = proxy.Connection( lambda: local_reader.read(1024), lambda data: local_writer.write(data) ) remote_connection = proxy.Connection( lambda: q.get(), lambda data: remote_writer.write(protocol.encode(downloaderid, protocol.PACKET, data)) ) p = proxy.Proxy(local_connection, remote_connection) yield from p.run() local_writer.close() remote_writer.write(protocol.encode(downloaderid, protocol.DISCONNECTED)) print("all done") del downloaders[downloaderid]
def wrapper(self, to, *args, **kwargs): meth = getattr(self, fn.__name__) data = fn(self, *args, **kwargs) data = protocol.encode((meth, data)) for sub in to: sub.send(data)
def write_json(self, json): self.content_type = ("Content-Type", "application/json; charset=UTF-8") self.headers += [self.content_type] self.start_response("200 OK", self.headers) self.result = [protocol.encode(json)] self.log_request() self.process_result()
def connect_to_server(): sslcontext = ssl.create_default_context() sslcontext.load_verify_locations("server.crt") create_new_certificate = True if os.path.isfile("certificate") and os.path.isfile("privatekey"): logging.info("Going to reuse existing certificate.") sslcontext.load_cert_chain(certfile="certificate", keyfile="privatekey") create_new_certificate = False logging.info("Connecting...") sreader, swriter = yield from asyncio.open_connection("share.fkmzblt.net", 443, ssl=sslcontext) readbuffer = b"" data = yield from protocol.read_next_packet(sreader, readbuffer) readbuffer = data[3] if data[1] == protocol.DATA: data = data[2].decode() if data.startswith("id"): host = "{}.fkmzblt.net".format(data[2:]) fileid = protocol.get_unique_id() logging.info("File shared at: https://{}/{}".format(host, fileid)) if create_new_certificate: logging.info("Making a new certificate.") req = create_certificate_signing_request(host) swriter.write(protocol.encode(data[2:], protocol.DATA, b"cert" + req)) if create_new_certificate: data = yield from protocol.read_next_packet(sreader, readbuffer) if data[1] == protocol.DATA: logging.info("Server signed our new certificate.") f = open("certificate", "wb") cert = data[2][4:] f.write(cert) cert = open("server.crt", "rb").read() f.write(cert) f.close() asyncio.async(run_local_server(host, args.filename, fileid)) while True: data = yield from protocol.read_next_packet(sreader, readbuffer) if not data: break readbuffer = data[3] if data[1] == protocol.CONNECTED: task = asyncio.async(handle_connection(data[0], swriter)) downloaders[data[0]] = (asyncio.Queue(), task) elif data[1] == protocol.PACKET: yield from downloaders[data[0]][0].put(data[2]) elif data[1] == protocol.DISCONNECTED: logging.info("Downloader ({}) disconnected.".format(data[0])) downloaders[data[0]][1].cancel() del downloaders[data[0]]
def write(data): """Encode data via protocol.py and attempt to publish it on the blockchain within one transaction of many outputs. """ proxy = bitcoin.rpc.Proxy() addresses = protocol.encode(data) mapping = {} for address in addresses: mapping[address] = 0.00005430 return proxy.sendmany('', mapping)
def handle_connection(connectionid, remote_writer): logging.info("Downloader ({}) connected.".format(connectionid)) q = downloaders[connectionid][0] local_reader, local_writer = yield from asyncio.open_connection("127.0.0.1", 6666) local_connection = proxy.Connection( lambda: local_reader.read(1024), lambda data: local_writer.write(data) ) remote_connection = proxy.Connection( lambda: q.get(), lambda data: remote_writer.write(protocol.encode(connectionid, protocol.PACKET, data)) ) p = proxy.Proxy(local_connection, remote_connection) yield from p.run() remote_writer.write(protocol.encode(connectionid, protocol.DISCONNECTED)) logging.info("Downloader ({}) disconnected.".format(connectionid))
def sharer_handle_data(sharerid, reader, writer, data): if data.startswith(b"cert"): certificate = data[4:] ca_cert = crypto.load_certificate(crypto.FILETYPE_PEM, open("server.crt", "rb").read()) ca_key = crypto.load_privatekey(crypto.FILETYPE_PEM, open("server.key", "rb").read()) req = crypto.load_certificate_request(crypto.FILETYPE_PEM, certificate) cert = crypto.X509() cert.set_subject(req.get_subject()) cert.set_serial_number(int(random.random() * sys.maxsize)) cert.gmtime_adj_notBefore(0) cert.gmtime_adj_notAfter(24*60*60) cert.set_issuer(ca_cert.get_subject()) cert.set_pubkey(req.get_pubkey()) cert.sign(ca_key, "sha1") signed_cert = crypto.dump_certificate(crypto.FILETYPE_PEM, cert) writer.write(protocol.encode(sharerid, protocol.DATA, b"cert" + signed_cert))
def sharer_client_connected(reader, writer): cert = writer.get_extra_info("socket").getpeercert() if cert: print(cert) for sub in cert.get("subject", ()): for key, value in sub: if key == "commonName": sharerid = value.replace(".fkmzblt.net", "") else: sharerid = protocol.get_unique_id() print("Sharerid: {}".format(sharerid)) sharers[sharerid] = writer writer.write(protocol.encode(sharerid, protocol.DATA, b"id" + sharerid.encode())) try: readbuffer = b"" while True: packet = yield from protocol.read_next_packet(reader, readbuffer) if not packet: return readbuffer = packet[3] connectionid = packet[0] if not connectionid in downloaders and not connectionid in sharers: #This happens when a downloader disconnected from us but the #uploader still sent us packets. continue if packet[1] == protocol.PACKET: yield from downloaders[connectionid].put(packet[2]) elif packet[1] == protocol.DISCONNECTED: print("downloader disconnected") yield from downloaders[connectionid].put(None) elif packet[1] == protocol.DATA: print("data") yield from sharer_handle_data(sharerid, reader, writer, packet[2]) finally: del sharers[sharerid]
if i=="Y": break elif i=="N": continue print "\nenter 'user'<space>'username' only to change reciepent" while True: msg=raw_input("\n\nEnter your messege >>>") msglist=msg.split(" ") if len(msglist)>0: if msglist[0]=="user": reciepent = msglist[1] protocol.setRec(msglist[1]) continue if reciepent == "": reciepent = raw_input("\nPlease enter reciepent >> ") protocol.setRec(reciepent) elif msg=="exit": exit() tele=protocol.encode(username,msg) client.publish("shekharApp/chat/"+reciepent,tele,1)
def controller(tasks_file, client_port, node_port, worker_port, debug): ''' Launch a controller process which publishes task definitions, keeps track of node states and allows clients to connect to request state and send commands. ''' # Static configuration. logging.basicConfig(level=logging.DEBUG if debug else logging.INFO) client_address = f'tcp://*:{client_port}' node_address = f'tcp://*:{node_port}' worker_address = f'tcp://*:{worker_port}' publish_ms = 1000 context = zmq.Context() # Client connections are one off REQ-ROUTER. client = context.socket(zmq.ROUTER) client.bind(client_address) # Node connections are DEALER-ROUTER. Recieve node heartbeats to track # operating nodes. Send on shutdown commands when requested by client. node = context.socket(zmq.ROUTER) node.bind(node_address) # Worker connections are PUB-SUB. Interaction with workers is one-way. worker = context.socket(zmq.PUB) worker.bind(worker_address) # Poll only the listening ports. poller = zmq.Poller() poller.register(client, zmq.POLLIN) poller.register(node, zmq.POLLIN) # Keep node states as published by connection id. tracked_nodes = {} # Keep time of last heartbeat by connection id. node_heartbeats = {} # Last time the task list was published. last_publish = datetime.now() # Current task list being sent out. task_def_message = protocol.encode(json.load(tasks_file)) logging.info(f'Accepting client connections on {client_address}') logging.info(f'Registering nodes on {node_address}') logging.info(f'Publishing tasks on {worker_address}') while True: # Wait for incoming client or node messages. Timeout functions as the # task publishing frequency. This should be the only blocking I/O in # the loop. socks = dict(poller.poll(timeout=publish_ms)) now = datetime.now() # Discard any node states that haven't been heard from in a while. new_tracked_nodes = { ident: state for ident, state in tracked_nodes.items() if (now - node_heartbeats[ident]) < timedelta(seconds=3) } lost_nodes = [ tracked_nodes[ident]['name'] for ident in set(tracked_nodes) - set(new_tracked_nodes)] if lost_nodes: logging.info('Dropped nodes: ' + str(lost_nodes)) tracked_nodes = new_tracked_nodes # Respond immediately if there was a client request. if client in socks: ident, mid, request = client.recv_multipart() assert mid == b'' try: command, request = protocol.decode_client_request(request) if command == 'state': logging.debug('Responding to state request.') response = protocol.msg_controller_state(tracked_nodes) elif command == 'shutdown': node_ident = request['node'] if node_ident in tracked_nodes: node_name = tracked_nodes[node_ident]['name'] logging.info(f'Sending shutdown request to node {node_name}.') node.send_multipart([node_ident, b'', protocol.MSG_NODE_SHUTDOWN]) response = protocol.MSG_OK else: logging.warn(f'Received shutdown request for unknown node {node_ident}.') response = protocol.MSG_UNKNOWN_NODE except ValueError as e: logging.warn(str(e)) response = protocol.MSG_BAD_COMMAND client.send_multipart([ident, b'', response]) # Update node tracking if there was a heartbeat. if node in socks: ident, mid, message = node.recv_multipart() assert mid == b'' try: is_new = ident not in tracked_nodes tracked_nodes[ident] = protocol.decode_node_heartbeat(message) node_heartbeats[ident] = now node_name = tracked_nodes[ident]['name'] if is_new: logging.info(f'Registered new node: {node_name}') else: logging.debug(f'Received heartbeat from {node_name}') except ValueError as e: logging.warning(str(e)) # Re-publish the task definition if period has expired. if (now - last_publish) > timedelta(milliseconds=publish_ms): worker.send(task_def_message) last_publish = now logging.debug(f'Sent task definition on {worker_address}')
def respond(self, data, connection): if self.debug: print(f"Responding: {data}\n\n") print(f"{connection}") response = protocol.encode(data) connection.send(response)
def destructor(self, to): meth = getattr(self, 'destructor') data = protocol.encode((meth, ())) to.send(data)
def wrapper(self, to, *args, **kwargs): data = fn(self, *args, **kwargs) to.send(protocol.encode((type(self), (id(self), data))))
def encode(self, data): """ Wrapper around the protocol's frame encoding. """ return protocol.encode(data)
def send_message(sock, command): sock.send(protocol.encode(command))
def price_without_fee(data): addresses = protocol.encode(data) return 0.00005430 * len(addresses)