diff --git a/.env.example b/.env.example index 9c932365..68c958e4 100644 --- a/.env.example +++ b/.env.example @@ -14,7 +14,7 @@ UVICORN_PORT = 8000 # XRAY_JSON = "xray_config.json" # XRAY_SUBSCRIPTION_URL_PREFIX = "https://example.com" -# XRAY_SUBSCRIPTION_PATH = sub +# XRAY_SUBSCRIPTION_PATH = "sub" # XRAY_EXECUTABLE_PATH = "/usr/local/bin/xray" # XRAY_ASSETS_PATH = "/usr/local/share/xray" # XRAY_EXCLUDE_INBOUND_TAGS = "INBOUND_X INBOUND_Y" @@ -37,8 +37,12 @@ UVICORN_PORT = 8000 # SINGBOX_SUBSCRIPTION_TEMPLATE="singbox/default.json" # MUX_TEMPLATE="mux/default.json" -## Enable JSON support for compatible clients to use mux, fragment, etc. Default is null, meaning false. -# USE_CUSTOM_JSON_DEFAULT="True" +## Enable JSON config for compatible clients to use mux, fragment, etc. Default False. +# USE_CUSTOM_JSON_DEFAULT=True +## Your preferred config type for different clients +## If USE_CUSTOM_JSON_DEFAULT is set True, all following programs will use the JSON config +# USE_CUSTOM_JSON_FOR_V2RAYN=False +# USE_CUSTOM_JSON_FOR_V2RAYNG=True ## Set headers for subscription # SUB_PROFILE_TITLE = "Susbcription" @@ -55,8 +59,8 @@ UVICORN_PORT = 8000 # ONHOLD_STATUS_TEXT = "On-Hold" ### for developers -# DOCS=true -# DEBUG=true +# DOCS=True +# DEBUG=True # If You Want To Send Webhook To Multiple Server Add Multi Address # WEBHOOK_ADDRESS = "http://127.0.0.1:9000/,http://127.0.0.1:9001/" diff --git a/app/views/subscription.py b/app/views/subscription.py index 8c1d21d9..31e021fd 100644 --- a/app/views/subscription.py +++ b/app/views/subscription.py @@ -17,7 +17,9 @@ SUB_UPDATE_INTERVAL, SUBSCRIPTION_PAGE_TEMPLATE, XRAY_SUBSCRIPTION_PATH, - USE_CUSTOM_JSON_DEFAULT + USE_CUSTOM_JSON_DEFAULT, + USE_CUSTOM_JSON_FOR_V2RAYN, + USE_CUSTOM_JSON_FOR_V2RAYNG ) @@ -92,14 +94,25 @@ def get_subscription_user_info(user: UserResponse) -> dict: conf = generate_subscription(user=user, config_format="outline", as_base64=False) return Response(content=conf, media_type="application/json", headers=response_headers) + elif re.match('^v2rayN/(\d+\.\d+)', user_agent): + version_str = re.match('^v2rayN/(\d+\.\d+)', user_agent).group(1) + if LooseVersion(version_str) >= LooseVersion("6.40") and \ + (USE_CUSTOM_JSON_DEFAULT or USE_CUSTOM_JSON_FOR_V2RAYN): + conf = generate_subscription(user=user, config_format="v2ray-json", as_base64=False) + return Response(content=conf, media_type="application/json", headers=response_headers) + else: + conf = generate_subscription(user=user, config_format="v2ray", as_base64=True) + return Response(content=conf, media_type="text/plain", headers=response_headers) + elif re.match('^v2rayNG/(\d+\.\d+\.\d+)', user_agent): version_str = re.match('^v2rayNG/(\d+\.\d+\.\d+)', user_agent).group(1) - if LooseVersion(version_str) >= LooseVersion("1.8.16") and USE_CUSTOM_JSON_DEFAULT: + if LooseVersion(version_str) >= LooseVersion("1.8.16") and \ + (USE_CUSTOM_JSON_DEFAULT or USE_CUSTOM_JSON_FOR_V2RAYNG): conf = generate_subscription(user=user, config_format="v2ray-json", as_base64=False) return Response(content=conf, media_type="application/json", headers=response_headers) else: conf = generate_subscription(user=user, config_format="v2ray", as_base64=True) - return Response(content=conf, media_type="application/json", headers=response_headers) + return Response(content=conf, media_type="text/plain", headers=response_headers) else: conf = generate_subscription(user=user, config_format="v2ray", as_base64=True) @@ -141,7 +154,7 @@ def user_get_usage(token: str, return Response(status_code=204) if start is None: - start_date = datetime.fromtimestamp(datetime.utcnow().timestamp() - 30 * 24 * 3600) + start_date = datetime.utcfromtimestamp(datetime.utcnow().timestamp() - 30 * 24 * 3600) else: start_date = datetime.fromisoformat(start) diff --git a/config.py b/config.py index ec415b1a..a75cb230 100755 --- a/config.py +++ b/config.py @@ -50,7 +50,10 @@ SINGBOX_SUBSCRIPTION_TEMPLATE = config("SINGBOX_SUBSCRIPTION_TEMPLATE", default="singbox/default.json") MUX_TEMPLATE = config("MUX_TEMPLATE", default="mux/default.json") V2RAY_SUBSCRIPTION_TEMPLATE = config("V2RAY_SUBSCRIPTION_TEMPLATE", default="v2ray/default.json") -USE_CUSTOM_JSON_DEFAULT = config("USE_CUSTOM_JSON_DEFAULT", default="") + +USE_CUSTOM_JSON_DEFAULT = config("USE_CUSTOM_JSON_DEFAULT", default=False, cast=bool) +USE_CUSTOM_JSON_FOR_V2RAYN = config("USE_CUSTOM_JSON_FOR_V2RAYN", default=False, cast=bool) +USE_CUSTOM_JSON_FOR_V2RAYNG = config("USE_CUSTOM_JSON_FOR_V2RAYNG", default=False, cast=bool) ACTIVE_STATUS_TEXT = config("ACTIVE_STATUS_TEXT", default="Active") EXPIRED_STATUS_TEXT = config("EXPIRED_STATUS_TEXT", default="Expired")