def get_writer(index_name): ix = get(index_name) wr = AsyncWriter(ix) logger.debug("created index_writer for \"{}\" {}" .format(index_name, id(wr))) return wr
def talk(self): self.reader, self.writer = yield from \ asyncio.open_unix_connection(self.admin_sock) for cmd in iter(self.commands): logger.debug('sending cmd: {}'.format(cmd)) yield from self.send(cmd) yield from self.read()
def connection_lost(self, exc): logger.debug('admin lost connection') # commit index writer here if self.handler.index_writer: self.handler.index_writer.commit() logger.debug('(admin handler) committed data in index_writer {}'. format(id(self.handler.index_writer)))
def connection_lost(self, exc): logger.debug('admin lost connection') # commit index writer here if self.handler.index_writer: self.handler.index_writer.commit() logger.debug( '(admin handler) committed data in index_writer {}'.format( id(self.handler.index_writer)))
def index_add(handler, values, index_name='seeded'): logger.debug('index add_msg {}'.format(values)) # check in the handler already has an index_writer if handler.index_writer: handler.index_writer.add_document(**values) else: handler.index_writer = index.get_writer(index_name) handler.index_writer.add_document(**values)
def connection_made(self, transport): logger.debug('admin connection made') self.handler = PomaresAdminHandler(transport) self.data_buffer = bytearray() self.data_buffer_size = 0 if self.payload: self.handler.send_data(self.payload) self.payload = None
def data_received(self, data): logger.debug('received admin data: {}'.format(data)) # connection is made self.data_buffer.extend(data) for line in self.data_buffer.splitlines(keepends=True): if line.endswith(b'\n'): self.route(self.handler, line[:-1]) else: self.data_buffer = line
def __init__(self, address, key_path, server_pub_key, command): self.keyobj = load_key(key_path) logger.debug("client init_box pk: {}".format(self.keyobj.pk)) logger.debug("client init_box sk: {}".format(self.keyobj.sk)) self.command = command PomaresProtocol.route = self.route self.host, self.port = address self.server_pub_key = server_pub_key self.do_handshake_init()
def connection_made(self, transport): self.handler = self.handler_type(transport) self.data_buffer = bytearray() self.data_buffer_size = 0 self.msg_size = 0 logger.debug('connection made') if self.payload: self.handler.send_data(self.payload) self.payload = None
def route(self, handler, msg): logger.debug('routing: {}'.format(msg)) try: msg = loads(msg.decode()) except ValueError: # Any ValueError means a bad json message logger.debug('raising BadJsonMessage') handler.send_data('["bad_json"]') raise BadJsonMessage() admin_commands = { 'ping': (ping_msg, {}), 'seed': (seed_msg, seed_msg_schema), 'planted': (plant_msg, {}) } try: # split header from values # default cmd_values is {} try: cmd_header = msg[0] cmd_values = msg[1] except IndexError: cmd_values = {} # default # grab func, schema and validate func, schema = admin_commands[cmd_header] try: if not validator.validate(cmd_values, schema): logger.info('msg not valid') raise BadSchema() except ValidationError: logger.info('validation error') raise BadSchema() # run func cmd_reply = func(handler, cmd_values) if cmd_reply: logger.debug('sending: {}'.format(cmd_reply)) handler.send_data(cmd_reply) else: # Ack if cmd_reply is empty logger.debug('sending: {}'.format(dumps(['ack']))) handler.send_data(dumps(['ack'])) except KeyError: # Any KeyError means a bad request handler.send_data(dumps(['bad_request'])) except BadSchema: # Any KeyError means a bad request handler.send_data(dumps(['bad_schema'])) except BadJsonMessage: # Any KeyError means a bad request handler.send_data(dumps(['bad_json']))
def route(self, handler, msg): logger.debug('routing: {}'.format(msg)) try: msg = loads(msg.decode()) except ValueError: # Any ValueError means a bad json message logger.debug('raising BadJsonMessage') handler.send_data('["bad_json"]') raise BadJsonMessage() admin_commands = {'ping': (ping_msg, {}), 'seed': (seed_msg, seed_msg_schema), 'planted': (plant_msg, {}) } try: # split header from values # default cmd_values is {} try: cmd_header = msg[0] cmd_values = msg[1] except IndexError: cmd_values = {} # default # grab func, schema and validate func, schema = admin_commands[cmd_header] try: if not validator.validate(cmd_values, schema): logger.info('msg not valid') raise BadSchema() except ValidationError: logger.info('validation error') raise BadSchema() # run func cmd_reply = func(handler, cmd_values) if cmd_reply: logger.debug('sending: {}'.format(cmd_reply)) handler.send_data(cmd_reply) else: # Ack if cmd_reply is empty logger.debug('sending: {}'.format(dumps(['ack']))) handler.send_data(dumps(['ack'])) except KeyError: # Any KeyError means a bad request handler.send_data(dumps(['bad_request'])) except BadSchema: # Any KeyError means a bad request handler.send_data(dumps(['bad_schema'])) except BadJsonMessage: # Any KeyError means a bad request handler.send_data(dumps(['bad_json']))
def route(self, handler, msg): logger.debug('routing msg: {}'.format(msg)) try: msg = decompress_buff(msg) logger.debug('decompressed msg: {}'.format(msg)) if not handler.handshaked: logger.debug('decoded msg: {}'.format(msg)) # at this point we can only expect PubKeyReply self.do_handshake(handler, msg) else: # receive messages: msg = handler.box.decrypt(msg) request = decode(msg) logger.debug('got request: {}'.format(request)) # TODO treat server replies here routes.talk_client(handler, request) except Exception as err: logger.debug('ignoring request [bad key] {}'.format(err)) raise
def run(self): session = self.loop.run_until_complete(self.server) session_admin = self.loop.run_until_complete(self.admin_server) session_io = self.loop.run_until_complete(self.io_server) logger.debug('serving on {}'.format(session.sockets[0].getsockname())) logger.debug('serving admin on {}'.format( session_admin.sockets[0].getsockname())) logger.debug('serving io on {}'.format( session_io.sockets[0].getsockname())) self.loop.run_forever()
def run(self): session = self.loop.run_until_complete(self.server) session_admin = self.loop.run_until_complete(self.admin_server) session_io = self.loop.run_until_complete(self.io_server) logger.debug('serving on {}'.format(session.sockets[0].getsockname())) logger.debug('serving admin on {}'.format(session_admin.sockets[0]. getsockname())) logger.debug('serving io on {}'.format(session_io.sockets[0]. getsockname())) self.loop.run_forever()
def do_handshake(self, handler, msg): logger.debug('do_handshake()') # expect server to send secret key to init_box handler.init_box = CryptoBox(self.keyobj) handler.init_box.box_with(self.server_pub_key) # receive server secretkey msg = handler.init_box.decrypt(msg) msg = decode(msg) handler.box = SecretBox(key=msg.key) handler.handshaked = True logger.debug('HANDSHAKED2') # XXX send first command msg logger.debug('sending command msg') self.send_command(handler)
def do_handshake(self, handler, msg): logger.debug('do_handshake()') try: # receive client pubkey and create my init_box handler.init_box = CryptoBox(self.keyobj) logger.debug("server init_box pk: {}".format(self.keyobj.pk)) logger.debug("server init_box sk: {}".format(self.keyobj.sk)) # init box with client's pubkey handler.init_box.box_with(msg.key) # create and send secret handler.box = SecretBox() sk_msg = encode(PubKeyReply(handler.box.sk)) sk_msg = handler.init_box.encrypt(sk_msg) handler.send_data(compress_buff(sk_msg)) handler.handshaked = True logger.info('HANDSHAKED1') except: raise BadHandshake()
def read(self): answer = yield from self.reader.readline() logger.debug('got data: {}'.format(answer)) return answer
def get_writer(index_name): ix = get(index_name) wr = AsyncWriter(ix) logger.debug('created index_writer for "{}" {}'.format(index_name, id(wr))) return wr
def route(self, handler, msg): logger.debug('got admin message: {}'.format(msg))
def data_received(self, data): logger.debug('received data: {}'.format(data)) # connection is made self.data_buffer.extend(data) self.data_buffer_size += len(data) if (not self.msg_size) and (self.data_buffer_size >= self.header_size): self.msg_size = self.encoded_size(self.data_buffer) logger.debug('set msg_size to {}'.format(self.msg_size)) logger.debug('data_buffer_size: {}'.format(self.data_buffer_size)) logger.debug('msg_size: {}'.format(self.msg_size)) if (self.data_buffer_size - self.header_size) >= self.msg_size: # got a complete msg, do stuff with it: logger.debug('got a complete msg, call route') self.route(self.handler, data[self.header_size:]) # reset for next msg logger.debug('## RESET ##') self.msg_size = 0 self.data_buffer = bytearray(data[self.data_buffer_size:]) self.data_buffer_size = len(self.data_buffer)
def send_data(self, payload): payload_size = len(payload) payload = pack('<I{:d}s'.format(payload_size), payload_size, payload) logger.debug('sending payload ({} bytes): {}'.format(payload_size, payload)) self.transport.write(payload)
def route(self, handler, msg): logger.debug('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> got message: {}'.format(msg))
def connection_lost(self, exc): logger.debug('lost connection')
def route(self, handler, msg): logger.debug( '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> got message: {}'.format(msg))
def send_data(self, payload): payload_size = len(payload) payload = pack('<I{:d}s'.format(payload_size), payload_size, payload) logger.debug('sending payload ({} bytes): {}'.format( payload_size, payload)) self.transport.write(payload)
def do_handshake_init(self): logger.debug('do_handshake_init()') # send my pubkey to server handshk_payload = compress_buff(encode(PubKeyReply(self.keyobj.pk))) self.client_prot = PomaresProtocol(handshk_payload)