3x-ui/internal/web/controller/xray_setting.go

656 lines
22 KiB
Go
Raw Normal View History

2023-12-04 10:20:46 -08:00
package controller
import (
"encoding/json"
"errors"
feat: add support for subscription-based outbounds with auto-update (#5037) * feat: add support for subscription-based outbounds with auto-update - New OutboundSubscription model (full support on both SQLite and PostgreSQL) - Go subscription link parser (vmess/vless/trojan/ss/hysteria2/wireguard) matching frontend behavior - Stable tag assignment across refreshes (designed for balancer + routing use) - Runtime merge of subscription outbounds into Xray config (additive only) - Full CRUD + manual refresh + preview API - Background auto-update job (per-subscription interval) - Frontend management UI in Outbounds tab (Subscriptions drawer) + tag integration in balancers/routing rules - Proper dual-database support including CLI migration path Review & hardening notes: - Fixed merge logic bug that could drop manual outbounds - Added SSRF/private-IP protection on subscription URLs using SanitizePublicHTTPURL - Improved update interval UX (hours + minutes) - Auto-fetch on first subscription creation - Added detailed comments on tag stability strategy and balancer implications when servers are added/removed/rotated - Updated migrationModels() for CLI migrate-db support * fix: resolve frontend lint/type errors and Go build break Frontend (eslint + tsc clean): - Destructure subscriptionOutboundTags prop in RoutingTab and BalancersTab. It was declared in the interface and used in useMemo but never destructured, so it resolved as an unresolved global (react-hooks warning + tsc "Cannot find name"). The prop is passed by XrayPage, so the feature was silently inert. - OutboundsTab: remove unused useEffect import, add an OutboundSub type to replace any[] state and the any/any table render signature, type the subscriptionOutbounds cast, and replace unused catch (e) bindings with parameter-less catch. Also type HttpUtil.post as OutboundSub so r.obj?.id type-checks. Backend (go build clean): - outbound_subscription_job: websocket.MessageTypeXray is undefined; use the existing MessageTypeOutbounds since the job refreshes outbound subscriptions. * fix(xray): make outbound subscription creation work end-to-end - Correct API paths from /panel/xray/outbound-subs to /panel/api/xray/outbound-subs. The controller is mounted under /panel/api, so the old paths hit the SPA page route (GET-only) and 404'd on POST. - Send the create-subscription body as a plain object instead of URLSearchParams. The axios request interceptor serializes bodies with qs.stringify, which can't read URLSearchParams' internal storage and produced an empty body, so the backend rejected it with "subscription URL is required". - Use message.useMessage() + context holder instead of the static antd message API (resolves the "Static function can not consume context" warning), matching XrayPage's pattern. - Migrate the subscriptions Drawer to antd v6 props: width -> size, destroyOnClose -> destroyOnHidden, and Space direction -> orientation. * feat(xray): show traffic/test for subscription outbounds; harden + test the feature Display (the reported issue): - Replace the flat read-only pills with a proper read-only table (desktop) and cards (mobile) in a new SubscriptionOutbounds component, showing Address, Protocol, Traffic (matched by tag — already collected by Xray), and a Test button with Latency. No edit/delete/move (read-only). - Test subscription outbounds via the existing /testOutbound endpoint, with results keyed by tag (subscriptionTestStates + testSubscriptionOutbound in useXraySetting, wired through XrayPage). Generalize isTesting/testResult to a string|number key so the same helpers serve index- and tag-keyed states. i18n: - Replace all hardcoded English subscription strings with t() calls and add pages.xray.outboundSub.* keys to en-US.json (other locales fall back). Backend hardening + tests: - xray.go: drop the tautological `subSvc != nil` check. - outbound_subscription: re-validate every redirect hop against private/ internal addresses (CheckRedirect) and cap the redirect chain, closing an SSRF gap where only the initial host was checked. - Extract assignStableTags as a pure function and add unit tests for tag stability and SSRF rejection (the feature previously had no tests). Misc: - gofmt util/link/outbound.go (it was not gofmt-clean). * fix(xray): make outbound-subs feature pass CI (test compile, route docs, openapi) - outbound_test.go: remove unused `inner`/`lines` variables that broke the `util/link` test build (declared and not used). - Document the 7 outbound-subscription routes in endpoints.ts (list, create, update, delete, del alias, refresh, parse) so TestAPIRoutesDocumented passes. - Regenerate frontend/public/openapi.json (npm run gen) to include the new endpoints, satisfying the codegen freshness check. * feat(xray): per-subscription allow-private, gap-filled tags, UI tweaks, delete refresh Backend: - Add a per-subscription AllowPrivate flag (default off). Create/Update/refresh and the redirect check sanitize the URL with it, so localhost/LAN sources work only when explicitly opted in; the SSRF guard still blocks private targets by default. Controller reads the allowPrivate form field on create/update/parse. - Default outbound tag prefix now uses the smallest free "subN-" number instead of the auto-increment id, so deleting a subscription frees its number for reuse (a fresh start gives sub1) while staying stable per subscription. Extracted a pure defaultPrefixNumber() with unit tests. - deleteOutboundSub now signals SetToNeedRestart so xray drops the outbounds. Frontend: - "Allow private address" toggle in the add form (sends allowPrivate). - Delete now refreshes the xray view immediately (no manual page reload). - Subscriptions manager opens as a centered Modal instead of a right-side Drawer. - Move Outbounds to a top-level sidebar item under Nodes (out of Xray Configs). - Collapse WARP/NordVPN into a "more" dropdown. - Document the allowPrivate param in endpoints.ts. * i18n(xray): translate outbound-subscription UI into all locales - Translate the pages.xray.outboundSub.* strings (and allowPrivate label/hint) into all 12 non-English locales, matching each file's existing terminology. - Remove the unused outboundSub.add ("Add subscription") key from every locale. * feat(xray): subscription manager — edit, reorder/priority, status, preview, refresh-all Backend: - Per-subscription Priority + Prepend: subscriptions are ordered by Priority and placed before (Prepend) or after the manual template outbounds in the merge, so a subscription server can become the default. New Move(up/down) endpoint re-normalizes priorities; merge split into prepend/template/append. - List now returns a derived OutboundCount and orders by priority, and strips the heavy LastFetchedOutbounds/LinkIdentities blobs from the list payload. - Create/Update accept the prepend flag; new subs append at the end of priority. Frontend (Outbound Subscriptions modal): - Edit existing subscriptions (reuses the form + Update endpoint). - Inline enable/disable Switch, Status column (OK / error tooltip), Outbounds count column, per-row refresh spinner, "Refresh all" button. - Reorder (move up/down) controls + a "Before manual outbounds" toggle. - Preview button: fetch+parse a URL via /parse without saving. - Document the move route + prepend param in endpoints.ts; regenerate openapi.json. * i18n(xray): translate new subscription-manager strings into all locales Add the prepend/prependHint, preview/previewEmpty, refreshAll, statusOk and toastUpdated keys to all 12 non-English locales, matching each file's terminology. --------- Co-authored-by: MHSanaei <ho3ein.sanaei@gmail.com>
2026-06-08 09:09:53 -07:00
"fmt"
"strconv"
"strings"
"time"
piaprotocol "github.com/mhsanaei/3x-ui/v3/internal/pia"
refactor: focused service files, leaf subpackages, and an internal/ layout (#5167) * refactor(service): split client.go into focused files client.go had grown to 4455 lines mixing ~10 responsibilities. Split it verbatim into cohesive same-package files (no behavior change): client.go foundation: ClientService, ClientWithAttachments, ClientCreatePayload, ErrClientNotInInbound, sqlInChunk client_locks.go inbound mutation locks, delete tombstones, compactOrphans client_lookup.go read-only lookups (GetByID, List, EffectiveFlow, ...) client_link.go inbound association sync (SyncInbound, DetachInbound, ...) client_crud.go single-client CRUD + validation + protocol defaults client_inbound_apply.go low-level inbound-settings mutators + by-email setters client_bulk.go bulk attach/detach/adjust/delete/create + DelDepleted client_traffic.go traffic-reset paths client_groups.go client group management client_paging.go paged listing, filtering, sorting, summary Every declaration moved unchanged (verified: identical func/type/const/var signature set before vs after). Imports redistributed per file via goimports. go build ./..., go vet, and go test ./web/service/... all pass. * refactor(service): split inbound.go into focused files inbound.go was 4100 lines. Split it verbatim into cohesive same-package files (no behavior change): inbound.go core inbound CRUD + InboundService (keeps pkg doc) inbound_protocol.go protocol / stream capability helpers inbound_node.go node/runtime/remote coordination + online tracking inbound_traffic.go traffic accounting, reset, client stats inbound_client_ips.go per-client IP tracking inbound_clients.go client lookups within inbounds + copy-clients inbound_disable.go auto-disable invalid inbounds/clients inbound_migration.go DB migrations inbound_sublink.go subscription link providers inbound_util.go generic slice/string helpers Identical func/type/const/var signature set before vs after; package doc comment preserved on inbound.go. Imports redistributed via goimports. Build, vet, and go test ./web/service/... all pass. * refactor(service): split tgbot.go into focused files tgbot.go was 3738 lines dominated by a 1246-line answerCallback. Split it verbatim into cohesive same-package files (no behavior change): tgbot.go lifecycle, bot setup, caches, small utils tgbot_router.go incoming update / command / callback dispatch tgbot_send.go outbound messaging primitives tgbot_client.go client views, actions, subscription links tgbot_inbound.go inbound listing / pickers tgbot_report.go server usage, exhausted, online, backups, notifications Identical func/type/const/var signature set before vs after. Imports redistributed via goimports. Build, vet, and go test ./web/service/... pass. * refactor(client): dedupe single-field by-email setters ResetClientIpLimitByEmail, ResetClientExpiryTimeByEmail, and ResetClientTrafficLimitByEmail shared an identical ~50-line body that resolves the inbound by email, confirms the client exists, rewrites a single-client settings payload, and delegates to UpdateInboundClient. Extract that into applyClientFieldByEmail(inboundSvc, email, mutate) and reduce each setter to a 3-line wrapper. Behavior is unchanged: same checks and error strings, same single-client payload contract, same totalGB guard. SetClientTelegramUserID (resolves by traffic id, different error text) and ToggleClientEnableByEmail/SetClientEnableByEmail (different return shape and a pre-read of the old state) intentionally keep their own bodies. * refactor(service): extract panel/ subpackage Move the panel-administration leaf services out of the flat service package into web/service/panel/ (package panel): user.go UserService (auth / 2FA / LDAP) panel.go PanelService (restart / self-update) + version helpers panel_other.go non-unix RestartPanel panel_unix.go unix RestartPanel api_token.go ApiTokenService websocket.go WebSocketService panel_test.go version/shellQuote unit tests These are leaves: they depend on core (SettingService, Release) but no core file references them, so the extraction creates no import cycle. Core references are now qualified (service.SettingService, service.Release); callers in main.go, web/web.go, and web/controller/* updated to panel.*. Build, vet, and go test ./web/... pass. * refactor(service): extract integration/ subpackage Move the external-provider integration leaves into web/service/integration/ (package integration): warp.go WarpService (Cloudflare WARP) nord.go NordService (NordVPN) custom_geo.go CustomGeoService (custom geo asset management) *_test.go custom_geo / panel-proxy tests These depend on core (SettingService, ServerService, XraySettingService) but no core file references them. xray_setting.go stays in core because it calls the unexported SettingService.saveSetting. The shared isBlockedIP SSRF helper (used by core url_safety.go and by custom_geo) now has a small copy in each package rather than being exported. Core references qualified; callers in web/web.go, web/job/*, and web/controller/* updated to integration.*. Build, vet, and go test ./web/... pass. * refactor(service): extract tgbot/ subpackage Move the Telegram bot (6 files + test) into web/service/tgbot/ (package tgbot). It is a leaf: it embeds five core services (Inbound/Client/Setting/ Server/Xray) and the core never references it, so no import cycle. To support the package boundary without changing behavior: - core exposes XrayProcess() *xray.Process so tgbot keeps calling the exact same running-process methods it used via the package-level `p`; - three core methods tgbot calls are exported: ClientService.checkIs- EnabledByEmail -> CheckIsEnabledByEmail, InboundService.getAllEmails -> GetAllEmails (callers updated in-package); - tgbot's embedded-field types and the few core type refs (Status, ClientCreatePayload, SanitizePublicHTTPURL) are now service-qualified. Callers in main.go, web/web.go, web/job/*, and web/controller/* updated to tgbot.*. Build, vet, and go test ./web/... pass. * refactor(service): extract outbound/ subpackage OutboundService (outbound.go) imports only neutral packages (config, database, model, xray) and its production code is referenced by no core or sibling service file — only by web/controller/xray_setting.go and web/job/xray_traffic_job.go. Move it to web/service/outbound/ (package outbound); no core qualification needed inside. Callers updated to outbound.*. The one coupling was a tiny pure test helper, outboundsContainTag, used by both outbound.go and the core outbound_subscription_test.go; it now has a small copy in that test file rather than being shared across the boundary. Build, vet, and go test ./web/... pass. * refactor(util): move wireguard into its own subpackage util/wireguard.go was the lone file of the root `util` package (24 lines, one exported func GenerateWireguardKeypair), while every other util concern lives in a focused subpackage (util/common, util/crypto, util/netsafe, ...). Move it to util/wireguard/ (package wireguard) for consistency; its only importer, web/service/integration/warp.go, is updated. The root `util` package no longer exists. * refactor(sub): drop redundant sub prefix from filenames Inside package sub the subXxx.go prefix just repeats the package name (like client_*.go did inside service). Rename for consistency; content and type names are unchanged: subController.go -> controller.go subService.go -> service.go subClashService.go -> clash_service.go subJsonService.go -> json_service.go (+ matching _test.go files) * refactor(controller): rename xui.go -> spa.go XUIController serves the panel's single-page-app shell; spa.go names that role plainly (the other controller files are domain-named). File rename only — the type stays XUIController. api_docs_test.go keys route base paths by filename, so its "xui.go" case is updated to "spa.go". * refactor: move backend packages under internal/ Adopt the idiomatic Go application layout: the backend packages now live under internal/ (a boundary the toolchain enforces), signalling private implementation instead of a library-style flat root. No runtime behavior changes — only import paths and a few build/config paths move. Moved: config, database, logger, mtproto, sub, util, web, xray -> internal/. main.go stays at the repo root and tools/openapigen stays under tools/ (both still import internal/* because the internal rule keys off the module root). The module path github.com/mhsanaei/3x-ui/v3 is unchanged; 149 .go files had their import prefix rewritten to .../internal/<pkg>. Couplings the Go compiler can't see, updated to the new layout: - frontend i18n imports of web/translation (react.ts, setup.components.ts) - vite outDir + eslint/tsconfig ignore globs -> internal/web/dist - Dockerfile COPY paths for web/dist and web/translation - locale.go os.DirFS("web") disk fallback -> "internal/web" - .gitignore and ci.yml go:embed stub for internal/web/dist - api_docs_test.go repo-root relative walk (one level deeper) - tools/openapigen filesystem package paths; ApiTokenView repointed to the web/service/panel subpackage and codegen regenerated (clears a stale type the ci.yml codegen check was failing on) Verified: go build/vet/test (all packages), and frontend typecheck, lint, vitest (478 tests), and production build into internal/web/dist. * fix(config): keep test runs from writing logs into the source tree GetLogFolder() returns a CWD-relative "./log" on Windows. Under `go test` the working directory is each package's own folder, so InitLogger (called by tests in web/job, web/service, xray, web/websocket) created stray log/ directories scattered through the source tree (e.g. internal/web/job/log/). Redirect to a shared temp folder when testing.Testing() reports a test run. Production behavior is unchanged: Windows still uses ./log next to the binary and Linux /var/log/x-ui. The log files were always gitignored (*.log) and never committed; this just stops the noise at the source. * docs: move subscription-template guide out of root into docs/ sub_templates/ was a top-level folder holding only a README and no actual templates (3x-ui ships none by design), referenced nowhere and unlinked from any doc — it read like an empty placeholder cluttering the repo root. Move the guide to docs/custom-subscription-templates.md (a proper docs home), reword its intro to read as documentation rather than a folder note, link it from the Features list in README.md, and drop the empty sub_templates/ folder. * fix: update stale web/ path references after the internal/ move The internal/ migration rewrote Go import paths but left some references to the old top-level layout in docs, comments, and a few runtime disk paths. Functional (dev-mode only): the disk-serving fallbacks that read the Vite build from disk when running from source still pointed at web/dist/, which moved to internal/web/dist/ — so `os.DirFS`/`os.Stat`/`os.ReadFile` in internal/web/web.go and internal/sub/{sub,controller}.go are corrected. Production was unaffected (it serves the embedded FS; verified by the Docker build), but `go run` with a live frontend build silently fell back to embed. Docs/comments: frontend/README.md, CONTRIBUTING.md, the claude-issue-bot and release workflows, the openapigen -root help text, and assorted Go comments now reference internal/web, internal/database, internal/sub, internal/xray, etc. Package-name mentions (the "web" package), root paths (main.go, frontend/, install scripts, /etc/x-ui), routes (/panel/api/xray), and the historical "web/assets no longer exists" note were intentionally left as-is. * refactor(web): remove the legacy /xui -> /panel redirect middleware RedirectMiddleware existed only for backward compatibility with the old `/xui` URL scheme (301-redirecting /xui and /xui/API to /panel and /panel/api). That cutover was long ago, so drop the middleware, its registration in initRouter, and the now-inaccurate "URL redirection" mention in the middleware package doc. Old /xui URLs now 404 like any other unknown path. HTTPS auto-redirect and auth redirects are unrelated and stay. * build: fix .dockerignore for internal/ layout and exclude runtime dir - web/dist -> internal/web/dist: the embedded frontend moved under internal/, so the stale exclude no longer matched and the locally-built dist could be sent to the build context (the frontend stage rebuilds it fresh anyway). - exclude x-ui/: the local runtime directory (SQLite db, geo .dat files, xray binaries, certs — ~150MB) was being shipped into the build context for no reason. Verified the pattern excludes only the directory and still keeps x-ui.sh, which the Dockerfile copies to /usr/bin/x-ui.
2026-06-10 06:19:22 -07:00
"github.com/mhsanaei/3x-ui/v3/internal/util/common"
"github.com/mhsanaei/3x-ui/v3/internal/web/service"
"github.com/mhsanaei/3x-ui/v3/internal/web/service/integration"
"github.com/mhsanaei/3x-ui/v3/internal/web/service/outbound"
"github.com/mhsanaei/3x-ui/v3/internal/xray"
2023-12-04 10:20:46 -08:00
"github.com/gin-gonic/gin"
)
2025-09-20 00:35:50 -07:00
// XraySettingController handles Xray configuration and settings operations.
2023-12-04 10:20:46 -08:00
type XraySettingController struct {
XraySettingService service.XraySettingService
SettingService service.SettingService
InboundService service.InboundService
refactor: focused service files, leaf subpackages, and an internal/ layout (#5167) * refactor(service): split client.go into focused files client.go had grown to 4455 lines mixing ~10 responsibilities. Split it verbatim into cohesive same-package files (no behavior change): client.go foundation: ClientService, ClientWithAttachments, ClientCreatePayload, ErrClientNotInInbound, sqlInChunk client_locks.go inbound mutation locks, delete tombstones, compactOrphans client_lookup.go read-only lookups (GetByID, List, EffectiveFlow, ...) client_link.go inbound association sync (SyncInbound, DetachInbound, ...) client_crud.go single-client CRUD + validation + protocol defaults client_inbound_apply.go low-level inbound-settings mutators + by-email setters client_bulk.go bulk attach/detach/adjust/delete/create + DelDepleted client_traffic.go traffic-reset paths client_groups.go client group management client_paging.go paged listing, filtering, sorting, summary Every declaration moved unchanged (verified: identical func/type/const/var signature set before vs after). Imports redistributed per file via goimports. go build ./..., go vet, and go test ./web/service/... all pass. * refactor(service): split inbound.go into focused files inbound.go was 4100 lines. Split it verbatim into cohesive same-package files (no behavior change): inbound.go core inbound CRUD + InboundService (keeps pkg doc) inbound_protocol.go protocol / stream capability helpers inbound_node.go node/runtime/remote coordination + online tracking inbound_traffic.go traffic accounting, reset, client stats inbound_client_ips.go per-client IP tracking inbound_clients.go client lookups within inbounds + copy-clients inbound_disable.go auto-disable invalid inbounds/clients inbound_migration.go DB migrations inbound_sublink.go subscription link providers inbound_util.go generic slice/string helpers Identical func/type/const/var signature set before vs after; package doc comment preserved on inbound.go. Imports redistributed via goimports. Build, vet, and go test ./web/service/... all pass. * refactor(service): split tgbot.go into focused files tgbot.go was 3738 lines dominated by a 1246-line answerCallback. Split it verbatim into cohesive same-package files (no behavior change): tgbot.go lifecycle, bot setup, caches, small utils tgbot_router.go incoming update / command / callback dispatch tgbot_send.go outbound messaging primitives tgbot_client.go client views, actions, subscription links tgbot_inbound.go inbound listing / pickers tgbot_report.go server usage, exhausted, online, backups, notifications Identical func/type/const/var signature set before vs after. Imports redistributed via goimports. Build, vet, and go test ./web/service/... pass. * refactor(client): dedupe single-field by-email setters ResetClientIpLimitByEmail, ResetClientExpiryTimeByEmail, and ResetClientTrafficLimitByEmail shared an identical ~50-line body that resolves the inbound by email, confirms the client exists, rewrites a single-client settings payload, and delegates to UpdateInboundClient. Extract that into applyClientFieldByEmail(inboundSvc, email, mutate) and reduce each setter to a 3-line wrapper. Behavior is unchanged: same checks and error strings, same single-client payload contract, same totalGB guard. SetClientTelegramUserID (resolves by traffic id, different error text) and ToggleClientEnableByEmail/SetClientEnableByEmail (different return shape and a pre-read of the old state) intentionally keep their own bodies. * refactor(service): extract panel/ subpackage Move the panel-administration leaf services out of the flat service package into web/service/panel/ (package panel): user.go UserService (auth / 2FA / LDAP) panel.go PanelService (restart / self-update) + version helpers panel_other.go non-unix RestartPanel panel_unix.go unix RestartPanel api_token.go ApiTokenService websocket.go WebSocketService panel_test.go version/shellQuote unit tests These are leaves: they depend on core (SettingService, Release) but no core file references them, so the extraction creates no import cycle. Core references are now qualified (service.SettingService, service.Release); callers in main.go, web/web.go, and web/controller/* updated to panel.*. Build, vet, and go test ./web/... pass. * refactor(service): extract integration/ subpackage Move the external-provider integration leaves into web/service/integration/ (package integration): warp.go WarpService (Cloudflare WARP) nord.go NordService (NordVPN) custom_geo.go CustomGeoService (custom geo asset management) *_test.go custom_geo / panel-proxy tests These depend on core (SettingService, ServerService, XraySettingService) but no core file references them. xray_setting.go stays in core because it calls the unexported SettingService.saveSetting. The shared isBlockedIP SSRF helper (used by core url_safety.go and by custom_geo) now has a small copy in each package rather than being exported. Core references qualified; callers in web/web.go, web/job/*, and web/controller/* updated to integration.*. Build, vet, and go test ./web/... pass. * refactor(service): extract tgbot/ subpackage Move the Telegram bot (6 files + test) into web/service/tgbot/ (package tgbot). It is a leaf: it embeds five core services (Inbound/Client/Setting/ Server/Xray) and the core never references it, so no import cycle. To support the package boundary without changing behavior: - core exposes XrayProcess() *xray.Process so tgbot keeps calling the exact same running-process methods it used via the package-level `p`; - three core methods tgbot calls are exported: ClientService.checkIs- EnabledByEmail -> CheckIsEnabledByEmail, InboundService.getAllEmails -> GetAllEmails (callers updated in-package); - tgbot's embedded-field types and the few core type refs (Status, ClientCreatePayload, SanitizePublicHTTPURL) are now service-qualified. Callers in main.go, web/web.go, web/job/*, and web/controller/* updated to tgbot.*. Build, vet, and go test ./web/... pass. * refactor(service): extract outbound/ subpackage OutboundService (outbound.go) imports only neutral packages (config, database, model, xray) and its production code is referenced by no core or sibling service file — only by web/controller/xray_setting.go and web/job/xray_traffic_job.go. Move it to web/service/outbound/ (package outbound); no core qualification needed inside. Callers updated to outbound.*. The one coupling was a tiny pure test helper, outboundsContainTag, used by both outbound.go and the core outbound_subscription_test.go; it now has a small copy in that test file rather than being shared across the boundary. Build, vet, and go test ./web/... pass. * refactor(util): move wireguard into its own subpackage util/wireguard.go was the lone file of the root `util` package (24 lines, one exported func GenerateWireguardKeypair), while every other util concern lives in a focused subpackage (util/common, util/crypto, util/netsafe, ...). Move it to util/wireguard/ (package wireguard) for consistency; its only importer, web/service/integration/warp.go, is updated. The root `util` package no longer exists. * refactor(sub): drop redundant sub prefix from filenames Inside package sub the subXxx.go prefix just repeats the package name (like client_*.go did inside service). Rename for consistency; content and type names are unchanged: subController.go -> controller.go subService.go -> service.go subClashService.go -> clash_service.go subJsonService.go -> json_service.go (+ matching _test.go files) * refactor(controller): rename xui.go -> spa.go XUIController serves the panel's single-page-app shell; spa.go names that role plainly (the other controller files are domain-named). File rename only — the type stays XUIController. api_docs_test.go keys route base paths by filename, so its "xui.go" case is updated to "spa.go". * refactor: move backend packages under internal/ Adopt the idiomatic Go application layout: the backend packages now live under internal/ (a boundary the toolchain enforces), signalling private implementation instead of a library-style flat root. No runtime behavior changes — only import paths and a few build/config paths move. Moved: config, database, logger, mtproto, sub, util, web, xray -> internal/. main.go stays at the repo root and tools/openapigen stays under tools/ (both still import internal/* because the internal rule keys off the module root). The module path github.com/mhsanaei/3x-ui/v3 is unchanged; 149 .go files had their import prefix rewritten to .../internal/<pkg>. Couplings the Go compiler can't see, updated to the new layout: - frontend i18n imports of web/translation (react.ts, setup.components.ts) - vite outDir + eslint/tsconfig ignore globs -> internal/web/dist - Dockerfile COPY paths for web/dist and web/translation - locale.go os.DirFS("web") disk fallback -> "internal/web" - .gitignore and ci.yml go:embed stub for internal/web/dist - api_docs_test.go repo-root relative walk (one level deeper) - tools/openapigen filesystem package paths; ApiTokenView repointed to the web/service/panel subpackage and codegen regenerated (clears a stale type the ci.yml codegen check was failing on) Verified: go build/vet/test (all packages), and frontend typecheck, lint, vitest (478 tests), and production build into internal/web/dist. * fix(config): keep test runs from writing logs into the source tree GetLogFolder() returns a CWD-relative "./log" on Windows. Under `go test` the working directory is each package's own folder, so InitLogger (called by tests in web/job, web/service, xray, web/websocket) created stray log/ directories scattered through the source tree (e.g. internal/web/job/log/). Redirect to a shared temp folder when testing.Testing() reports a test run. Production behavior is unchanged: Windows still uses ./log next to the binary and Linux /var/log/x-ui. The log files were always gitignored (*.log) and never committed; this just stops the noise at the source. * docs: move subscription-template guide out of root into docs/ sub_templates/ was a top-level folder holding only a README and no actual templates (3x-ui ships none by design), referenced nowhere and unlinked from any doc — it read like an empty placeholder cluttering the repo root. Move the guide to docs/custom-subscription-templates.md (a proper docs home), reword its intro to read as documentation rather than a folder note, link it from the Features list in README.md, and drop the empty sub_templates/ folder. * fix: update stale web/ path references after the internal/ move The internal/ migration rewrote Go import paths but left some references to the old top-level layout in docs, comments, and a few runtime disk paths. Functional (dev-mode only): the disk-serving fallbacks that read the Vite build from disk when running from source still pointed at web/dist/, which moved to internal/web/dist/ — so `os.DirFS`/`os.Stat`/`os.ReadFile` in internal/web/web.go and internal/sub/{sub,controller}.go are corrected. Production was unaffected (it serves the embedded FS; verified by the Docker build), but `go run` with a live frontend build silently fell back to embed. Docs/comments: frontend/README.md, CONTRIBUTING.md, the claude-issue-bot and release workflows, the openapigen -root help text, and assorted Go comments now reference internal/web, internal/database, internal/sub, internal/xray, etc. Package-name mentions (the "web" package), root paths (main.go, frontend/, install scripts, /etc/x-ui), routes (/panel/api/xray), and the historical "web/assets no longer exists" note were intentionally left as-is. * refactor(web): remove the legacy /xui -> /panel redirect middleware RedirectMiddleware existed only for backward compatibility with the old `/xui` URL scheme (301-redirecting /xui and /xui/API to /panel and /panel/api). That cutover was long ago, so drop the middleware, its registration in initRouter, and the now-inaccurate "URL redirection" mention in the middleware package doc. Old /xui URLs now 404 like any other unknown path. HTTPS auto-redirect and auth redirects are unrelated and stay. * build: fix .dockerignore for internal/ layout and exclude runtime dir - web/dist -> internal/web/dist: the embedded frontend moved under internal/, so the stale exclude no longer matched and the locally-built dist could be sent to the build context (the frontend stage rebuilds it fresh anyway). - exclude x-ui/: the local runtime directory (SQLite db, geo .dat files, xray binaries, certs — ~150MB) was being shipped into the build context for no reason. Verified the pattern excludes only the directory and still keeps x-ui.sh, which the Dockerfile copies to /usr/bin/x-ui.
2026-06-10 06:19:22 -07:00
OutboundService outbound.OutboundService
XrayService service.XrayService
refactor: focused service files, leaf subpackages, and an internal/ layout (#5167) * refactor(service): split client.go into focused files client.go had grown to 4455 lines mixing ~10 responsibilities. Split it verbatim into cohesive same-package files (no behavior change): client.go foundation: ClientService, ClientWithAttachments, ClientCreatePayload, ErrClientNotInInbound, sqlInChunk client_locks.go inbound mutation locks, delete tombstones, compactOrphans client_lookup.go read-only lookups (GetByID, List, EffectiveFlow, ...) client_link.go inbound association sync (SyncInbound, DetachInbound, ...) client_crud.go single-client CRUD + validation + protocol defaults client_inbound_apply.go low-level inbound-settings mutators + by-email setters client_bulk.go bulk attach/detach/adjust/delete/create + DelDepleted client_traffic.go traffic-reset paths client_groups.go client group management client_paging.go paged listing, filtering, sorting, summary Every declaration moved unchanged (verified: identical func/type/const/var signature set before vs after). Imports redistributed per file via goimports. go build ./..., go vet, and go test ./web/service/... all pass. * refactor(service): split inbound.go into focused files inbound.go was 4100 lines. Split it verbatim into cohesive same-package files (no behavior change): inbound.go core inbound CRUD + InboundService (keeps pkg doc) inbound_protocol.go protocol / stream capability helpers inbound_node.go node/runtime/remote coordination + online tracking inbound_traffic.go traffic accounting, reset, client stats inbound_client_ips.go per-client IP tracking inbound_clients.go client lookups within inbounds + copy-clients inbound_disable.go auto-disable invalid inbounds/clients inbound_migration.go DB migrations inbound_sublink.go subscription link providers inbound_util.go generic slice/string helpers Identical func/type/const/var signature set before vs after; package doc comment preserved on inbound.go. Imports redistributed via goimports. Build, vet, and go test ./web/service/... all pass. * refactor(service): split tgbot.go into focused files tgbot.go was 3738 lines dominated by a 1246-line answerCallback. Split it verbatim into cohesive same-package files (no behavior change): tgbot.go lifecycle, bot setup, caches, small utils tgbot_router.go incoming update / command / callback dispatch tgbot_send.go outbound messaging primitives tgbot_client.go client views, actions, subscription links tgbot_inbound.go inbound listing / pickers tgbot_report.go server usage, exhausted, online, backups, notifications Identical func/type/const/var signature set before vs after. Imports redistributed via goimports. Build, vet, and go test ./web/service/... pass. * refactor(client): dedupe single-field by-email setters ResetClientIpLimitByEmail, ResetClientExpiryTimeByEmail, and ResetClientTrafficLimitByEmail shared an identical ~50-line body that resolves the inbound by email, confirms the client exists, rewrites a single-client settings payload, and delegates to UpdateInboundClient. Extract that into applyClientFieldByEmail(inboundSvc, email, mutate) and reduce each setter to a 3-line wrapper. Behavior is unchanged: same checks and error strings, same single-client payload contract, same totalGB guard. SetClientTelegramUserID (resolves by traffic id, different error text) and ToggleClientEnableByEmail/SetClientEnableByEmail (different return shape and a pre-read of the old state) intentionally keep their own bodies. * refactor(service): extract panel/ subpackage Move the panel-administration leaf services out of the flat service package into web/service/panel/ (package panel): user.go UserService (auth / 2FA / LDAP) panel.go PanelService (restart / self-update) + version helpers panel_other.go non-unix RestartPanel panel_unix.go unix RestartPanel api_token.go ApiTokenService websocket.go WebSocketService panel_test.go version/shellQuote unit tests These are leaves: they depend on core (SettingService, Release) but no core file references them, so the extraction creates no import cycle. Core references are now qualified (service.SettingService, service.Release); callers in main.go, web/web.go, and web/controller/* updated to panel.*. Build, vet, and go test ./web/... pass. * refactor(service): extract integration/ subpackage Move the external-provider integration leaves into web/service/integration/ (package integration): warp.go WarpService (Cloudflare WARP) nord.go NordService (NordVPN) custom_geo.go CustomGeoService (custom geo asset management) *_test.go custom_geo / panel-proxy tests These depend on core (SettingService, ServerService, XraySettingService) but no core file references them. xray_setting.go stays in core because it calls the unexported SettingService.saveSetting. The shared isBlockedIP SSRF helper (used by core url_safety.go and by custom_geo) now has a small copy in each package rather than being exported. Core references qualified; callers in web/web.go, web/job/*, and web/controller/* updated to integration.*. Build, vet, and go test ./web/... pass. * refactor(service): extract tgbot/ subpackage Move the Telegram bot (6 files + test) into web/service/tgbot/ (package tgbot). It is a leaf: it embeds five core services (Inbound/Client/Setting/ Server/Xray) and the core never references it, so no import cycle. To support the package boundary without changing behavior: - core exposes XrayProcess() *xray.Process so tgbot keeps calling the exact same running-process methods it used via the package-level `p`; - three core methods tgbot calls are exported: ClientService.checkIs- EnabledByEmail -> CheckIsEnabledByEmail, InboundService.getAllEmails -> GetAllEmails (callers updated in-package); - tgbot's embedded-field types and the few core type refs (Status, ClientCreatePayload, SanitizePublicHTTPURL) are now service-qualified. Callers in main.go, web/web.go, web/job/*, and web/controller/* updated to tgbot.*. Build, vet, and go test ./web/... pass. * refactor(service): extract outbound/ subpackage OutboundService (outbound.go) imports only neutral packages (config, database, model, xray) and its production code is referenced by no core or sibling service file — only by web/controller/xray_setting.go and web/job/xray_traffic_job.go. Move it to web/service/outbound/ (package outbound); no core qualification needed inside. Callers updated to outbound.*. The one coupling was a tiny pure test helper, outboundsContainTag, used by both outbound.go and the core outbound_subscription_test.go; it now has a small copy in that test file rather than being shared across the boundary. Build, vet, and go test ./web/... pass. * refactor(util): move wireguard into its own subpackage util/wireguard.go was the lone file of the root `util` package (24 lines, one exported func GenerateWireguardKeypair), while every other util concern lives in a focused subpackage (util/common, util/crypto, util/netsafe, ...). Move it to util/wireguard/ (package wireguard) for consistency; its only importer, web/service/integration/warp.go, is updated. The root `util` package no longer exists. * refactor(sub): drop redundant sub prefix from filenames Inside package sub the subXxx.go prefix just repeats the package name (like client_*.go did inside service). Rename for consistency; content and type names are unchanged: subController.go -> controller.go subService.go -> service.go subClashService.go -> clash_service.go subJsonService.go -> json_service.go (+ matching _test.go files) * refactor(controller): rename xui.go -> spa.go XUIController serves the panel's single-page-app shell; spa.go names that role plainly (the other controller files are domain-named). File rename only — the type stays XUIController. api_docs_test.go keys route base paths by filename, so its "xui.go" case is updated to "spa.go". * refactor: move backend packages under internal/ Adopt the idiomatic Go application layout: the backend packages now live under internal/ (a boundary the toolchain enforces), signalling private implementation instead of a library-style flat root. No runtime behavior changes — only import paths and a few build/config paths move. Moved: config, database, logger, mtproto, sub, util, web, xray -> internal/. main.go stays at the repo root and tools/openapigen stays under tools/ (both still import internal/* because the internal rule keys off the module root). The module path github.com/mhsanaei/3x-ui/v3 is unchanged; 149 .go files had their import prefix rewritten to .../internal/<pkg>. Couplings the Go compiler can't see, updated to the new layout: - frontend i18n imports of web/translation (react.ts, setup.components.ts) - vite outDir + eslint/tsconfig ignore globs -> internal/web/dist - Dockerfile COPY paths for web/dist and web/translation - locale.go os.DirFS("web") disk fallback -> "internal/web" - .gitignore and ci.yml go:embed stub for internal/web/dist - api_docs_test.go repo-root relative walk (one level deeper) - tools/openapigen filesystem package paths; ApiTokenView repointed to the web/service/panel subpackage and codegen regenerated (clears a stale type the ci.yml codegen check was failing on) Verified: go build/vet/test (all packages), and frontend typecheck, lint, vitest (478 tests), and production build into internal/web/dist. * fix(config): keep test runs from writing logs into the source tree GetLogFolder() returns a CWD-relative "./log" on Windows. Under `go test` the working directory is each package's own folder, so InitLogger (called by tests in web/job, web/service, xray, web/websocket) created stray log/ directories scattered through the source tree (e.g. internal/web/job/log/). Redirect to a shared temp folder when testing.Testing() reports a test run. Production behavior is unchanged: Windows still uses ./log next to the binary and Linux /var/log/x-ui. The log files were always gitignored (*.log) and never committed; this just stops the noise at the source. * docs: move subscription-template guide out of root into docs/ sub_templates/ was a top-level folder holding only a README and no actual templates (3x-ui ships none by design), referenced nowhere and unlinked from any doc — it read like an empty placeholder cluttering the repo root. Move the guide to docs/custom-subscription-templates.md (a proper docs home), reword its intro to read as documentation rather than a folder note, link it from the Features list in README.md, and drop the empty sub_templates/ folder. * fix: update stale web/ path references after the internal/ move The internal/ migration rewrote Go import paths but left some references to the old top-level layout in docs, comments, and a few runtime disk paths. Functional (dev-mode only): the disk-serving fallbacks that read the Vite build from disk when running from source still pointed at web/dist/, which moved to internal/web/dist/ — so `os.DirFS`/`os.Stat`/`os.ReadFile` in internal/web/web.go and internal/sub/{sub,controller}.go are corrected. Production was unaffected (it serves the embedded FS; verified by the Docker build), but `go run` with a live frontend build silently fell back to embed. Docs/comments: frontend/README.md, CONTRIBUTING.md, the claude-issue-bot and release workflows, the openapigen -root help text, and assorted Go comments now reference internal/web, internal/database, internal/sub, internal/xray, etc. Package-name mentions (the "web" package), root paths (main.go, frontend/, install scripts, /etc/x-ui), routes (/panel/api/xray), and the historical "web/assets no longer exists" note were intentionally left as-is. * refactor(web): remove the legacy /xui -> /panel redirect middleware RedirectMiddleware existed only for backward compatibility with the old `/xui` URL scheme (301-redirecting /xui and /xui/API to /panel and /panel/api). That cutover was long ago, so drop the middleware, its registration in initRouter, and the now-inaccurate "URL redirection" mention in the middleware package doc. Old /xui URLs now 404 like any other unknown path. HTTPS auto-redirect and auth redirects are unrelated and stay. * build: fix .dockerignore for internal/ layout and exclude runtime dir - web/dist -> internal/web/dist: the embedded frontend moved under internal/, so the stale exclude no longer matched and the locally-built dist could be sent to the build context (the frontend stage rebuilds it fresh anyway). - exclude x-ui/: the local runtime directory (SQLite db, geo .dat files, xray binaries, certs — ~150MB) was being shipped into the build context for no reason. Verified the pattern excludes only the directory and still keeps x-ui.sh, which the Dockerfile copies to /usr/bin/x-ui.
2026-06-10 06:19:22 -07:00
WarpService integration.WarpService
NordService integration.NordService
PiaService integration.PiaService
feat: add support for subscription-based outbounds with auto-update (#5037) * feat: add support for subscription-based outbounds with auto-update - New OutboundSubscription model (full support on both SQLite and PostgreSQL) - Go subscription link parser (vmess/vless/trojan/ss/hysteria2/wireguard) matching frontend behavior - Stable tag assignment across refreshes (designed for balancer + routing use) - Runtime merge of subscription outbounds into Xray config (additive only) - Full CRUD + manual refresh + preview API - Background auto-update job (per-subscription interval) - Frontend management UI in Outbounds tab (Subscriptions drawer) + tag integration in balancers/routing rules - Proper dual-database support including CLI migration path Review & hardening notes: - Fixed merge logic bug that could drop manual outbounds - Added SSRF/private-IP protection on subscription URLs using SanitizePublicHTTPURL - Improved update interval UX (hours + minutes) - Auto-fetch on first subscription creation - Added detailed comments on tag stability strategy and balancer implications when servers are added/removed/rotated - Updated migrationModels() for CLI migrate-db support * fix: resolve frontend lint/type errors and Go build break Frontend (eslint + tsc clean): - Destructure subscriptionOutboundTags prop in RoutingTab and BalancersTab. It was declared in the interface and used in useMemo but never destructured, so it resolved as an unresolved global (react-hooks warning + tsc "Cannot find name"). The prop is passed by XrayPage, so the feature was silently inert. - OutboundsTab: remove unused useEffect import, add an OutboundSub type to replace any[] state and the any/any table render signature, type the subscriptionOutbounds cast, and replace unused catch (e) bindings with parameter-less catch. Also type HttpUtil.post as OutboundSub so r.obj?.id type-checks. Backend (go build clean): - outbound_subscription_job: websocket.MessageTypeXray is undefined; use the existing MessageTypeOutbounds since the job refreshes outbound subscriptions. * fix(xray): make outbound subscription creation work end-to-end - Correct API paths from /panel/xray/outbound-subs to /panel/api/xray/outbound-subs. The controller is mounted under /panel/api, so the old paths hit the SPA page route (GET-only) and 404'd on POST. - Send the create-subscription body as a plain object instead of URLSearchParams. The axios request interceptor serializes bodies with qs.stringify, which can't read URLSearchParams' internal storage and produced an empty body, so the backend rejected it with "subscription URL is required". - Use message.useMessage() + context holder instead of the static antd message API (resolves the "Static function can not consume context" warning), matching XrayPage's pattern. - Migrate the subscriptions Drawer to antd v6 props: width -> size, destroyOnClose -> destroyOnHidden, and Space direction -> orientation. * feat(xray): show traffic/test for subscription outbounds; harden + test the feature Display (the reported issue): - Replace the flat read-only pills with a proper read-only table (desktop) and cards (mobile) in a new SubscriptionOutbounds component, showing Address, Protocol, Traffic (matched by tag — already collected by Xray), and a Test button with Latency. No edit/delete/move (read-only). - Test subscription outbounds via the existing /testOutbound endpoint, with results keyed by tag (subscriptionTestStates + testSubscriptionOutbound in useXraySetting, wired through XrayPage). Generalize isTesting/testResult to a string|number key so the same helpers serve index- and tag-keyed states. i18n: - Replace all hardcoded English subscription strings with t() calls and add pages.xray.outboundSub.* keys to en-US.json (other locales fall back). Backend hardening + tests: - xray.go: drop the tautological `subSvc != nil` check. - outbound_subscription: re-validate every redirect hop against private/ internal addresses (CheckRedirect) and cap the redirect chain, closing an SSRF gap where only the initial host was checked. - Extract assignStableTags as a pure function and add unit tests for tag stability and SSRF rejection (the feature previously had no tests). Misc: - gofmt util/link/outbound.go (it was not gofmt-clean). * fix(xray): make outbound-subs feature pass CI (test compile, route docs, openapi) - outbound_test.go: remove unused `inner`/`lines` variables that broke the `util/link` test build (declared and not used). - Document the 7 outbound-subscription routes in endpoints.ts (list, create, update, delete, del alias, refresh, parse) so TestAPIRoutesDocumented passes. - Regenerate frontend/public/openapi.json (npm run gen) to include the new endpoints, satisfying the codegen freshness check. * feat(xray): per-subscription allow-private, gap-filled tags, UI tweaks, delete refresh Backend: - Add a per-subscription AllowPrivate flag (default off). Create/Update/refresh and the redirect check sanitize the URL with it, so localhost/LAN sources work only when explicitly opted in; the SSRF guard still blocks private targets by default. Controller reads the allowPrivate form field on create/update/parse. - Default outbound tag prefix now uses the smallest free "subN-" number instead of the auto-increment id, so deleting a subscription frees its number for reuse (a fresh start gives sub1) while staying stable per subscription. Extracted a pure defaultPrefixNumber() with unit tests. - deleteOutboundSub now signals SetToNeedRestart so xray drops the outbounds. Frontend: - "Allow private address" toggle in the add form (sends allowPrivate). - Delete now refreshes the xray view immediately (no manual page reload). - Subscriptions manager opens as a centered Modal instead of a right-side Drawer. - Move Outbounds to a top-level sidebar item under Nodes (out of Xray Configs). - Collapse WARP/NordVPN into a "more" dropdown. - Document the allowPrivate param in endpoints.ts. * i18n(xray): translate outbound-subscription UI into all locales - Translate the pages.xray.outboundSub.* strings (and allowPrivate label/hint) into all 12 non-English locales, matching each file's existing terminology. - Remove the unused outboundSub.add ("Add subscription") key from every locale. * feat(xray): subscription manager — edit, reorder/priority, status, preview, refresh-all Backend: - Per-subscription Priority + Prepend: subscriptions are ordered by Priority and placed before (Prepend) or after the manual template outbounds in the merge, so a subscription server can become the default. New Move(up/down) endpoint re-normalizes priorities; merge split into prepend/template/append. - List now returns a derived OutboundCount and orders by priority, and strips the heavy LastFetchedOutbounds/LinkIdentities blobs from the list payload. - Create/Update accept the prepend flag; new subs append at the end of priority. Frontend (Outbound Subscriptions modal): - Edit existing subscriptions (reuses the form + Update endpoint). - Inline enable/disable Switch, Status column (OK / error tooltip), Outbounds count column, per-row refresh spinner, "Refresh all" button. - Reorder (move up/down) controls + a "Before manual outbounds" toggle. - Preview button: fetch+parse a URL via /parse without saving. - Document the move route + prepend param in endpoints.ts; regenerate openapi.json. * i18n(xray): translate new subscription-manager strings into all locales Add the prepend/prependHint, preview/previewEmpty, refreshAll, statusOk and toastUpdated keys to all 12 non-English locales, matching each file's terminology. --------- Co-authored-by: MHSanaei <ho3ein.sanaei@gmail.com>
2026-06-08 09:09:53 -07:00
OutboundSubscriptionService service.OutboundSubscriptionService
feat(xray): browse geosite/geoip categories from routing rules (#6165) * feat(xray): browse geosite/geoip categories from routing rules Routing rules made you type category names from memory: nothing showed which categories a database actually contains, what is inside one, or whether a name resolves at all — a typo only surfaced when Xray refused the config. The panel now reads Xray's .dat databases itself and exposes them over four endpoints: databases in the asset folder, a database's categories, one page of a category's rules, and validation of the tokens already in a rule. The reader walks the protobuf wire format directly rather than decoding into Go structs, because a 10 MB geosite.dat holds well over a million domains and materialising them costs ~284 MB where streaming costs ~19 MB. Only the category index is cached, entry pages are scanned on demand, and scans are serialised, so twenty concurrent requests peak at 87 MB instead of 1 GB. A database's type is decided by its contents, not its file name, since custom .dat files are named freely. In the rule form, the source-IP, IP and domain fields gain a database button opening the browser: search over categories, a preview of what a category holds, and a multi-select that merges into the field. Plain domains, CIDRs and categories the panel does not know are left untouched; categories already present come back ticked, and unticking one removes it from the rule. * fix(xray): read geo databases through os.Root and match codes verbatim CodeQL flagged the database read as a path built from a user-supplied value, and it was right about the shape of it. The file name arrives in a request; resolve() rejects traversal and stats the file through an os.Root, but the read itself went through a joined path with os.ReadFile. That left the symlink defence incomplete: the stat could pass while the read followed a link planted — or swapped in — afterwards. Reads now go through the same root, so a request-supplied name never becomes a path this code resolves on its own, and the size limit is applied to the opened file rather than to a separate stat of it. Lookup no longer trims the category code either. It backs the routing-token validator, and the core matches codes verbatim: "geosite: cn" will not start Xray, so repairing that space here hid exactly the typo the validator exists to report. * fix(xray): address review findings on the geo category browser Asset folder. The browser read config.GetBinFolderPath() unconditionally, but the core honours a preset XRAY_LOCATION_ASSET and only falls back to the bin folder (ensureXrayAssetLocation). On an install pointing at a shared asset directory the panel listed an empty folder and reported perfectly valid geosite:/geoip: tokens as missing — the validator warning about a correct config. The directory is now resolved with the core's precedence. Paging. Serving one page read and rescanned the whole database, so walking category-ads-all re-read it per page. The index now records each category's byte range and a page reads only that record through the os.Root handle, with the current category's records held for the duration of a paging session. Profiling that also showed the real cost was not the read but the slice of payload pointers built per call — a category holds a hundred thousand of them — so records are now walked with a callback instead. Ten pages over category-ads-all: 239 MB allocated, now 4.3 MB. Cached failures. Any error from reading a file was latched under the file's size+mtime, so a transient ENOMEM or EMFILE marked a healthy database as damaged until it changed on disk. Only deterministic failures are cached. Wrong kind. A geoip: token typed into a domain field parsed as a plain domain and was waved through, though the core cannot resolve it as one. It is now reported, with its own reason and wording. Frontend. The category filter fed the query key on every keystroke, so each character triggered a request that re-scanned the database; it is debounced now. GeoTokenInput accepts and forwards a ref, so React Hook Form can focus these three fields on a validation error again. A failed validation shows that it failed instead of rendering the same empty state as "no issues". Also drops an unreachable branch in the token-count guard and corrects the categories endpoint docs, where limit is unbounded by default. --------- Co-authored-by: STRENCH0 <17428017+STRENCH0@users.noreply.github.com>
2026-08-15 08:12:59 -07:00
GeodataService service.GeodataService
2023-12-04 10:20:46 -08:00
}
2025-09-20 00:35:50 -07:00
// NewXraySettingController creates a new XraySettingController and initializes its routes.
2023-12-04 10:20:46 -08:00
func NewXraySettingController(g *gin.RouterGroup) *XraySettingController {
a := &XraySettingController{PiaService: *integration.NewPiaService()}
2023-12-04 10:20:46 -08:00
a.initRouter(g)
return a
}
2025-09-20 00:35:50 -07:00
// initRouter sets up the routes for Xray settings management.
2023-12-04 10:20:46 -08:00
func (a *XraySettingController) initRouter(g *gin.RouterGroup) {
g = g.Group("/xray")
2025-09-16 16:08:59 -07:00
g.GET("/getDefaultJsonConfig", a.getDefaultXrayConfig)
g.GET("/getOutboundsTraffic", a.getOutboundsTraffic)
g.GET("/getXrayResult", a.getXrayResult)
2023-12-04 10:20:46 -08:00
g.POST("/", a.getXraySetting)
g.POST("/warp/:action", a.warp)
g.POST("/nord/:action", a.nord)
g.POST("/pia/:action", a.pia)
2025-09-16 16:08:59 -07:00
g.POST("/update", a.updateSetting)
2024-02-06 23:55:31 -08:00
g.POST("/resetOutboundsTraffic", a.resetOutboundsTraffic)
g.POST("/testOutbound", a.testOutbound)
g.POST("/testOutbounds", a.testOutbounds)
g.POST("/balancerStatus", a.balancerStatus)
g.POST("/balancerOverride", a.balancerOverride)
g.POST("/routeTest", a.routeTest)
feat: add support for subscription-based outbounds with auto-update (#5037) * feat: add support for subscription-based outbounds with auto-update - New OutboundSubscription model (full support on both SQLite and PostgreSQL) - Go subscription link parser (vmess/vless/trojan/ss/hysteria2/wireguard) matching frontend behavior - Stable tag assignment across refreshes (designed for balancer + routing use) - Runtime merge of subscription outbounds into Xray config (additive only) - Full CRUD + manual refresh + preview API - Background auto-update job (per-subscription interval) - Frontend management UI in Outbounds tab (Subscriptions drawer) + tag integration in balancers/routing rules - Proper dual-database support including CLI migration path Review & hardening notes: - Fixed merge logic bug that could drop manual outbounds - Added SSRF/private-IP protection on subscription URLs using SanitizePublicHTTPURL - Improved update interval UX (hours + minutes) - Auto-fetch on first subscription creation - Added detailed comments on tag stability strategy and balancer implications when servers are added/removed/rotated - Updated migrationModels() for CLI migrate-db support * fix: resolve frontend lint/type errors and Go build break Frontend (eslint + tsc clean): - Destructure subscriptionOutboundTags prop in RoutingTab and BalancersTab. It was declared in the interface and used in useMemo but never destructured, so it resolved as an unresolved global (react-hooks warning + tsc "Cannot find name"). The prop is passed by XrayPage, so the feature was silently inert. - OutboundsTab: remove unused useEffect import, add an OutboundSub type to replace any[] state and the any/any table render signature, type the subscriptionOutbounds cast, and replace unused catch (e) bindings with parameter-less catch. Also type HttpUtil.post as OutboundSub so r.obj?.id type-checks. Backend (go build clean): - outbound_subscription_job: websocket.MessageTypeXray is undefined; use the existing MessageTypeOutbounds since the job refreshes outbound subscriptions. * fix(xray): make outbound subscription creation work end-to-end - Correct API paths from /panel/xray/outbound-subs to /panel/api/xray/outbound-subs. The controller is mounted under /panel/api, so the old paths hit the SPA page route (GET-only) and 404'd on POST. - Send the create-subscription body as a plain object instead of URLSearchParams. The axios request interceptor serializes bodies with qs.stringify, which can't read URLSearchParams' internal storage and produced an empty body, so the backend rejected it with "subscription URL is required". - Use message.useMessage() + context holder instead of the static antd message API (resolves the "Static function can not consume context" warning), matching XrayPage's pattern. - Migrate the subscriptions Drawer to antd v6 props: width -> size, destroyOnClose -> destroyOnHidden, and Space direction -> orientation. * feat(xray): show traffic/test for subscription outbounds; harden + test the feature Display (the reported issue): - Replace the flat read-only pills with a proper read-only table (desktop) and cards (mobile) in a new SubscriptionOutbounds component, showing Address, Protocol, Traffic (matched by tag — already collected by Xray), and a Test button with Latency. No edit/delete/move (read-only). - Test subscription outbounds via the existing /testOutbound endpoint, with results keyed by tag (subscriptionTestStates + testSubscriptionOutbound in useXraySetting, wired through XrayPage). Generalize isTesting/testResult to a string|number key so the same helpers serve index- and tag-keyed states. i18n: - Replace all hardcoded English subscription strings with t() calls and add pages.xray.outboundSub.* keys to en-US.json (other locales fall back). Backend hardening + tests: - xray.go: drop the tautological `subSvc != nil` check. - outbound_subscription: re-validate every redirect hop against private/ internal addresses (CheckRedirect) and cap the redirect chain, closing an SSRF gap where only the initial host was checked. - Extract assignStableTags as a pure function and add unit tests for tag stability and SSRF rejection (the feature previously had no tests). Misc: - gofmt util/link/outbound.go (it was not gofmt-clean). * fix(xray): make outbound-subs feature pass CI (test compile, route docs, openapi) - outbound_test.go: remove unused `inner`/`lines` variables that broke the `util/link` test build (declared and not used). - Document the 7 outbound-subscription routes in endpoints.ts (list, create, update, delete, del alias, refresh, parse) so TestAPIRoutesDocumented passes. - Regenerate frontend/public/openapi.json (npm run gen) to include the new endpoints, satisfying the codegen freshness check. * feat(xray): per-subscription allow-private, gap-filled tags, UI tweaks, delete refresh Backend: - Add a per-subscription AllowPrivate flag (default off). Create/Update/refresh and the redirect check sanitize the URL with it, so localhost/LAN sources work only when explicitly opted in; the SSRF guard still blocks private targets by default. Controller reads the allowPrivate form field on create/update/parse. - Default outbound tag prefix now uses the smallest free "subN-" number instead of the auto-increment id, so deleting a subscription frees its number for reuse (a fresh start gives sub1) while staying stable per subscription. Extracted a pure defaultPrefixNumber() with unit tests. - deleteOutboundSub now signals SetToNeedRestart so xray drops the outbounds. Frontend: - "Allow private address" toggle in the add form (sends allowPrivate). - Delete now refreshes the xray view immediately (no manual page reload). - Subscriptions manager opens as a centered Modal instead of a right-side Drawer. - Move Outbounds to a top-level sidebar item under Nodes (out of Xray Configs). - Collapse WARP/NordVPN into a "more" dropdown. - Document the allowPrivate param in endpoints.ts. * i18n(xray): translate outbound-subscription UI into all locales - Translate the pages.xray.outboundSub.* strings (and allowPrivate label/hint) into all 12 non-English locales, matching each file's existing terminology. - Remove the unused outboundSub.add ("Add subscription") key from every locale. * feat(xray): subscription manager — edit, reorder/priority, status, preview, refresh-all Backend: - Per-subscription Priority + Prepend: subscriptions are ordered by Priority and placed before (Prepend) or after the manual template outbounds in the merge, so a subscription server can become the default. New Move(up/down) endpoint re-normalizes priorities; merge split into prepend/template/append. - List now returns a derived OutboundCount and orders by priority, and strips the heavy LastFetchedOutbounds/LinkIdentities blobs from the list payload. - Create/Update accept the prepend flag; new subs append at the end of priority. Frontend (Outbound Subscriptions modal): - Edit existing subscriptions (reuses the form + Update endpoint). - Inline enable/disable Switch, Status column (OK / error tooltip), Outbounds count column, per-row refresh spinner, "Refresh all" button. - Reorder (move up/down) controls + a "Before manual outbounds" toggle. - Preview button: fetch+parse a URL via /parse without saving. - Document the move route + prepend param in endpoints.ts; regenerate openapi.json. * i18n(xray): translate new subscription-manager strings into all locales Add the prepend/prependHint, preview/previewEmpty, refreshAll, statusOk and toastUpdated keys to all 12 non-English locales, matching each file's terminology. --------- Co-authored-by: MHSanaei <ho3ein.sanaei@gmail.com>
2026-06-08 09:09:53 -07:00
feat(xray): browse geosite/geoip categories from routing rules (#6165) * feat(xray): browse geosite/geoip categories from routing rules Routing rules made you type category names from memory: nothing showed which categories a database actually contains, what is inside one, or whether a name resolves at all — a typo only surfaced when Xray refused the config. The panel now reads Xray's .dat databases itself and exposes them over four endpoints: databases in the asset folder, a database's categories, one page of a category's rules, and validation of the tokens already in a rule. The reader walks the protobuf wire format directly rather than decoding into Go structs, because a 10 MB geosite.dat holds well over a million domains and materialising them costs ~284 MB where streaming costs ~19 MB. Only the category index is cached, entry pages are scanned on demand, and scans are serialised, so twenty concurrent requests peak at 87 MB instead of 1 GB. A database's type is decided by its contents, not its file name, since custom .dat files are named freely. In the rule form, the source-IP, IP and domain fields gain a database button opening the browser: search over categories, a preview of what a category holds, and a multi-select that merges into the field. Plain domains, CIDRs and categories the panel does not know are left untouched; categories already present come back ticked, and unticking one removes it from the rule. * fix(xray): read geo databases through os.Root and match codes verbatim CodeQL flagged the database read as a path built from a user-supplied value, and it was right about the shape of it. The file name arrives in a request; resolve() rejects traversal and stats the file through an os.Root, but the read itself went through a joined path with os.ReadFile. That left the symlink defence incomplete: the stat could pass while the read followed a link planted — or swapped in — afterwards. Reads now go through the same root, so a request-supplied name never becomes a path this code resolves on its own, and the size limit is applied to the opened file rather than to a separate stat of it. Lookup no longer trims the category code either. It backs the routing-token validator, and the core matches codes verbatim: "geosite: cn" will not start Xray, so repairing that space here hid exactly the typo the validator exists to report. * fix(xray): address review findings on the geo category browser Asset folder. The browser read config.GetBinFolderPath() unconditionally, but the core honours a preset XRAY_LOCATION_ASSET and only falls back to the bin folder (ensureXrayAssetLocation). On an install pointing at a shared asset directory the panel listed an empty folder and reported perfectly valid geosite:/geoip: tokens as missing — the validator warning about a correct config. The directory is now resolved with the core's precedence. Paging. Serving one page read and rescanned the whole database, so walking category-ads-all re-read it per page. The index now records each category's byte range and a page reads only that record through the os.Root handle, with the current category's records held for the duration of a paging session. Profiling that also showed the real cost was not the read but the slice of payload pointers built per call — a category holds a hundred thousand of them — so records are now walked with a callback instead. Ten pages over category-ads-all: 239 MB allocated, now 4.3 MB. Cached failures. Any error from reading a file was latched under the file's size+mtime, so a transient ENOMEM or EMFILE marked a healthy database as damaged until it changed on disk. Only deterministic failures are cached. Wrong kind. A geoip: token typed into a domain field parsed as a plain domain and was waved through, though the core cannot resolve it as one. It is now reported, with its own reason and wording. Frontend. The category filter fed the query key on every keystroke, so each character triggered a request that re-scanned the database; it is debounced now. GeoTokenInput accepts and forwards a ref, so React Hook Form can focus these three fields on a validation error again. A failed validation shows that it failed instead of rendering the same empty state as "no issues". Also drops an unreachable branch in the token-count guard and corrects the categories endpoint docs, where limit is unbounded by default. --------- Co-authored-by: STRENCH0 <17428017+STRENCH0@users.noreply.github.com>
2026-08-15 08:12:59 -07:00
g.GET("/geodata/files", a.geodataFiles)
g.GET("/geodata/categories", a.geodataCategories)
g.GET("/geodata/entries", a.geodataEntries)
g.POST("/geodata/validate", a.geodataValidate)
feat: add support for subscription-based outbounds with auto-update (#5037) * feat: add support for subscription-based outbounds with auto-update - New OutboundSubscription model (full support on both SQLite and PostgreSQL) - Go subscription link parser (vmess/vless/trojan/ss/hysteria2/wireguard) matching frontend behavior - Stable tag assignment across refreshes (designed for balancer + routing use) - Runtime merge of subscription outbounds into Xray config (additive only) - Full CRUD + manual refresh + preview API - Background auto-update job (per-subscription interval) - Frontend management UI in Outbounds tab (Subscriptions drawer) + tag integration in balancers/routing rules - Proper dual-database support including CLI migration path Review & hardening notes: - Fixed merge logic bug that could drop manual outbounds - Added SSRF/private-IP protection on subscription URLs using SanitizePublicHTTPURL - Improved update interval UX (hours + minutes) - Auto-fetch on first subscription creation - Added detailed comments on tag stability strategy and balancer implications when servers are added/removed/rotated - Updated migrationModels() for CLI migrate-db support * fix: resolve frontend lint/type errors and Go build break Frontend (eslint + tsc clean): - Destructure subscriptionOutboundTags prop in RoutingTab and BalancersTab. It was declared in the interface and used in useMemo but never destructured, so it resolved as an unresolved global (react-hooks warning + tsc "Cannot find name"). The prop is passed by XrayPage, so the feature was silently inert. - OutboundsTab: remove unused useEffect import, add an OutboundSub type to replace any[] state and the any/any table render signature, type the subscriptionOutbounds cast, and replace unused catch (e) bindings with parameter-less catch. Also type HttpUtil.post as OutboundSub so r.obj?.id type-checks. Backend (go build clean): - outbound_subscription_job: websocket.MessageTypeXray is undefined; use the existing MessageTypeOutbounds since the job refreshes outbound subscriptions. * fix(xray): make outbound subscription creation work end-to-end - Correct API paths from /panel/xray/outbound-subs to /panel/api/xray/outbound-subs. The controller is mounted under /panel/api, so the old paths hit the SPA page route (GET-only) and 404'd on POST. - Send the create-subscription body as a plain object instead of URLSearchParams. The axios request interceptor serializes bodies with qs.stringify, which can't read URLSearchParams' internal storage and produced an empty body, so the backend rejected it with "subscription URL is required". - Use message.useMessage() + context holder instead of the static antd message API (resolves the "Static function can not consume context" warning), matching XrayPage's pattern. - Migrate the subscriptions Drawer to antd v6 props: width -> size, destroyOnClose -> destroyOnHidden, and Space direction -> orientation. * feat(xray): show traffic/test for subscription outbounds; harden + test the feature Display (the reported issue): - Replace the flat read-only pills with a proper read-only table (desktop) and cards (mobile) in a new SubscriptionOutbounds component, showing Address, Protocol, Traffic (matched by tag — already collected by Xray), and a Test button with Latency. No edit/delete/move (read-only). - Test subscription outbounds via the existing /testOutbound endpoint, with results keyed by tag (subscriptionTestStates + testSubscriptionOutbound in useXraySetting, wired through XrayPage). Generalize isTesting/testResult to a string|number key so the same helpers serve index- and tag-keyed states. i18n: - Replace all hardcoded English subscription strings with t() calls and add pages.xray.outboundSub.* keys to en-US.json (other locales fall back). Backend hardening + tests: - xray.go: drop the tautological `subSvc != nil` check. - outbound_subscription: re-validate every redirect hop against private/ internal addresses (CheckRedirect) and cap the redirect chain, closing an SSRF gap where only the initial host was checked. - Extract assignStableTags as a pure function and add unit tests for tag stability and SSRF rejection (the feature previously had no tests). Misc: - gofmt util/link/outbound.go (it was not gofmt-clean). * fix(xray): make outbound-subs feature pass CI (test compile, route docs, openapi) - outbound_test.go: remove unused `inner`/`lines` variables that broke the `util/link` test build (declared and not used). - Document the 7 outbound-subscription routes in endpoints.ts (list, create, update, delete, del alias, refresh, parse) so TestAPIRoutesDocumented passes. - Regenerate frontend/public/openapi.json (npm run gen) to include the new endpoints, satisfying the codegen freshness check. * feat(xray): per-subscription allow-private, gap-filled tags, UI tweaks, delete refresh Backend: - Add a per-subscription AllowPrivate flag (default off). Create/Update/refresh and the redirect check sanitize the URL with it, so localhost/LAN sources work only when explicitly opted in; the SSRF guard still blocks private targets by default. Controller reads the allowPrivate form field on create/update/parse. - Default outbound tag prefix now uses the smallest free "subN-" number instead of the auto-increment id, so deleting a subscription frees its number for reuse (a fresh start gives sub1) while staying stable per subscription. Extracted a pure defaultPrefixNumber() with unit tests. - deleteOutboundSub now signals SetToNeedRestart so xray drops the outbounds. Frontend: - "Allow private address" toggle in the add form (sends allowPrivate). - Delete now refreshes the xray view immediately (no manual page reload). - Subscriptions manager opens as a centered Modal instead of a right-side Drawer. - Move Outbounds to a top-level sidebar item under Nodes (out of Xray Configs). - Collapse WARP/NordVPN into a "more" dropdown. - Document the allowPrivate param in endpoints.ts. * i18n(xray): translate outbound-subscription UI into all locales - Translate the pages.xray.outboundSub.* strings (and allowPrivate label/hint) into all 12 non-English locales, matching each file's existing terminology. - Remove the unused outboundSub.add ("Add subscription") key from every locale. * feat(xray): subscription manager — edit, reorder/priority, status, preview, refresh-all Backend: - Per-subscription Priority + Prepend: subscriptions are ordered by Priority and placed before (Prepend) or after the manual template outbounds in the merge, so a subscription server can become the default. New Move(up/down) endpoint re-normalizes priorities; merge split into prepend/template/append. - List now returns a derived OutboundCount and orders by priority, and strips the heavy LastFetchedOutbounds/LinkIdentities blobs from the list payload. - Create/Update accept the prepend flag; new subs append at the end of priority. Frontend (Outbound Subscriptions modal): - Edit existing subscriptions (reuses the form + Update endpoint). - Inline enable/disable Switch, Status column (OK / error tooltip), Outbounds count column, per-row refresh spinner, "Refresh all" button. - Reorder (move up/down) controls + a "Before manual outbounds" toggle. - Preview button: fetch+parse a URL via /parse without saving. - Document the move route + prepend param in endpoints.ts; regenerate openapi.json. * i18n(xray): translate new subscription-manager strings into all locales Add the prepend/prependHint, preview/previewEmpty, refreshAll, statusOk and toastUpdated keys to all 12 non-English locales, matching each file's terminology. --------- Co-authored-by: MHSanaei <ho3ein.sanaei@gmail.com>
2026-06-08 09:09:53 -07:00
// Outbound subscription (remote outbound lists)
g.GET("/outbound-subs", a.listOutboundSubs)
g.POST("/outbound-subs", a.createOutboundSub)
g.POST("/outbound-subs/:id/refresh", a.refreshOutboundSub)
g.POST("/outbound-subs/:id/move", a.moveOutboundSub)
g.POST("/outbound-subs/:id", a.updateOutboundSub)
g.DELETE("/outbound-subs/:id", a.deleteOutboundSub)
g.POST("/outbound-subs/:id/del", a.deleteOutboundSub) // POST alias for clients that can't send DELETE
feat: add support for subscription-based outbounds with auto-update (#5037) * feat: add support for subscription-based outbounds with auto-update - New OutboundSubscription model (full support on both SQLite and PostgreSQL) - Go subscription link parser (vmess/vless/trojan/ss/hysteria2/wireguard) matching frontend behavior - Stable tag assignment across refreshes (designed for balancer + routing use) - Runtime merge of subscription outbounds into Xray config (additive only) - Full CRUD + manual refresh + preview API - Background auto-update job (per-subscription interval) - Frontend management UI in Outbounds tab (Subscriptions drawer) + tag integration in balancers/routing rules - Proper dual-database support including CLI migration path Review & hardening notes: - Fixed merge logic bug that could drop manual outbounds - Added SSRF/private-IP protection on subscription URLs using SanitizePublicHTTPURL - Improved update interval UX (hours + minutes) - Auto-fetch on first subscription creation - Added detailed comments on tag stability strategy and balancer implications when servers are added/removed/rotated - Updated migrationModels() for CLI migrate-db support * fix: resolve frontend lint/type errors and Go build break Frontend (eslint + tsc clean): - Destructure subscriptionOutboundTags prop in RoutingTab and BalancersTab. It was declared in the interface and used in useMemo but never destructured, so it resolved as an unresolved global (react-hooks warning + tsc "Cannot find name"). The prop is passed by XrayPage, so the feature was silently inert. - OutboundsTab: remove unused useEffect import, add an OutboundSub type to replace any[] state and the any/any table render signature, type the subscriptionOutbounds cast, and replace unused catch (e) bindings with parameter-less catch. Also type HttpUtil.post as OutboundSub so r.obj?.id type-checks. Backend (go build clean): - outbound_subscription_job: websocket.MessageTypeXray is undefined; use the existing MessageTypeOutbounds since the job refreshes outbound subscriptions. * fix(xray): make outbound subscription creation work end-to-end - Correct API paths from /panel/xray/outbound-subs to /panel/api/xray/outbound-subs. The controller is mounted under /panel/api, so the old paths hit the SPA page route (GET-only) and 404'd on POST. - Send the create-subscription body as a plain object instead of URLSearchParams. The axios request interceptor serializes bodies with qs.stringify, which can't read URLSearchParams' internal storage and produced an empty body, so the backend rejected it with "subscription URL is required". - Use message.useMessage() + context holder instead of the static antd message API (resolves the "Static function can not consume context" warning), matching XrayPage's pattern. - Migrate the subscriptions Drawer to antd v6 props: width -> size, destroyOnClose -> destroyOnHidden, and Space direction -> orientation. * feat(xray): show traffic/test for subscription outbounds; harden + test the feature Display (the reported issue): - Replace the flat read-only pills with a proper read-only table (desktop) and cards (mobile) in a new SubscriptionOutbounds component, showing Address, Protocol, Traffic (matched by tag — already collected by Xray), and a Test button with Latency. No edit/delete/move (read-only). - Test subscription outbounds via the existing /testOutbound endpoint, with results keyed by tag (subscriptionTestStates + testSubscriptionOutbound in useXraySetting, wired through XrayPage). Generalize isTesting/testResult to a string|number key so the same helpers serve index- and tag-keyed states. i18n: - Replace all hardcoded English subscription strings with t() calls and add pages.xray.outboundSub.* keys to en-US.json (other locales fall back). Backend hardening + tests: - xray.go: drop the tautological `subSvc != nil` check. - outbound_subscription: re-validate every redirect hop against private/ internal addresses (CheckRedirect) and cap the redirect chain, closing an SSRF gap where only the initial host was checked. - Extract assignStableTags as a pure function and add unit tests for tag stability and SSRF rejection (the feature previously had no tests). Misc: - gofmt util/link/outbound.go (it was not gofmt-clean). * fix(xray): make outbound-subs feature pass CI (test compile, route docs, openapi) - outbound_test.go: remove unused `inner`/`lines` variables that broke the `util/link` test build (declared and not used). - Document the 7 outbound-subscription routes in endpoints.ts (list, create, update, delete, del alias, refresh, parse) so TestAPIRoutesDocumented passes. - Regenerate frontend/public/openapi.json (npm run gen) to include the new endpoints, satisfying the codegen freshness check. * feat(xray): per-subscription allow-private, gap-filled tags, UI tweaks, delete refresh Backend: - Add a per-subscription AllowPrivate flag (default off). Create/Update/refresh and the redirect check sanitize the URL with it, so localhost/LAN sources work only when explicitly opted in; the SSRF guard still blocks private targets by default. Controller reads the allowPrivate form field on create/update/parse. - Default outbound tag prefix now uses the smallest free "subN-" number instead of the auto-increment id, so deleting a subscription frees its number for reuse (a fresh start gives sub1) while staying stable per subscription. Extracted a pure defaultPrefixNumber() with unit tests. - deleteOutboundSub now signals SetToNeedRestart so xray drops the outbounds. Frontend: - "Allow private address" toggle in the add form (sends allowPrivate). - Delete now refreshes the xray view immediately (no manual page reload). - Subscriptions manager opens as a centered Modal instead of a right-side Drawer. - Move Outbounds to a top-level sidebar item under Nodes (out of Xray Configs). - Collapse WARP/NordVPN into a "more" dropdown. - Document the allowPrivate param in endpoints.ts. * i18n(xray): translate outbound-subscription UI into all locales - Translate the pages.xray.outboundSub.* strings (and allowPrivate label/hint) into all 12 non-English locales, matching each file's existing terminology. - Remove the unused outboundSub.add ("Add subscription") key from every locale. * feat(xray): subscription manager — edit, reorder/priority, status, preview, refresh-all Backend: - Per-subscription Priority + Prepend: subscriptions are ordered by Priority and placed before (Prepend) or after the manual template outbounds in the merge, so a subscription server can become the default. New Move(up/down) endpoint re-normalizes priorities; merge split into prepend/template/append. - List now returns a derived OutboundCount and orders by priority, and strips the heavy LastFetchedOutbounds/LinkIdentities blobs from the list payload. - Create/Update accept the prepend flag; new subs append at the end of priority. Frontend (Outbound Subscriptions modal): - Edit existing subscriptions (reuses the form + Update endpoint). - Inline enable/disable Switch, Status column (OK / error tooltip), Outbounds count column, per-row refresh spinner, "Refresh all" button. - Reorder (move up/down) controls + a "Before manual outbounds" toggle. - Preview button: fetch+parse a URL via /parse without saving. - Document the move route + prepend param in endpoints.ts; regenerate openapi.json. * i18n(xray): translate new subscription-manager strings into all locales Add the prepend/prependHint, preview/previewEmpty, refreshAll, statusOk and toastUpdated keys to all 12 non-English locales, matching each file's terminology. --------- Co-authored-by: MHSanaei <ho3ein.sanaei@gmail.com>
2026-06-08 09:09:53 -07:00
g.POST("/outbound-subs/parse", a.parseOutboundSubURL) // preview without saving
2023-12-04 10:20:46 -08:00
}
// getXraySetting retrieves the Xray configuration template, inbound tags, and outbound test URL.
2023-12-04 10:20:46 -08:00
func (a *XraySettingController) getXraySetting(c *gin.Context) {
xraySetting, err := a.SettingService.GetXrayConfigTemplate()
if err != nil {
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
return
}
Fix blank Xray Settings page from wrapped xrayTemplateConfig (#4059) (#4069) `getXraySetting` builds its response as { "xraySetting": <db value>, "inboundTags": ..., "outboundTestUrl": ... } and embeds the raw DB value as the `xraySetting` field without checking whether the stored value already has that exact shape. The frontend pulls the textarea content from `result.xraySetting` and saves it back verbatim. If the DB ever ends up holding the response-shaped wrapper instead of a real xray config (older installs where this happened at least once, users who imported a copy-pasted response into the textarea, a botched migration, etc.), the next save nests another layer, the one after that nests a third, and the Vue-side JSON.parse of the resulting blob silently fails — the Xray Settings page goes blank. Fix both ends of the round-trip: * Add `service.UnwrapXrayTemplateConfig`. It peels off any number of `xraySetting`-keyed layers, leaving a real xray config behind. The check is conservative: if the outer object already contains any top-level xray key (`inbounds`, `outbounds`, `routing`, `api`, `dns`, `log`, `policy`, `stats`), it is returned unchanged, and there is a depth cap to avoid pathological inputs. * `SaveXraySetting` unwraps before validation so a round-tripped wrapper from an already-corrupted page can no longer re-poison the DB on save. * `getXraySetting` unwraps on read and, when it finds a wrapper, rewrites the DB with the corrected value. Existing broken installs heal themselves on the next visit to the page. Includes unit tests for the passthrough, single-wrap, multi-wrap, string-encoded-inner, and false-positive cases. Co-authored-by: pwnnex <eternxles@gmail.com>
2026-04-21 11:30:02 -07:00
// Older versions of this handler embedded the raw DB value as
// `xraySetting` in the response without checking if the value
// already had that wrapper shape. When the frontend saved it
// back through the textarea verbatim, the wrapper got persisted
// and every subsequent save nested another layer, which is what
// eventually produced the blank Xray Settings page in #4059.
// Strip any such wrapper here, and heal the DB if we found one so
// the next read is O(1) instead of climbing the same pile again.
if unwrapped := service.UnwrapXrayTemplateConfig(xraySetting); unwrapped != xraySetting {
if saveErr := a.XraySettingService.SaveXraySetting(unwrapped); saveErr == nil {
xraySetting = unwrapped
} else {
// Don't fail the read — just serve the unwrapped value
// and leave the DB healing for a later save.
xraySetting = unwrapped
}
}
2023-12-05 09:13:36 -08:00
inboundTags, err := a.InboundService.GetInboundTags()
if err != nil {
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
return
}
clientReverseTags, err := a.InboundService.GetClientReverseTags()
if err != nil {
clientReverseTags = "[]"
}
outboundTestUrl, _ := a.SettingService.GetXrayOutboundTestUrl()
if outboundTestUrl == "" {
outboundTestUrl = "https://www.google.com/generate_204"
}
xrayResponse := map[string]any{
"xraySetting": json.RawMessage(xraySetting),
"inboundTags": json.RawMessage(inboundTags),
"clientReverseTags": json.RawMessage(clientReverseTags),
"outboundTestUrl": outboundTestUrl,
"geodataSources": service.StandardGeodataSources(),
2026-02-09 13:56:21 -08:00
}
feat: add support for subscription-based outbounds with auto-update (#5037) * feat: add support for subscription-based outbounds with auto-update - New OutboundSubscription model (full support on both SQLite and PostgreSQL) - Go subscription link parser (vmess/vless/trojan/ss/hysteria2/wireguard) matching frontend behavior - Stable tag assignment across refreshes (designed for balancer + routing use) - Runtime merge of subscription outbounds into Xray config (additive only) - Full CRUD + manual refresh + preview API - Background auto-update job (per-subscription interval) - Frontend management UI in Outbounds tab (Subscriptions drawer) + tag integration in balancers/routing rules - Proper dual-database support including CLI migration path Review & hardening notes: - Fixed merge logic bug that could drop manual outbounds - Added SSRF/private-IP protection on subscription URLs using SanitizePublicHTTPURL - Improved update interval UX (hours + minutes) - Auto-fetch on first subscription creation - Added detailed comments on tag stability strategy and balancer implications when servers are added/removed/rotated - Updated migrationModels() for CLI migrate-db support * fix: resolve frontend lint/type errors and Go build break Frontend (eslint + tsc clean): - Destructure subscriptionOutboundTags prop in RoutingTab and BalancersTab. It was declared in the interface and used in useMemo but never destructured, so it resolved as an unresolved global (react-hooks warning + tsc "Cannot find name"). The prop is passed by XrayPage, so the feature was silently inert. - OutboundsTab: remove unused useEffect import, add an OutboundSub type to replace any[] state and the any/any table render signature, type the subscriptionOutbounds cast, and replace unused catch (e) bindings with parameter-less catch. Also type HttpUtil.post as OutboundSub so r.obj?.id type-checks. Backend (go build clean): - outbound_subscription_job: websocket.MessageTypeXray is undefined; use the existing MessageTypeOutbounds since the job refreshes outbound subscriptions. * fix(xray): make outbound subscription creation work end-to-end - Correct API paths from /panel/xray/outbound-subs to /panel/api/xray/outbound-subs. The controller is mounted under /panel/api, so the old paths hit the SPA page route (GET-only) and 404'd on POST. - Send the create-subscription body as a plain object instead of URLSearchParams. The axios request interceptor serializes bodies with qs.stringify, which can't read URLSearchParams' internal storage and produced an empty body, so the backend rejected it with "subscription URL is required". - Use message.useMessage() + context holder instead of the static antd message API (resolves the "Static function can not consume context" warning), matching XrayPage's pattern. - Migrate the subscriptions Drawer to antd v6 props: width -> size, destroyOnClose -> destroyOnHidden, and Space direction -> orientation. * feat(xray): show traffic/test for subscription outbounds; harden + test the feature Display (the reported issue): - Replace the flat read-only pills with a proper read-only table (desktop) and cards (mobile) in a new SubscriptionOutbounds component, showing Address, Protocol, Traffic (matched by tag — already collected by Xray), and a Test button with Latency. No edit/delete/move (read-only). - Test subscription outbounds via the existing /testOutbound endpoint, with results keyed by tag (subscriptionTestStates + testSubscriptionOutbound in useXraySetting, wired through XrayPage). Generalize isTesting/testResult to a string|number key so the same helpers serve index- and tag-keyed states. i18n: - Replace all hardcoded English subscription strings with t() calls and add pages.xray.outboundSub.* keys to en-US.json (other locales fall back). Backend hardening + tests: - xray.go: drop the tautological `subSvc != nil` check. - outbound_subscription: re-validate every redirect hop against private/ internal addresses (CheckRedirect) and cap the redirect chain, closing an SSRF gap where only the initial host was checked. - Extract assignStableTags as a pure function and add unit tests for tag stability and SSRF rejection (the feature previously had no tests). Misc: - gofmt util/link/outbound.go (it was not gofmt-clean). * fix(xray): make outbound-subs feature pass CI (test compile, route docs, openapi) - outbound_test.go: remove unused `inner`/`lines` variables that broke the `util/link` test build (declared and not used). - Document the 7 outbound-subscription routes in endpoints.ts (list, create, update, delete, del alias, refresh, parse) so TestAPIRoutesDocumented passes. - Regenerate frontend/public/openapi.json (npm run gen) to include the new endpoints, satisfying the codegen freshness check. * feat(xray): per-subscription allow-private, gap-filled tags, UI tweaks, delete refresh Backend: - Add a per-subscription AllowPrivate flag (default off). Create/Update/refresh and the redirect check sanitize the URL with it, so localhost/LAN sources work only when explicitly opted in; the SSRF guard still blocks private targets by default. Controller reads the allowPrivate form field on create/update/parse. - Default outbound tag prefix now uses the smallest free "subN-" number instead of the auto-increment id, so deleting a subscription frees its number for reuse (a fresh start gives sub1) while staying stable per subscription. Extracted a pure defaultPrefixNumber() with unit tests. - deleteOutboundSub now signals SetToNeedRestart so xray drops the outbounds. Frontend: - "Allow private address" toggle in the add form (sends allowPrivate). - Delete now refreshes the xray view immediately (no manual page reload). - Subscriptions manager opens as a centered Modal instead of a right-side Drawer. - Move Outbounds to a top-level sidebar item under Nodes (out of Xray Configs). - Collapse WARP/NordVPN into a "more" dropdown. - Document the allowPrivate param in endpoints.ts. * i18n(xray): translate outbound-subscription UI into all locales - Translate the pages.xray.outboundSub.* strings (and allowPrivate label/hint) into all 12 non-English locales, matching each file's existing terminology. - Remove the unused outboundSub.add ("Add subscription") key from every locale. * feat(xray): subscription manager — edit, reorder/priority, status, preview, refresh-all Backend: - Per-subscription Priority + Prepend: subscriptions are ordered by Priority and placed before (Prepend) or after the manual template outbounds in the merge, so a subscription server can become the default. New Move(up/down) endpoint re-normalizes priorities; merge split into prepend/template/append. - List now returns a derived OutboundCount and orders by priority, and strips the heavy LastFetchedOutbounds/LinkIdentities blobs from the list payload. - Create/Update accept the prepend flag; new subs append at the end of priority. Frontend (Outbound Subscriptions modal): - Edit existing subscriptions (reuses the form + Update endpoint). - Inline enable/disable Switch, Status column (OK / error tooltip), Outbounds count column, per-row refresh spinner, "Refresh all" button. - Reorder (move up/down) controls + a "Before manual outbounds" toggle. - Preview button: fetch+parse a URL via /parse without saving. - Document the move route + prepend param in endpoints.ts; regenerate openapi.json. * i18n(xray): translate new subscription-manager strings into all locales Add the prepend/prependHint, preview/previewEmpty, refreshAll, statusOk and toastUpdated keys to all 12 non-English locales, matching each file's terminology. --------- Co-authored-by: MHSanaei <ho3ein.sanaei@gmail.com>
2026-06-08 09:09:53 -07:00
// Surface subscription outbounds (and their tags) so the frontend can:
// - show them as read-only items in the Outbounds tab
// - let users pick them in balancers and routing rules
// These are not part of the editable template; they are injected at runtime.
if subObs, err := a.OutboundSubscriptionService.AllActiveOutbounds(); err == nil && len(subObs) > 0 {
xrayResponse["subscriptionOutbounds"] = subObs
}
if subTags, err := a.OutboundSubscriptionService.AllActiveOutboundTags(); err == nil && len(subTags) > 0 {
xrayResponse["subscriptionOutboundTags"] = subTags
}
2026-02-09 13:56:21 -08:00
result, err := json.Marshal(xrayResponse)
if err != nil {
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
return
}
jsonObj(c, string(result), nil)
2023-12-04 10:20:46 -08:00
}
// updateSetting updates the Xray configuration settings and applies them to
// the running core right away — through the gRPC API when only inbounds,
// outbounds or routing rules changed, with a process restart otherwise.
2023-12-04 10:20:46 -08:00
func (a *XraySettingController) updateSetting(c *gin.Context) {
xraySetting := c.PostForm("xraySetting")
if err := a.XraySettingService.SaveXraySetting(xraySetting); err != nil {
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
return
}
outboundTestUrl := c.PostForm("outboundTestUrl")
if outboundTestUrl == "" {
outboundTestUrl = "https://www.google.com/generate_204"
}
if err := a.SettingService.SetXrayOutboundTestUrl(outboundTestUrl); err != nil {
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
return
}
// Only reconcile a running core; a manually stopped xray stays stopped.
if a.XrayService.IsXrayRunning() {
if err := a.XrayService.RestartXray(false); err != nil {
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
return
}
}
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), nil)
2023-12-04 10:20:46 -08:00
}
2025-09-20 00:35:50 -07:00
// getDefaultXrayConfig retrieves the default Xray configuration.
2023-12-04 10:20:46 -08:00
func (a *XraySettingController) getDefaultXrayConfig(c *gin.Context) {
defaultJsonConfig, err := a.SettingService.GetDefaultXrayConfig()
if err != nil {
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
return
}
jsonObj(c, defaultJsonConfig, nil)
}
2023-12-10 03:57:39 -08:00
2025-09-20 00:35:50 -07:00
// getXrayResult retrieves the current Xray service result.
2023-12-10 03:57:39 -08:00
func (a *XraySettingController) getXrayResult(c *gin.Context) {
jsonObj(c, a.XrayService.GetXrayResult(), nil)
}
2025-09-20 00:35:50 -07:00
// warp handles Warp-related operations based on the action parameter.
func (a *XraySettingController) warp(c *gin.Context) {
action := c.Param("action")
var resp string
var err error
switch action {
case "data":
resp, err = a.WarpService.GetWarpData()
case "del":
err = a.WarpService.DelWarpData()
case "config":
resp, err = a.WarpService.GetWarpConfig()
case "reg":
skey := c.PostForm("privateKey")
pkey := c.PostForm("publicKey")
resp, err = a.WarpService.RegWarp(skey, pkey)
case "changeIp":
resp, err = a.WarpService.ChangeWarpIP()
if err == nil {
a.XrayService.SetToNeedRestart()
// Restart the auto-update clock so a scheduled rotation
// doesn't fire right after this manual one.
err = a.SettingService.SetWarpLastUpdate(time.Now().Unix())
}
case "license":
license := c.PostForm("license")
resp, err = a.WarpService.SetWarpLicense(license)
case "interval":
interval, convErr := strconv.Atoi(c.PostForm("interval"))
if convErr != nil || interval < 0 {
err = common.NewError("invalid warp update interval")
} else if err = a.SettingService.SetWarpUpdateInterval(interval); err == nil && interval > 0 {
// Count the interval from now rather than from epoch 0,
// otherwise the job would rotate on its next tick.
err = a.SettingService.SetWarpLastUpdate(time.Now().Unix())
}
}
jsonObj(c, resp, err)
}
// nord handles NordVPN-related operations based on the action parameter.
func (a *XraySettingController) nord(c *gin.Context) {
action := c.Param("action")
var resp string
var err error
switch action {
case "countries":
resp, err = a.NordService.GetCountries()
case "servers":
countryId := c.PostForm("countryId")
resp, err = a.NordService.GetServers(countryId)
case "reg":
token := c.PostForm("token")
resp, err = a.NordService.GetCredentials(token)
case "setKey":
key := c.PostForm("key")
resp, err = a.NordService.SetKey(key)
case "data":
resp, err = a.NordService.GetNordData()
case "del":
err = a.NordService.DelNordData()
}
jsonObj(c, resp, err)
}
func (a *XraySettingController) pia(c *gin.Context) {
action := c.Param("action")
var resp any
var err error
switch action {
case "countries":
resp, err = a.PiaService.GetCountries()
case "servers":
resp, err = a.PiaService.GetServers(c.PostForm("countryCode"))
case "reg":
resp, err = a.PiaService.Login(c.PostForm("username"), c.PostForm("password"))
case "data":
resp, err = a.PiaService.GetPiaData()
case "del":
err = a.PiaService.DelPiaData()
case "addKey":
resp, err = a.PiaService.AddKey(c.PostForm("hostname"))
default:
jsonMsg(c, "unknown action", common.NewError("unknown action"))
return
}
if err != nil {
var pe *piaprotocol.Error
if errors.As(err, &pe) && pe != nil {
jsonObj(c, nil, common.NewError(pe.Message))
return
}
jsonObj(c, nil, err)
return
}
jsonObj(c, resp, nil)
}
2025-09-20 00:35:50 -07:00
// getOutboundsTraffic retrieves the traffic statistics for outbounds.
func (a *XraySettingController) getOutboundsTraffic(c *gin.Context) {
outboundsTraffic, err := a.OutboundService.GetOutboundsTraffic()
if err != nil {
2025-05-08 20:46:29 -07:00
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getOutboundTrafficError"), err)
return
}
jsonObj(c, outboundsTraffic, nil)
}
2024-02-06 23:55:31 -08:00
2025-09-20 00:35:50 -07:00
// resetOutboundsTraffic resets the traffic statistics for the specified outbound tag.
2024-02-06 23:55:31 -08:00
func (a *XraySettingController) resetOutboundsTraffic(c *gin.Context) {
tag := c.PostForm("tag")
err := a.OutboundService.ResetOutboundTraffic(tag)
if err != nil {
2025-05-08 20:46:29 -07:00
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.resetOutboundTrafficError"), err)
2024-02-06 23:55:31 -08:00
return
}
jsonObj(c, "", nil)
}
// testOutbound tests an outbound configuration and returns the delay/response time.
// Optional form "allOutbounds": JSON array of all outbounds; used to resolve sockopt.dialerProxy dependencies.
// Optional form "mode": "tcp" for a fast dial-only probe, "real" for the cold
// full-request delay, anything else (default) for a full HTTP probe through a temp xray instance.
func (a *XraySettingController) testOutbound(c *gin.Context) {
outboundJSON := c.PostForm("outbound")
allOutboundsJSON := c.PostForm("allOutbounds")
feat(xray/outbounds): TCP probe mode + Test All + timing breakdown - service.TestOutbound now dispatches on `mode`: - "tcp": parallel net.DialTimeout to every server/peer endpoint (vmess/vless/trojan/ss/socks/http/wireguard). No xray spin-up, no semaphore — safe to run concurrently across outbounds. - "http" (default): existing temp-xray + SOCKS path, now with an httptrace.ClientTrace breakdown (DNS / Connect / TLS / TTFB) alongside the total delay and status code. - testSemaphore renamed to httpTestSemaphore — only HTTP probes serialise, TCP runs free. - TestOutboundResult carries the per-mode extras: timing fields for HTTP, per-endpoint dial list for TCP, plus a `mode` echo. - Controller reads `mode` from the form and passes it through. - useXraySetting: testOutbound accepts mode (default "tcp"); new testAllOutbounds(mode) runs a worker pool (concurrency 8 for TCP, 1 for HTTP) and skips blackhole / loopback / blocked outbounds — also skips freedom / dns under TCP since they have no endpoint. - OutboundsTab: TCP/HTTP radio toggle and a Test All button land in the toolbar; the per-row ⚡ now uses the selected mode. Results surface in a popover with the full timing breakdown plus the endpoint list for TCP probes. Latency header replaces the duplicate "check" column title. Practical effect: testing ten outbounds in TCP mode drops from ~50–100s (serial HTTP) to ~1–2s (parallel dial × 8). HTTP mode stays as the authoritative probe and now shows where the latency actually lives.
2026-05-10 19:17:23 -07:00
mode := c.PostForm("mode")
if outboundJSON == "" {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), common.NewError("outbound parameter is required"))
return
}
2026-02-09 13:56:21 -08:00
// Load the test URL from server settings to prevent SSRF via user-controlled URLs
testURL, _ := a.SettingService.GetXrayOutboundTestUrl()
Security hardening: sessions, SSRF, CSP nonce, CSRF logout, trusted proxies (#4275) * refactor(session): store user ID in session instead of full struct Replaces storing the full User object in the session cookie with just the user ID. GetLoginUser now re-fetches the user from the database on every request so credential/permission changes take effect immediately without requiring a re-login. Includes a backward-compatible migration path for existing sessions that still carry the old struct payload. * feat(auth): block panel with default admin/admin credentials and guide credential change checkLogin middleware now detects default admin/admin credentials and redirects every panel route to /panel/settings until they are changed. The settings page auto-opens the Authentication tab, shows a non-dismissible error banner, and lists 'Default credentials' first in the security checklist. Login response includes mustChangeCredentials so the login page can redirect directly. Logout is now POST-only. Password must be at least 10 characters and cannot be admin/admin. * feat(settings): redact secrets in AllSettingView and add TrustedProxyCIDRs Introduces AllSettingView which strips tgBotToken, twoFactorToken, ldapPassword, apiToken and warp/nord secrets before sending them to the browser, replacing them with boolean hasFoo presence flags. A new /panel/setting/secret endpoint allows updating individual secrets by key. Secrets that arrive blank on a save are preserved from the DB rather than overwritten. Adds TrustedProxyCIDRs as a configurable setting (defaults to localhost CIDRs). URL fields are validated before save. * fix(security): SSRF prevention, trusted-proxy header gating, CSP nonce, HTTP timeouts Adds SanitizeHTTPURL / SanitizePublicHTTPURL to reject private-range and loopback targets before any outbound HTTP request (node probe, xray download, outbound test, external traffic inform, tgbot API server, panel updater). Forwarded headers (X-Real-IP, X-Forwarded-For, X-Forwarded-Host) are now only trusted when the direct connection arrives from a CIDR in TrustedProxyCIDRs. CSP policy is tightened with a per-request nonce. HTTP server gains read/write/idle timeouts. Panel updater downloads the script to a temp file instead of piping curl into shell. Xray archive download adds a size cap and response-code check. backuptotgbot is changed from GET to POST. * feat(nodes): add allow-private-address toggle per node Adds AllowPrivateAddress to the Node model (DB default false). When enabled it bypasses the SSRF private-range check for that node's probe URL, allowing nodes hosted on RFC-1918 or loopback addresses (e.g. a private VPN or LAN setup). * chore: frontend UX improvements, CI pipeline, and dev tooling - AppSidebar: logout via POST /logout instead of navigating to GET - InboundList: persist filter state (search, protocol, node) to localStorage across page reloads; add protocol and node filter dropdowns - IndexPage: add health status strip (Xray, CPU, Memory, Update) with quick-action buttons - dependabot: weekly go mod and npm update schedule - ci.yml: add GitHub Actions workflow for build and vet - .nvmrc: pin Node 22 for local development - frontend: bump package.json and package-lock.json - SubPage, DnsPresetsModal, api-docs: minor fixes * fix(ci): stub web/dist before go list to satisfy go:embed at compile time * chore(ui): remove health-strip bar from dashboard top * Revert "feat(auth): block panel with default admin/admin credentials and guide credential change" This reverts commit 56ce6073ce09f08147f989858e0e88b3a4359546. * fix(auth): make logout POST+CSRF and propagate session loss to other tabs - Switch /logout from GET to POST with CSRFMiddleware so it matches the SPA's existing HttpUtil.post('/logout') call (previously 404'd silently) and blocks GET-based logout via image tags or link prefetchers. Handler now returns JSON; the SPA already navigates client-side. - Return 401 (instead of 404) from /panel/api/* when the caller is a browser XHR (X-Requested-With: XMLHttpRequest) so the axios interceptor redirects to the login page on logout-in-another-tab, cookie expiry, and server restart. Anonymous callers still get 404 to keep endpoints hidden from casual scanners. - One-shot the 401 redirect in axios-init.js and hang the rejected promise so queued polls don't stack reloads or surface error toasts while the browser is navigating away. - Add the CSP nonce to the runtime-injected <script> in dist.go so the panel loads under the existing script-src 'nonce-...' policy. - Update api-docs endpoints.js: GET /logout doc entry was missing. * fix(settings): POST /logout after credential change * fix(auth): invalidate other sessions when credentials change When the admin changes username/password from one machine, sessions on every other machine kept working until they manually logged out because session storage is a signed client-side cookie — there is no server-side session list to revoke. Add a per-user LoginEpoch counter stamped into the session at login and re-verified on every authenticated request. UpdateUser and UpdateFirstUser bump the epoch (UpdateUser via gorm.Expr so a single update statement is atomic), so any cookie issued before the change no longer matches the user's current epoch and GetLoginUser returns nil — the SPA's 401 interceptor then redirects to the login page. Backward compatible: the column defaults to 0 and missing cookie values are treated as 0, so sessions issued before this change remain valid until the first credential update. --------- Co-authored-by: Sanaei <ho3ein.sanaei@gmail.com>
2026-05-13 03:52:52 -07:00
testURL, err := service.SanitizePublicHTTPURL(testURL, false)
if err != nil {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
return
}
2026-02-09 13:56:21 -08:00
feat(xray/outbounds): TCP probe mode + Test All + timing breakdown - service.TestOutbound now dispatches on `mode`: - "tcp": parallel net.DialTimeout to every server/peer endpoint (vmess/vless/trojan/ss/socks/http/wireguard). No xray spin-up, no semaphore — safe to run concurrently across outbounds. - "http" (default): existing temp-xray + SOCKS path, now with an httptrace.ClientTrace breakdown (DNS / Connect / TLS / TTFB) alongside the total delay and status code. - testSemaphore renamed to httpTestSemaphore — only HTTP probes serialise, TCP runs free. - TestOutboundResult carries the per-mode extras: timing fields for HTTP, per-endpoint dial list for TCP, plus a `mode` echo. - Controller reads `mode` from the form and passes it through. - useXraySetting: testOutbound accepts mode (default "tcp"); new testAllOutbounds(mode) runs a worker pool (concurrency 8 for TCP, 1 for HTTP) and skips blackhole / loopback / blocked outbounds — also skips freedom / dns under TCP since they have no endpoint. - OutboundsTab: TCP/HTTP radio toggle and a Test All button land in the toolbar; the per-row ⚡ now uses the selected mode. Results surface in a popover with the full timing breakdown plus the endpoint list for TCP probes. Latency header replaces the duplicate "check" column title. Practical effect: testing ten outbounds in TCP mode drops from ~50–100s (serial HTTP) to ~1–2s (parallel dial × 8). HTTP mode stays as the authoritative probe and now shows where the latency actually lives.
2026-05-10 19:17:23 -07:00
result, err := a.OutboundService.TestOutbound(outboundJSON, testURL, allOutboundsJSON, mode)
if err != nil {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
return
}
jsonObj(c, result, nil)
}
feat: add support for subscription-based outbounds with auto-update (#5037) * feat: add support for subscription-based outbounds with auto-update - New OutboundSubscription model (full support on both SQLite and PostgreSQL) - Go subscription link parser (vmess/vless/trojan/ss/hysteria2/wireguard) matching frontend behavior - Stable tag assignment across refreshes (designed for balancer + routing use) - Runtime merge of subscription outbounds into Xray config (additive only) - Full CRUD + manual refresh + preview API - Background auto-update job (per-subscription interval) - Frontend management UI in Outbounds tab (Subscriptions drawer) + tag integration in balancers/routing rules - Proper dual-database support including CLI migration path Review & hardening notes: - Fixed merge logic bug that could drop manual outbounds - Added SSRF/private-IP protection on subscription URLs using SanitizePublicHTTPURL - Improved update interval UX (hours + minutes) - Auto-fetch on first subscription creation - Added detailed comments on tag stability strategy and balancer implications when servers are added/removed/rotated - Updated migrationModels() for CLI migrate-db support * fix: resolve frontend lint/type errors and Go build break Frontend (eslint + tsc clean): - Destructure subscriptionOutboundTags prop in RoutingTab and BalancersTab. It was declared in the interface and used in useMemo but never destructured, so it resolved as an unresolved global (react-hooks warning + tsc "Cannot find name"). The prop is passed by XrayPage, so the feature was silently inert. - OutboundsTab: remove unused useEffect import, add an OutboundSub type to replace any[] state and the any/any table render signature, type the subscriptionOutbounds cast, and replace unused catch (e) bindings with parameter-less catch. Also type HttpUtil.post as OutboundSub so r.obj?.id type-checks. Backend (go build clean): - outbound_subscription_job: websocket.MessageTypeXray is undefined; use the existing MessageTypeOutbounds since the job refreshes outbound subscriptions. * fix(xray): make outbound subscription creation work end-to-end - Correct API paths from /panel/xray/outbound-subs to /panel/api/xray/outbound-subs. The controller is mounted under /panel/api, so the old paths hit the SPA page route (GET-only) and 404'd on POST. - Send the create-subscription body as a plain object instead of URLSearchParams. The axios request interceptor serializes bodies with qs.stringify, which can't read URLSearchParams' internal storage and produced an empty body, so the backend rejected it with "subscription URL is required". - Use message.useMessage() + context holder instead of the static antd message API (resolves the "Static function can not consume context" warning), matching XrayPage's pattern. - Migrate the subscriptions Drawer to antd v6 props: width -> size, destroyOnClose -> destroyOnHidden, and Space direction -> orientation. * feat(xray): show traffic/test for subscription outbounds; harden + test the feature Display (the reported issue): - Replace the flat read-only pills with a proper read-only table (desktop) and cards (mobile) in a new SubscriptionOutbounds component, showing Address, Protocol, Traffic (matched by tag — already collected by Xray), and a Test button with Latency. No edit/delete/move (read-only). - Test subscription outbounds via the existing /testOutbound endpoint, with results keyed by tag (subscriptionTestStates + testSubscriptionOutbound in useXraySetting, wired through XrayPage). Generalize isTesting/testResult to a string|number key so the same helpers serve index- and tag-keyed states. i18n: - Replace all hardcoded English subscription strings with t() calls and add pages.xray.outboundSub.* keys to en-US.json (other locales fall back). Backend hardening + tests: - xray.go: drop the tautological `subSvc != nil` check. - outbound_subscription: re-validate every redirect hop against private/ internal addresses (CheckRedirect) and cap the redirect chain, closing an SSRF gap where only the initial host was checked. - Extract assignStableTags as a pure function and add unit tests for tag stability and SSRF rejection (the feature previously had no tests). Misc: - gofmt util/link/outbound.go (it was not gofmt-clean). * fix(xray): make outbound-subs feature pass CI (test compile, route docs, openapi) - outbound_test.go: remove unused `inner`/`lines` variables that broke the `util/link` test build (declared and not used). - Document the 7 outbound-subscription routes in endpoints.ts (list, create, update, delete, del alias, refresh, parse) so TestAPIRoutesDocumented passes. - Regenerate frontend/public/openapi.json (npm run gen) to include the new endpoints, satisfying the codegen freshness check. * feat(xray): per-subscription allow-private, gap-filled tags, UI tweaks, delete refresh Backend: - Add a per-subscription AllowPrivate flag (default off). Create/Update/refresh and the redirect check sanitize the URL with it, so localhost/LAN sources work only when explicitly opted in; the SSRF guard still blocks private targets by default. Controller reads the allowPrivate form field on create/update/parse. - Default outbound tag prefix now uses the smallest free "subN-" number instead of the auto-increment id, so deleting a subscription frees its number for reuse (a fresh start gives sub1) while staying stable per subscription. Extracted a pure defaultPrefixNumber() with unit tests. - deleteOutboundSub now signals SetToNeedRestart so xray drops the outbounds. Frontend: - "Allow private address" toggle in the add form (sends allowPrivate). - Delete now refreshes the xray view immediately (no manual page reload). - Subscriptions manager opens as a centered Modal instead of a right-side Drawer. - Move Outbounds to a top-level sidebar item under Nodes (out of Xray Configs). - Collapse WARP/NordVPN into a "more" dropdown. - Document the allowPrivate param in endpoints.ts. * i18n(xray): translate outbound-subscription UI into all locales - Translate the pages.xray.outboundSub.* strings (and allowPrivate label/hint) into all 12 non-English locales, matching each file's existing terminology. - Remove the unused outboundSub.add ("Add subscription") key from every locale. * feat(xray): subscription manager — edit, reorder/priority, status, preview, refresh-all Backend: - Per-subscription Priority + Prepend: subscriptions are ordered by Priority and placed before (Prepend) or after the manual template outbounds in the merge, so a subscription server can become the default. New Move(up/down) endpoint re-normalizes priorities; merge split into prepend/template/append. - List now returns a derived OutboundCount and orders by priority, and strips the heavy LastFetchedOutbounds/LinkIdentities blobs from the list payload. - Create/Update accept the prepend flag; new subs append at the end of priority. Frontend (Outbound Subscriptions modal): - Edit existing subscriptions (reuses the form + Update endpoint). - Inline enable/disable Switch, Status column (OK / error tooltip), Outbounds count column, per-row refresh spinner, "Refresh all" button. - Reorder (move up/down) controls + a "Before manual outbounds" toggle. - Preview button: fetch+parse a URL via /parse without saving. - Document the move route + prepend param in endpoints.ts; regenerate openapi.json. * i18n(xray): translate new subscription-manager strings into all locales Add the prepend/prependHint, preview/previewEmpty, refreshAll, statusOk and toastUpdated keys to all 12 non-English locales, matching each file's terminology. --------- Co-authored-by: MHSanaei <ho3ein.sanaei@gmail.com>
2026-06-08 09:09:53 -07:00
// testOutbounds tests a batch of outbound configurations through one shared
// temp xray instance and returns an array of results in input order.
// Form "outbounds": JSON array of outbound configs (required).
// Optional form "allOutbounds": JSON array of all outbounds; used to resolve sockopt.dialerProxy dependencies.
// Optional form "mode": "tcp" for fast dial-only probes, "real" for the cold
// full-request delay, anything else (default) for real HTTP requests routed through each outbound.
func (a *XraySettingController) testOutbounds(c *gin.Context) {
outboundsJSON := c.PostForm("outbounds")
allOutboundsJSON := c.PostForm("allOutbounds")
mode := c.PostForm("mode")
if outboundsJSON == "" {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), common.NewError("outbounds parameter is required"))
return
}
// Load the test URL from server settings to prevent SSRF via user-controlled URLs
testURL, _ := a.SettingService.GetXrayOutboundTestUrl()
testURL, err := service.SanitizePublicHTTPURL(testURL, false)
if err != nil {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
return
}
results, err := a.OutboundService.TestOutbounds(outboundsJSON, testURL, allOutboundsJSON, mode)
if err != nil {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
return
}
jsonObj(c, results, nil)
}
// balancerStatus reports the live state (override + strategy picks) of the
// balancer tags given as a comma-separated "tags" form field.
func (a *XraySettingController) balancerStatus(c *gin.Context) {
raw := c.PostForm("tags")
var tags []string
for tag := range strings.SplitSeq(raw, ",") {
if tag = strings.TrimSpace(tag); tag != "" {
tags = append(tags, tag)
}
}
statuses, err := a.XrayService.GetBalancersStatus(tags)
if err != nil {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
return
}
byTag := make(map[string]service.BalancerStatus, len(statuses))
for _, status := range statuses {
byTag[status.Tag] = status
}
jsonObj(c, byTag, nil)
}
// balancerOverride forces a balancer to a specific outbound tag; an empty
// "target" clears the override.
func (a *XraySettingController) balancerOverride(c *gin.Context) {
tag := c.PostForm("tag")
if tag == "" {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), common.NewError("tag is required"))
return
}
target := c.PostForm("target")
if err := a.XrayService.OverrideBalancer(tag, target); err != nil {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
return
}
jsonObj(c, "", nil)
}
// routeTest asks the running core which outbound it would route a synthetic
// connection to.
func (a *XraySettingController) routeTest(c *gin.Context) {
port := 0
if portStr := c.PostForm("port"); portStr != "" {
parsed, err := strconv.Atoi(portStr)
if err != nil || parsed < 0 || parsed > 65535 {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), common.NewError("invalid port"))
return
}
port = parsed
}
req := xray.RouteTestRequest{
InboundTag: c.PostForm("inboundTag"),
Domain: c.PostForm("domain"),
IP: c.PostForm("ip"),
Port: port,
Network: c.PostForm("network"),
Protocol: c.PostForm("protocol"),
Email: c.PostForm("email"),
}
if req.Domain == "" && req.IP == "" {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), common.NewError("domain or ip is required"))
return
}
result, err := a.XrayService.TestRoute(req)
if err != nil {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
return
}
jsonObj(c, result, nil)
}
feat(xray): browse geosite/geoip categories from routing rules (#6165) * feat(xray): browse geosite/geoip categories from routing rules Routing rules made you type category names from memory: nothing showed which categories a database actually contains, what is inside one, or whether a name resolves at all — a typo only surfaced when Xray refused the config. The panel now reads Xray's .dat databases itself and exposes them over four endpoints: databases in the asset folder, a database's categories, one page of a category's rules, and validation of the tokens already in a rule. The reader walks the protobuf wire format directly rather than decoding into Go structs, because a 10 MB geosite.dat holds well over a million domains and materialising them costs ~284 MB where streaming costs ~19 MB. Only the category index is cached, entry pages are scanned on demand, and scans are serialised, so twenty concurrent requests peak at 87 MB instead of 1 GB. A database's type is decided by its contents, not its file name, since custom .dat files are named freely. In the rule form, the source-IP, IP and domain fields gain a database button opening the browser: search over categories, a preview of what a category holds, and a multi-select that merges into the field. Plain domains, CIDRs and categories the panel does not know are left untouched; categories already present come back ticked, and unticking one removes it from the rule. * fix(xray): read geo databases through os.Root and match codes verbatim CodeQL flagged the database read as a path built from a user-supplied value, and it was right about the shape of it. The file name arrives in a request; resolve() rejects traversal and stats the file through an os.Root, but the read itself went through a joined path with os.ReadFile. That left the symlink defence incomplete: the stat could pass while the read followed a link planted — or swapped in — afterwards. Reads now go through the same root, so a request-supplied name never becomes a path this code resolves on its own, and the size limit is applied to the opened file rather than to a separate stat of it. Lookup no longer trims the category code either. It backs the routing-token validator, and the core matches codes verbatim: "geosite: cn" will not start Xray, so repairing that space here hid exactly the typo the validator exists to report. * fix(xray): address review findings on the geo category browser Asset folder. The browser read config.GetBinFolderPath() unconditionally, but the core honours a preset XRAY_LOCATION_ASSET and only falls back to the bin folder (ensureXrayAssetLocation). On an install pointing at a shared asset directory the panel listed an empty folder and reported perfectly valid geosite:/geoip: tokens as missing — the validator warning about a correct config. The directory is now resolved with the core's precedence. Paging. Serving one page read and rescanned the whole database, so walking category-ads-all re-read it per page. The index now records each category's byte range and a page reads only that record through the os.Root handle, with the current category's records held for the duration of a paging session. Profiling that also showed the real cost was not the read but the slice of payload pointers built per call — a category holds a hundred thousand of them — so records are now walked with a callback instead. Ten pages over category-ads-all: 239 MB allocated, now 4.3 MB. Cached failures. Any error from reading a file was latched under the file's size+mtime, so a transient ENOMEM or EMFILE marked a healthy database as damaged until it changed on disk. Only deterministic failures are cached. Wrong kind. A geoip: token typed into a domain field parsed as a plain domain and was waved through, though the core cannot resolve it as one. It is now reported, with its own reason and wording. Frontend. The category filter fed the query key on every keystroke, so each character triggered a request that re-scanned the database; it is debounced now. GeoTokenInput accepts and forwards a ref, so React Hook Form can focus these three fields on a validation error again. A failed validation shows that it failed instead of rendering the same empty state as "no issues". Also drops an unreachable branch in the token-count guard and corrects the categories endpoint docs, where limit is unbounded by default. --------- Co-authored-by: STRENCH0 <17428017+STRENCH0@users.noreply.github.com>
2026-08-15 08:12:59 -07:00
// maxGeodataTokens bounds one validation request; a routing rule listing more
// categories than this is not something the panel needs to answer for.
const maxGeodataTokens = 500
// geodataFiles lists the geo databases Xray resolves geosite:/geoip: tokens
// against, including ones that failed to parse.
func (a *XraySettingController) geodataFiles(c *gin.Context) {
files, err := a.GeodataService.Files()
if err != nil {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
return
}
jsonObj(c, files, nil)
}
// geodataCategories returns one page of a database's categories.
func (a *XraySettingController) geodataCategories(c *gin.Context) {
offset, limit := geodataPaging(c)
page, err := a.GeodataService.Categories(c.Query("file"), c.Query("q"), offset, limit)
if err != nil {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
return
}
jsonObj(c, page, nil)
}
// geodataEntries returns one page of the domains or CIDRs inside a category.
func (a *XraySettingController) geodataEntries(c *gin.Context) {
code := c.Query("code")
if code == "" {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), common.NewError("code is required"))
return
}
offset, limit := geodataPaging(c)
page, err := a.GeodataService.Entries(c.Query("file"), code, c.Query("q"), offset, limit)
if err != nil {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
return
}
jsonObj(c, page, nil)
}
// geodataValidate reports which routing tokens do not resolve against the
// databases on disk.
func (a *XraySettingController) geodataValidate(c *gin.Context) {
// Split with a bound rather than splitting first: a 10 MB body of commas
// would otherwise allocate millions of strings before the limit is checked.
tokens := strings.SplitN(c.PostForm("tokens"), ",", maxGeodataTokens+1)
if len(tokens) > maxGeodataTokens {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), common.NewErrorf("too many tokens: over %d", maxGeodataTokens))
return
}
jsonObj(c, a.GeodataService.Validate(c.PostForm("kind") == "ip", tokens), nil)
}
func geodataPaging(c *gin.Context) (int, int) {
offset, err := strconv.Atoi(c.Query("offset"))
if err != nil {
offset = 0
}
limit, err := strconv.Atoi(c.Query("limit"))
if err != nil {
limit = 0
}
return offset, limit
}
feat: add support for subscription-based outbounds with auto-update (#5037) * feat: add support for subscription-based outbounds with auto-update - New OutboundSubscription model (full support on both SQLite and PostgreSQL) - Go subscription link parser (vmess/vless/trojan/ss/hysteria2/wireguard) matching frontend behavior - Stable tag assignment across refreshes (designed for balancer + routing use) - Runtime merge of subscription outbounds into Xray config (additive only) - Full CRUD + manual refresh + preview API - Background auto-update job (per-subscription interval) - Frontend management UI in Outbounds tab (Subscriptions drawer) + tag integration in balancers/routing rules - Proper dual-database support including CLI migration path Review & hardening notes: - Fixed merge logic bug that could drop manual outbounds - Added SSRF/private-IP protection on subscription URLs using SanitizePublicHTTPURL - Improved update interval UX (hours + minutes) - Auto-fetch on first subscription creation - Added detailed comments on tag stability strategy and balancer implications when servers are added/removed/rotated - Updated migrationModels() for CLI migrate-db support * fix: resolve frontend lint/type errors and Go build break Frontend (eslint + tsc clean): - Destructure subscriptionOutboundTags prop in RoutingTab and BalancersTab. It was declared in the interface and used in useMemo but never destructured, so it resolved as an unresolved global (react-hooks warning + tsc "Cannot find name"). The prop is passed by XrayPage, so the feature was silently inert. - OutboundsTab: remove unused useEffect import, add an OutboundSub type to replace any[] state and the any/any table render signature, type the subscriptionOutbounds cast, and replace unused catch (e) bindings with parameter-less catch. Also type HttpUtil.post as OutboundSub so r.obj?.id type-checks. Backend (go build clean): - outbound_subscription_job: websocket.MessageTypeXray is undefined; use the existing MessageTypeOutbounds since the job refreshes outbound subscriptions. * fix(xray): make outbound subscription creation work end-to-end - Correct API paths from /panel/xray/outbound-subs to /panel/api/xray/outbound-subs. The controller is mounted under /panel/api, so the old paths hit the SPA page route (GET-only) and 404'd on POST. - Send the create-subscription body as a plain object instead of URLSearchParams. The axios request interceptor serializes bodies with qs.stringify, which can't read URLSearchParams' internal storage and produced an empty body, so the backend rejected it with "subscription URL is required". - Use message.useMessage() + context holder instead of the static antd message API (resolves the "Static function can not consume context" warning), matching XrayPage's pattern. - Migrate the subscriptions Drawer to antd v6 props: width -> size, destroyOnClose -> destroyOnHidden, and Space direction -> orientation. * feat(xray): show traffic/test for subscription outbounds; harden + test the feature Display (the reported issue): - Replace the flat read-only pills with a proper read-only table (desktop) and cards (mobile) in a new SubscriptionOutbounds component, showing Address, Protocol, Traffic (matched by tag — already collected by Xray), and a Test button with Latency. No edit/delete/move (read-only). - Test subscription outbounds via the existing /testOutbound endpoint, with results keyed by tag (subscriptionTestStates + testSubscriptionOutbound in useXraySetting, wired through XrayPage). Generalize isTesting/testResult to a string|number key so the same helpers serve index- and tag-keyed states. i18n: - Replace all hardcoded English subscription strings with t() calls and add pages.xray.outboundSub.* keys to en-US.json (other locales fall back). Backend hardening + tests: - xray.go: drop the tautological `subSvc != nil` check. - outbound_subscription: re-validate every redirect hop against private/ internal addresses (CheckRedirect) and cap the redirect chain, closing an SSRF gap where only the initial host was checked. - Extract assignStableTags as a pure function and add unit tests for tag stability and SSRF rejection (the feature previously had no tests). Misc: - gofmt util/link/outbound.go (it was not gofmt-clean). * fix(xray): make outbound-subs feature pass CI (test compile, route docs, openapi) - outbound_test.go: remove unused `inner`/`lines` variables that broke the `util/link` test build (declared and not used). - Document the 7 outbound-subscription routes in endpoints.ts (list, create, update, delete, del alias, refresh, parse) so TestAPIRoutesDocumented passes. - Regenerate frontend/public/openapi.json (npm run gen) to include the new endpoints, satisfying the codegen freshness check. * feat(xray): per-subscription allow-private, gap-filled tags, UI tweaks, delete refresh Backend: - Add a per-subscription AllowPrivate flag (default off). Create/Update/refresh and the redirect check sanitize the URL with it, so localhost/LAN sources work only when explicitly opted in; the SSRF guard still blocks private targets by default. Controller reads the allowPrivate form field on create/update/parse. - Default outbound tag prefix now uses the smallest free "subN-" number instead of the auto-increment id, so deleting a subscription frees its number for reuse (a fresh start gives sub1) while staying stable per subscription. Extracted a pure defaultPrefixNumber() with unit tests. - deleteOutboundSub now signals SetToNeedRestart so xray drops the outbounds. Frontend: - "Allow private address" toggle in the add form (sends allowPrivate). - Delete now refreshes the xray view immediately (no manual page reload). - Subscriptions manager opens as a centered Modal instead of a right-side Drawer. - Move Outbounds to a top-level sidebar item under Nodes (out of Xray Configs). - Collapse WARP/NordVPN into a "more" dropdown. - Document the allowPrivate param in endpoints.ts. * i18n(xray): translate outbound-subscription UI into all locales - Translate the pages.xray.outboundSub.* strings (and allowPrivate label/hint) into all 12 non-English locales, matching each file's existing terminology. - Remove the unused outboundSub.add ("Add subscription") key from every locale. * feat(xray): subscription manager — edit, reorder/priority, status, preview, refresh-all Backend: - Per-subscription Priority + Prepend: subscriptions are ordered by Priority and placed before (Prepend) or after the manual template outbounds in the merge, so a subscription server can become the default. New Move(up/down) endpoint re-normalizes priorities; merge split into prepend/template/append. - List now returns a derived OutboundCount and orders by priority, and strips the heavy LastFetchedOutbounds/LinkIdentities blobs from the list payload. - Create/Update accept the prepend flag; new subs append at the end of priority. Frontend (Outbound Subscriptions modal): - Edit existing subscriptions (reuses the form + Update endpoint). - Inline enable/disable Switch, Status column (OK / error tooltip), Outbounds count column, per-row refresh spinner, "Refresh all" button. - Reorder (move up/down) controls + a "Before manual outbounds" toggle. - Preview button: fetch+parse a URL via /parse without saving. - Document the move route + prepend param in endpoints.ts; regenerate openapi.json. * i18n(xray): translate new subscription-manager strings into all locales Add the prepend/prependHint, preview/previewEmpty, refreshAll, statusOk and toastUpdated keys to all 12 non-English locales, matching each file's terminology. --------- Co-authored-by: MHSanaei <ho3ein.sanaei@gmail.com>
2026-06-08 09:09:53 -07:00
// --- Outbound Subscription handlers ---
func (a *XraySettingController) listOutboundSubs(c *gin.Context) {
list, err := a.OutboundSubscriptionService.List()
if err != nil {
jsonMsg(c, "Failed to list outbound subscriptions", err)
return
}
jsonObj(c, list, nil)
}
func (a *XraySettingController) createOutboundSub(c *gin.Context) {
remark := c.PostForm("remark")
rawURL := c.PostForm("url")
prefix := c.PostForm("tagPrefix")
userAgent := c.PostForm("userAgent")
feat: add support for subscription-based outbounds with auto-update (#5037) * feat: add support for subscription-based outbounds with auto-update - New OutboundSubscription model (full support on both SQLite and PostgreSQL) - Go subscription link parser (vmess/vless/trojan/ss/hysteria2/wireguard) matching frontend behavior - Stable tag assignment across refreshes (designed for balancer + routing use) - Runtime merge of subscription outbounds into Xray config (additive only) - Full CRUD + manual refresh + preview API - Background auto-update job (per-subscription interval) - Frontend management UI in Outbounds tab (Subscriptions drawer) + tag integration in balancers/routing rules - Proper dual-database support including CLI migration path Review & hardening notes: - Fixed merge logic bug that could drop manual outbounds - Added SSRF/private-IP protection on subscription URLs using SanitizePublicHTTPURL - Improved update interval UX (hours + minutes) - Auto-fetch on first subscription creation - Added detailed comments on tag stability strategy and balancer implications when servers are added/removed/rotated - Updated migrationModels() for CLI migrate-db support * fix: resolve frontend lint/type errors and Go build break Frontend (eslint + tsc clean): - Destructure subscriptionOutboundTags prop in RoutingTab and BalancersTab. It was declared in the interface and used in useMemo but never destructured, so it resolved as an unresolved global (react-hooks warning + tsc "Cannot find name"). The prop is passed by XrayPage, so the feature was silently inert. - OutboundsTab: remove unused useEffect import, add an OutboundSub type to replace any[] state and the any/any table render signature, type the subscriptionOutbounds cast, and replace unused catch (e) bindings with parameter-less catch. Also type HttpUtil.post as OutboundSub so r.obj?.id type-checks. Backend (go build clean): - outbound_subscription_job: websocket.MessageTypeXray is undefined; use the existing MessageTypeOutbounds since the job refreshes outbound subscriptions. * fix(xray): make outbound subscription creation work end-to-end - Correct API paths from /panel/xray/outbound-subs to /panel/api/xray/outbound-subs. The controller is mounted under /panel/api, so the old paths hit the SPA page route (GET-only) and 404'd on POST. - Send the create-subscription body as a plain object instead of URLSearchParams. The axios request interceptor serializes bodies with qs.stringify, which can't read URLSearchParams' internal storage and produced an empty body, so the backend rejected it with "subscription URL is required". - Use message.useMessage() + context holder instead of the static antd message API (resolves the "Static function can not consume context" warning), matching XrayPage's pattern. - Migrate the subscriptions Drawer to antd v6 props: width -> size, destroyOnClose -> destroyOnHidden, and Space direction -> orientation. * feat(xray): show traffic/test for subscription outbounds; harden + test the feature Display (the reported issue): - Replace the flat read-only pills with a proper read-only table (desktop) and cards (mobile) in a new SubscriptionOutbounds component, showing Address, Protocol, Traffic (matched by tag — already collected by Xray), and a Test button with Latency. No edit/delete/move (read-only). - Test subscription outbounds via the existing /testOutbound endpoint, with results keyed by tag (subscriptionTestStates + testSubscriptionOutbound in useXraySetting, wired through XrayPage). Generalize isTesting/testResult to a string|number key so the same helpers serve index- and tag-keyed states. i18n: - Replace all hardcoded English subscription strings with t() calls and add pages.xray.outboundSub.* keys to en-US.json (other locales fall back). Backend hardening + tests: - xray.go: drop the tautological `subSvc != nil` check. - outbound_subscription: re-validate every redirect hop against private/ internal addresses (CheckRedirect) and cap the redirect chain, closing an SSRF gap where only the initial host was checked. - Extract assignStableTags as a pure function and add unit tests for tag stability and SSRF rejection (the feature previously had no tests). Misc: - gofmt util/link/outbound.go (it was not gofmt-clean). * fix(xray): make outbound-subs feature pass CI (test compile, route docs, openapi) - outbound_test.go: remove unused `inner`/`lines` variables that broke the `util/link` test build (declared and not used). - Document the 7 outbound-subscription routes in endpoints.ts (list, create, update, delete, del alias, refresh, parse) so TestAPIRoutesDocumented passes. - Regenerate frontend/public/openapi.json (npm run gen) to include the new endpoints, satisfying the codegen freshness check. * feat(xray): per-subscription allow-private, gap-filled tags, UI tweaks, delete refresh Backend: - Add a per-subscription AllowPrivate flag (default off). Create/Update/refresh and the redirect check sanitize the URL with it, so localhost/LAN sources work only when explicitly opted in; the SSRF guard still blocks private targets by default. Controller reads the allowPrivate form field on create/update/parse. - Default outbound tag prefix now uses the smallest free "subN-" number instead of the auto-increment id, so deleting a subscription frees its number for reuse (a fresh start gives sub1) while staying stable per subscription. Extracted a pure defaultPrefixNumber() with unit tests. - deleteOutboundSub now signals SetToNeedRestart so xray drops the outbounds. Frontend: - "Allow private address" toggle in the add form (sends allowPrivate). - Delete now refreshes the xray view immediately (no manual page reload). - Subscriptions manager opens as a centered Modal instead of a right-side Drawer. - Move Outbounds to a top-level sidebar item under Nodes (out of Xray Configs). - Collapse WARP/NordVPN into a "more" dropdown. - Document the allowPrivate param in endpoints.ts. * i18n(xray): translate outbound-subscription UI into all locales - Translate the pages.xray.outboundSub.* strings (and allowPrivate label/hint) into all 12 non-English locales, matching each file's existing terminology. - Remove the unused outboundSub.add ("Add subscription") key from every locale. * feat(xray): subscription manager — edit, reorder/priority, status, preview, refresh-all Backend: - Per-subscription Priority + Prepend: subscriptions are ordered by Priority and placed before (Prepend) or after the manual template outbounds in the merge, so a subscription server can become the default. New Move(up/down) endpoint re-normalizes priorities; merge split into prepend/template/append. - List now returns a derived OutboundCount and orders by priority, and strips the heavy LastFetchedOutbounds/LinkIdentities blobs from the list payload. - Create/Update accept the prepend flag; new subs append at the end of priority. Frontend (Outbound Subscriptions modal): - Edit existing subscriptions (reuses the form + Update endpoint). - Inline enable/disable Switch, Status column (OK / error tooltip), Outbounds count column, per-row refresh spinner, "Refresh all" button. - Reorder (move up/down) controls + a "Before manual outbounds" toggle. - Preview button: fetch+parse a URL via /parse without saving. - Document the move route + prepend param in endpoints.ts; regenerate openapi.json. * i18n(xray): translate new subscription-manager strings into all locales Add the prepend/prependHint, preview/previewEmpty, refreshAll, statusOk and toastUpdated keys to all 12 non-English locales, matching each file's terminology. --------- Co-authored-by: MHSanaei <ho3ein.sanaei@gmail.com>
2026-06-08 09:09:53 -07:00
enabled := c.PostForm("enabled") != "false"
allowPrivate := c.PostForm("allowPrivate") == "true"
Bug-label issue sweep: 16 fixes (#6083) * fix(xray): block private-range egress in default freedom finalRules (#6037) With domainStrategy AsIs the router never resolves domains, so a domain with a private A record (e.g. 127-0-0-1.nip.io) sails past the geoip:private routing block and freedom's allow-all finalRules let it reach loopback services such as the xray gRPC API and metrics listener. Prepend a block rule for geoip:private to the default template and add the FreedomFinalRulesPrivateEgressBlock seeder so existing installs still carrying the stock allow-only (or legacy private-only-allow) finalRules are upgraded in place; customized rules are left untouched. * fix(sub): version-gate unencrypted-outbound drops in outbound subscriptions (#6033) Commit d38c912d taught CheckXrayConfig to keep unencrypted vless/trojan outbounds when the running core predates the v26.7.11 rejection, but filterOutboundsRejectedByCore still consulted the embedded validator unconditionally, so outbound subscriptions kept silently dropping those outbounds even on downgraded cores. Apply the same shouldSkipLegacyUnencryptedOutboundRejection gate when filtering fetched subscription outbounds. * fix(xray): resolve geodata assets before building outbound configs (#5928) Saving routing or template settings validates each outbound through the embedded config loader, and a freedom outbound whose finalRules reference geoip:private opens geoip.dat during that build. Unlike ApplyRoutingConfig, ValidateOutboundConfig and AddOutbound never pointed the in-process loader at the bin folder, so xray-core resolved the file relative to the panel executable and saving failed with 'stat /usr/local/x-ui/geoip.dat: no such file or directory'. Call ensureXrayAssetLocation before both build paths. * fix(api): use a real i18n key in the client get handler (#5911) The client fetch endpoint localized its error prefix with the bare key 'get', which exists in no translation file, so every lookup of a deleted client's email logged 'message "get" not found in language ...' noise alongside the expected record-not-found warning. Reuse the same pages.inbounds.toasts.obtain key the sibling list handler uses. * fix(sub): carry host record Host header and path into Clash/JSON output (#5944) The raw-link path overrides the host/path share params from a Host record via applyEndpointHostPath, but the Clash and JSON renderers read the transport settings object, which applyHostStreamOverrides never touched — so a Host record's WebSocket Host header (and path) silently vanished from Clash/Mihomo and JSON subscriptions whenever the inbound's own ws settings left them empty. Inject hostHeader/path into the ws/httpupgrade/xhttp settings of the per-host stream, mirroring the raw-link override. * fix(metrics): accept Unicode outbound tags in the observatory (#5972) The observatory validator whitelisted ASCII word characters, so any outbound whose tag carries a flag emoji or other non-ASCII text was silently dropped from the metrics snapshot, delay history, and health notifications. The history store is an in-process map, so the strict charset bought nothing. Validate tags as non-empty, bounded, control-character-free UTF-8 instead, keeping spaces and emoji while still rejecting garbage input on the query path. * fix(database): default sqlite to WAL to stop background-job lock storms (#6057, #6068) With journal_mode=DELETE every write serializes the whole database and blocks readers, so under normal multi-job load (traffic sampling, node sync, mtproto reconcile) transactions regularly outwaited the 10s busy timeout and jobs failed with 'database is locked'. Move to WAL by default: readers no longer block writers and vice versa, which removes the observed contention while writer-writer access still serializes safely. The single-file-at-rest property is preserved where it matters — Checkpoint() now issues wal_checkpoint(TRUNCATE), so panel and Telegram backups read a complete main file, and sqlite folds the WAL back into the db on clean shutdown. XUI_DB_JOURNAL_MODE=DELETE restores the previous behavior for setups that copy the live file directly. * fix(database): strip finalmask.tcp from REALITY inbounds on upgrade (#6038) validateFinalMaskRealityCombo blocks saving finalmask.tcp together with REALITY because that combination crashes Xray-core 26.7.11 on the first connection (XTLS/Xray-core#6453), but it only runs on add/update. An inbound saved before the validator existed sailed through the upgrade untouched and took the core down at boot. Add the InboundRealityFinalmaskTcpStrip seeder: one-time scan that removes finalmask.tcp from REALITY inbounds (other finalmask transports survive), so upgraded panels start cleanly. * fix(xray): stop deleting hand-written direct routing rules on save (#6056) The DNS allow-rule sync recognized 'its' rules purely by shape (type=field, ip, port, outboundTag=direct, nothing else), so any manual rule of that shape — e.g. routing a LAN CIDR to a NAS port over direct — was silently stripped on every settings save. Mark managed rules with ruleTag=xui-dns-allow (round-tripped untouched by both xray-core and the Routing tab editor) and only strip rules that carry the tag. Untagged legacy managed rules are adopted when their exact ip-set/port matches a currently configured private DNS endpoint; anything else is left alone. A stale pre-tag managed rule whose DNS server was removed now lingers until deleted manually — the safe side of the trade against eating user rules. * fix(clients): resolve email lookups through client_inbounds after a move (#6059) GetClientInboundByEmail trusted the client_traffics.inbound_id pointer whenever that inbound still existed, but a client moved between inbounds leaves the row pointing at its old (still existing) inbound. The lookup then searched the wrong inbound's clients and failed with 'Client Not Found In Inbound For Email', which broke the Telegram bot's link and QR generation for moved clients. When the pointed-at inbound no longer carries the email, re-resolve through the authoritative client_inbounds link to the inbound that actually hosts the client. * fix(nodes): replicate inbound fallbacks to nodes (#5963) Fallbacks live in the inbound_fallbacks table and were only merged into settings by the master's local config builder; the runtime inbound pushed to nodes rebuilt settings without them, and the reconcile job additionally fingerprinted the raw DB row, so fallback edits neither reached nodes nor triggered a re-push. Inject settings.fallbacks in buildRuntimeInboundForAPI (mirroring the local builder, gated on inboundCanHostFallbacks) and make ReconcileNode push and fingerprint that same runtime-built payload, aligning the interactive and reconcile paths. * fix(database): survive PostgreSQL outages without a runaway restart loop (#6023) A PostgreSQL that was down or still starting made InitDB fail instantly; the process exited with a generic startup error and systemd restarted it every 5s forever, flooding the journal. Retry the initial postgres connection with backoff (~70s total) and log the real driver error on every attempt, and cap the systemd units with StartLimitIntervalSec/StartLimitBurst so a persistently unreachable database stops the unit instead of looping indefinitely. * fix(xray): force a full restart when REALITY stream settings change (#6010) A changed inbound is normally hot-swapped over gRPC as RemoveInbound + AddInbound, but xray-core does not reliably rebuild a REALITY listener's authenticator on a runtime re-add — key or shortId edits appeared applied yet clients kept authenticating against the old parameters until someone restarted the core manually, on nodes in particular. Treat any non-client change to an inbound that uses (or starts using) REALITY as not hot-appliable so the panel restarts the core instead. Client-only edits on REALITY inbounds keep flowing through the per-user AlterInbound path and still avoid restarts. * feat(sub): allow insecure TLS for outbound subscription fetches (#6067) An outbound subscription served over HTTPS with a self-signed or private-CA certificate could never be fetched: the fetch client had no TLS options, so refreshes died with 'x509: certificate signed by unknown authority' and there was nothing the admin could toggle. Add a per-subscription 'Allow insecure' switch (persisted as allow_insecure, default off) that sets InsecureSkipVerify on the fetch transport — including when the fetch is routed through the panel egress proxy. The SSRF-guarded dialer and redirect re-validation stay in force either way. * fix(reality): send PROXY protocol header in the target scanner when xver is set (#6082) The REALITY target scanner always probed with a plain TLS handshake, so a target fronted by an Nginx listener that requires the PROXY protocol (matching the inbound's xver>=1) reset the connection and the panel reported a false 'TLS handshake failed'. Thread the inbound's xver into the scan request and, when it is >=1, lead with the matching PROXY protocol header (v1 for xver 1, binary v2 for xver 2) built from the dialed connection's own address pair. Batch candidate scans against public sites are unaffected (xver 0). * fix(frontend): default sockopt fields when editing a stored inbound (#5956) Opening an existing inbound ran rawInboundToFormValues over the raw DB row, and only xhttpSettings was re-parsed through its Zod schema to fill defaults. A sockopt object saved before the TProxy control existed has no tproxy key, so the Select rendered blank; picking Off didn't help because the wire normalizer drops tproxy=off, recreating the missing key on the next edit. Re-parse streamSettings.sockopt through SockoptStreamSettingsSchema on load, mirroring the xhttpSettings handling, so absent keys (tproxy, tcpcongestion, …) get their schema defaults every time the form opens.
2026-07-23 06:34:42 -07:00
allowInsecure := c.PostForm("allowInsecure") == "true"
feat: add support for subscription-based outbounds with auto-update (#5037) * feat: add support for subscription-based outbounds with auto-update - New OutboundSubscription model (full support on both SQLite and PostgreSQL) - Go subscription link parser (vmess/vless/trojan/ss/hysteria2/wireguard) matching frontend behavior - Stable tag assignment across refreshes (designed for balancer + routing use) - Runtime merge of subscription outbounds into Xray config (additive only) - Full CRUD + manual refresh + preview API - Background auto-update job (per-subscription interval) - Frontend management UI in Outbounds tab (Subscriptions drawer) + tag integration in balancers/routing rules - Proper dual-database support including CLI migration path Review & hardening notes: - Fixed merge logic bug that could drop manual outbounds - Added SSRF/private-IP protection on subscription URLs using SanitizePublicHTTPURL - Improved update interval UX (hours + minutes) - Auto-fetch on first subscription creation - Added detailed comments on tag stability strategy and balancer implications when servers are added/removed/rotated - Updated migrationModels() for CLI migrate-db support * fix: resolve frontend lint/type errors and Go build break Frontend (eslint + tsc clean): - Destructure subscriptionOutboundTags prop in RoutingTab and BalancersTab. It was declared in the interface and used in useMemo but never destructured, so it resolved as an unresolved global (react-hooks warning + tsc "Cannot find name"). The prop is passed by XrayPage, so the feature was silently inert. - OutboundsTab: remove unused useEffect import, add an OutboundSub type to replace any[] state and the any/any table render signature, type the subscriptionOutbounds cast, and replace unused catch (e) bindings with parameter-less catch. Also type HttpUtil.post as OutboundSub so r.obj?.id type-checks. Backend (go build clean): - outbound_subscription_job: websocket.MessageTypeXray is undefined; use the existing MessageTypeOutbounds since the job refreshes outbound subscriptions. * fix(xray): make outbound subscription creation work end-to-end - Correct API paths from /panel/xray/outbound-subs to /panel/api/xray/outbound-subs. The controller is mounted under /panel/api, so the old paths hit the SPA page route (GET-only) and 404'd on POST. - Send the create-subscription body as a plain object instead of URLSearchParams. The axios request interceptor serializes bodies with qs.stringify, which can't read URLSearchParams' internal storage and produced an empty body, so the backend rejected it with "subscription URL is required". - Use message.useMessage() + context holder instead of the static antd message API (resolves the "Static function can not consume context" warning), matching XrayPage's pattern. - Migrate the subscriptions Drawer to antd v6 props: width -> size, destroyOnClose -> destroyOnHidden, and Space direction -> orientation. * feat(xray): show traffic/test for subscription outbounds; harden + test the feature Display (the reported issue): - Replace the flat read-only pills with a proper read-only table (desktop) and cards (mobile) in a new SubscriptionOutbounds component, showing Address, Protocol, Traffic (matched by tag — already collected by Xray), and a Test button with Latency. No edit/delete/move (read-only). - Test subscription outbounds via the existing /testOutbound endpoint, with results keyed by tag (subscriptionTestStates + testSubscriptionOutbound in useXraySetting, wired through XrayPage). Generalize isTesting/testResult to a string|number key so the same helpers serve index- and tag-keyed states. i18n: - Replace all hardcoded English subscription strings with t() calls and add pages.xray.outboundSub.* keys to en-US.json (other locales fall back). Backend hardening + tests: - xray.go: drop the tautological `subSvc != nil` check. - outbound_subscription: re-validate every redirect hop against private/ internal addresses (CheckRedirect) and cap the redirect chain, closing an SSRF gap where only the initial host was checked. - Extract assignStableTags as a pure function and add unit tests for tag stability and SSRF rejection (the feature previously had no tests). Misc: - gofmt util/link/outbound.go (it was not gofmt-clean). * fix(xray): make outbound-subs feature pass CI (test compile, route docs, openapi) - outbound_test.go: remove unused `inner`/`lines` variables that broke the `util/link` test build (declared and not used). - Document the 7 outbound-subscription routes in endpoints.ts (list, create, update, delete, del alias, refresh, parse) so TestAPIRoutesDocumented passes. - Regenerate frontend/public/openapi.json (npm run gen) to include the new endpoints, satisfying the codegen freshness check. * feat(xray): per-subscription allow-private, gap-filled tags, UI tweaks, delete refresh Backend: - Add a per-subscription AllowPrivate flag (default off). Create/Update/refresh and the redirect check sanitize the URL with it, so localhost/LAN sources work only when explicitly opted in; the SSRF guard still blocks private targets by default. Controller reads the allowPrivate form field on create/update/parse. - Default outbound tag prefix now uses the smallest free "subN-" number instead of the auto-increment id, so deleting a subscription frees its number for reuse (a fresh start gives sub1) while staying stable per subscription. Extracted a pure defaultPrefixNumber() with unit tests. - deleteOutboundSub now signals SetToNeedRestart so xray drops the outbounds. Frontend: - "Allow private address" toggle in the add form (sends allowPrivate). - Delete now refreshes the xray view immediately (no manual page reload). - Subscriptions manager opens as a centered Modal instead of a right-side Drawer. - Move Outbounds to a top-level sidebar item under Nodes (out of Xray Configs). - Collapse WARP/NordVPN into a "more" dropdown. - Document the allowPrivate param in endpoints.ts. * i18n(xray): translate outbound-subscription UI into all locales - Translate the pages.xray.outboundSub.* strings (and allowPrivate label/hint) into all 12 non-English locales, matching each file's existing terminology. - Remove the unused outboundSub.add ("Add subscription") key from every locale. * feat(xray): subscription manager — edit, reorder/priority, status, preview, refresh-all Backend: - Per-subscription Priority + Prepend: subscriptions are ordered by Priority and placed before (Prepend) or after the manual template outbounds in the merge, so a subscription server can become the default. New Move(up/down) endpoint re-normalizes priorities; merge split into prepend/template/append. - List now returns a derived OutboundCount and orders by priority, and strips the heavy LastFetchedOutbounds/LinkIdentities blobs from the list payload. - Create/Update accept the prepend flag; new subs append at the end of priority. Frontend (Outbound Subscriptions modal): - Edit existing subscriptions (reuses the form + Update endpoint). - Inline enable/disable Switch, Status column (OK / error tooltip), Outbounds count column, per-row refresh spinner, "Refresh all" button. - Reorder (move up/down) controls + a "Before manual outbounds" toggle. - Preview button: fetch+parse a URL via /parse without saving. - Document the move route + prepend param in endpoints.ts; regenerate openapi.json. * i18n(xray): translate new subscription-manager strings into all locales Add the prepend/prependHint, preview/previewEmpty, refreshAll, statusOk and toastUpdated keys to all 12 non-English locales, matching each file's terminology. --------- Co-authored-by: MHSanaei <ho3ein.sanaei@gmail.com>
2026-06-08 09:09:53 -07:00
prepend := c.PostForm("prepend") == "true"
intervalStr := c.PostForm("updateInterval")
interval := 600
if intervalStr != "" {
if v, err := parseIntSafe(intervalStr); err == nil && v > 0 {
interval = v
}
}
sub, err := a.OutboundSubscriptionService.Create(remark, rawURL, prefix, userAgent, enabled, interval, allowPrivate, prepend, allowInsecure)
feat: add support for subscription-based outbounds with auto-update (#5037) * feat: add support for subscription-based outbounds with auto-update - New OutboundSubscription model (full support on both SQLite and PostgreSQL) - Go subscription link parser (vmess/vless/trojan/ss/hysteria2/wireguard) matching frontend behavior - Stable tag assignment across refreshes (designed for balancer + routing use) - Runtime merge of subscription outbounds into Xray config (additive only) - Full CRUD + manual refresh + preview API - Background auto-update job (per-subscription interval) - Frontend management UI in Outbounds tab (Subscriptions drawer) + tag integration in balancers/routing rules - Proper dual-database support including CLI migration path Review & hardening notes: - Fixed merge logic bug that could drop manual outbounds - Added SSRF/private-IP protection on subscription URLs using SanitizePublicHTTPURL - Improved update interval UX (hours + minutes) - Auto-fetch on first subscription creation - Added detailed comments on tag stability strategy and balancer implications when servers are added/removed/rotated - Updated migrationModels() for CLI migrate-db support * fix: resolve frontend lint/type errors and Go build break Frontend (eslint + tsc clean): - Destructure subscriptionOutboundTags prop in RoutingTab and BalancersTab. It was declared in the interface and used in useMemo but never destructured, so it resolved as an unresolved global (react-hooks warning + tsc "Cannot find name"). The prop is passed by XrayPage, so the feature was silently inert. - OutboundsTab: remove unused useEffect import, add an OutboundSub type to replace any[] state and the any/any table render signature, type the subscriptionOutbounds cast, and replace unused catch (e) bindings with parameter-less catch. Also type HttpUtil.post as OutboundSub so r.obj?.id type-checks. Backend (go build clean): - outbound_subscription_job: websocket.MessageTypeXray is undefined; use the existing MessageTypeOutbounds since the job refreshes outbound subscriptions. * fix(xray): make outbound subscription creation work end-to-end - Correct API paths from /panel/xray/outbound-subs to /panel/api/xray/outbound-subs. The controller is mounted under /panel/api, so the old paths hit the SPA page route (GET-only) and 404'd on POST. - Send the create-subscription body as a plain object instead of URLSearchParams. The axios request interceptor serializes bodies with qs.stringify, which can't read URLSearchParams' internal storage and produced an empty body, so the backend rejected it with "subscription URL is required". - Use message.useMessage() + context holder instead of the static antd message API (resolves the "Static function can not consume context" warning), matching XrayPage's pattern. - Migrate the subscriptions Drawer to antd v6 props: width -> size, destroyOnClose -> destroyOnHidden, and Space direction -> orientation. * feat(xray): show traffic/test for subscription outbounds; harden + test the feature Display (the reported issue): - Replace the flat read-only pills with a proper read-only table (desktop) and cards (mobile) in a new SubscriptionOutbounds component, showing Address, Protocol, Traffic (matched by tag — already collected by Xray), and a Test button with Latency. No edit/delete/move (read-only). - Test subscription outbounds via the existing /testOutbound endpoint, with results keyed by tag (subscriptionTestStates + testSubscriptionOutbound in useXraySetting, wired through XrayPage). Generalize isTesting/testResult to a string|number key so the same helpers serve index- and tag-keyed states. i18n: - Replace all hardcoded English subscription strings with t() calls and add pages.xray.outboundSub.* keys to en-US.json (other locales fall back). Backend hardening + tests: - xray.go: drop the tautological `subSvc != nil` check. - outbound_subscription: re-validate every redirect hop against private/ internal addresses (CheckRedirect) and cap the redirect chain, closing an SSRF gap where only the initial host was checked. - Extract assignStableTags as a pure function and add unit tests for tag stability and SSRF rejection (the feature previously had no tests). Misc: - gofmt util/link/outbound.go (it was not gofmt-clean). * fix(xray): make outbound-subs feature pass CI (test compile, route docs, openapi) - outbound_test.go: remove unused `inner`/`lines` variables that broke the `util/link` test build (declared and not used). - Document the 7 outbound-subscription routes in endpoints.ts (list, create, update, delete, del alias, refresh, parse) so TestAPIRoutesDocumented passes. - Regenerate frontend/public/openapi.json (npm run gen) to include the new endpoints, satisfying the codegen freshness check. * feat(xray): per-subscription allow-private, gap-filled tags, UI tweaks, delete refresh Backend: - Add a per-subscription AllowPrivate flag (default off). Create/Update/refresh and the redirect check sanitize the URL with it, so localhost/LAN sources work only when explicitly opted in; the SSRF guard still blocks private targets by default. Controller reads the allowPrivate form field on create/update/parse. - Default outbound tag prefix now uses the smallest free "subN-" number instead of the auto-increment id, so deleting a subscription frees its number for reuse (a fresh start gives sub1) while staying stable per subscription. Extracted a pure defaultPrefixNumber() with unit tests. - deleteOutboundSub now signals SetToNeedRestart so xray drops the outbounds. Frontend: - "Allow private address" toggle in the add form (sends allowPrivate). - Delete now refreshes the xray view immediately (no manual page reload). - Subscriptions manager opens as a centered Modal instead of a right-side Drawer. - Move Outbounds to a top-level sidebar item under Nodes (out of Xray Configs). - Collapse WARP/NordVPN into a "more" dropdown. - Document the allowPrivate param in endpoints.ts. * i18n(xray): translate outbound-subscription UI into all locales - Translate the pages.xray.outboundSub.* strings (and allowPrivate label/hint) into all 12 non-English locales, matching each file's existing terminology. - Remove the unused outboundSub.add ("Add subscription") key from every locale. * feat(xray): subscription manager — edit, reorder/priority, status, preview, refresh-all Backend: - Per-subscription Priority + Prepend: subscriptions are ordered by Priority and placed before (Prepend) or after the manual template outbounds in the merge, so a subscription server can become the default. New Move(up/down) endpoint re-normalizes priorities; merge split into prepend/template/append. - List now returns a derived OutboundCount and orders by priority, and strips the heavy LastFetchedOutbounds/LinkIdentities blobs from the list payload. - Create/Update accept the prepend flag; new subs append at the end of priority. Frontend (Outbound Subscriptions modal): - Edit existing subscriptions (reuses the form + Update endpoint). - Inline enable/disable Switch, Status column (OK / error tooltip), Outbounds count column, per-row refresh spinner, "Refresh all" button. - Reorder (move up/down) controls + a "Before manual outbounds" toggle. - Preview button: fetch+parse a URL via /parse without saving. - Document the move route + prepend param in endpoints.ts; regenerate openapi.json. * i18n(xray): translate new subscription-manager strings into all locales Add the prepend/prependHint, preview/previewEmpty, refreshAll, statusOk and toastUpdated keys to all 12 non-English locales, matching each file's terminology. --------- Co-authored-by: MHSanaei <ho3ein.sanaei@gmail.com>
2026-06-08 09:09:53 -07:00
if err != nil {
jsonMsg(c, "Failed to create outbound subscription", err)
return
}
jsonObj(c, sub, nil)
}
func (a *XraySettingController) updateOutboundSub(c *gin.Context) {
id := c.Param("id")
var subID int
if _, err := fmt.Sscanf(id, "%d", &subID); err != nil {
jsonMsg(c, "Invalid id", err)
return
}
remark := c.PostForm("remark")
rawURL := c.PostForm("url")
prefix := c.PostForm("tagPrefix")
userAgent := c.PostForm("userAgent")
feat: add support for subscription-based outbounds with auto-update (#5037) * feat: add support for subscription-based outbounds with auto-update - New OutboundSubscription model (full support on both SQLite and PostgreSQL) - Go subscription link parser (vmess/vless/trojan/ss/hysteria2/wireguard) matching frontend behavior - Stable tag assignment across refreshes (designed for balancer + routing use) - Runtime merge of subscription outbounds into Xray config (additive only) - Full CRUD + manual refresh + preview API - Background auto-update job (per-subscription interval) - Frontend management UI in Outbounds tab (Subscriptions drawer) + tag integration in balancers/routing rules - Proper dual-database support including CLI migration path Review & hardening notes: - Fixed merge logic bug that could drop manual outbounds - Added SSRF/private-IP protection on subscription URLs using SanitizePublicHTTPURL - Improved update interval UX (hours + minutes) - Auto-fetch on first subscription creation - Added detailed comments on tag stability strategy and balancer implications when servers are added/removed/rotated - Updated migrationModels() for CLI migrate-db support * fix: resolve frontend lint/type errors and Go build break Frontend (eslint + tsc clean): - Destructure subscriptionOutboundTags prop in RoutingTab and BalancersTab. It was declared in the interface and used in useMemo but never destructured, so it resolved as an unresolved global (react-hooks warning + tsc "Cannot find name"). The prop is passed by XrayPage, so the feature was silently inert. - OutboundsTab: remove unused useEffect import, add an OutboundSub type to replace any[] state and the any/any table render signature, type the subscriptionOutbounds cast, and replace unused catch (e) bindings with parameter-less catch. Also type HttpUtil.post as OutboundSub so r.obj?.id type-checks. Backend (go build clean): - outbound_subscription_job: websocket.MessageTypeXray is undefined; use the existing MessageTypeOutbounds since the job refreshes outbound subscriptions. * fix(xray): make outbound subscription creation work end-to-end - Correct API paths from /panel/xray/outbound-subs to /panel/api/xray/outbound-subs. The controller is mounted under /panel/api, so the old paths hit the SPA page route (GET-only) and 404'd on POST. - Send the create-subscription body as a plain object instead of URLSearchParams. The axios request interceptor serializes bodies with qs.stringify, which can't read URLSearchParams' internal storage and produced an empty body, so the backend rejected it with "subscription URL is required". - Use message.useMessage() + context holder instead of the static antd message API (resolves the "Static function can not consume context" warning), matching XrayPage's pattern. - Migrate the subscriptions Drawer to antd v6 props: width -> size, destroyOnClose -> destroyOnHidden, and Space direction -> orientation. * feat(xray): show traffic/test for subscription outbounds; harden + test the feature Display (the reported issue): - Replace the flat read-only pills with a proper read-only table (desktop) and cards (mobile) in a new SubscriptionOutbounds component, showing Address, Protocol, Traffic (matched by tag — already collected by Xray), and a Test button with Latency. No edit/delete/move (read-only). - Test subscription outbounds via the existing /testOutbound endpoint, with results keyed by tag (subscriptionTestStates + testSubscriptionOutbound in useXraySetting, wired through XrayPage). Generalize isTesting/testResult to a string|number key so the same helpers serve index- and tag-keyed states. i18n: - Replace all hardcoded English subscription strings with t() calls and add pages.xray.outboundSub.* keys to en-US.json (other locales fall back). Backend hardening + tests: - xray.go: drop the tautological `subSvc != nil` check. - outbound_subscription: re-validate every redirect hop against private/ internal addresses (CheckRedirect) and cap the redirect chain, closing an SSRF gap where only the initial host was checked. - Extract assignStableTags as a pure function and add unit tests for tag stability and SSRF rejection (the feature previously had no tests). Misc: - gofmt util/link/outbound.go (it was not gofmt-clean). * fix(xray): make outbound-subs feature pass CI (test compile, route docs, openapi) - outbound_test.go: remove unused `inner`/`lines` variables that broke the `util/link` test build (declared and not used). - Document the 7 outbound-subscription routes in endpoints.ts (list, create, update, delete, del alias, refresh, parse) so TestAPIRoutesDocumented passes. - Regenerate frontend/public/openapi.json (npm run gen) to include the new endpoints, satisfying the codegen freshness check. * feat(xray): per-subscription allow-private, gap-filled tags, UI tweaks, delete refresh Backend: - Add a per-subscription AllowPrivate flag (default off). Create/Update/refresh and the redirect check sanitize the URL with it, so localhost/LAN sources work only when explicitly opted in; the SSRF guard still blocks private targets by default. Controller reads the allowPrivate form field on create/update/parse. - Default outbound tag prefix now uses the smallest free "subN-" number instead of the auto-increment id, so deleting a subscription frees its number for reuse (a fresh start gives sub1) while staying stable per subscription. Extracted a pure defaultPrefixNumber() with unit tests. - deleteOutboundSub now signals SetToNeedRestart so xray drops the outbounds. Frontend: - "Allow private address" toggle in the add form (sends allowPrivate). - Delete now refreshes the xray view immediately (no manual page reload). - Subscriptions manager opens as a centered Modal instead of a right-side Drawer. - Move Outbounds to a top-level sidebar item under Nodes (out of Xray Configs). - Collapse WARP/NordVPN into a "more" dropdown. - Document the allowPrivate param in endpoints.ts. * i18n(xray): translate outbound-subscription UI into all locales - Translate the pages.xray.outboundSub.* strings (and allowPrivate label/hint) into all 12 non-English locales, matching each file's existing terminology. - Remove the unused outboundSub.add ("Add subscription") key from every locale. * feat(xray): subscription manager — edit, reorder/priority, status, preview, refresh-all Backend: - Per-subscription Priority + Prepend: subscriptions are ordered by Priority and placed before (Prepend) or after the manual template outbounds in the merge, so a subscription server can become the default. New Move(up/down) endpoint re-normalizes priorities; merge split into prepend/template/append. - List now returns a derived OutboundCount and orders by priority, and strips the heavy LastFetchedOutbounds/LinkIdentities blobs from the list payload. - Create/Update accept the prepend flag; new subs append at the end of priority. Frontend (Outbound Subscriptions modal): - Edit existing subscriptions (reuses the form + Update endpoint). - Inline enable/disable Switch, Status column (OK / error tooltip), Outbounds count column, per-row refresh spinner, "Refresh all" button. - Reorder (move up/down) controls + a "Before manual outbounds" toggle. - Preview button: fetch+parse a URL via /parse without saving. - Document the move route + prepend param in endpoints.ts; regenerate openapi.json. * i18n(xray): translate new subscription-manager strings into all locales Add the prepend/prependHint, preview/previewEmpty, refreshAll, statusOk and toastUpdated keys to all 12 non-English locales, matching each file's terminology. --------- Co-authored-by: MHSanaei <ho3ein.sanaei@gmail.com>
2026-06-08 09:09:53 -07:00
enabled := c.PostForm("enabled") != "false"
allowPrivate := c.PostForm("allowPrivate") == "true"
Bug-label issue sweep: 16 fixes (#6083) * fix(xray): block private-range egress in default freedom finalRules (#6037) With domainStrategy AsIs the router never resolves domains, so a domain with a private A record (e.g. 127-0-0-1.nip.io) sails past the geoip:private routing block and freedom's allow-all finalRules let it reach loopback services such as the xray gRPC API and metrics listener. Prepend a block rule for geoip:private to the default template and add the FreedomFinalRulesPrivateEgressBlock seeder so existing installs still carrying the stock allow-only (or legacy private-only-allow) finalRules are upgraded in place; customized rules are left untouched. * fix(sub): version-gate unencrypted-outbound drops in outbound subscriptions (#6033) Commit d38c912d taught CheckXrayConfig to keep unencrypted vless/trojan outbounds when the running core predates the v26.7.11 rejection, but filterOutboundsRejectedByCore still consulted the embedded validator unconditionally, so outbound subscriptions kept silently dropping those outbounds even on downgraded cores. Apply the same shouldSkipLegacyUnencryptedOutboundRejection gate when filtering fetched subscription outbounds. * fix(xray): resolve geodata assets before building outbound configs (#5928) Saving routing or template settings validates each outbound through the embedded config loader, and a freedom outbound whose finalRules reference geoip:private opens geoip.dat during that build. Unlike ApplyRoutingConfig, ValidateOutboundConfig and AddOutbound never pointed the in-process loader at the bin folder, so xray-core resolved the file relative to the panel executable and saving failed with 'stat /usr/local/x-ui/geoip.dat: no such file or directory'. Call ensureXrayAssetLocation before both build paths. * fix(api): use a real i18n key in the client get handler (#5911) The client fetch endpoint localized its error prefix with the bare key 'get', which exists in no translation file, so every lookup of a deleted client's email logged 'message "get" not found in language ...' noise alongside the expected record-not-found warning. Reuse the same pages.inbounds.toasts.obtain key the sibling list handler uses. * fix(sub): carry host record Host header and path into Clash/JSON output (#5944) The raw-link path overrides the host/path share params from a Host record via applyEndpointHostPath, but the Clash and JSON renderers read the transport settings object, which applyHostStreamOverrides never touched — so a Host record's WebSocket Host header (and path) silently vanished from Clash/Mihomo and JSON subscriptions whenever the inbound's own ws settings left them empty. Inject hostHeader/path into the ws/httpupgrade/xhttp settings of the per-host stream, mirroring the raw-link override. * fix(metrics): accept Unicode outbound tags in the observatory (#5972) The observatory validator whitelisted ASCII word characters, so any outbound whose tag carries a flag emoji or other non-ASCII text was silently dropped from the metrics snapshot, delay history, and health notifications. The history store is an in-process map, so the strict charset bought nothing. Validate tags as non-empty, bounded, control-character-free UTF-8 instead, keeping spaces and emoji while still rejecting garbage input on the query path. * fix(database): default sqlite to WAL to stop background-job lock storms (#6057, #6068) With journal_mode=DELETE every write serializes the whole database and blocks readers, so under normal multi-job load (traffic sampling, node sync, mtproto reconcile) transactions regularly outwaited the 10s busy timeout and jobs failed with 'database is locked'. Move to WAL by default: readers no longer block writers and vice versa, which removes the observed contention while writer-writer access still serializes safely. The single-file-at-rest property is preserved where it matters — Checkpoint() now issues wal_checkpoint(TRUNCATE), so panel and Telegram backups read a complete main file, and sqlite folds the WAL back into the db on clean shutdown. XUI_DB_JOURNAL_MODE=DELETE restores the previous behavior for setups that copy the live file directly. * fix(database): strip finalmask.tcp from REALITY inbounds on upgrade (#6038) validateFinalMaskRealityCombo blocks saving finalmask.tcp together with REALITY because that combination crashes Xray-core 26.7.11 on the first connection (XTLS/Xray-core#6453), but it only runs on add/update. An inbound saved before the validator existed sailed through the upgrade untouched and took the core down at boot. Add the InboundRealityFinalmaskTcpStrip seeder: one-time scan that removes finalmask.tcp from REALITY inbounds (other finalmask transports survive), so upgraded panels start cleanly. * fix(xray): stop deleting hand-written direct routing rules on save (#6056) The DNS allow-rule sync recognized 'its' rules purely by shape (type=field, ip, port, outboundTag=direct, nothing else), so any manual rule of that shape — e.g. routing a LAN CIDR to a NAS port over direct — was silently stripped on every settings save. Mark managed rules with ruleTag=xui-dns-allow (round-tripped untouched by both xray-core and the Routing tab editor) and only strip rules that carry the tag. Untagged legacy managed rules are adopted when their exact ip-set/port matches a currently configured private DNS endpoint; anything else is left alone. A stale pre-tag managed rule whose DNS server was removed now lingers until deleted manually — the safe side of the trade against eating user rules. * fix(clients): resolve email lookups through client_inbounds after a move (#6059) GetClientInboundByEmail trusted the client_traffics.inbound_id pointer whenever that inbound still existed, but a client moved between inbounds leaves the row pointing at its old (still existing) inbound. The lookup then searched the wrong inbound's clients and failed with 'Client Not Found In Inbound For Email', which broke the Telegram bot's link and QR generation for moved clients. When the pointed-at inbound no longer carries the email, re-resolve through the authoritative client_inbounds link to the inbound that actually hosts the client. * fix(nodes): replicate inbound fallbacks to nodes (#5963) Fallbacks live in the inbound_fallbacks table and were only merged into settings by the master's local config builder; the runtime inbound pushed to nodes rebuilt settings without them, and the reconcile job additionally fingerprinted the raw DB row, so fallback edits neither reached nodes nor triggered a re-push. Inject settings.fallbacks in buildRuntimeInboundForAPI (mirroring the local builder, gated on inboundCanHostFallbacks) and make ReconcileNode push and fingerprint that same runtime-built payload, aligning the interactive and reconcile paths. * fix(database): survive PostgreSQL outages without a runaway restart loop (#6023) A PostgreSQL that was down or still starting made InitDB fail instantly; the process exited with a generic startup error and systemd restarted it every 5s forever, flooding the journal. Retry the initial postgres connection with backoff (~70s total) and log the real driver error on every attempt, and cap the systemd units with StartLimitIntervalSec/StartLimitBurst so a persistently unreachable database stops the unit instead of looping indefinitely. * fix(xray): force a full restart when REALITY stream settings change (#6010) A changed inbound is normally hot-swapped over gRPC as RemoveInbound + AddInbound, but xray-core does not reliably rebuild a REALITY listener's authenticator on a runtime re-add — key or shortId edits appeared applied yet clients kept authenticating against the old parameters until someone restarted the core manually, on nodes in particular. Treat any non-client change to an inbound that uses (or starts using) REALITY as not hot-appliable so the panel restarts the core instead. Client-only edits on REALITY inbounds keep flowing through the per-user AlterInbound path and still avoid restarts. * feat(sub): allow insecure TLS for outbound subscription fetches (#6067) An outbound subscription served over HTTPS with a self-signed or private-CA certificate could never be fetched: the fetch client had no TLS options, so refreshes died with 'x509: certificate signed by unknown authority' and there was nothing the admin could toggle. Add a per-subscription 'Allow insecure' switch (persisted as allow_insecure, default off) that sets InsecureSkipVerify on the fetch transport — including when the fetch is routed through the panel egress proxy. The SSRF-guarded dialer and redirect re-validation stay in force either way. * fix(reality): send PROXY protocol header in the target scanner when xver is set (#6082) The REALITY target scanner always probed with a plain TLS handshake, so a target fronted by an Nginx listener that requires the PROXY protocol (matching the inbound's xver>=1) reset the connection and the panel reported a false 'TLS handshake failed'. Thread the inbound's xver into the scan request and, when it is >=1, lead with the matching PROXY protocol header (v1 for xver 1, binary v2 for xver 2) built from the dialed connection's own address pair. Batch candidate scans against public sites are unaffected (xver 0). * fix(frontend): default sockopt fields when editing a stored inbound (#5956) Opening an existing inbound ran rawInboundToFormValues over the raw DB row, and only xhttpSettings was re-parsed through its Zod schema to fill defaults. A sockopt object saved before the TProxy control existed has no tproxy key, so the Select rendered blank; picking Off didn't help because the wire normalizer drops tproxy=off, recreating the missing key on the next edit. Re-parse streamSettings.sockopt through SockoptStreamSettingsSchema on load, mirroring the xhttpSettings handling, so absent keys (tproxy, tcpcongestion, …) get their schema defaults every time the form opens.
2026-07-23 06:34:42 -07:00
allowInsecure := c.PostForm("allowInsecure") == "true"
feat: add support for subscription-based outbounds with auto-update (#5037) * feat: add support for subscription-based outbounds with auto-update - New OutboundSubscription model (full support on both SQLite and PostgreSQL) - Go subscription link parser (vmess/vless/trojan/ss/hysteria2/wireguard) matching frontend behavior - Stable tag assignment across refreshes (designed for balancer + routing use) - Runtime merge of subscription outbounds into Xray config (additive only) - Full CRUD + manual refresh + preview API - Background auto-update job (per-subscription interval) - Frontend management UI in Outbounds tab (Subscriptions drawer) + tag integration in balancers/routing rules - Proper dual-database support including CLI migration path Review & hardening notes: - Fixed merge logic bug that could drop manual outbounds - Added SSRF/private-IP protection on subscription URLs using SanitizePublicHTTPURL - Improved update interval UX (hours + minutes) - Auto-fetch on first subscription creation - Added detailed comments on tag stability strategy and balancer implications when servers are added/removed/rotated - Updated migrationModels() for CLI migrate-db support * fix: resolve frontend lint/type errors and Go build break Frontend (eslint + tsc clean): - Destructure subscriptionOutboundTags prop in RoutingTab and BalancersTab. It was declared in the interface and used in useMemo but never destructured, so it resolved as an unresolved global (react-hooks warning + tsc "Cannot find name"). The prop is passed by XrayPage, so the feature was silently inert. - OutboundsTab: remove unused useEffect import, add an OutboundSub type to replace any[] state and the any/any table render signature, type the subscriptionOutbounds cast, and replace unused catch (e) bindings with parameter-less catch. Also type HttpUtil.post as OutboundSub so r.obj?.id type-checks. Backend (go build clean): - outbound_subscription_job: websocket.MessageTypeXray is undefined; use the existing MessageTypeOutbounds since the job refreshes outbound subscriptions. * fix(xray): make outbound subscription creation work end-to-end - Correct API paths from /panel/xray/outbound-subs to /panel/api/xray/outbound-subs. The controller is mounted under /panel/api, so the old paths hit the SPA page route (GET-only) and 404'd on POST. - Send the create-subscription body as a plain object instead of URLSearchParams. The axios request interceptor serializes bodies with qs.stringify, which can't read URLSearchParams' internal storage and produced an empty body, so the backend rejected it with "subscription URL is required". - Use message.useMessage() + context holder instead of the static antd message API (resolves the "Static function can not consume context" warning), matching XrayPage's pattern. - Migrate the subscriptions Drawer to antd v6 props: width -> size, destroyOnClose -> destroyOnHidden, and Space direction -> orientation. * feat(xray): show traffic/test for subscription outbounds; harden + test the feature Display (the reported issue): - Replace the flat read-only pills with a proper read-only table (desktop) and cards (mobile) in a new SubscriptionOutbounds component, showing Address, Protocol, Traffic (matched by tag — already collected by Xray), and a Test button with Latency. No edit/delete/move (read-only). - Test subscription outbounds via the existing /testOutbound endpoint, with results keyed by tag (subscriptionTestStates + testSubscriptionOutbound in useXraySetting, wired through XrayPage). Generalize isTesting/testResult to a string|number key so the same helpers serve index- and tag-keyed states. i18n: - Replace all hardcoded English subscription strings with t() calls and add pages.xray.outboundSub.* keys to en-US.json (other locales fall back). Backend hardening + tests: - xray.go: drop the tautological `subSvc != nil` check. - outbound_subscription: re-validate every redirect hop against private/ internal addresses (CheckRedirect) and cap the redirect chain, closing an SSRF gap where only the initial host was checked. - Extract assignStableTags as a pure function and add unit tests for tag stability and SSRF rejection (the feature previously had no tests). Misc: - gofmt util/link/outbound.go (it was not gofmt-clean). * fix(xray): make outbound-subs feature pass CI (test compile, route docs, openapi) - outbound_test.go: remove unused `inner`/`lines` variables that broke the `util/link` test build (declared and not used). - Document the 7 outbound-subscription routes in endpoints.ts (list, create, update, delete, del alias, refresh, parse) so TestAPIRoutesDocumented passes. - Regenerate frontend/public/openapi.json (npm run gen) to include the new endpoints, satisfying the codegen freshness check. * feat(xray): per-subscription allow-private, gap-filled tags, UI tweaks, delete refresh Backend: - Add a per-subscription AllowPrivate flag (default off). Create/Update/refresh and the redirect check sanitize the URL with it, so localhost/LAN sources work only when explicitly opted in; the SSRF guard still blocks private targets by default. Controller reads the allowPrivate form field on create/update/parse. - Default outbound tag prefix now uses the smallest free "subN-" number instead of the auto-increment id, so deleting a subscription frees its number for reuse (a fresh start gives sub1) while staying stable per subscription. Extracted a pure defaultPrefixNumber() with unit tests. - deleteOutboundSub now signals SetToNeedRestart so xray drops the outbounds. Frontend: - "Allow private address" toggle in the add form (sends allowPrivate). - Delete now refreshes the xray view immediately (no manual page reload). - Subscriptions manager opens as a centered Modal instead of a right-side Drawer. - Move Outbounds to a top-level sidebar item under Nodes (out of Xray Configs). - Collapse WARP/NordVPN into a "more" dropdown. - Document the allowPrivate param in endpoints.ts. * i18n(xray): translate outbound-subscription UI into all locales - Translate the pages.xray.outboundSub.* strings (and allowPrivate label/hint) into all 12 non-English locales, matching each file's existing terminology. - Remove the unused outboundSub.add ("Add subscription") key from every locale. * feat(xray): subscription manager — edit, reorder/priority, status, preview, refresh-all Backend: - Per-subscription Priority + Prepend: subscriptions are ordered by Priority and placed before (Prepend) or after the manual template outbounds in the merge, so a subscription server can become the default. New Move(up/down) endpoint re-normalizes priorities; merge split into prepend/template/append. - List now returns a derived OutboundCount and orders by priority, and strips the heavy LastFetchedOutbounds/LinkIdentities blobs from the list payload. - Create/Update accept the prepend flag; new subs append at the end of priority. Frontend (Outbound Subscriptions modal): - Edit existing subscriptions (reuses the form + Update endpoint). - Inline enable/disable Switch, Status column (OK / error tooltip), Outbounds count column, per-row refresh spinner, "Refresh all" button. - Reorder (move up/down) controls + a "Before manual outbounds" toggle. - Preview button: fetch+parse a URL via /parse without saving. - Document the move route + prepend param in endpoints.ts; regenerate openapi.json. * i18n(xray): translate new subscription-manager strings into all locales Add the prepend/prependHint, preview/previewEmpty, refreshAll, statusOk and toastUpdated keys to all 12 non-English locales, matching each file's terminology. --------- Co-authored-by: MHSanaei <ho3ein.sanaei@gmail.com>
2026-06-08 09:09:53 -07:00
prepend := c.PostForm("prepend") == "true"
intervalStr := c.PostForm("updateInterval")
interval := 600
if intervalStr != "" {
if v, err := parseIntSafe(intervalStr); err == nil && v > 0 {
interval = v
}
}
if err := a.OutboundSubscriptionService.Update(subID, remark, rawURL, prefix, userAgent, enabled, interval, allowPrivate, prepend, allowInsecure); err != nil {
feat: add support for subscription-based outbounds with auto-update (#5037) * feat: add support for subscription-based outbounds with auto-update - New OutboundSubscription model (full support on both SQLite and PostgreSQL) - Go subscription link parser (vmess/vless/trojan/ss/hysteria2/wireguard) matching frontend behavior - Stable tag assignment across refreshes (designed for balancer + routing use) - Runtime merge of subscription outbounds into Xray config (additive only) - Full CRUD + manual refresh + preview API - Background auto-update job (per-subscription interval) - Frontend management UI in Outbounds tab (Subscriptions drawer) + tag integration in balancers/routing rules - Proper dual-database support including CLI migration path Review & hardening notes: - Fixed merge logic bug that could drop manual outbounds - Added SSRF/private-IP protection on subscription URLs using SanitizePublicHTTPURL - Improved update interval UX (hours + minutes) - Auto-fetch on first subscription creation - Added detailed comments on tag stability strategy and balancer implications when servers are added/removed/rotated - Updated migrationModels() for CLI migrate-db support * fix: resolve frontend lint/type errors and Go build break Frontend (eslint + tsc clean): - Destructure subscriptionOutboundTags prop in RoutingTab and BalancersTab. It was declared in the interface and used in useMemo but never destructured, so it resolved as an unresolved global (react-hooks warning + tsc "Cannot find name"). The prop is passed by XrayPage, so the feature was silently inert. - OutboundsTab: remove unused useEffect import, add an OutboundSub type to replace any[] state and the any/any table render signature, type the subscriptionOutbounds cast, and replace unused catch (e) bindings with parameter-less catch. Also type HttpUtil.post as OutboundSub so r.obj?.id type-checks. Backend (go build clean): - outbound_subscription_job: websocket.MessageTypeXray is undefined; use the existing MessageTypeOutbounds since the job refreshes outbound subscriptions. * fix(xray): make outbound subscription creation work end-to-end - Correct API paths from /panel/xray/outbound-subs to /panel/api/xray/outbound-subs. The controller is mounted under /panel/api, so the old paths hit the SPA page route (GET-only) and 404'd on POST. - Send the create-subscription body as a plain object instead of URLSearchParams. The axios request interceptor serializes bodies with qs.stringify, which can't read URLSearchParams' internal storage and produced an empty body, so the backend rejected it with "subscription URL is required". - Use message.useMessage() + context holder instead of the static antd message API (resolves the "Static function can not consume context" warning), matching XrayPage's pattern. - Migrate the subscriptions Drawer to antd v6 props: width -> size, destroyOnClose -> destroyOnHidden, and Space direction -> orientation. * feat(xray): show traffic/test for subscription outbounds; harden + test the feature Display (the reported issue): - Replace the flat read-only pills with a proper read-only table (desktop) and cards (mobile) in a new SubscriptionOutbounds component, showing Address, Protocol, Traffic (matched by tag — already collected by Xray), and a Test button with Latency. No edit/delete/move (read-only). - Test subscription outbounds via the existing /testOutbound endpoint, with results keyed by tag (subscriptionTestStates + testSubscriptionOutbound in useXraySetting, wired through XrayPage). Generalize isTesting/testResult to a string|number key so the same helpers serve index- and tag-keyed states. i18n: - Replace all hardcoded English subscription strings with t() calls and add pages.xray.outboundSub.* keys to en-US.json (other locales fall back). Backend hardening + tests: - xray.go: drop the tautological `subSvc != nil` check. - outbound_subscription: re-validate every redirect hop against private/ internal addresses (CheckRedirect) and cap the redirect chain, closing an SSRF gap where only the initial host was checked. - Extract assignStableTags as a pure function and add unit tests for tag stability and SSRF rejection (the feature previously had no tests). Misc: - gofmt util/link/outbound.go (it was not gofmt-clean). * fix(xray): make outbound-subs feature pass CI (test compile, route docs, openapi) - outbound_test.go: remove unused `inner`/`lines` variables that broke the `util/link` test build (declared and not used). - Document the 7 outbound-subscription routes in endpoints.ts (list, create, update, delete, del alias, refresh, parse) so TestAPIRoutesDocumented passes. - Regenerate frontend/public/openapi.json (npm run gen) to include the new endpoints, satisfying the codegen freshness check. * feat(xray): per-subscription allow-private, gap-filled tags, UI tweaks, delete refresh Backend: - Add a per-subscription AllowPrivate flag (default off). Create/Update/refresh and the redirect check sanitize the URL with it, so localhost/LAN sources work only when explicitly opted in; the SSRF guard still blocks private targets by default. Controller reads the allowPrivate form field on create/update/parse. - Default outbound tag prefix now uses the smallest free "subN-" number instead of the auto-increment id, so deleting a subscription frees its number for reuse (a fresh start gives sub1) while staying stable per subscription. Extracted a pure defaultPrefixNumber() with unit tests. - deleteOutboundSub now signals SetToNeedRestart so xray drops the outbounds. Frontend: - "Allow private address" toggle in the add form (sends allowPrivate). - Delete now refreshes the xray view immediately (no manual page reload). - Subscriptions manager opens as a centered Modal instead of a right-side Drawer. - Move Outbounds to a top-level sidebar item under Nodes (out of Xray Configs). - Collapse WARP/NordVPN into a "more" dropdown. - Document the allowPrivate param in endpoints.ts. * i18n(xray): translate outbound-subscription UI into all locales - Translate the pages.xray.outboundSub.* strings (and allowPrivate label/hint) into all 12 non-English locales, matching each file's existing terminology. - Remove the unused outboundSub.add ("Add subscription") key from every locale. * feat(xray): subscription manager — edit, reorder/priority, status, preview, refresh-all Backend: - Per-subscription Priority + Prepend: subscriptions are ordered by Priority and placed before (Prepend) or after the manual template outbounds in the merge, so a subscription server can become the default. New Move(up/down) endpoint re-normalizes priorities; merge split into prepend/template/append. - List now returns a derived OutboundCount and orders by priority, and strips the heavy LastFetchedOutbounds/LinkIdentities blobs from the list payload. - Create/Update accept the prepend flag; new subs append at the end of priority. Frontend (Outbound Subscriptions modal): - Edit existing subscriptions (reuses the form + Update endpoint). - Inline enable/disable Switch, Status column (OK / error tooltip), Outbounds count column, per-row refresh spinner, "Refresh all" button. - Reorder (move up/down) controls + a "Before manual outbounds" toggle. - Preview button: fetch+parse a URL via /parse without saving. - Document the move route + prepend param in endpoints.ts; regenerate openapi.json. * i18n(xray): translate new subscription-manager strings into all locales Add the prepend/prependHint, preview/previewEmpty, refreshAll, statusOk and toastUpdated keys to all 12 non-English locales, matching each file's terminology. --------- Co-authored-by: MHSanaei <ho3ein.sanaei@gmail.com>
2026-06-08 09:09:53 -07:00
jsonMsg(c, "Failed to update outbound subscription", err)
return
}
jsonObj(c, "", nil)
}
func (a *XraySettingController) deleteOutboundSub(c *gin.Context) {
id := c.Param("id")
var subID int
if _, err := fmt.Sscanf(id, "%d", &subID); err != nil {
jsonMsg(c, "Invalid id", err)
return
}
if err := a.OutboundSubscriptionService.Delete(subID); err != nil {
jsonMsg(c, "Failed to delete outbound subscription", err)
return
}
// Signal that xray should drop this subscription's outbounds on next reload.
a.XrayService.SetToNeedRestart()
jsonObj(c, "", nil)
}
func (a *XraySettingController) refreshOutboundSub(c *gin.Context) {
id := c.Param("id")
var subID int
if _, err := fmt.Sscanf(id, "%d", &subID); err != nil {
jsonMsg(c, "Invalid id", err)
return
}
obs, err := a.OutboundSubscriptionService.Refresh(subID)
if err != nil {
jsonMsg(c, "Refresh failed", err)
return
}
// Signal that xray should pick up the new outbounds on next restart/reload
a.XrayService.SetToNeedRestart()
jsonObj(c, obs, nil)
}
func (a *XraySettingController) moveOutboundSub(c *gin.Context) {
id := c.Param("id")
var subID int
if _, err := fmt.Sscanf(id, "%d", &subID); err != nil {
jsonMsg(c, "Invalid id", err)
return
}
up := c.PostForm("dir") == "up"
if err := a.OutboundSubscriptionService.Move(subID, up); err != nil {
jsonMsg(c, "Failed to reorder outbound subscription", err)
return
}
// Order affects the merged outbounds, so xray needs a reload.
a.XrayService.SetToNeedRestart()
jsonObj(c, "", nil)
}
// parseOutboundSubURL is a preview endpoint: it fetches + parses the provided
// URL but does not persist anything. Useful for the "add subscription" flow
// so the user can see the resulting outbounds (and assigned tags) before saving.
func (a *XraySettingController) parseOutboundSubURL(c *gin.Context) {
rawURL := c.PostForm("url")
if rawURL == "" {
jsonMsg(c, "url is required", common.NewError("missing url"))
return
}
allowPrivate := c.PostForm("allowPrivate") == "true"
Bug-label issue sweep: 16 fixes (#6083) * fix(xray): block private-range egress in default freedom finalRules (#6037) With domainStrategy AsIs the router never resolves domains, so a domain with a private A record (e.g. 127-0-0-1.nip.io) sails past the geoip:private routing block and freedom's allow-all finalRules let it reach loopback services such as the xray gRPC API and metrics listener. Prepend a block rule for geoip:private to the default template and add the FreedomFinalRulesPrivateEgressBlock seeder so existing installs still carrying the stock allow-only (or legacy private-only-allow) finalRules are upgraded in place; customized rules are left untouched. * fix(sub): version-gate unencrypted-outbound drops in outbound subscriptions (#6033) Commit d38c912d taught CheckXrayConfig to keep unencrypted vless/trojan outbounds when the running core predates the v26.7.11 rejection, but filterOutboundsRejectedByCore still consulted the embedded validator unconditionally, so outbound subscriptions kept silently dropping those outbounds even on downgraded cores. Apply the same shouldSkipLegacyUnencryptedOutboundRejection gate when filtering fetched subscription outbounds. * fix(xray): resolve geodata assets before building outbound configs (#5928) Saving routing or template settings validates each outbound through the embedded config loader, and a freedom outbound whose finalRules reference geoip:private opens geoip.dat during that build. Unlike ApplyRoutingConfig, ValidateOutboundConfig and AddOutbound never pointed the in-process loader at the bin folder, so xray-core resolved the file relative to the panel executable and saving failed with 'stat /usr/local/x-ui/geoip.dat: no such file or directory'. Call ensureXrayAssetLocation before both build paths. * fix(api): use a real i18n key in the client get handler (#5911) The client fetch endpoint localized its error prefix with the bare key 'get', which exists in no translation file, so every lookup of a deleted client's email logged 'message "get" not found in language ...' noise alongside the expected record-not-found warning. Reuse the same pages.inbounds.toasts.obtain key the sibling list handler uses. * fix(sub): carry host record Host header and path into Clash/JSON output (#5944) The raw-link path overrides the host/path share params from a Host record via applyEndpointHostPath, but the Clash and JSON renderers read the transport settings object, which applyHostStreamOverrides never touched — so a Host record's WebSocket Host header (and path) silently vanished from Clash/Mihomo and JSON subscriptions whenever the inbound's own ws settings left them empty. Inject hostHeader/path into the ws/httpupgrade/xhttp settings of the per-host stream, mirroring the raw-link override. * fix(metrics): accept Unicode outbound tags in the observatory (#5972) The observatory validator whitelisted ASCII word characters, so any outbound whose tag carries a flag emoji or other non-ASCII text was silently dropped from the metrics snapshot, delay history, and health notifications. The history store is an in-process map, so the strict charset bought nothing. Validate tags as non-empty, bounded, control-character-free UTF-8 instead, keeping spaces and emoji while still rejecting garbage input on the query path. * fix(database): default sqlite to WAL to stop background-job lock storms (#6057, #6068) With journal_mode=DELETE every write serializes the whole database and blocks readers, so under normal multi-job load (traffic sampling, node sync, mtproto reconcile) transactions regularly outwaited the 10s busy timeout and jobs failed with 'database is locked'. Move to WAL by default: readers no longer block writers and vice versa, which removes the observed contention while writer-writer access still serializes safely. The single-file-at-rest property is preserved where it matters — Checkpoint() now issues wal_checkpoint(TRUNCATE), so panel and Telegram backups read a complete main file, and sqlite folds the WAL back into the db on clean shutdown. XUI_DB_JOURNAL_MODE=DELETE restores the previous behavior for setups that copy the live file directly. * fix(database): strip finalmask.tcp from REALITY inbounds on upgrade (#6038) validateFinalMaskRealityCombo blocks saving finalmask.tcp together with REALITY because that combination crashes Xray-core 26.7.11 on the first connection (XTLS/Xray-core#6453), but it only runs on add/update. An inbound saved before the validator existed sailed through the upgrade untouched and took the core down at boot. Add the InboundRealityFinalmaskTcpStrip seeder: one-time scan that removes finalmask.tcp from REALITY inbounds (other finalmask transports survive), so upgraded panels start cleanly. * fix(xray): stop deleting hand-written direct routing rules on save (#6056) The DNS allow-rule sync recognized 'its' rules purely by shape (type=field, ip, port, outboundTag=direct, nothing else), so any manual rule of that shape — e.g. routing a LAN CIDR to a NAS port over direct — was silently stripped on every settings save. Mark managed rules with ruleTag=xui-dns-allow (round-tripped untouched by both xray-core and the Routing tab editor) and only strip rules that carry the tag. Untagged legacy managed rules are adopted when their exact ip-set/port matches a currently configured private DNS endpoint; anything else is left alone. A stale pre-tag managed rule whose DNS server was removed now lingers until deleted manually — the safe side of the trade against eating user rules. * fix(clients): resolve email lookups through client_inbounds after a move (#6059) GetClientInboundByEmail trusted the client_traffics.inbound_id pointer whenever that inbound still existed, but a client moved between inbounds leaves the row pointing at its old (still existing) inbound. The lookup then searched the wrong inbound's clients and failed with 'Client Not Found In Inbound For Email', which broke the Telegram bot's link and QR generation for moved clients. When the pointed-at inbound no longer carries the email, re-resolve through the authoritative client_inbounds link to the inbound that actually hosts the client. * fix(nodes): replicate inbound fallbacks to nodes (#5963) Fallbacks live in the inbound_fallbacks table and were only merged into settings by the master's local config builder; the runtime inbound pushed to nodes rebuilt settings without them, and the reconcile job additionally fingerprinted the raw DB row, so fallback edits neither reached nodes nor triggered a re-push. Inject settings.fallbacks in buildRuntimeInboundForAPI (mirroring the local builder, gated on inboundCanHostFallbacks) and make ReconcileNode push and fingerprint that same runtime-built payload, aligning the interactive and reconcile paths. * fix(database): survive PostgreSQL outages without a runaway restart loop (#6023) A PostgreSQL that was down or still starting made InitDB fail instantly; the process exited with a generic startup error and systemd restarted it every 5s forever, flooding the journal. Retry the initial postgres connection with backoff (~70s total) and log the real driver error on every attempt, and cap the systemd units with StartLimitIntervalSec/StartLimitBurst so a persistently unreachable database stops the unit instead of looping indefinitely. * fix(xray): force a full restart when REALITY stream settings change (#6010) A changed inbound is normally hot-swapped over gRPC as RemoveInbound + AddInbound, but xray-core does not reliably rebuild a REALITY listener's authenticator on a runtime re-add — key or shortId edits appeared applied yet clients kept authenticating against the old parameters until someone restarted the core manually, on nodes in particular. Treat any non-client change to an inbound that uses (or starts using) REALITY as not hot-appliable so the panel restarts the core instead. Client-only edits on REALITY inbounds keep flowing through the per-user AlterInbound path and still avoid restarts. * feat(sub): allow insecure TLS for outbound subscription fetches (#6067) An outbound subscription served over HTTPS with a self-signed or private-CA certificate could never be fetched: the fetch client had no TLS options, so refreshes died with 'x509: certificate signed by unknown authority' and there was nothing the admin could toggle. Add a per-subscription 'Allow insecure' switch (persisted as allow_insecure, default off) that sets InsecureSkipVerify on the fetch transport — including when the fetch is routed through the panel egress proxy. The SSRF-guarded dialer and redirect re-validation stay in force either way. * fix(reality): send PROXY protocol header in the target scanner when xver is set (#6082) The REALITY target scanner always probed with a plain TLS handshake, so a target fronted by an Nginx listener that requires the PROXY protocol (matching the inbound's xver>=1) reset the connection and the panel reported a false 'TLS handshake failed'. Thread the inbound's xver into the scan request and, when it is >=1, lead with the matching PROXY protocol header (v1 for xver 1, binary v2 for xver 2) built from the dialed connection's own address pair. Batch candidate scans against public sites are unaffected (xver 0). * fix(frontend): default sockopt fields when editing a stored inbound (#5956) Opening an existing inbound ran rawInboundToFormValues over the raw DB row, and only xhttpSettings was re-parsed through its Zod schema to fill defaults. A sockopt object saved before the TProxy control existed has no tproxy key, so the Select rendered blank; picking Off didn't help because the wire normalizer drops tproxy=off, recreating the missing key on the next edit. Re-parse streamSettings.sockopt through SockoptStreamSettingsSchema on load, mirroring the xhttpSettings handling, so absent keys (tproxy, tcpcongestion, …) get their schema defaults every time the form opens.
2026-07-23 06:34:42 -07:00
allowInsecure := c.PostForm("allowInsecure") == "true"
userAgent := c.PostForm("userAgent")
feat: add support for subscription-based outbounds with auto-update (#5037) * feat: add support for subscription-based outbounds with auto-update - New OutboundSubscription model (full support on both SQLite and PostgreSQL) - Go subscription link parser (vmess/vless/trojan/ss/hysteria2/wireguard) matching frontend behavior - Stable tag assignment across refreshes (designed for balancer + routing use) - Runtime merge of subscription outbounds into Xray config (additive only) - Full CRUD + manual refresh + preview API - Background auto-update job (per-subscription interval) - Frontend management UI in Outbounds tab (Subscriptions drawer) + tag integration in balancers/routing rules - Proper dual-database support including CLI migration path Review & hardening notes: - Fixed merge logic bug that could drop manual outbounds - Added SSRF/private-IP protection on subscription URLs using SanitizePublicHTTPURL - Improved update interval UX (hours + minutes) - Auto-fetch on first subscription creation - Added detailed comments on tag stability strategy and balancer implications when servers are added/removed/rotated - Updated migrationModels() for CLI migrate-db support * fix: resolve frontend lint/type errors and Go build break Frontend (eslint + tsc clean): - Destructure subscriptionOutboundTags prop in RoutingTab and BalancersTab. It was declared in the interface and used in useMemo but never destructured, so it resolved as an unresolved global (react-hooks warning + tsc "Cannot find name"). The prop is passed by XrayPage, so the feature was silently inert. - OutboundsTab: remove unused useEffect import, add an OutboundSub type to replace any[] state and the any/any table render signature, type the subscriptionOutbounds cast, and replace unused catch (e) bindings with parameter-less catch. Also type HttpUtil.post as OutboundSub so r.obj?.id type-checks. Backend (go build clean): - outbound_subscription_job: websocket.MessageTypeXray is undefined; use the existing MessageTypeOutbounds since the job refreshes outbound subscriptions. * fix(xray): make outbound subscription creation work end-to-end - Correct API paths from /panel/xray/outbound-subs to /panel/api/xray/outbound-subs. The controller is mounted under /panel/api, so the old paths hit the SPA page route (GET-only) and 404'd on POST. - Send the create-subscription body as a plain object instead of URLSearchParams. The axios request interceptor serializes bodies with qs.stringify, which can't read URLSearchParams' internal storage and produced an empty body, so the backend rejected it with "subscription URL is required". - Use message.useMessage() + context holder instead of the static antd message API (resolves the "Static function can not consume context" warning), matching XrayPage's pattern. - Migrate the subscriptions Drawer to antd v6 props: width -> size, destroyOnClose -> destroyOnHidden, and Space direction -> orientation. * feat(xray): show traffic/test for subscription outbounds; harden + test the feature Display (the reported issue): - Replace the flat read-only pills with a proper read-only table (desktop) and cards (mobile) in a new SubscriptionOutbounds component, showing Address, Protocol, Traffic (matched by tag — already collected by Xray), and a Test button with Latency. No edit/delete/move (read-only). - Test subscription outbounds via the existing /testOutbound endpoint, with results keyed by tag (subscriptionTestStates + testSubscriptionOutbound in useXraySetting, wired through XrayPage). Generalize isTesting/testResult to a string|number key so the same helpers serve index- and tag-keyed states. i18n: - Replace all hardcoded English subscription strings with t() calls and add pages.xray.outboundSub.* keys to en-US.json (other locales fall back). Backend hardening + tests: - xray.go: drop the tautological `subSvc != nil` check. - outbound_subscription: re-validate every redirect hop against private/ internal addresses (CheckRedirect) and cap the redirect chain, closing an SSRF gap where only the initial host was checked. - Extract assignStableTags as a pure function and add unit tests for tag stability and SSRF rejection (the feature previously had no tests). Misc: - gofmt util/link/outbound.go (it was not gofmt-clean). * fix(xray): make outbound-subs feature pass CI (test compile, route docs, openapi) - outbound_test.go: remove unused `inner`/`lines` variables that broke the `util/link` test build (declared and not used). - Document the 7 outbound-subscription routes in endpoints.ts (list, create, update, delete, del alias, refresh, parse) so TestAPIRoutesDocumented passes. - Regenerate frontend/public/openapi.json (npm run gen) to include the new endpoints, satisfying the codegen freshness check. * feat(xray): per-subscription allow-private, gap-filled tags, UI tweaks, delete refresh Backend: - Add a per-subscription AllowPrivate flag (default off). Create/Update/refresh and the redirect check sanitize the URL with it, so localhost/LAN sources work only when explicitly opted in; the SSRF guard still blocks private targets by default. Controller reads the allowPrivate form field on create/update/parse. - Default outbound tag prefix now uses the smallest free "subN-" number instead of the auto-increment id, so deleting a subscription frees its number for reuse (a fresh start gives sub1) while staying stable per subscription. Extracted a pure defaultPrefixNumber() with unit tests. - deleteOutboundSub now signals SetToNeedRestart so xray drops the outbounds. Frontend: - "Allow private address" toggle in the add form (sends allowPrivate). - Delete now refreshes the xray view immediately (no manual page reload). - Subscriptions manager opens as a centered Modal instead of a right-side Drawer. - Move Outbounds to a top-level sidebar item under Nodes (out of Xray Configs). - Collapse WARP/NordVPN into a "more" dropdown. - Document the allowPrivate param in endpoints.ts. * i18n(xray): translate outbound-subscription UI into all locales - Translate the pages.xray.outboundSub.* strings (and allowPrivate label/hint) into all 12 non-English locales, matching each file's existing terminology. - Remove the unused outboundSub.add ("Add subscription") key from every locale. * feat(xray): subscription manager — edit, reorder/priority, status, preview, refresh-all Backend: - Per-subscription Priority + Prepend: subscriptions are ordered by Priority and placed before (Prepend) or after the manual template outbounds in the merge, so a subscription server can become the default. New Move(up/down) endpoint re-normalizes priorities; merge split into prepend/template/append. - List now returns a derived OutboundCount and orders by priority, and strips the heavy LastFetchedOutbounds/LinkIdentities blobs from the list payload. - Create/Update accept the prepend flag; new subs append at the end of priority. Frontend (Outbound Subscriptions modal): - Edit existing subscriptions (reuses the form + Update endpoint). - Inline enable/disable Switch, Status column (OK / error tooltip), Outbounds count column, per-row refresh spinner, "Refresh all" button. - Reorder (move up/down) controls + a "Before manual outbounds" toggle. - Preview button: fetch+parse a URL via /parse without saving. - Document the move route + prepend param in endpoints.ts; regenerate openapi.json. * i18n(xray): translate new subscription-manager strings into all locales Add the prepend/prependHint, preview/previewEmpty, refreshAll, statusOk and toastUpdated keys to all 12 non-English locales, matching each file's terminology. --------- Co-authored-by: MHSanaei <ho3ein.sanaei@gmail.com>
2026-06-08 09:09:53 -07:00
// Use a throw-away service instance; it only needs the settingService for proxy.
svc := service.OutboundSubscriptionService{}
// We don't have a direct "fetch once" that returns without storing, so we
// temporarily create a disabled row, refresh it, then delete. Cleaner would
// be to expose a pure ParseURL on the service, but this keeps the surface small.
tmp, err := svc.Create("preview", rawURL, "", userAgent, false, 600, allowPrivate, false, allowInsecure)
feat: add support for subscription-based outbounds with auto-update (#5037) * feat: add support for subscription-based outbounds with auto-update - New OutboundSubscription model (full support on both SQLite and PostgreSQL) - Go subscription link parser (vmess/vless/trojan/ss/hysteria2/wireguard) matching frontend behavior - Stable tag assignment across refreshes (designed for balancer + routing use) - Runtime merge of subscription outbounds into Xray config (additive only) - Full CRUD + manual refresh + preview API - Background auto-update job (per-subscription interval) - Frontend management UI in Outbounds tab (Subscriptions drawer) + tag integration in balancers/routing rules - Proper dual-database support including CLI migration path Review & hardening notes: - Fixed merge logic bug that could drop manual outbounds - Added SSRF/private-IP protection on subscription URLs using SanitizePublicHTTPURL - Improved update interval UX (hours + minutes) - Auto-fetch on first subscription creation - Added detailed comments on tag stability strategy and balancer implications when servers are added/removed/rotated - Updated migrationModels() for CLI migrate-db support * fix: resolve frontend lint/type errors and Go build break Frontend (eslint + tsc clean): - Destructure subscriptionOutboundTags prop in RoutingTab and BalancersTab. It was declared in the interface and used in useMemo but never destructured, so it resolved as an unresolved global (react-hooks warning + tsc "Cannot find name"). The prop is passed by XrayPage, so the feature was silently inert. - OutboundsTab: remove unused useEffect import, add an OutboundSub type to replace any[] state and the any/any table render signature, type the subscriptionOutbounds cast, and replace unused catch (e) bindings with parameter-less catch. Also type HttpUtil.post as OutboundSub so r.obj?.id type-checks. Backend (go build clean): - outbound_subscription_job: websocket.MessageTypeXray is undefined; use the existing MessageTypeOutbounds since the job refreshes outbound subscriptions. * fix(xray): make outbound subscription creation work end-to-end - Correct API paths from /panel/xray/outbound-subs to /panel/api/xray/outbound-subs. The controller is mounted under /panel/api, so the old paths hit the SPA page route (GET-only) and 404'd on POST. - Send the create-subscription body as a plain object instead of URLSearchParams. The axios request interceptor serializes bodies with qs.stringify, which can't read URLSearchParams' internal storage and produced an empty body, so the backend rejected it with "subscription URL is required". - Use message.useMessage() + context holder instead of the static antd message API (resolves the "Static function can not consume context" warning), matching XrayPage's pattern. - Migrate the subscriptions Drawer to antd v6 props: width -> size, destroyOnClose -> destroyOnHidden, and Space direction -> orientation. * feat(xray): show traffic/test for subscription outbounds; harden + test the feature Display (the reported issue): - Replace the flat read-only pills with a proper read-only table (desktop) and cards (mobile) in a new SubscriptionOutbounds component, showing Address, Protocol, Traffic (matched by tag — already collected by Xray), and a Test button with Latency. No edit/delete/move (read-only). - Test subscription outbounds via the existing /testOutbound endpoint, with results keyed by tag (subscriptionTestStates + testSubscriptionOutbound in useXraySetting, wired through XrayPage). Generalize isTesting/testResult to a string|number key so the same helpers serve index- and tag-keyed states. i18n: - Replace all hardcoded English subscription strings with t() calls and add pages.xray.outboundSub.* keys to en-US.json (other locales fall back). Backend hardening + tests: - xray.go: drop the tautological `subSvc != nil` check. - outbound_subscription: re-validate every redirect hop against private/ internal addresses (CheckRedirect) and cap the redirect chain, closing an SSRF gap where only the initial host was checked. - Extract assignStableTags as a pure function and add unit tests for tag stability and SSRF rejection (the feature previously had no tests). Misc: - gofmt util/link/outbound.go (it was not gofmt-clean). * fix(xray): make outbound-subs feature pass CI (test compile, route docs, openapi) - outbound_test.go: remove unused `inner`/`lines` variables that broke the `util/link` test build (declared and not used). - Document the 7 outbound-subscription routes in endpoints.ts (list, create, update, delete, del alias, refresh, parse) so TestAPIRoutesDocumented passes. - Regenerate frontend/public/openapi.json (npm run gen) to include the new endpoints, satisfying the codegen freshness check. * feat(xray): per-subscription allow-private, gap-filled tags, UI tweaks, delete refresh Backend: - Add a per-subscription AllowPrivate flag (default off). Create/Update/refresh and the redirect check sanitize the URL with it, so localhost/LAN sources work only when explicitly opted in; the SSRF guard still blocks private targets by default. Controller reads the allowPrivate form field on create/update/parse. - Default outbound tag prefix now uses the smallest free "subN-" number instead of the auto-increment id, so deleting a subscription frees its number for reuse (a fresh start gives sub1) while staying stable per subscription. Extracted a pure defaultPrefixNumber() with unit tests. - deleteOutboundSub now signals SetToNeedRestart so xray drops the outbounds. Frontend: - "Allow private address" toggle in the add form (sends allowPrivate). - Delete now refreshes the xray view immediately (no manual page reload). - Subscriptions manager opens as a centered Modal instead of a right-side Drawer. - Move Outbounds to a top-level sidebar item under Nodes (out of Xray Configs). - Collapse WARP/NordVPN into a "more" dropdown. - Document the allowPrivate param in endpoints.ts. * i18n(xray): translate outbound-subscription UI into all locales - Translate the pages.xray.outboundSub.* strings (and allowPrivate label/hint) into all 12 non-English locales, matching each file's existing terminology. - Remove the unused outboundSub.add ("Add subscription") key from every locale. * feat(xray): subscription manager — edit, reorder/priority, status, preview, refresh-all Backend: - Per-subscription Priority + Prepend: subscriptions are ordered by Priority and placed before (Prepend) or after the manual template outbounds in the merge, so a subscription server can become the default. New Move(up/down) endpoint re-normalizes priorities; merge split into prepend/template/append. - List now returns a derived OutboundCount and orders by priority, and strips the heavy LastFetchedOutbounds/LinkIdentities blobs from the list payload. - Create/Update accept the prepend flag; new subs append at the end of priority. Frontend (Outbound Subscriptions modal): - Edit existing subscriptions (reuses the form + Update endpoint). - Inline enable/disable Switch, Status column (OK / error tooltip), Outbounds count column, per-row refresh spinner, "Refresh all" button. - Reorder (move up/down) controls + a "Before manual outbounds" toggle. - Preview button: fetch+parse a URL via /parse without saving. - Document the move route + prepend param in endpoints.ts; regenerate openapi.json. * i18n(xray): translate new subscription-manager strings into all locales Add the prepend/prependHint, preview/previewEmpty, refreshAll, statusOk and toastUpdated keys to all 12 non-English locales, matching each file's terminology. --------- Co-authored-by: MHSanaei <ho3ein.sanaei@gmail.com>
2026-06-08 09:09:53 -07:00
if err != nil {
jsonMsg(c, "Failed to preview subscription", err)
return
}
obs, err := svc.Refresh(tmp.Id)
// best-effort cleanup
_ = svc.Delete(tmp.Id)
if err != nil {
jsonMsg(c, "Failed to fetch/parse subscription", err)
return
}
jsonObj(c, obs, nil)
}
func parseIntSafe(s string) (int, error) {
var v int
_, err := fmt.Sscanf(s, "%d", &v)
return v, err
}