diff --git a/frontend/src/env.d.ts b/frontend/src/env.d.ts index e71d9d2d4..441eef46b 100644 --- a/frontend/src/env.d.ts +++ b/frontend/src/env.d.ts @@ -20,6 +20,7 @@ interface SubPageData { subUpdates?: number; links?: string[]; emails?: string[]; + linkRemarks?: string[]; datepicker?: 'gregorian' | 'jalalian'; announce?: string; downloadByte?: string | number; diff --git a/frontend/src/pages/sub/SubConfigsTab.tsx b/frontend/src/pages/sub/SubConfigsTab.tsx index 615b8b6c7..65175cbc7 100644 --- a/frontend/src/pages/sub/SubConfigsTab.tsx +++ b/frontend/src/pages/sub/SubConfigsTab.tsx @@ -14,10 +14,11 @@ import SubQrButton from './SubQrButton'; interface SubConfigsTabProps { links: string[]; + linkRemarks?: string[]; onCopy: (value: string, toast?: string) => void; } -export default function SubConfigsTab({ links, onCopy }: SubConfigsTabProps) { +export default function SubConfigsTab({ links, linkRemarks = [], onCopy }: SubConfigsTabProps) { const { t } = useTranslation(); return ( @@ -32,7 +33,7 @@ export default function SubConfigsTab({ links, onCopy }: SubConfigsTabProps) { {links.map((link, idx) => { const parts = parseLinkParts(link); - const rowTitle = parts?.remark || `Link ${idx + 1}`; + const rowTitle = parts?.remark || linkRemarks[idx] || `Link ${idx + 1}`; const isWireguardLink = link.startsWith('wireguard://') || link.startsWith('wg://'); const wireguardConfig = isWireguardLink ? wireguardConfigFromLink(link, rowTitle) : ''; const amneziawgConfig = link.startsWith('vpn://') ? amneziawgConfigFromLink(link) : ''; diff --git a/frontend/src/pages/sub/SubPage.tsx b/frontend/src/pages/sub/SubPage.tsx index d001c37cb..4470db414 100644 --- a/frontend/src/pages/sub/SubPage.tsx +++ b/frontend/src/pages/sub/SubPage.tsx @@ -34,6 +34,7 @@ const updateHours = Number(subData.subUpdates || 0); const announce = subData.announce || ''; const links: string[] = Array.isArray(subData.links) ? subData.links : []; const linkEmails: string[] = Array.isArray(subData.emails) ? subData.emails : []; +const linkRemarks: string[] = Array.isArray(subData.linkRemarks) ? subData.linkRemarks : []; const totalByte = Number(subData.totalByte || 0); const usedByte = Number(subData.usedByte || 0) || @@ -124,7 +125,7 @@ export default function SubPage() { {links.length} ), - children: , + children: , }); } return items; diff --git a/frontend/src/test/sub-links-tab.test.tsx b/frontend/src/test/sub-links-tab.test.tsx index dcf7a80b2..acb91fe23 100644 --- a/frontend/src/test/sub-links-tab.test.tsx +++ b/frontend/src/test/sub-links-tab.test.tsx @@ -2,6 +2,7 @@ import { fireEvent, render, within } from '@testing-library/react'; import { QRCode } from 'antd'; import { describe, expect, it, vi } from 'vitest'; +import SubConfigsTab from '@/pages/sub/SubConfigsTab'; import SubLinksTab from '@/pages/sub/SubLinksTab'; import { renderWithProviders } from './test-utils'; @@ -79,3 +80,21 @@ describe('SubLinksTab inline QR card', () => { expect(card.querySelector('.ant-segmented')).toBeNull(); }); }); + +describe('SubConfigsTab row titles (#7198)', () => { + it('names a remark-less tg://proxy row from linkRemarks at its own index', () => { + const links = [ + 'vless://11111111-1111-4111-8111-111111111111@h.example.com:443?type=tcp#DE-alice', + 'tg://proxy?server=203.0.113.7&port=8443&secret=ee00', + 'tg://proxy?server=203.0.113.8&port=8443&secret=ee11', + ]; + const view = renderWithProviders( + , + ); + + const titles = [...view.container.querySelectorAll('.sub-row-title')].map( + (el) => el.textContent, + ); + expect(titles).toEqual(['DE-alice', 'mt-sub-user', 'Link 3']); + }); +}); diff --git a/internal/sub/controller.go b/internal/sub/controller.go index 098ae568d..ef155e99f 100644 --- a/internal/sub/controller.go +++ b/internal/sub/controller.go @@ -450,6 +450,7 @@ func (a *SUBController) maybeServeSubInfo(c *gin.Context) bool { } info := a.subPageContext(page) delete(info, "links") + delete(info, "linkRemarks") delete(info, "subFormat") info["emails"] = dedupeEmails(page.Emails) c.JSON(http.StatusOK, info) @@ -462,6 +463,7 @@ func (a *SUBController) buildSubPageData(c *gin.Context) (PageData, bool) { subReq := a.subService.ForRequest(host) subReq.wireguardDevice = c.Query("wgDevice") subReq.subscriptionBody = false + subReq.linkRemarks = map[string]string{} subs, emails, lastOnline, traffic, err := subReq.getSubs(subId) if err != nil || subs == nil { writeSubError(c, err) @@ -746,6 +748,7 @@ func (a *SUBController) subPageContext(page PageData) map[string]any { "subUpdates": updateHours, "links": page.Result, "emails": page.Emails, + "linkRemarks": page.LinkRemarks, "datepicker": datepicker, "announce": page.SubAnnounce, } diff --git a/internal/sub/info_endpoint_test.go b/internal/sub/info_endpoint_test.go index ddb2f583e..78494410b 100644 --- a/internal/sub/info_endpoint_test.go +++ b/internal/sub/info_endpoint_test.go @@ -175,3 +175,68 @@ func TestSubPageDataNamesTheFormatOfTheVisitedRoute(t *testing.T) { }) } } + +// tg://proxy links carry no #remark, so the page data must name the MTProto row +// at the link's own index and leave self-named links empty (#7198). +func TestSubPage_MtprotoRowCarriesInboundRemark(t *testing.T) { + gin.SetMode(gin.TestMode) + initSubDB(t) + seedInfoEndpointSub(t, "mt-page", "user") + db := database.GetDB() + inbound := &model.Inbound{ + Listen: "203.0.113.7", Port: 8443, Protocol: model.MTProto, Enable: true, + Tag: "mt-page", Remark: "mt-sub", + Settings: `{"fakeTlsDomain":"www.cloudflare.com","clients":[{"email":"user","enable":true,"subId":"mt-page","secret":"` + mtprotoTestSecret + `"}]}`, + } + if err := db.Create(inbound).Error; err != nil { + t.Fatalf("create inbound: %v", err) + } + var client model.ClientRecord + if err := db.Where("email = ?", "user").First(&client).Error; err != nil { + t.Fatalf("load client: %v", err) + } + if err := db.Model(&client).Update("secret", mtprotoTestSecret).Error; err != nil { + t.Fatalf("set secret: %v", err) + } + if err := db.Create(&model.ClientInbound{ClientId: client.Id, InboundId: inbound.Id}).Error; err != nil { + t.Fatalf("attach client: %v", err) + } + oldDistFS := distFS + distFS = testDistFS + t.Cleanup(func() { distFS = oldDistFS }) + + router := gin.New() + NewSUBController(router.Group("/")) + req := httptest.NewRequest(http.MethodGet, "/sub/mt-page", nil) + req.Host = "sub.example.com" + req.Header.Set("Accept", "text/html") + w := httptest.NewRecorder() + router.ServeHTTP(w, req) + + _, rest, found := strings.Cut(w.Body.String(), "window.__SUB_PAGE_DATA__=") + raw, _, closed := strings.Cut(rest, ";") + if w.Code != http.StatusOK || !found || !closed { + t.Fatalf("status = %d, page data not found; body=%s", w.Code, w.Body.String()) + } + var page struct { + Links []string `json:"links"` + LinkRemarks []string `json:"linkRemarks"` + } + if err := json.Unmarshal([]byte(raw), &page); err != nil { + t.Fatalf("page data is not valid JSON: %v; data=%s", err, raw) + } + want := make([]string, len(page.Links)) + mtproto := 0 + for i, link := range page.Links { + if strings.HasPrefix(link, "tg://proxy") { + want[i] = "mt-sub-user" + mtproto++ + } + } + if mtproto != 1 || len(page.Links) != 2 { + t.Fatalf("links = %q, want one tg://proxy link and one external link", page.Links) + } + if len(page.LinkRemarks) != len(want) || page.LinkRemarks[0] != want[0] || page.LinkRemarks[1] != want[1] { + t.Fatalf("linkRemarks = %q, want %q for links %q", page.LinkRemarks, want, page.Links) + } +} diff --git a/internal/sub/service.go b/internal/sub/service.go index a931379b3..d1a7079c7 100644 --- a/internal/sub/service.go +++ b/internal/sub/service.go @@ -103,6 +103,9 @@ type SubService struct { // settingsByInbound: one decode per request, not one per link of a subId. tuicByInbound map[int]*tuicLinkInstance amneziaWGServerByInbound map[int]*amneziawg.ServerSettings + // linkRemarks maps a remark-less link (mtproto) to its remark; only the sub page + // sets it, so the Configs tab can name the row. + linkRemarks map[string]string } // tuicLinkInstance indexes a decoded TUIC inbound's clients by email @@ -1313,11 +1316,15 @@ func (s *SubService) genMtprotoLink(inbound *model.Inbound, email string) string endpoints := s.advertisedEndpoints(inbound) links := make([]string, 0, len(endpoints)) for _, endpoint := range endpoints { - links = append(links, buildLinkWithParams("tg://proxy", map[string]string{ + link := buildLinkWithParams("tg://proxy", map[string]string{ "server": endpoint.Address, "port": fmt.Sprintf("%d", endpoint.Port), "secret": resolved.Secret, - }, "")) + }, "") + if s.linkRemarks != nil { + s.linkRemarks[link] = s.endpointRemark(inbound, email, endpoint.ep, "") + } + links = append(links, link) } return strings.Join(links, "\n") } @@ -3400,6 +3407,7 @@ type PageData struct { SubAnnounce string Result []string Emails []string + LinkRemarks []string } // ResolveRequest extracts scheme, host, and header information from an HTTP request. @@ -3548,6 +3556,7 @@ func (s *SubService) BuildPageData(subId string, hostHeader string, traffic xray pageLinks := make([]string, 0, len(subs)) pageEmails := make([]string, 0, len(subs)) + pageRemarks := make([]string, 0, len(subs)) for i, sub := range subs { email := "" if i < len(emails) { @@ -3556,6 +3565,7 @@ func (s *SubService) BuildPageData(subId string, hostHeader string, traffic xray for _, link := range splitLinkLines(sub) { pageLinks = append(pageLinks, link) pageEmails = append(pageEmails, email) + pageRemarks = append(pageRemarks, s.linkRemarks[link]) } } @@ -3583,6 +3593,7 @@ func (s *SubService) BuildPageData(subId string, hostHeader string, traffic xray SubSupportUrl: subSupportUrl, Result: pageLinks, Emails: pageEmails, + LinkRemarks: pageRemarks, } }