Files
mirror-3x-ui/internal/web/controller/inbound_info.go
T
Hamed fda676e62a fix(inbounds): page inbound info and stream peer exports (#7341)
Replace full inbound hydration in Show Info with owner-scoped, no-store reads of one display client or up to 50 WireGuard/AmneziaWG peers. Preserve absolute peer numbering, Host/node endpoint projection, selected tab and IP logs across live traffic updates; keep edit and clone on full detail.

Stream ordered NDJSON pages from a single stored inbound snapshot for explicit Export All Peers. Generate an ordered ZIP64 archive with existing client config/link generators; validate page offsets, protocol and total, and reject incomplete or cancelled exports. Renew the per-page HTTP write deadline through the original response writer before gzip wrapping, keeping ordinary endpoint timeouts unchanged.

Validated on head 0f853351: all 12 CI jobs and Docs CI passed, including 2,350 frontend tests, bounded-info Chromium stories, live-traffic and archive regressions, Go/race, PostgreSQL, and node/Xray E2E. The final review also verified a conflict-free test merge with main 38452eac, matching regenerated API contracts and passing focused controller tests, including the real HTTP deadline regression for identity and gzip.

The author's synthetic 50,000-peer fixture reduces the uncompressed Show Info response from 2,650,526 to 3,184 bytes. Stored JSON still requires a linear scan per page; explicit ZIP export retains archive data in browser memory. Slow individual writes and reverse-proxy timeouts remain operational limits. No production latency or memory guarantee is implied.

Fixes #7330.
2026-12-03 04:57:45 +03:30

93 lines
2.8 KiB
Go

package controller
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/mhsanaei/3x-ui/v3/internal/logger"
"github.com/mhsanaei/3x-ui/v3/internal/web/service"
"github.com/mhsanaei/3x-ui/v3/internal/web/session"
)
const (
inboundPeerExportRoute = "/:id/peerExport"
peerExportWriterKey = "inboundPeerExportResponseController"
peerExportWriteTimeout = 30 * time.Second
)
// CaptureInboundPeerExportWriter preserves access to the transport's deadlines.
// Register it before gzip, whose writer does not expose the underlying writer.
func CaptureInboundPeerExportWriter(basePath string) gin.HandlerFunc {
route := strings.TrimRight(basePath, "/") + "/panel/api/inbounds" + inboundPeerExportRoute
return func(c *gin.Context) {
if c.FullPath() == route {
c.Set(peerExportWriterKey, http.NewResponseController(c.Writer))
}
}
}
func (a *InboundController) getInboundInfo(c *gin.Context) {
user := session.GetLoginUser(c)
if user == nil {
c.AbortWithStatus(http.StatusUnauthorized)
return
}
id, idErr := strconv.Atoi(c.Param("id"))
page, pageErr := strconv.Atoi(c.DefaultQuery("page", "1"))
c.Header("Cache-Control", "no-store")
if idErr != nil || pageErr != nil {
jsonObj(c, nil, fmt.Errorf("invalid inbound info page"))
return
}
result, err := a.inboundService.GetInboundInfo(c.Request.Context(), user.Id, id, page)
jsonObj(c, result, err)
}
func (a *InboundController) exportInboundPeers(c *gin.Context) {
user := session.GetLoginUser(c)
if user == nil {
c.AbortWithStatus(http.StatusUnauthorized)
return
}
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
c.Header("Cache-Control", "no-store")
response, _ := c.Get(peerExportWriterKey)
deadlines, _ := response.(*http.ResponseController)
if deadlines == nil {
deadlines = http.NewResponseController(c.Writer)
}
enc := json.NewEncoder(c.Writer)
err = a.inboundService.WalkInboundPeerPages(c.Request.Context(), user.Id, id, func(page *service.InboundInfoPage) error {
// A slow but progressing export can exceed the server's whole-response
// deadline. Refresh the budget for each page, including its flush.
if err := deadlines.SetWriteDeadline(time.Now().Add(peerExportWriteTimeout)); err != nil && !errors.Is(err, http.ErrNotSupported) {
return err
}
c.Header("Content-Type", "application/x-ndjson")
if err := enc.Encode(page); err != nil {
return err
}
c.Writer.Flush()
return c.Request.Context().Err()
})
if err == nil || c.Request.Context().Err() != nil {
return
}
logger.Warning("inbound peer export failed:", err)
if !c.Writer.Written() {
c.Header("Content-Type", "")
c.String(http.StatusBadRequest, "%s", I18nWeb(c, "somethingWentWrong"))
}
}