2024-11-02 20:04:56 +00:00
|
|
|
# app.py
|
2024-11-07 17:00:54 +00:00
|
|
|
from flask import Flask, render_template, request, redirect, url_for, session, flash, send_file
|
2024-11-03 15:34:10 +00:00
|
|
|
from flask_caching import Cache
|
2024-11-02 20:04:56 +00:00
|
|
|
import requests.auth
|
2024-11-05 08:09:08 +00:00
|
|
|
import os
|
2024-11-07 20:04:54 +00:00
|
|
|
from lib.datetime import filter_accounts_current_month, filter_accounts_expired
|
2024-11-05 15:54:21 +00:00
|
|
|
from lib.reqs import get_urls, get_user_accounts, add_user_account, delete_user_account, get_user_accounts_count
|
2024-11-05 08:09:08 +00:00
|
|
|
from flask import send_from_directory
|
2024-11-02 20:04:56 +00:00
|
|
|
import requests
|
|
|
|
import base64
|
|
|
|
from flask import Flask
|
2024-11-05 15:54:21 +00:00
|
|
|
from config import DevelopmentConfig
|
2024-11-02 20:04:56 +00:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
2024-11-05 08:09:08 +00:00
|
|
|
app.config.from_object(
|
|
|
|
DevelopmentConfig
|
2024-11-05 15:54:21 +00:00
|
|
|
)
|
2024-11-05 08:09:08 +00:00
|
|
|
cache = Cache(app, config={"CACHE_TYPE": "SimpleCache"})
|
2024-11-02 20:04:56 +00:00
|
|
|
|
2024-11-07 17:00:54 +00:00
|
|
|
@app.route('/manifest.json')
|
|
|
|
def serve_manifest():
|
|
|
|
return send_file('manifest.json', mimetype='application/manifest+json')
|
2024-11-05 08:09:08 +00:00
|
|
|
|
|
|
|
@app.route("/favicon.ico")
|
|
|
|
def favicon():
|
|
|
|
return send_from_directory(
|
|
|
|
os.path.join(app.root_path, "static"),
|
|
|
|
"favicon.ico",
|
|
|
|
mimetype="image/vnd.microsoft.icon",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/")
|
2024-11-03 19:54:56 +00:00
|
|
|
def index():
|
2024-11-02 20:04:56 +00:00
|
|
|
# If the user is logged in, redirect to a protected page like /accounts
|
2024-11-05 08:09:08 +00:00
|
|
|
if session.get("logged_in"):
|
|
|
|
return redirect(url_for("home"))
|
|
|
|
return render_template("index.html")
|
|
|
|
|
2024-11-02 20:04:56 +00:00
|
|
|
|
2024-11-05 08:09:08 +00:00
|
|
|
@app.route("/home")
|
2024-11-03 19:54:56 +00:00
|
|
|
@cache.cached(timeout=60) # cache for 120 seconds
|
|
|
|
def home():
|
2024-11-11 12:36:02 +00:00
|
|
|
if session.get("logged_in"):
|
|
|
|
base_url = app.config["BASE_URL"] # Access base_url from the config
|
|
|
|
all_accounts = get_user_accounts(base_url, session["auth_credentials"])
|
|
|
|
count = len(all_accounts)
|
|
|
|
current_month_accounts = filter_accounts_current_month(all_accounts)
|
|
|
|
expired_accounts = filter_accounts_expired(all_accounts)
|
|
|
|
return render_template(
|
|
|
|
"home.html",
|
|
|
|
username=session["username"],
|
|
|
|
accounts=count,
|
|
|
|
current_month_accounts=current_month_accounts,
|
|
|
|
expired_accounts=expired_accounts,
|
|
|
|
)
|
|
|
|
return render_template("index.html")
|
2024-11-05 08:09:08 +00:00
|
|
|
|
2024-11-03 19:54:56 +00:00
|
|
|
|
2024-11-05 08:09:08 +00:00
|
|
|
@app.route("/login", methods=["POST"])
|
2024-11-02 20:04:56 +00:00
|
|
|
def login():
|
2024-11-05 08:09:08 +00:00
|
|
|
username = request.form["username"]
|
|
|
|
password = request.form["password"]
|
|
|
|
|
2024-11-02 20:04:56 +00:00
|
|
|
# Encode the username and password in Base64
|
|
|
|
credentials = f"{username}:{password}"
|
|
|
|
encoded_credentials = base64.b64encode(credentials.encode()).decode()
|
2024-11-05 08:09:08 +00:00
|
|
|
|
|
|
|
base_url = app.config["BASE_URL"] # Access base_url from the config
|
2024-11-02 20:04:56 +00:00
|
|
|
login_url = f"{base_url}/Login" # Construct the full URL
|
|
|
|
|
|
|
|
# Send GET request to the external login API with Basic Auth
|
|
|
|
response = requests.get(
|
2024-11-05 08:09:08 +00:00
|
|
|
login_url, auth=requests.auth.HTTPBasicAuth(username, password)
|
2024-11-02 20:04:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Check if login was successful
|
|
|
|
if response.status_code == 200 and response.json().get("auth") == "Success":
|
|
|
|
# Set session variable to indicate the user is logged in
|
2024-11-05 08:09:08 +00:00
|
|
|
session["logged_in"] = True
|
|
|
|
session["username"] = username
|
|
|
|
session["auth_credentials"] = encoded_credentials
|
|
|
|
return redirect(url_for("home")) # Redirect to the Accounts page
|
2024-11-02 20:04:56 +00:00
|
|
|
else:
|
|
|
|
# Show error on the login page
|
|
|
|
error = "Invalid username or password. Please try again."
|
2024-11-05 08:09:08 +00:00
|
|
|
return render_template("index.html", error=error)
|
2024-11-02 20:04:56 +00:00
|
|
|
|
2024-11-05 08:09:08 +00:00
|
|
|
|
|
|
|
@app.route("/urls", methods=["GET"])
|
2024-11-03 15:34:10 +00:00
|
|
|
@cache.cached(timeout=300) # cache for 5 minutes
|
2024-11-02 20:04:56 +00:00
|
|
|
def urls():
|
|
|
|
# Check if the user is logged in
|
2024-11-05 08:09:08 +00:00
|
|
|
if not session.get("logged_in"):
|
|
|
|
return redirect(url_for("home"))
|
2024-11-02 20:04:56 +00:00
|
|
|
# Placeholder content for Accounts page
|
2024-11-05 08:09:08 +00:00
|
|
|
base_url = app.config["BASE_URL"] # Access base_url from the config
|
|
|
|
return render_template(
|
|
|
|
"urls.html", urls=get_urls(base_url, session["auth_credentials"])
|
|
|
|
)
|
2024-11-02 20:04:56 +00:00
|
|
|
|
2024-11-05 08:09:08 +00:00
|
|
|
|
|
|
|
@app.route("/accounts", methods=["GET"])
|
2024-11-03 15:34:10 +00:00
|
|
|
@cache.cached(timeout=120) # cache for 120 seconds
|
2024-11-02 20:04:56 +00:00
|
|
|
def user_accounts():
|
|
|
|
# Check if the user is logged in
|
2024-11-05 08:09:08 +00:00
|
|
|
if not session.get("logged_in"):
|
|
|
|
return redirect(url_for("home"))
|
2024-11-02 20:04:56 +00:00
|
|
|
# Placeholder content for Accounts page
|
2024-11-05 08:09:08 +00:00
|
|
|
base_url = app.config["BASE_URL"] # Access base_url from the config
|
|
|
|
return render_template(
|
|
|
|
"user_accounts.html",
|
|
|
|
username=session["username"],
|
|
|
|
user_accounts=get_user_accounts(base_url, session["auth_credentials"]),
|
|
|
|
auth=session["auth_credentials"],
|
|
|
|
)
|
2024-11-02 20:04:56 +00:00
|
|
|
|
2024-11-04 16:39:02 +00:00
|
|
|
|
2024-11-05 08:09:08 +00:00
|
|
|
@app.route("/accounts/add", methods=["GET", "POST"])
|
2024-11-04 16:39:02 +00:00
|
|
|
def add_account():
|
2024-11-05 08:09:08 +00:00
|
|
|
base_url = app.config["BASE_URL"] # Access base_url from the config
|
|
|
|
if request.method == "POST":
|
|
|
|
username = request.form["username"]
|
|
|
|
password = request.form["password"]
|
|
|
|
stream = request.form["stream"]
|
|
|
|
|
|
|
|
if add_user_account(
|
|
|
|
base_url, session["auth_credentials"], username, password, stream
|
|
|
|
):
|
2024-11-04 16:39:02 +00:00
|
|
|
cache.clear() # Clears all cache entries
|
2024-11-05 08:09:08 +00:00
|
|
|
return redirect(url_for("user_accounts"))
|
|
|
|
return render_template("add_account.html")
|
|
|
|
|
|
|
|
return render_template("add_account.html")
|
2024-11-04 16:39:02 +00:00
|
|
|
|
2024-11-05 08:09:08 +00:00
|
|
|
|
|
|
|
@app.route("/accounts/delete", methods=["POST"])
|
2024-11-04 16:39:02 +00:00
|
|
|
def delete_account():
|
2024-11-05 08:09:08 +00:00
|
|
|
stream = request.form.get("stream")
|
|
|
|
username = request.form.get("username")
|
|
|
|
base_url = app.config["BASE_URL"]
|
|
|
|
|
|
|
|
if delete_user_account(base_url, session["auth_credentials"], stream, username):
|
2024-11-04 16:39:02 +00:00
|
|
|
cache.clear() # Clears all cache entries
|
2024-11-05 08:09:08 +00:00
|
|
|
return redirect(url_for("user_accounts"))
|
|
|
|
return redirect(url_for("user_accounts"))
|
2024-11-04 16:39:02 +00:00
|
|
|
|
|
|
|
|
2024-11-05 08:09:08 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(debug=app.config["DEBUG"], host=app.config["HOST"], port=app.config["PORT"])
|