From 0b6dafec62f22383bf501e1594d3b9a8c93f2337 Mon Sep 17 00:00:00 2001 From: Parker Date: Sun, 3 Nov 2024 21:29:10 -0600 Subject: [PATCH] Fix link redirects and remove BASE_URL --- README.md | 1 - app/main.py | 5 ++--- app/util/log.py | 8 ++++---- config.py | 14 ++------------ docker-compose.yaml | 1 - linklogger.py | 4 ++-- 6 files changed, 10 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 20ef84e..d9fd3d6 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,5 @@ Below are all of the configuration variables that are used within LinkLogger. Variable | Description | Requirement ---|---|--- -BASE_URL | `URL`: Redirect URL for when people visit old, dead, or non-existant links | **Required** IP_TO_LOCATION | `BOOLEAN`: Whether or not you want toe IP to Location feature
*(requires IP2Location.io account)* | **Required** API_KEY | `KEY`: IP2Location.io API Key | **Required** *only if IP_TO_LOCATION is set to True* \ No newline at end of file diff --git a/app/main.py b/app/main.py index 266c589..78a65c8 100644 --- a/app/main.py +++ b/app/main.py @@ -15,7 +15,6 @@ import random from models import User, Link from database import * from app.util.log import log -from config import BASE_URL class FlaskUser(UserMixin): @@ -158,14 +157,14 @@ def log_redirect(link: str): link = link.upper() # If `link` is not exactly 5 characters, return redirect to base url if len(link) != 5: - return redirect(BASE_URL) + return redirect(url_for("login")) # Make sure the link exists in the database db = SessionLocal() link_record = db.query(Link).filter(Link.link == link).first() if not link_record: db.close() - return redirect(BASE_URL) + return redirect(url_for("login")) else: # Log the visit if request.headers.get("X-Real-IP"): diff --git a/app/util/log.py b/app/util/log.py index 943863d..6be82e7 100644 --- a/app/util/log.py +++ b/app/util/log.py @@ -4,10 +4,10 @@ from ua_parser import user_agent_parser from ip2locationio.ipgeolocation import IP2LocationIOAPIError from database import SessionLocal -from config import LOG, API_KEY, IP_TO_LOCATION +import config from models import Link, Record -configuration = ip2locationio.Configuration(API_KEY) +configuration = ip2locationio.Configuration(config.API_KEY) ipgeolocation = ip2locationio.IPGeolocation(configuration) """ @@ -25,7 +25,7 @@ def log(link, ip, user_agent): .first() ) - if IP_TO_LOCATION: + if config.IP_TO_LOCATION: # Get IP to GEO via IP2Location.io try: data = ipgeolocation.lookup(ip) @@ -33,7 +33,7 @@ def log(link, ip, user_agent): isp = data["as"] # Fatal error, API key is invalid or out of requests, quit except IP2LocationIOAPIError: - LOG.error( + config.LOG.error( "Invalid API key or insufficient credits. Change the" " `config.ini` file if you no longer need IP geolocation." ) diff --git a/config.py b/config.py index 9c56add..bb878bf 100644 --- a/config.py +++ b/config.py @@ -1,7 +1,6 @@ import jsonschema import os import yaml -import validators import sys import logging from colorlog import ColoredFormatter @@ -23,7 +22,6 @@ LOG = logging.getLogger("pythonConfig") LOG.setLevel(log_level) LOG.addHandler(stream) -BASE_URL = None IP_TO_LOCATION = None API_KEY = None @@ -33,11 +31,10 @@ schema = { "config": { "type": "object", "properties": { - "base_url": {"type": "string"}, "ip_to_location": {"type": "boolean"}, "api_key": {"type": "string"}, }, - "required": ["base_url", "ip_to_location"], + "required": ["ip_to_location"], } }, "required": ["config"], @@ -61,7 +58,6 @@ def load_config(): with open(file_path, "w") as f: f.write( """ - base_url: "" ip_to_location: "" api_key: "" """ @@ -75,7 +71,7 @@ def load_config(): # Validate the options within config.yaml def validate_config(file_contents): - global BASE_URL, IP_TO_LOCATION, API_KEY + global IP_TO_LOCATION, API_KEY config = yaml.safe_load(file_contents) try: @@ -84,12 +80,6 @@ def validate_config(file_contents): LOG.error(e.message) sys.exit() - # Validate BASE_URL - if not validators.url(config["config"]["base_url"]): - LOG.error("BASE_URL is not a valid URL") - else: - BASE_URL = config["config"]["base_url"] - # Make IP_TO_LOCATION a boolean IP_TO_LOCATION = bool(config["config"]["ip_to_location"]) diff --git a/docker-compose.yaml b/docker-compose.yaml index 80f6942..b6599b5 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -4,7 +4,6 @@ services: image: ghcr.io/packetparker/linklogger:latest network_mode: host environment: - - BASE_URL=https://your.domain - IP_TO_LOCATION=True - API_KEY=your_api_key volumes: diff --git a/linklogger.py b/linklogger.py index bc735a1..90348e3 100644 --- a/linklogger.py +++ b/linklogger.py @@ -1,7 +1,7 @@ from werkzeug.middleware.dispatcher import DispatcherMiddleware from a2wsgi import ASGIMiddleware -from config import load_config +import config from app.main import app as flask_app from api.main import app as fastapi_app from database import Base, engine @@ -17,5 +17,5 @@ flask_app.wsgi_app = DispatcherMiddleware( ) if __name__ == "__main__": - load_config() + config.load_config() flask_app.run(port=5252)