Fix link redirects and remove BASE_URL

This commit is contained in:
Parker M. 2024-11-03 21:29:10 -06:00
parent 9e99695611
commit 0b6dafec62
Signed by: parker
GPG Key ID: 505ED36FC12B5D5E
6 changed files with 10 additions and 23 deletions

View File

@ -51,6 +51,5 @@ Below are all of the configuration variables that are used within LinkLogger.
Variable | Description | Requirement 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 <br> *(requires IP2Location.io account)* | **Required** IP_TO_LOCATION | `BOOLEAN`: Whether or not you want toe IP to Location feature <br> *(requires IP2Location.io account)* | **Required**
API_KEY | `KEY`: IP2Location.io API Key | **Required** *only if IP_TO_LOCATION is set to True* API_KEY | `KEY`: IP2Location.io API Key | **Required** *only if IP_TO_LOCATION is set to True*

View File

@ -15,7 +15,6 @@ import random
from models import User, Link from models import User, Link
from database import * from database import *
from app.util.log import log from app.util.log import log
from config import BASE_URL
class FlaskUser(UserMixin): class FlaskUser(UserMixin):
@ -158,14 +157,14 @@ def log_redirect(link: str):
link = link.upper() link = link.upper()
# If `link` is not exactly 5 characters, return redirect to base url # If `link` is not exactly 5 characters, return redirect to base url
if len(link) != 5: if len(link) != 5:
return redirect(BASE_URL) return redirect(url_for("login"))
# Make sure the link exists in the database # Make sure the link exists in the database
db = SessionLocal() db = SessionLocal()
link_record = db.query(Link).filter(Link.link == link).first() link_record = db.query(Link).filter(Link.link == link).first()
if not link_record: if not link_record:
db.close() db.close()
return redirect(BASE_URL) return redirect(url_for("login"))
else: else:
# Log the visit # Log the visit
if request.headers.get("X-Real-IP"): if request.headers.get("X-Real-IP"):

View File

@ -4,10 +4,10 @@ from ua_parser import user_agent_parser
from ip2locationio.ipgeolocation import IP2LocationIOAPIError from ip2locationio.ipgeolocation import IP2LocationIOAPIError
from database import SessionLocal from database import SessionLocal
from config import LOG, API_KEY, IP_TO_LOCATION import config
from models import Link, Record from models import Link, Record
configuration = ip2locationio.Configuration(API_KEY) configuration = ip2locationio.Configuration(config.API_KEY)
ipgeolocation = ip2locationio.IPGeolocation(configuration) ipgeolocation = ip2locationio.IPGeolocation(configuration)
""" """
@ -25,7 +25,7 @@ def log(link, ip, user_agent):
.first() .first()
) )
if IP_TO_LOCATION: if config.IP_TO_LOCATION:
# Get IP to GEO via IP2Location.io # Get IP to GEO via IP2Location.io
try: try:
data = ipgeolocation.lookup(ip) data = ipgeolocation.lookup(ip)
@ -33,7 +33,7 @@ def log(link, ip, user_agent):
isp = data["as"] isp = data["as"]
# Fatal error, API key is invalid or out of requests, quit # Fatal error, API key is invalid or out of requests, quit
except IP2LocationIOAPIError: except IP2LocationIOAPIError:
LOG.error( config.LOG.error(
"Invalid API key or insufficient credits. Change the" "Invalid API key or insufficient credits. Change the"
" `config.ini` file if you no longer need IP geolocation." " `config.ini` file if you no longer need IP geolocation."
) )

View File

@ -1,7 +1,6 @@
import jsonschema import jsonschema
import os import os
import yaml import yaml
import validators
import sys import sys
import logging import logging
from colorlog import ColoredFormatter from colorlog import ColoredFormatter
@ -23,7 +22,6 @@ LOG = logging.getLogger("pythonConfig")
LOG.setLevel(log_level) LOG.setLevel(log_level)
LOG.addHandler(stream) LOG.addHandler(stream)
BASE_URL = None
IP_TO_LOCATION = None IP_TO_LOCATION = None
API_KEY = None API_KEY = None
@ -33,11 +31,10 @@ schema = {
"config": { "config": {
"type": "object", "type": "object",
"properties": { "properties": {
"base_url": {"type": "string"},
"ip_to_location": {"type": "boolean"}, "ip_to_location": {"type": "boolean"},
"api_key": {"type": "string"}, "api_key": {"type": "string"},
}, },
"required": ["base_url", "ip_to_location"], "required": ["ip_to_location"],
} }
}, },
"required": ["config"], "required": ["config"],
@ -61,7 +58,6 @@ def load_config():
with open(file_path, "w") as f: with open(file_path, "w") as f:
f.write( f.write(
""" """
base_url: ""
ip_to_location: "" ip_to_location: ""
api_key: "" api_key: ""
""" """
@ -75,7 +71,7 @@ def load_config():
# Validate the options within config.yaml # Validate the options within config.yaml
def validate_config(file_contents): 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) config = yaml.safe_load(file_contents)
try: try:
@ -84,12 +80,6 @@ def validate_config(file_contents):
LOG.error(e.message) LOG.error(e.message)
sys.exit() 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 # Make IP_TO_LOCATION a boolean
IP_TO_LOCATION = bool(config["config"]["ip_to_location"]) IP_TO_LOCATION = bool(config["config"]["ip_to_location"])

View File

@ -4,7 +4,6 @@ services:
image: ghcr.io/packetparker/linklogger:latest image: ghcr.io/packetparker/linklogger:latest
network_mode: host network_mode: host
environment: environment:
- BASE_URL=https://your.domain
- IP_TO_LOCATION=True - IP_TO_LOCATION=True
- API_KEY=your_api_key - API_KEY=your_api_key
volumes: volumes:

View File

@ -1,7 +1,7 @@
from werkzeug.middleware.dispatcher import DispatcherMiddleware from werkzeug.middleware.dispatcher import DispatcherMiddleware
from a2wsgi import ASGIMiddleware from a2wsgi import ASGIMiddleware
from config import load_config import config
from app.main import app as flask_app from app.main import app as flask_app
from api.main import app as fastapi_app from api.main import app as fastapi_app
from database import Base, engine from database import Base, engine
@ -17,5 +17,5 @@ flask_app.wsgi_app = DispatcherMiddleware(
) )
if __name__ == "__main__": if __name__ == "__main__":
load_config() config.load_config()
flask_app.run(port=5252) flask_app.run(port=5252)