Fix link redirects and remove BASE_URL
This commit is contained in:
parent
9e99695611
commit
0b6dafec62
@ -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 <br> *(requires IP2Location.io account)* | **Required**
|
||||
API_KEY | `KEY`: IP2Location.io API Key | **Required** *only if IP_TO_LOCATION is set to True*
|
@ -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"):
|
||||
|
@ -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."
|
||||
)
|
||||
|
14
config.py
14
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"])
|
||||
|
||||
|
@ -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:
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user