def listener_delete(self): log = logs.ice_log(__name__, self.request) #TODO add maybe some sort of authentication between wordpress and this # Try to get listener data from POST try: new_user_uuid = self.request.params['uuid'] except KeyError as e: log.critical('Wrong POST {}'.format(e)) raise e # Check if listener with given uuid exists query = self.request.dbsession.query(models.Listeners) listener = query.filter(models.Listeners.uuid == new_user_uuid).first() if not listener: log.critical( 'Someone tries to unregister listener that not exists (uuid {})' .format(new_user_uuid), True) raise exc.HTTPInternalServerError() # Delete listener listener_id = listener.id self.request.dbsession.delete(listener) log.info('Listener with id {} unregistered'.format(listener_id)) return Response(status_int=200)
def listener_add(self): log = logs.ice_log(__name__, self.request) # Try to find listener by the data from POST listener = self.findListenerByPost() if not listener: raise exc.HTTPUnauthorized() # If there is no access data connected to current listener, raise exception access = self.accessParams(listener) if not access: log.critical( 'There is no access data for listener with id {}'.format( listener.id, True)) raise exc.HTTPInternalServerError() # Check if user has active account if datetime.now().date() > access.expiration_date: raise exc.HTTPForbidden() # Check if user is not already connected if self.countActiveListeners(listener) >= access.max_listeners: raise exc.HTTPForbidden() # Add user to active_listeners table new_listener = models.ActiveListeners( listener_id=listener.id, listener_ip=self.request.remote_addr) self.request.dbsession.add(new_listener) log.info('Listener with id {} connected to icecast'.format( listener.id)) return Response(headerlist=[("listener_accepted", "1")], status_int=200)
def listener_new(self): log = logs.ice_log(__name__, self.request) #TODO add maybe some sort of authentication between wordpress and this # Try to get listener data from POST try: new_user_uuid = self.request.params['uuid'] new_user_exp_date = self.request.params['exp_date'] new_user_licences_num = self.request.params['licences_num'] except KeyError as e: log.critical('Wrong POST {}'.format(e)) raise e # Check if there is no already registered listener with given uuid query = self.request.dbsession.query(models.Listeners) listener = query.filter(models.Listeners.uuid == new_user_uuid).first() if listener: # log.critical('Someone tries to register listener that already exists (id {}) (uuid {})'.format(listener.id, listener.uuid), True) return Response(status_int=234) # 234 means that user was skipped # Add new listener new_listener = models.Listeners(uuid=new_user_uuid) self.request.dbsession.add(new_listener) self.request.dbsession.flush( ) # this flush needs to be here, cause we want to use auto incremented id as listener_id below new_listener_access = models.IcecastAccess( listener_id=new_listener.id, max_listeners=new_user_licences_num, expiration_date=datetime.strptime(new_user_exp_date, '%Y-%m-%d')) self.request.dbsession.add(new_listener_access) log.info('Listener with id {} registered'.format(new_listener.id)) return Response(status_int=200)
def accessParams(self, listener): log = logs.ice_log(__name__, self.request) if len(listener.access) != 1: log.critical( 'The number of access data for listener with id {} is different than 1. | Access data num: {}' .format(len(listener.access)), True) raise exc.HTTPInternalServerError() return listener.access[0]
def uuidFromPost(self): # Try to get mount data from POST icecast_data = self.request.params['mount'] # Parse 'mount' data to get uuid (eg. data: '/auth_test?uuid=222-222') parsed_data = parse('/{}?uuid={}', icecast_data) if not parsed_data: log = logs.ice_log(__name__, self.request) log.error('Can not parse uuid', True) raise exc.HTTPBadRequest() # Try to get uuid from parsed data listener_uuid = parsed_data[-1] return listener_uuid
def listener_remove(self): log = logs.ice_log(__name__, self.request) # Try to find listener by the data from POST listener = self.findListenerByPost() if not listener: raise exc.HTTPUnauthorized() if self.countActiveListeners(listener) <= 0: log.critical( 'Number of active listeners is <= 0 during listener_remove. | Listener id: {}' .format(listener.id), True) raise exc.HTTPInternalServerError() # Try to find active listener with given id and ip query = self.request.dbsession.query(models.ActiveListeners) listener_to_remove = query.filter( models.ActiveListeners.listener_id == listener.id).filter( models.ActiveListeners.listener_ip == self.request.remote_addr).first() # If there is no active listener that match to given ip, remove first with given id if not listener_to_remove: listener_to_remove = query.filter( models.ActiveListeners.listener_id == listener.id).first() # Check if we finally find him if not listener_to_remove: log.critical( 'Can not find listener with given id during listener_remove. | Listener id: {}' .format(listener.id), True) raise exc.HTTPInternalServerError() self.request.dbsession.delete(listener_to_remove) log.info('Listener with id {} disconnected from icecast'.format( listener.id)) return Response(status_int=200)
def exception_view(exc, request): log = logs.ice_log(__name__, request) log.warn('[EXCEPTION] [NAME {}] {}'.format(type(exc).__name__, exc)) capture_exception(exc) return Response(status_int=500)
def http_exception_view(exc, request): log = logs.ice_log(__name__, request) log.info('[HTTP_EXCEPTION] [NAME {}] [CODE {}]'.format( type(exc).__name__, exc.code)) return Response(status_int=exc.code)