Exemplo n.º 1
0
def check_rate_limit(ip_address):
    monitor_db = MonitorDatabase()
    result = monitor_db.select('SELECT timestamp, success FROM user_login '
                               'WHERE ip_address = ? '
                               'AND timestamp >= ( '
                               'SELECT CASE WHEN MAX(timestamp) IS NULL THEN 0 ELSE MAX(timestamp) END '
                               'FROM user_login WHERE ip_address = ? AND success = 1) '
                               'ORDER BY timestamp DESC',
                               [ip_address, ip_address])

    try:
        last_timestamp = result[0]['timestamp']
    except IndexError:
        last_timestamp = 0

    try:
        last_success = max(login['timestamp'] for login in result if login['success'])
    except ValueError:
        last_success = 0

    max_timestamp = max(last_success, last_timestamp - plexpy.CONFIG.HTTP_RATE_LIMIT_ATTEMPTS_INTERVAL)
    attempts = [login for login in result if login['timestamp'] >= max_timestamp and not login['success']]

    if len(attempts) >= plexpy.CONFIG.HTTP_RATE_LIMIT_ATTEMPTS:
        return max(last_timestamp - (timestamp() - plexpy.CONFIG.HTTP_RATE_LIMIT_LOCKOUT_TIME), 0)
Exemplo n.º 2
0
 def run_check():
     global transmissions
     global checking_thread
     global lock
     with lock:
         with MonitorDatabase('/app/data/monitor.db') as db:
             try:
                 rows = db.fetch_and_parse_latest()
                 lines = []
                 for row in rows:
                     if row:
                         lines.append("%s: %smins bat(%f) pos(%f, %f)" % (row['name'], int(row['age'] / 60), row['charge'], row['lat'], row['lon']))
                 lines.sort()
                 sc.api_call(
                     "chat.postMessage",
                     channel="#testing",
                     text="Status Update\n```" + "\n".join(lines) + "\n```"
                 )
             finally:
                 start_checking_thread(5 * 60)
Exemplo n.º 3
0
def plex_user_login(token=None, headers=None):
    user_token = None
    user_id = None

    # Try to login to Plex.tv to check if the user has a vaild account
    if token:
        plex_tv = PlexTV(token=token, headers=headers)
        plex_user = plex_tv.get_plex_account_details()
        if plex_user:
            user_token = token
            user_id = plex_user['user_id']
    else:
        return None

    if user_token and user_id:
        # Try to retrieve the user from the database.
        # Also make sure guest access is enabled for the user and the user is not deleted.
        user_data = Users()
        user_details = user_data.get_details(user_id=user_id)
        if user_id != str(user_details['user_id']):
            # The user is not in the database.
            return None
        elif plexpy.CONFIG.HTTP_PLEX_ADMIN and user_details['is_admin']:
            # Plex admin login
            return user_details, 'admin'
        elif not user_details['allow_guest'] or user_details['deleted_user']:
            # Guest access is disabled or the user is deleted.
            return None

        # Stop here if guest access is not enabled
        if not plexpy.CONFIG.ALLOW_GUEST_ACCESS:
            return None

        # The user is in the database, and guest access is enabled, so try to retrieve a server token.
        # If a server token is returned, then the user is a valid friend of the server.
        plex_tv = PlexTV(token=user_token, headers=headers)
        server_token = plex_tv.get_server_token()
        if server_token:

            # Register the new user / update the access tokens.
            monitor_db = MonitorDatabase()
            try:
                logger.debug("Tautulli WebAuth :: Registering token for user '%s' in the database."
                             % user_details['username'])
                result = monitor_db.action('UPDATE users SET server_token = ? WHERE user_id = ?',
                                           [server_token, user_details['user_id']])

                if result:
                    # Refresh the users list to make sure we have all the correct permissions.
                    refresh_users()
                    # Successful login
                    return user_details, 'guest'
                else:
                    logger.warn("Tautulli WebAuth :: Unable to register user '%s' in database."
                                % user_details['username'])
                    return None
            except Exception as e:
                logger.warn("Tautulli WebAuth :: Unable to register user '%s' in database: %s."
                            % (user_details['username'], e))
                return None
        else:
            logger.warn("Tautulli WebAuth :: Unable to retrieve Plex.tv server token for user '%s'."
                        % user_details['username'])
            return None

    elif token:
        logger.warn("Tautulli WebAuth :: Unable to retrieve Plex.tv user token for Plex OAuth.")
        return None
Exemplo n.º 4
0
def welcome():
    with MonitorDatabase('/app/data/monitor.db') as db:
        data = db.fetch_and_parse_latest()
        app.logger.info(data)
        return jsonify({ 'transmissions': data })
Exemplo n.º 5
0
 def save(self, instance, time, data, source):
     with MonitorDatabase('/app/data/monitor.db') as db:
         db.add_transmission(instance, time, data, source)
Exemplo n.º 6
0
from database import MonitorDatabase

if __name__ == '__main__':
    with MonitorDatabase('/app/data/monitor.db') as db:
        for row in db.fetch_and_parse_latest():
            print row