Skip to content

Commit e7332d8

Browse files
feat: Add host variables for import url
1 parent d4c41a7 commit e7332d8

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

app/operation/subscription.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ async def user_subscription(
132132
user_agent: str = "",
133133
request_url: str = "",
134134
):
135-
"""Provides a subscription link based on the user agent (Clash, V2Ray, etc.)."""
135+
"""
136+
Provides a subscription link based on the user agent (Clash, V2Ray, etc.).
137+
"""
136138
# Handle HTML request (subscription page)
137139
sub_settings: SubSettings = await subscription_settings()
138140
db_user = await self.get_validated_sub(db, token)
@@ -146,21 +148,20 @@ async def user_subscription(
146148
if db_user.admin and db_user.admin.sub_template
147149
else SUBSCRIPTION_PAGE_TEMPLATE
148150
)
149-
150151
links = []
151152
if sub_settings.allow_browser_config:
152153
conf, media_type = await self.fetch_config(user, ConfigFormat.links)
153154
links = conf.splitlines()
154155

155-
sub_url = await UserOperation.generate_subscription_url(db_user)
156+
format_variables = await self.get_format_variables(user)
156157

157158
return HTMLResponse(
158159
render_template(
159160
template,
160161
{
161162
"user": user,
162163
"links": links,
163-
"apps": self._make_apps_import_urls(sub_url, sub_settings.applications),
164+
"apps": self._make_apps_import_urls(sub_settings.applications, format_variables),
164165
},
165166
)
166167
)
@@ -176,6 +177,16 @@ async def user_subscription(
176177
# Create response with appropriate headers
177178
return Response(content=conf, media_type=media_type, headers=response_headers)
178179

180+
async def get_format_variables(self, user: UsersResponseWithInbounds) -> dict:
181+
"""Get format variables for URL formatting."""
182+
sub_settings: SubSettings = await subscription_settings()
183+
format_variables = setup_format_variables(user)
184+
sub_url = await UserOperation.generate_subscription_url(user)
185+
186+
format_variables.update({"PROFILE_TITLE": sub_settings.profile_title})
187+
format_variables.update({"url": sub_url})
188+
return format_variables
189+
179190
async def user_subscription_with_client_type(
180191
self, db: AsyncSession, token: str, client_type: ConfigFormat, request_url: str = ""
181192
):
@@ -193,9 +204,7 @@ async def user_subscription_with_client_type(
193204
# Create response headers
194205
return Response(content=conf, media_type=media_type, headers=response_headers)
195206

196-
async def user_subscription_info(
197-
self, db: AsyncSession, token: str, request_url: str = ""
198-
) -> tuple[SubscriptionUserResponse, dict]:
207+
async def user_subscription_info(self, db: AsyncSession, token: str) -> tuple[SubscriptionUserResponse, dict]:
199208
"""Retrieves detailed information about the user's subscription."""
200209
sub_settings: SubSettings = await subscription_settings()
201210
db_user = await self.get_validated_sub(db, token=token)
@@ -206,19 +215,21 @@ async def user_subscription_info(
206215

207216
return user_response, response_headers
208217

209-
async def user_subscription_apps(self, db: AsyncSession, token: str, request_url: str) -> list[Application]:
218+
async def user_subscription_apps(self, db: AsyncSession, token: str) -> list[Application]:
210219
"""
211220
Get available applications for user's subscription.
212221
"""
213-
_, _ = await self.user_subscription_info(db, token, request_url)
222+
user, _ = await self.user_subscription_info(db, token)
214223
sub_settings: SubSettings = await subscription_settings()
215-
return self._make_apps_import_urls(request_url, sub_settings.applications)
224+
format_variables = await self.get_format_variables(user)
225+
return self._make_apps_import_urls(sub_settings.applications, format_variables)
216226

217-
def _make_apps_import_urls(self, sub_url: str, applications: list[Application]):
227+
def _make_apps_import_urls(self, applications: list[Application], format_variables: dict) -> list[Application]:
218228
apps_with_updated_urls = []
219229
for app in applications:
220230
updated_app = app.model_copy()
221-
updated_app.import_url = app.import_url.format(url=sub_url)
231+
import_url = app.import_url.format_map(format_variables)
232+
updated_app.import_url = import_url
222233
apps_with_updated_urls.append(updated_app)
223234

224235
return apps_with_updated_urls

app/routers/subscription.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,16 @@ async def user_subscription(
3636
@router.get("/{token}/info", response_model=SubscriptionUserResponse)
3737
async def user_subscription_info(request: Request, token: str, db: AsyncSession = Depends(get_db)):
3838
"""Retrieves detailed information about the user's subscription."""
39-
user_data, response_headers = await subscription_operator.user_subscription_info(
40-
db, token=token, request_url=str(request.url)
41-
)
39+
user_data, response_headers = await subscription_operator.user_subscription_info(db, token=token)
4240
return JSONResponse(content=user_data.model_dump(mode="json"), headers=response_headers)
4341

4442

4543
@router.get("/{token}/apps", response_model=list[Application])
46-
async def user_subscription_apps(token: str, request: Request, db: AsyncSession = Depends(get_db)):
44+
async def user_subscription_apps(token: str, db: AsyncSession = Depends(get_db)):
4745
"""
4846
Get applications available for user's subscription.
4947
"""
50-
return await subscription_operator.user_subscription_apps(db, token, str(request.url))
48+
return await subscription_operator.user_subscription_apps(db, token)
5149

5250

5351
@router.get("/{token}/usage", response_model=UserUsageStatsList)

0 commit comments

Comments
 (0)