2020-09-04 10:47:20 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
configtest.py
|
|
|
|
|
|
|
|
Tests the validity of your configuration in settings.py.
|
|
|
|
"""
|
2020-09-06 15:24:20 -05:00
|
|
|
|
2020-08-31 17:29:54 -05:00
|
|
|
import os
|
|
|
|
import settings as settings
|
|
|
|
|
|
|
|
|
|
|
|
# Default settings
|
|
|
|
defaults = {
|
|
|
|
"UPLOAD_FOLDER": "/var/www/img",
|
|
|
|
"ALLOWED_EXTENSIONS": [".png", ".jpg", ".jpeg", ".svg", ".bmp", ".gif", ".ico", ".webp"],
|
|
|
|
"ROOTURL": "https://img.bbaovanc.com/",
|
|
|
|
"SAVELOG": "savelog.log",
|
|
|
|
"SAVELOG_CHMOD": "0o644",
|
|
|
|
"SAVELOG_KEYPREFIX": 4,
|
|
|
|
}
|
|
|
|
|
|
|
|
deftypes = {
|
|
|
|
"UPLOAD_FOLDER": str,
|
|
|
|
"ALLOWED_EXTENSIONS": list,
|
|
|
|
"ROOTURL": str,
|
|
|
|
"SAVELOG": str,
|
|
|
|
"SAVELOG_CHMOD": int,
|
|
|
|
"SAVELOG_KEYPREFIX": int,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Check for unset settings
|
|
|
|
checksettings = list(defaults.keys())
|
|
|
|
unset_settings = [i for i in defaults.keys() if i not in dir(settings)]
|
|
|
|
if len(unset_settings) > 0:
|
|
|
|
for unset in unset_settings:
|
|
|
|
checksettings.remove(unset)
|
2020-10-04 11:13:59 -05:00
|
|
|
print(f"[!] {unset} is unset. The default value is type {deftypes[unset].__name__} with value {defaults[unset]}")
|
2020-08-31 17:29:54 -05:00
|
|
|
else:
|
|
|
|
print("[" + u"\u2713" + "] Found all required settings!")
|
|
|
|
|
|
|
|
|
|
|
|
# Check if types of settings are correct
|
|
|
|
typesgood = True
|
|
|
|
typeswrong = []
|
|
|
|
for testtype in checksettings:
|
|
|
|
if type(getattr(settings, testtype)) is not deftypes[testtype]:
|
2020-10-04 11:13:59 -05:00
|
|
|
print(f"[!] {testtype} requires {deftypes[testtype].__name__}, but is {type(getattr(settings, testtype)).__name__}")
|
2020-08-31 17:29:54 -05:00
|
|
|
typeswrong.append(testtype)
|
|
|
|
typesgood = False
|
|
|
|
|
|
|
|
if typesgood:
|
|
|
|
print("[" + u"\u2713" + "] Types are good!")
|
|
|
|
|
|
|
|
|
|
|
|
# Check if allowed extensions all start with a .
|
|
|
|
invalid_exts = []
|
|
|
|
if "ALLOWED_EXTENSIONS" in checksettings:
|
|
|
|
for e in settings.ALLOWED_EXTENSIONS:
|
|
|
|
if not e.startswith("."):
|
|
|
|
invalid_exts.append(e)
|
2020-08-31 20:29:07 -05:00
|
|
|
|
2020-08-31 17:29:54 -05:00
|
|
|
if len(invalid_exts) > 0:
|
|
|
|
print("[!] The following extensions listed in ALLOWED_EXTENSIONS are invalid:")
|
|
|
|
for e in invalid_exts:
|
2020-10-04 11:13:59 -05:00
|
|
|
print(f" {e} is listed in ALLOWED_EXTENSIONS, but doesn't start with a .")
|
2020-08-31 17:29:54 -05:00
|
|
|
else:
|
|
|
|
print("[" + u"\u2713" + "] ALLOWED_EXTENSIONS is good!")
|
|
|
|
|
|
|
|
|
|
|
|
# Check if UPLOAD_FOLDER exists
|
|
|
|
uploadfolder_exists = True
|
|
|
|
if "UPLOAD_FOLDER" in checksettings:
|
|
|
|
if not os.path.isdir(settings.UPLOAD_FOLDER):
|
|
|
|
uploadfolder_exists = False
|
2020-10-04 11:13:59 -05:00
|
|
|
print(f"[!] The directory set in UPLOAD_FOLDER ('{settings.UPLOAD_FOLDER}') doesn't exist!")
|
2020-08-31 17:29:54 -05:00
|
|
|
else:
|
|
|
|
print("[" + u"\u2713" + "] UPLOAD_FOLDER exists!")
|
|
|
|
|
|
|
|
|
|
|
|
# Check if ROOTURL starts with http(s):// and ends with /
|
|
|
|
rooturl_good = True
|
|
|
|
if "ROOTURL" in checksettings:
|
|
|
|
if settings.ROOTURL.startswith("http://") or settings.ROOTURL.startswith("https://"):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
rooturl_good = False
|
|
|
|
print("[!] ROOTURL does not start with `http://` or `https://`! This may cause issues!")
|
|
|
|
if not settings.ROOTURL.endswith("/"):
|
|
|
|
rooturl_good = False
|
|
|
|
print("[!] ROOTURL does not end with a `/`. This WILL cause issues!")
|
|
|
|
|
|
|
|
if not rooturl_good:
|
|
|
|
print(" With your current settings, this is what a generated url would look like:")
|
2020-10-04 11:13:59 -05:00
|
|
|
print(f" {settings.ROOTURL}example.png")
|
2020-08-31 17:29:54 -05:00
|
|
|
else:
|
|
|
|
print("[" + u"\u2713" + "] ROOTURL is good!")
|
|
|
|
|
|
|
|
|
2020-08-31 20:29:07 -05:00
|
|
|
# Ask the user if SAVELOG is the intended filename
|
|
|
|
if "SAVELOG" in checksettings:
|
2020-10-04 11:13:59 -05:00
|
|
|
print(f"[*] SAVELOG was interpreted to be {settings.SAVELOG}")
|
2020-08-31 20:29:07 -05:00
|
|
|
print("[*] If this is not the intended filename, please fix it.")
|
|
|
|
|
|
|
|
|
2020-08-31 17:29:54 -05:00
|
|
|
# Show summary
|
|
|
|
print()
|
|
|
|
print("----- SUMMARY -----")
|
|
|
|
summarygood = True
|
|
|
|
if len(unset_settings) > 0:
|
|
|
|
summarygood = False
|
|
|
|
print("Unset settings:")
|
|
|
|
for unset in unset_settings:
|
2020-10-04 11:13:59 -05:00
|
|
|
print(f" {unset}")
|
2020-08-31 17:29:54 -05:00
|
|
|
|
|
|
|
if len(typeswrong) > 0:
|
|
|
|
summarygood = False
|
|
|
|
print("Incorrect types:")
|
|
|
|
for wtype in typeswrong:
|
2020-10-04 11:13:59 -05:00
|
|
|
print(f" {wtype}")
|
2020-08-31 17:29:54 -05:00
|
|
|
|
|
|
|
if len(invalid_exts) > 0:
|
|
|
|
summarygood = False
|
|
|
|
print("Invalid extensions:")
|
|
|
|
for wext in invalid_exts:
|
2020-10-04 11:13:59 -05:00
|
|
|
print(f" '{wext}'")
|
2020-08-31 17:29:54 -05:00
|
|
|
|
|
|
|
if not uploadfolder_exists:
|
|
|
|
summarygood = False
|
2020-10-04 11:13:59 -05:00
|
|
|
print(f"UPLOAD_FOLDER ({settings.UPLOAD_FOLDER}) does not exist!")
|
2020-08-31 17:29:54 -05:00
|
|
|
|
|
|
|
if not rooturl_good:
|
|
|
|
summarygood = False
|
|
|
|
print("ROOTURL may cause issues!")
|
|
|
|
print("With current settings, this is what a generated URL would look like:")
|
2020-10-04 11:13:59 -05:00
|
|
|
print(f"{settings.ROOTURL}example.png")
|
2020-08-31 17:29:54 -05:00
|
|
|
|
|
|
|
if "SAVELOG" in checksettings:
|
2020-10-04 11:13:59 -05:00
|
|
|
print(f"[*] SAVELOG is {settings.SAVELOG}")
|
2020-08-31 17:29:54 -05:00
|
|
|
|
|
|
|
if summarygood:
|
|
|
|
print("[" + u"\u2713" + "] This configuration passes all tests!")
|