import os from pathlib import Path BASE_DIR = Path(__file__).resolve().parent.parent def env_bool(name, default=False): return os.getenv(name, str(default)).strip().lower() in {"1", "true", "yes", "on"} def env_list(name, default=""): return [item.strip() for item in os.getenv(name, default).split(",") if item.strip()] SECRET_KEY = os.getenv("DJANGO_SECRET_KEY", "") DEBUG = env_bool("DJANGO_DEBUG", False) if not SECRET_KEY: if DEBUG: SECRET_KEY = "development-only-insecure-secret-key" else: raise RuntimeError("DJANGO_SECRET_KEY is required when DJANGO_DEBUG is false") ALLOWED_HOSTS = env_list("DJANGO_ALLOWED_HOSTS", "vpn.toplc.cn,localhost,127.0.0.1") CSRF_TRUSTED_ORIGINS = env_list("DJANGO_CSRF_TRUSTED_ORIGINS", "https://vpn.toplc.cn") INSTALLED_APPS = [ "django.contrib.admin", "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", "accounts.apps.AccountsConfig", "control.apps.ControlConfig", ] MIDDLEWARE = [ "django.middleware.security.SecurityMiddleware", "whitenoise.middleware.WhiteNoiseMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", "django.middleware.common.CommonMiddleware", "django.middleware.csrf.CsrfViewMiddleware", "django.contrib.auth.middleware.AuthenticationMiddleware", "accounts.middleware.LoginRateLimitMiddleware", "django.contrib.messages.middleware.MessageMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", ] ROOT_URLCONF = "toplc_vpn.urls" TEMPLATES = [ { "BACKEND": "django.template.backends.django.DjangoTemplates", "DIRS": [BASE_DIR / "templates"], "APP_DIRS": True, "OPTIONS": { "context_processors": [ "django.template.context_processors.request", "django.contrib.auth.context_processors.auth", "django.contrib.messages.context_processors.messages", "control.context_processors.platform_settings", ], }, } ] WSGI_APPLICATION = "toplc_vpn.wsgi.application" ASGI_APPLICATION = "toplc_vpn.asgi.application" DATABASES = { "default": { "ENGINE": "django.db.backends.postgresql", "NAME": os.getenv("POSTGRES_DB", "toplc_vpn"), "USER": os.getenv("POSTGRES_USER", "toplc_vpn"), "PASSWORD": os.getenv("POSTGRES_PASSWORD", ""), "HOST": os.getenv("POSTGRES_HOST", "db"), "PORT": os.getenv("POSTGRES_PORT", "5432"), "CONN_MAX_AGE": 60, "CONN_HEALTH_CHECKS": True, } } AUTH_PASSWORD_VALIDATORS = [ {"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator"}, {"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", "OPTIONS": {"min_length": 10}}, {"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"}, {"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"}, ] PASSWORD_HASHERS = [ "django.contrib.auth.hashers.Argon2PasswordHasher", "django.contrib.auth.hashers.PBKDF2PasswordHasher", ] AUTH_USER_MODEL = "accounts.User" LOGIN_URL = "login" LOGIN_REDIRECT_URL = "portal-dashboard" LOGOUT_REDIRECT_URL = "login" LANGUAGE_CODE = "zh-hans" TIME_ZONE = "Asia/Shanghai" USE_I18N = True USE_TZ = True STATIC_URL = "/static/" STATIC_ROOT = BASE_DIR / "staticfiles" STATICFILES_DIRS = [BASE_DIR / "static"] STORAGES = { "default": {"BACKEND": "django.core.files.storage.FileSystemStorage"}, "staticfiles": {"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage"}, } DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" PUBLIC_BASE_URL = os.getenv("VPN_PUBLIC_BASE_URL", "https://vpn.toplc.cn").rstrip("/") # Credentials are injected server-side; no admin form or browser receives the secret. WECOM_ENABLED = env_bool("WECOM_ENABLED", False) WECOM_CORP_ID = os.getenv("WECOM_CORP_ID", "").strip() WECOM_AGENT_ID = os.getenv("WECOM_AGENT_ID", "").strip() WECOM_SECRET = os.getenv("WECOM_SECRET", "").strip() VPN_PUBLIC_GATEWAY_HOST = os.getenv("VPN_PUBLIC_GATEWAY_HOST", "vpn.toplc.cn").strip() VPN_PUBLIC_GATEWAY_IPV4 = os.getenv("VPN_PUBLIC_GATEWAY_IPV4", "").strip() # Optional employee-only same-node transport hint. Enabling also requires the # node/Tailnet firewall to explicitly authorize client sources; sharing is not login. VPN_TAILSCALE_ENDPOINT_ENABLED = env_bool("VPN_TAILSCALE_ENDPOINT_ENABLED", False) VPN_TAILSCALE_ENDPOINTS_JSON = os.getenv("VPN_TAILSCALE_ENDPOINTS_JSON", "") # Private auxiliary is NOT a public/core node. Keep disabled until board-side # per-device authorization/renewal/revocation and reboot acceptance are complete. VPN_PRIVATE_AUXILIARY_ENABLED = env_bool("VPN_PRIVATE_AUXILIARY_ENABLED", False) VPN_PRIVATE_AUXILIARY_VALIDATION_DEVICE_ID = int(os.getenv("VPN_PRIVATE_AUXILIARY_VALIDATION_DEVICE_ID", "0")) VPN_PRIVATE_AUXILIARY_TLS_LEAF_SHA256 = os.getenv("VPN_PRIVATE_AUXILIARY_TLS_LEAF_SHA256", "").strip() VPN_PRIVATE_AUXILIARY_AGENT_TOKEN_SHA256 = os.getenv("VPN_PRIVATE_AUXILIARY_AGENT_TOKEN_SHA256", "").strip() VPN_PRIVATE_AUXILIARY_ADDRESS = os.getenv("VPN_PRIVATE_AUXILIARY_ADDRESS", "192.168.50.111").strip() VPN_PRIVATE_AUXILIARY_CONNECT_ADDRESS = os.getenv("VPN_PRIVATE_AUXILIARY_CONNECT_ADDRESS", "").strip() SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") USE_X_FORWARDED_HOST = True SECURE_SSL_REDIRECT = env_bool("DJANGO_SECURE_SSL_REDIRECT", True) SECURE_HSTS_SECONDS = int(os.getenv("DJANGO_HSTS_SECONDS", "31536000")) SECURE_HSTS_INCLUDE_SUBDOMAINS = False SECURE_HSTS_PRELOAD = False SESSION_COOKIE_SECURE = not DEBUG SESSION_COOKIE_HTTPONLY = True SESSION_COOKIE_SAMESITE = "Lax" CSRF_COOKIE_SECURE = not DEBUG CSRF_COOKIE_SAMESITE = "Lax" X_FRAME_OPTIONS = "DENY" SECURE_CONTENT_TYPE_NOSNIFF = True SECURE_REFERRER_POLICY = "same-origin" EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" LOGGING = { "version": 1, "disable_existing_loggers": False, "handlers": {"console": {"class": "logging.StreamHandler"}}, "root": {"handlers": ["console"], "level": "INFO"}, }