perf: 优化 domains 获取

pull/11075/head
ibuler 2023-07-25 10:11:57 +08:00
parent 2a24fcc1bb
commit 4d2c4a9602
1 changed files with 22 additions and 13 deletions

View File

@ -67,32 +67,41 @@ SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# LOG LEVEL # LOG LEVEL
LOG_LEVEL = CONFIG.LOG_LEVEL LOG_LEVEL = CONFIG.LOG_LEVEL
DOMAINS = CONFIG.DOMAINS or CONFIG.SITE_URL DOMAINS = CONFIG.DOMAINS or 'localhost'
if os.environ.get('SERVER_NAME'):
DOMAINS += ',{}'.format(os.environ.get('SERVER_NAME'))
if CONFIG.SITE_URL:
DOMAINS += ',{}'.format(CONFIG.SITE_URL)
ALLOWED_DOMAINS = DOMAINS.split(',') if DOMAINS else ['localhost:8080'] ALLOWED_DOMAINS = DOMAINS.split(',') if DOMAINS else ['localhost:8080']
ALLOWED_DOMAINS = [host.strip() for host in ALLOWED_DOMAINS] ALLOWED_DOMAINS = [host.strip() for host in ALLOWED_DOMAINS]
ALLOWED_DOMAINS = [host.replace('http://', '').replace('https://', '') for host in ALLOWED_DOMAINS if host] ALLOWED_DOMAINS = [host.replace('http://', '').replace('https://', '') for host in ALLOWED_DOMAINS if host]
ALLOWED_DOMAINS = [host.split('/')[0] for host in ALLOWED_DOMAINS if host] ALLOWED_DOMAINS = [host.split('/')[0] for host in ALLOWED_DOMAINS if host]
DEBUG_HOSTS = ['127.0.0.1', 'localhost'] DEBUG_HOSTS = ('127.0.0.1', 'localhost')
DEBUG_PORT = ['8080', '80', '443', '4200', '9528'] DEBUG_PORT = ['8080', '80', ]
if DEBUG: if DEBUG:
DEBUG_HOST_PORTS = ['{}:{}'.format(host, port) for host in DEBUG_HOSTS for port in DEBUG_PORT] DEBUG_PORT.extend(['4200', '9528'])
ALLOWED_DOMAINS.extend(DEBUG_HOST_PORTS) DEBUG_HOST_PORTS = ['{}:{}'.format(host, port) for host in DEBUG_HOSTS for port in DEBUG_PORT]
ALLOWED_DOMAINS.extend(DEBUG_HOST_PORTS)
ALLOWED_HOSTS = list(set(['.' + host.split(':')[0] for host in ALLOWED_DOMAINS])) ALLOWED_HOSTS = list(set(['.' + host.split(':')[0] for host in ALLOWED_DOMAINS]))
print("ALLOWED_HOSTS: ", ALLOWED_HOSTS) print("ALLOWED_HOSTS: ", )
for host in ALLOWED_HOSTS:
print(' - ' + host)
# https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-CSRF_TRUSTED_ORIGINS # https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-CSRF_TRUSTED_ORIGINS
CSRF_TRUSTED_ORIGINS = [] CSRF_TRUSTED_ORIGINS = []
for host_port in ALLOWED_DOMAINS:
for host in ALLOWED_DOMAINS: origin = host_port.strip('.')
host = host.strip('.') if origin.startswith('http'):
if host.startswith('http'): CSRF_TRUSTED_ORIGINS.append(origin)
CSRF_TRUSTED_ORIGINS.append(host)
continue continue
is_local_origin = origin.split(':')[0] in DEBUG_HOSTS
for schema in ['https', 'http']: for schema in ['https', 'http']:
# CSRF_TRUSTED_ORIGINS.append('{}://{}'.format(schema, host)) if is_local_origin and schema == 'https':
CSRF_TRUSTED_ORIGINS.append('{}://*.{}'.format(schema, host)) continue
CSRF_TRUSTED_ORIGINS.append('{}://*.{}'.format(schema, origin))
print("CSRF_TRUSTED_ORIGINS: ") print("CSRF_TRUSTED_ORIGINS: ")
for origin in CSRF_TRUSTED_ORIGINS: for origin in CSRF_TRUSTED_ORIGINS: