[PR #5672] [CLOSED] feat(sub): auto-detect subscription format by User-Agent #5953

Closed
opened 2026-11-02 04:05:16 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/5672
Author: @Tomilla
Created: 6/30/2026
Status: ❌ Closed

Base: main ← Head: feat_auto_detect_sub_url


📝 Commits (10+)

  • fe4bd4a feat(settings): expose JSON and Clash subscription URLs
  • e4f2aad feat(sub): auto-detect Clash and Mihomo clients
  • 2b650ec refactor(settings): group subscription format endpoints
  • 61bdbae feat(sub): auto-detect Xray JSON clients
  • cfed712 fix(settings): validate regexes with Go RE2
  • a0d32ec feat(sub): make JSON array responses configurable
  • 8b174b3 fix(settings): preserve inherited regex defaults
  • 59e75a5 refactor(settings): clarify subscription format controls
  • b05a5b0 fix(xray): validate balancer regexes on submit
  • d4e9b98 refactor(sub): rename Clash auto-detect setting

📊 Changes

41 files changed (+1771 additions, -107 deletions)

View changed files

📝 frontend/public/openapi.json (+97 -0)
➕ frontend/src/components/form/GoRegexInput.tsx (+63 -0)
📝 frontend/src/components/form/index.ts (+1 -0)
📝 frontend/src/generated/examples.ts (+10 -0)
📝 frontend/src/generated/schemas.ts (+50 -0)
📝 frontend/src/generated/types.ts (+10 -0)
📝 frontend/src/generated/zod.ts (+10 -0)
📝 frontend/src/models/setting.ts (+5 -0)
📝 frontend/src/pages/api-docs/endpoints.ts (+9 -2)
📝 frontend/src/pages/settings/SubscriptionFormatsTab.css (+15 -0)
📝 frontend/src/pages/settings/SubscriptionFormatsTab.tsx (+70 -6)
📝 frontend/src/pages/settings/SubscriptionGeneralTab.tsx (+17 -1)
📝 frontend/src/pages/xray/balancers/BalancerFormModal.tsx (+71 -10)
📝 frontend/src/schemas/setting.ts (+5 -0)
📝 frontend/src/test/balancer-form-modal.test.tsx (+101 -5)
➕ frontend/src/test/subscription-general-tab.test.tsx (+29 -0)
📝 internal/sub/controller.go (+277 -57)
📝 internal/sub/controller_test.go (+318 -0)
📝 internal/sub/external_only_sub_test.go (+19 -1)
📝 internal/sub/host_sub_test.go (+2 -2)

...and 21 more files

📄 Description

Summary

Adds optional User-Agent detection to serve Clash/Mihomo YAML or Xray JSON from the standard subscription URL.

Also exposes JSON and Clash/Mihomo endpoint paths, reverse proxy URLs, auto-detection settings, and configurable Go RE2 User-Agent patterns in the panel.

Why

Currently, raw, JSON, and Clash subscriptions use separate URLs. This is inconvenient for administrators and users migrating from panels that expose one subscription URL and select the response format based on the requesting client.

This change keeps all existing endpoints available while optionally allowing the standard subscription URL to return the appropriate format.

Auto-detection is disabled by default to preserve existing behavior.

Closes #5570

This PR also adds form items to the subscription format page for configuring the JSON subscription path and JSON reverse proxy URI.

img_v3_02135_7caf20cf-b59c-41be-b658-05984c433cfg

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

How was this tested?

Automated checks:

  • go build ./...
  • go test ./...
  • npm run typecheck
  • npm run lint
  • npm run test — 637 tests passed
  • npm run build

Manual subscription checks:

  • A recognized Clash/Mihomo User-Agent receives Clash YAML from the standard subscription URL.
  • A recognized Xray JSON client receives a JSON configuration array.
  • v2rayNG and unknown clients continue receiving the existing raw/Base64 subscription.
  • Requests accepting HTML continue displaying the subscription information page.
  • Disabling auto-detection preserves the existing response.
  • Explicit raw, JSON, and Clash URLs remain available.
  • Custom Go RE2 patterns are validated by the backend.
  • Empty patterns remain empty in the database and inherit runtime defaults.
  • JSON single-profile responses preserve legacy object behavior unless “Always return a JSON array” is enabled.
  • Balancer regular expressions are validated before saving, including delayed-validation cancellation and edit protection.

Screenshots / recordings

Screenshots to attach:

  • Subscription tab showing the link to format-specific settings.
    img_v3_02135_55ee1500-becf-4d03-bf17-cc71a9edc11g

  • Sub Formats tab showing separate JSON and Clash/Mihomo sections.
    b20065ff-4a2b-47d8-b08a-361f9e23415e

  • User-Agent auto-detection and regex settings.
    0c5fb565-2cff-47a9-b8fd-be438677c02c

  • Invalid Go RE2 validation error.
    User Agent Regex field:
    image
    Balance Cost Regex field:
    30a60ffa-e587-4d76-bdb1-de886a08db16

Breaking changes

None.

Existing subscription URLs and response behavior remain unchanged unless an administrator explicitly enables auto-detection.

The existing JSON and Clash/Mihomo endpoints remain available.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass.
  • I updated the Wiki / README / API docs if user-facing behavior changed.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/5672 **Author:** [@Tomilla](https://github.com/Tomilla) **Created:** 6/30/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feat_auto_detect_sub_url` --- ### 📝 Commits (10+) - [`fe4bd4a`](https://github.com/MHSanaei/3x-ui/commit/fe4bd4a8c0429a694026366b13ff93fc79345bc2) feat(settings): expose JSON and Clash subscription URLs - [`e4f2aad`](https://github.com/MHSanaei/3x-ui/commit/e4f2aada7e26fdfe36aa48142e038ac85582184a) feat(sub): auto-detect Clash and Mihomo clients - [`2b650ec`](https://github.com/MHSanaei/3x-ui/commit/2b650ec67fa03d1c1f2b597801c60ebe8aed7fc3) refactor(settings): group subscription format endpoints - [`61bdbae`](https://github.com/MHSanaei/3x-ui/commit/61bdbae94a5c5d78c8c72e637e82e1fcff0d20d8) feat(sub): auto-detect Xray JSON clients - [`cfed712`](https://github.com/MHSanaei/3x-ui/commit/cfed7128fa310a6b2f3f9748397c2d83686a9feb) fix(settings): validate regexes with Go RE2 - [`a0d32ec`](https://github.com/MHSanaei/3x-ui/commit/a0d32ec46f0096f8afce2bc16178ecf15d1e9f46) feat(sub): make JSON array responses configurable - [`8b174b3`](https://github.com/MHSanaei/3x-ui/commit/8b174b359c66cc85536a3b516fbaa242aab5537e) fix(settings): preserve inherited regex defaults - [`59e75a5`](https://github.com/MHSanaei/3x-ui/commit/59e75a546be764fdb10955a7b6576d1fb2077292) refactor(settings): clarify subscription format controls - [`b05a5b0`](https://github.com/MHSanaei/3x-ui/commit/b05a5b0c2d6e902e8a59eb55cf2875a09b1aff51) fix(xray): validate balancer regexes on submit - [`d4e9b98`](https://github.com/MHSanaei/3x-ui/commit/d4e9b98d9461694e80cf52e9e1f901f3eea0834e) refactor(sub): rename Clash auto-detect setting ### 📊 Changes **41 files changed** (+1771 additions, -107 deletions) <details> <summary>View changed files</summary> 📝 `frontend/public/openapi.json` (+97 -0) ➕ `frontend/src/components/form/GoRegexInput.tsx` (+63 -0) 📝 `frontend/src/components/form/index.ts` (+1 -0) 📝 `frontend/src/generated/examples.ts` (+10 -0) 📝 `frontend/src/generated/schemas.ts` (+50 -0) 📝 `frontend/src/generated/types.ts` (+10 -0) 📝 `frontend/src/generated/zod.ts` (+10 -0) 📝 `frontend/src/models/setting.ts` (+5 -0) 📝 `frontend/src/pages/api-docs/endpoints.ts` (+9 -2) 📝 `frontend/src/pages/settings/SubscriptionFormatsTab.css` (+15 -0) 📝 `frontend/src/pages/settings/SubscriptionFormatsTab.tsx` (+70 -6) 📝 `frontend/src/pages/settings/SubscriptionGeneralTab.tsx` (+17 -1) 📝 `frontend/src/pages/xray/balancers/BalancerFormModal.tsx` (+71 -10) 📝 `frontend/src/schemas/setting.ts` (+5 -0) 📝 `frontend/src/test/balancer-form-modal.test.tsx` (+101 -5) ➕ `frontend/src/test/subscription-general-tab.test.tsx` (+29 -0) 📝 `internal/sub/controller.go` (+277 -57) 📝 `internal/sub/controller_test.go` (+318 -0) 📝 `internal/sub/external_only_sub_test.go` (+19 -1) 📝 `internal/sub/host_sub_test.go` (+2 -2) _...and 21 more files_ </details> ### 📄 Description ## Summary Adds optional User-Agent detection to serve Clash/Mihomo YAML or Xray JSON from the standard subscription URL. Also exposes JSON and Clash/Mihomo endpoint paths, reverse proxy URLs, auto-detection settings, and configurable Go RE2 User-Agent patterns in the panel. ## Why Currently, raw, JSON, and Clash subscriptions use separate URLs. This is inconvenient for administrators and users migrating from panels that expose one subscription URL and select the response format based on the requesting client. This change keeps all existing endpoints available while optionally allowing the standard subscription URL to return the appropriate format. Auto-detection is disabled by default to preserve existing behavior. Closes #5570 > This PR also adds form items to the subscription format page for configuring the JSON subscription path and JSON reverse proxy URI. <img width="2938" height="1644" alt="img_v3_02135_7caf20cf-b59c-41be-b658-05984c433cfg" src="https://github.com/user-attachments/assets/20322854-f1af-4d52-89f6-8b95993217d3" /> ## Type of change - [ ] Bug fix - [x] New feature - [x] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [x] Frontend (UI / panel pages) - [x] Backend (API endpoints, login, settings) - [x] Xray config generation - [x] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [ ] Telegram bot ## How was this tested? Automated checks: - `go build ./...` - `go test ./...` - `npm run typecheck` - `npm run lint` - `npm run test` — 637 tests passed - `npm run build` Manual subscription checks: - A recognized Clash/Mihomo User-Agent receives Clash YAML from the standard subscription URL. - A recognized Xray JSON client receives a JSON configuration array. - v2rayNG and unknown clients continue receiving the existing raw/Base64 subscription. - Requests accepting HTML continue displaying the subscription information page. - Disabling auto-detection preserves the existing response. - Explicit raw, JSON, and Clash URLs remain available. - Custom Go RE2 patterns are validated by the backend. - Empty patterns remain empty in the database and inherit runtime defaults. - JSON single-profile responses preserve legacy object behavior unless “Always return a JSON array” is enabled. - Balancer regular expressions are validated before saving, including delayed-validation cancellation and edit protection. ## Screenshots / recordings Screenshots to attach: - Subscription tab showing the link to format-specific settings. <img width="2934" height="1564" alt="img_v3_02135_55ee1500-becf-4d03-bf17-cc71a9edc11g" src="https://github.com/user-attachments/assets/c955b62e-8591-4605-977a-e9f535fa98da" /> - Sub Formats tab showing separate JSON and Clash/Mihomo sections. <img width="2928" height="1454" alt="b20065ff-4a2b-47d8-b08a-361f9e23415e" src="https://github.com/user-attachments/assets/b6cc2274-9138-42d6-b900-b400d34f42ab" /> - User-Agent auto-detection and regex settings. <img width="2926" height="1038" alt="0c5fb565-2cff-47a9-b8fd-be438677c02c" src="https://github.com/user-attachments/assets/b156db43-e712-44a1-be15-3783283b8966" /> - Invalid Go RE2 validation error. User Agent Regex field: <img width="2936" height="1756" alt="image" src="https://github.com/user-attachments/assets/3d320a0a-3762-444c-8843-0e1f6aeecbdd" /> Balance Cost Regex field: <img width="2938" height="1752" alt="30a60ffa-e587-4d76-bdb1-de886a08db16" src="https://github.com/user-attachments/assets/113f6e78-275e-436a-a597-4ca5666cda21" /> ## Breaking changes None. Existing subscription URLs and response behavior remain unchanged unless an administrator explicitly enables auto-detection. The existing JSON and Clash/Mihomo endpoints remain available. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [x] `go build ./...` and the test suite pass locally. - [x] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. - [x] I updated the Wiki / README / API docs if user-facing behavior changed. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:05:16 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#5953
No description provided.