def run(self): ldebug('starting AMI server thread', self) data = '' while self.runthread and self.socket: try: recv_buffer = self.socket.recv(128) except socket.timeout as exception: # Socket timeout occured continue except: lerror(format_exc(), self) self.runthread = False break if not recv_buffer: ldebug('client connection lost', self) break data += recv_buffer messages = data.split(self.LINE_SEPARATOR * 2) data = messages.pop() for message_str in messages: if not message_str: continue message = to_hash(message_str.split(self.LINE_SEPARATOR)) self.message_queue.appendleft({'type': 'ami_client_message', 'body': message}) ldebug('exiting AMI server thread', self)
def user_password_authentication(user_name, password): global configuration_options if configuration_options.user_ignore_name and configuration_options.user_ignore_password: ldebug('user-password authentication credentials provided but ignored - user: %s, password: %s' % (user_name, '*' * len(str(password)))) return True if configuration_options.user_override_name != None and configuration_options.user_override_password != None: if user_name == configuration_options.user_override_name and password == configuration_options.user_override_password: return True return False db = SQLiteR(configuration_options.user_db_name) if not db.connect(): lerror('cound not connect to user database "%s"' % configuration_options.user_db_name) return False user = db.find(configuration_options.user_db_table, {configuration_options.user_db_name_row: user_name, configuration_options.user_db_password_row: password}) db.disconnect() if user: ldebug('user-password authentication accepted - user: %s, password: %s' % (user_name, '*' * len(str(password)))) return True linfo('user-password authentication failed - user: %s, password: %s' % (user_name, '*' * len(str(password)))) return False
def connect(self): fs = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: fs.connect((self.host, self.port)) except: lerror(format_exc(), self) return False fs.settimeout(self.SOCKET_TIMEOUT) self.fs = fs return True
def connect(self): fs = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: fs.connect((self.host, self.port)) except: lerror(format_exc(), self) return False fs.settimeout(self.SOCKET_TIMEOUT) self.fs = fs return True
def run(self): ldebug('starting MonAMI server thread', self) serversocket = TCPServer(self.address, self.port).listen() #serversocket.setblocking(0) if not serversocket: ldebug('server socket could not be bound', self) return 1 while self.runthread: try: client_socket, client_address = serversocket.accept() except socket.timeout as exception: # Socket timeout occured continue except socket.error as exception: lerror('socket error (%s): %s - ' % (exception, format_exc()), self) sleep(1) continue except: lerror('general error: %s - ' % format_exc(), self) sleep(1) continue ldebug('connected to %s:%d' % client_address, self) client_thread = MonAMIHandler(client_socket, client_address, self.event_socket) client_thread.deregister_at_server = self.deregister_handler_thread client_thread.user_password_authentication = self.user_password_authentication client_thread.start() if client_thread.isAlive(): self.register_handler_thread(client_thread) ldebug( 'registered handler threads: %d' % len(self.handler_threads), self) ldebug('exiting MonAMI server thread', self)
def listen(self): tcpsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) tcpsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) ldebug( 'binding server to %s:%d, timeout: %d' % (self.address, self.port, self.socket_timeout), self) try: tcpsocket.bind((self.address, self.port)) except ValueError as exception: lerror( 'server socket address error: %s - %s' % (exception, format_exc()), self) return False except socket.error as exception: lerror( 'server socket error (%d): %s - %s' % (exception[0], exception[1], format_exc()), self) return False except: lerror('general server socket error: %s' % format_exc(), self) return False tcpsocket.listen(self.SOCKET_BACKLOG) tcpsocket.settimeout(self.socket_timeout) return tcpsocket
def user_password_authentication(user_name, password): global configuration_options if configuration_options.user_ignore_name and configuration_options.user_ignore_password: ldebug( 'user-password authentication credentials provided but ignored - user: %s, password: %s' % (user_name, '*' * len(str(password)))) return True if configuration_options.user_override_name != None and configuration_options.user_override_password != None: if user_name == configuration_options.user_override_name and password == configuration_options.user_override_password: return True return False db = MySQLR(configuration_options.user_db_name, configuration_options.user_db_user, configuration_options.user_db_password) if not db.connect(): lerror('cound not connect to user database "%s"' % configuration_options.user_db_name) return False user = db.find( configuration_options.user_db_table, { configuration_options.user_db_name_row: user_name, configuration_options.user_db_password_row: password }) db.disconnect() if user: ldebug( 'user-password authentication accepted - user: %s, password: %s' % (user_name, '*' * len(str(password)))) return True linfo('user-password authentication failed - user: %s, password: %s' % (user_name, '*' * len(str(password)))) return False
def run(self): ldebug("starting MonAMI server thread", self) serversocket = TCPServer(self.address, self.port).listen() # serversocket.setblocking(0) if not serversocket: ldebug("server socket could not be bound", self) return 1 while self.runthread: try: client_socket, client_address = serversocket.accept() except socket.timeout as exception: # Socket timeout occured continue except socket.error as exception: lerror("socket error (%s): %s - " % (exception, format_exc()), self) sleep(1) continue except: lerror("general error: %s - " % format_exc(), self) sleep(1) continue ldebug("connected to %s:%d" % client_address, self) client_thread = MonAMIHandler(client_socket, client_address, self.event_socket) client_thread.deregister_at_server = self.deregister_handler_thread client_thread.user_password_authentication = self.user_password_authentication client_thread.start() if client_thread.isAlive(): self.register_handler_thread(client_thread) ldebug("registered handler threads: %d" % len(self.handler_threads), self) ldebug("exiting MonAMI server thread", self)
def listen(self): tcpsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) tcpsocket.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 ) ldebug('binding server to %s:%d, timeout: %d' % (self.address, self.port, self.socket_timeout), self) try: tcpsocket.bind((self.address, self.port)) except ValueError as exception: lerror('server socket address error: %s - %s' % (exception, format_exc()), self) return False except socket.error as exception: lerror('server socket error (%d): %s - %s' % (exception[0], exception[1], format_exc()), self) return False except: lerror('general server socket error: %s' % format_exc(), self) return False tcpsocket.listen(self.SOCKET_BACKLOG) tcpsocket.settimeout(self.socket_timeout) return tcpsocket
def run(self): ldebug('starting FreeSWITCH event_socket thread', self) while self.runthread: if not self.connect(): ldebug('could not connect to FreeSWITCH - retry', self) sleep(self.SOCKET_TIMEOUT) continue ldebug('opening event_socket connection', self) data = '' while self.runthread and self.fs: try: recv_buffer = self.fs.recv(128) except socket.timeout as exception: # Socket timeout occured continue except: lerror(format_exc(), self) self.runthread = False break if not recv_buffer: ldebug('event_socket connection lost', self) break data += recv_buffer messages = data.split(self.LINE_SEPARATOR * 2) data = messages.pop() for message_str in messages: if not message_str: continue message_body = None message = to_hash(message_str.split(self.LINE_SEPARATOR)) if not 'Content-Type' in message: ldebug('message without Content-Type', self) continue if 'Content-Length' in message and int(message['Content-Length']) > 0: content_length = int(message['Content-Length']) while len(data) < int(message['Content-Length']): try: data += self.fs.recv(content_length - len(data)) except socket.timeout as exception: ldebug('Socket timeout in message body', self) continue except: lerror(format_exc(), self) break message_body = data.strip() data = '' else: content_length = 0 self.process_message(message['Content-Type'], message, content_length, message_body) ldebug('closing event_socket connection', self) if self.fs: self.fs.close()
def run(self): ldebug('starting FreeSWITCH event_socket thread', self) while self.runthread: if not self.connect(): ldebug('could not connect to FreeSWITCH - retry', self) sleep(self.SOCKET_TIMEOUT) continue ldebug('opening event_socket connection', self) data = '' while self.runthread and self.fs: try: recv_buffer = self.fs.recv(128) except socket.timeout as exception: # Socket timeout occured continue except: lerror(format_exc(), self) self.runthread = False break if not recv_buffer: ldebug('event_socket connection lost', self) break data += recv_buffer messages = data.split(self.LINE_SEPARATOR * 2) data = messages.pop() for message_str in messages: if not message_str: continue message_body = None message = to_hash(message_str.split(self.LINE_SEPARATOR)) if not 'Content-Type' in message: ldebug('message without Content-Type', self) continue if 'Content-Length' in message and int( message['Content-Length']) > 0: content_length = int(message['Content-Length']) while len(data) < int(message['Content-Length']): try: data += self.fs.recv(content_length - len(data)) except socket.timeout as exception: ldebug('Socket timeout in message body', self) continue except: lerror(format_exc(), self) break message_body = data.strip() data = '' else: content_length = 0 self.process_message(message['Content-Type'], message, content_length, message_body) ldebug('closing event_socket connection', self) if self.fs: self.fs.close()