def __init__(self, template, template_directory, args, timeout=0): self.template = template self.timeout = timeout self.command_responder = CommandResponder() self.banner = "\r\nWelcome...\r\nConnected to [{0}]\r\n" logger.info('Kamstrup management protocol server initialized.') self.server = None
class KamstrupManagementServer(object): def __init__(self, template, template_directory, args, timeout=0): self.template = template self.timeout = timeout self.command_responder = CommandResponder() self.banner = "\r\nWelcome...\r\nConnected to [{0}]\r\n" logger.info('Kamstrup management protocol server initialized.') self.server = None def handle(self, sock, address): session = conpot_core.get_session('kamstrup_management_protocol', address[0], address[1], sock.getsockname()[0], sock.getsockname()[1]) logger.info('New Kamstrup connection from %s:%s. (%s)', address[0], address[1], session.id) session.add_event({'type': 'NEW_CONNECTION'}) try: sock.send( str_to_bytes(self.banner.format(conpot_core.get_databus().get_value("mac_address"))) ) while True: data = sock.recv(1024) if not data: logger.info('Kamstrup client disconnected. (%s)', session.id) session.add_event({'type': 'CONNECTION_LOST'}) break request = data.decode() logdata = {'request': request} response = self.command_responder.respond(request) logdata['response'] = response logger.info('Kamstrup management traffic from %s: %s (%s)', address[0], logdata, session.id) session.add_event(logdata) gevent.sleep(0.25) # TODO measure delay and/or RTT if response is None: session.add_event({'type': 'CONNECTION_LOST'}) break # encode data before sending reply = str_to_bytes(response) sock.send(reply) except socket.timeout: logger.debug('Socket timeout, remote: %s. (%s)', address[0], session.id) session.add_event({'type': 'CONNECTION_LOST'}) sock.close() def start(self, host, port): self.host = host self.port = port connection = (host, port) self.server = StreamServer(connection, self.handle) logger.info('Kamstrup management protocol server started on: %s', connection) self.server.serve_forever() def stop(self): self.server.stop()
class KamstrupManagementServer(object): def __init__(self, template, template_directory, args): self.command_responder = CommandResponder() self.banner = "\r\nWelcome...\r\nConnected to [{0}]\r\n" logger.info("Kamstrup management protocol server initialized.") self.server = None def handle(self, sock, address): session = conpot_core.get_session( "kamstrup_management_protocol", address[0], address[1], sock.getsockname()[0], sock.getsockname()[1], ) logger.info( "New Kamstrup connection from %s:%s. (%s)", address[0], address[1], session.id, ) session.add_event({"type": "NEW_CONNECTION"}) try: sock.send( str_to_bytes( self.banner.format( conpot_core.get_databus().get_value("mac_address") ) ) ) while True: data = sock.recv(1024) if not data: logger.info("Kamstrup client disconnected. (%s)", session.id) session.add_event({"type": "CONNECTION_LOST"}) break request = data.decode() logdata = {"request": request} response = self.command_responder.respond(request) logdata["response"] = response logger.info( "Kamstrup management traffic from %s: %s (%s)", address[0], logdata, session.id, ) session.add_event(logdata) gevent.sleep(0.25) # TODO measure delay and/or RTT if response is None: session.add_event({"type": "CONNECTION_LOST"}) break # encode data before sending reply = str_to_bytes(response) sock.send(reply) except socket.timeout: logger.debug("Socket timeout, remote: %s. (%s)", address[0], session.id) session.add_event({"type": "CONNECTION_LOST"}) sock.close() def start(self, host, port): self.host = host self.port = port connection = (host, port) self.server = StreamServer(connection, self.handle) logger.info("Kamstrup management protocol server started on: %s", connection) self.server.serve_forever() def stop(self): self.server.stop()