Skip to content

Commit

Permalink
Merge branch 'optional-custom-json' of github.com:fodhelper/Marzban i…
Browse files Browse the repository at this point in the history
…nto fodhelper-optional-custom-json
  • Loading branch information
SaintShit committed May 19, 2024
2 parents 1ed5ec5 + 9844a6e commit 94f4955
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
14 changes: 9 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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/"
Expand Down
21 changes: 17 additions & 4 deletions app/views/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
5 changes: 4 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 94f4955

Please sign in to comment.