elaborate TA_HOST parsed, #441

This commit is contained in:
simon 2023-04-09 13:27:47 +07:00
parent a924d648d6
commit a98a30cc85
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
2 changed files with 19 additions and 11 deletions

View File

@ -18,6 +18,7 @@ import ldap
from corsheaders.defaults import default_headers
from django_auth_ldap.config import LDAPSearch
from home.src.ta.config import AppConfig
from home.src.ta.helper import ta_host_parser
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
@ -32,17 +33,7 @@ SECRET_KEY = PW_HASH.hexdigest()
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = bool(environ.get("DJANGO_DEBUG"))
ALLOWED_HOSTS = []
if environ.get("TA_HOST"):
ALLOWED_HOSTS = [i.strip() for i in environ.get("TA_HOST").split()]
CSRF_TRUSTED_ORIGINS = []
for host in ALLOWED_HOSTS:
if host.startswith("http://") or host.startswith("https://"):
CSRF_TRUSTED_ORIGINS.append(host)
else:
CSRF_TRUSTED_ORIGINS.append(f"http://{host}")
ALLOWED_HOSTS, CSRF_TRUSTED_ORIGINS = ta_host_parser(environ["TA_HOST"])
# Application definition

View File

@ -11,6 +11,7 @@ import string
import subprocess
import unicodedata
from datetime import datetime
from urllib.parse import urlparse
import requests
@ -148,6 +149,22 @@ def is_shorts(youtube_id):
return response.status_code == 200
def ta_host_parser(ta_host):
"""parse ta_host env var for ALLOWED_HOSTS and CSRF_TRUSTED_ORIGINS"""
allowed_hosts = []
csrf_trusted_origins = []
for host in ta_host.split():
host_clean = host.strip()
if not host_clean.startswith("http"):
host_clean = f"http://{host}"
parsed = urlparse(host_clean)
allowed_hosts.append(f"{parsed.hostname}")
csrf_trusted_origins.append(f"{parsed.scheme}://{parsed.hostname}")
return allowed_hosts, csrf_trusted_origins
class DurationConverter:
"""
using ffmpeg to get and parse duration from filepath