[PR #5826] [MERGED] feat(sub): auto-detect subscription format by User-Agent (Updated) #5998

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/5826
Author: @Tomilla
Created: 7/6/2026
Status: ✅ Merged
Merged: 7/14/2026
Merged by: @MHSanaei

Base: main ← Head: feat_auto_detect_sub_url


📝 Commits (9)

  • 8bb9a8c feat(settings): add subscription format controls
  • 9907245 feat(sub): auto-detect subscription formats
  • 8a208ce fix(xray): validate balancer regexes before save
  • f19df5c Revert "fix(xray): validate balancer regexes before save"
  • c7409bd doc(endpoints): align indent spaces
  • 054884a Merge branch 'main' into feat_auto_detect_sub_url
  • 612d732 doc(settings): improve error message formatting in validateSubUserAgentRegex
  • e53579c Merge branch 'main' into feat_auto_detect_sub_url
  • 0944dd4 refactor(sub): describe UA auto-detect behavior without naming client apps

📊 Changes

40 files changed (+1590 additions, -91 deletions)

View changed files

📝 frontend/public/openapi.json (+87 -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 (+40 -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/schemas/setting.ts (+5 -0)
➕ frontend/src/test/subscription-general-tab.test.tsx (+29 -0)
📝 internal/sub/controller.go (+273 -55)
📝 internal/sub/controller_test.go (+329 -0)
📝 internal/sub/external_only_sub_test.go (+19 -1)
📝 internal/sub/host_sub_test.go (+2 -2)
📝 internal/sub/json_service.go (+2 -3)
📝 internal/sub/sub.go (+54 -5)

...and 20 more files

📄 Description

Summary

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

The panel now exposes JSON and Clash/Mihomo endpoint paths, reverse proxy URLs, auto-detection controls, and configurable Go RE2 User-Agent patterns.

Why

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

This change retains every existing endpoint while optionally allowing the standard subscription URL to return the appropriate format.

Auto-detection is disabled by default, preserving existing behavior.

Closes #5570

This PR also fixes a bug: when a user has enabled JSON subscription but has no place to modify the JSON subscription path, a prompt "Default JSON subscription path "/json/" is well-known - change it." is constantly displayed upon saving.

Subscription format settings

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 -- --run — 704 tests passed
  • npm run build

Manual subscription checks:

  • Recognized Clash/Mihomo User-Agents receive Clash YAML from the standard subscription URL.
  • Recognized Xray JSON User-Agents receive 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/Mihomo URLs remain available.
  • Custom patterns are validated using Go RE2 before saving.
  • Empty patterns remain empty in the database and inherit runtime defaults.
  • Explicit JSON single-profile responses retain legacy object behavior unless “Always return a JSON array” is enabled.

Screenshots / recordings

Subscription settings link

JSON and Clash/Mihomo format settings

JSON and Clash/Mihomo format settings

User-Agent auto-detection

User-Agent auto-detection settings

Go RE2 validation

User-Agent regex:

Invalid User-Agent regex error

Breaking changes

None.

Existing subscription URLs and response behavior remain unchanged unless an administrator explicitly enables auto-detection. 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/5826 **Author:** [@Tomilla](https://github.com/Tomilla) **Created:** 7/6/2026 **Status:** ✅ Merged **Merged:** 7/14/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `feat_auto_detect_sub_url` --- ### 📝 Commits (9) - [`8bb9a8c`](https://github.com/MHSanaei/3x-ui/commit/8bb9a8c6d5309752a803126c14275e6f114ded11) feat(settings): add subscription format controls - [`9907245`](https://github.com/MHSanaei/3x-ui/commit/99072457d12e4f85b54853cbc8c83dbda25e696f) feat(sub): auto-detect subscription formats - [`8a208ce`](https://github.com/MHSanaei/3x-ui/commit/8a208ce71b7b3daed5d05607091203382d675e07) fix(xray): validate balancer regexes before save - [`f19df5c`](https://github.com/MHSanaei/3x-ui/commit/f19df5cac4f35fe945891b2c8bd9d23d2d5ba9cf) Revert "fix(xray): validate balancer regexes before save" - [`c7409bd`](https://github.com/MHSanaei/3x-ui/commit/c7409bd3583020ca5af669747190e977fd6541dc) doc(endpoints): align indent spaces - [`054884a`](https://github.com/MHSanaei/3x-ui/commit/054884a8a0a7a9569715d01a7bcb8a8b95b979fd) Merge branch 'main' into feat_auto_detect_sub_url - [`612d732`](https://github.com/MHSanaei/3x-ui/commit/612d732f778af2b3d93254742cd73aa00d5bb40c) doc(settings): improve error message formatting in validateSubUserAgentRegex - [`e53579c`](https://github.com/MHSanaei/3x-ui/commit/e53579cd23d0535644f8e7cc59393640effa7479) Merge branch 'main' into feat_auto_detect_sub_url - [`0944dd4`](https://github.com/MHSanaei/3x-ui/commit/0944dd40147a8da53c1450a3638be7c0fb5ef495) refactor(sub): describe UA auto-detect behavior without naming client apps ### 📊 Changes **40 files changed** (+1590 additions, -91 deletions) <details> <summary>View changed files</summary> 📝 `frontend/public/openapi.json` (+87 -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` (+40 -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/schemas/setting.ts` (+5 -0) ➕ `frontend/src/test/subscription-general-tab.test.tsx` (+29 -0) 📝 `internal/sub/controller.go` (+273 -55) 📝 `internal/sub/controller_test.go` (+329 -0) 📝 `internal/sub/external_only_sub_test.go` (+19 -1) 📝 `internal/sub/host_sub_test.go` (+2 -2) 📝 `internal/sub/json_service.go` (+2 -3) 📝 `internal/sub/sub.go` (+54 -5) _...and 20 more files_ </details> ### 📄 Description ## Summary Adds optional User-Agent detection to serve Clash/Mihomo YAML or Xray JSON from the standard subscription URL. The panel now exposes JSON and Clash/Mihomo endpoint paths, reverse proxy URLs, auto-detection controls, and configurable Go RE2 User-Agent patterns. ## Why Raw, JSON, and Clash subscriptions currently use separate URLs. This is inconvenient for administrators and users migrating from panels that use one subscription URL and select the response format based on the requesting client. This change retains every existing endpoint while optionally allowing the standard subscription URL to return the appropriate format. Auto-detection is disabled by default, preserving existing behavior. Closes #5570 This PR also fixes a bug: when a user has enabled JSON subscription but has no place to modify the JSON subscription path, a prompt "Default JSON subscription path "/json/" is well-known - change it." is constantly displayed upon saving. <img width="2938" height="1644" alt="Subscription format settings" src="https://github.com/user-attachments/assets/20322854-f1af-4d52-89f6-8b95993217d3" /> ## Type of change - [x] 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) - [ ] 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 -- --run` — 704 tests passed - `npm run build` Manual subscription checks: - Recognized Clash/Mihomo User-Agents receive Clash YAML from the standard subscription URL. - Recognized Xray JSON User-Agents receive 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/Mihomo URLs remain available. - Custom patterns are validated using Go RE2 before saving. - Empty patterns remain empty in the database and inherit runtime defaults. - Explicit JSON single-profile responses retain legacy object behavior unless “Always return a JSON array” is enabled. ## Screenshots / recordings ### Subscription settings link <img width="2934" height="1564" alt="Subscription settings link" src="https://github.com/user-attachments/assets/c955b62e-8591-4605-977a-e9f535fa98da" /> ### JSON and Clash/Mihomo format settings <img width="2928" height="1454" alt="JSON and Clash/Mihomo format settings" src="https://github.com/user-attachments/assets/b6cc2274-9138-42d6-b900-b400d34f42ab" /> ### User-Agent auto-detection <img width="2926" height="1038" alt="User-Agent auto-detection settings" src="https://github.com/user-attachments/assets/b156db43-e712-44a1-be15-3783283b8966" /> ### Go RE2 validation User-Agent regex: <img width="2936" height="1756" alt="Invalid User-Agent regex error" src="https://github.com/user-attachments/assets/3d320a0a-3762-444c-8843-0e1f6aeecbdd" /> ## Breaking changes None. Existing subscription URLs and response behavior remain unchanged unless an administrator explicitly enables auto-detection. 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:36 +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#5998
No description provided.