fix(groups): page client dialogs and submit server-resolved scopes

Keep group client tables and bulk requests bounded with paged queries, frozen selections, and a streamed subscription export. Reuse existing bulk mutations and preserve the explicit-email contracts.
This commit is contained in:
Hamed Sanaei
2026-12-01 11:36:45 +03:30
parent 267379456a
commit 1ac11c7783
46 changed files with 3204 additions and 791 deletions
+101 -9
View File
@@ -1,8 +1,12 @@
package controller
import (
"bufio"
"fmt"
"net/http"
"strings"
"github.com/mhsanaei/3x-ui/v3/internal/logger"
"github.com/mhsanaei/3x-ui/v3/internal/util/common"
"github.com/mhsanaei/3x-ui/v3/internal/web/service"
@@ -10,8 +14,9 @@ import (
)
type GroupController struct {
clientService service.ClientService
xrayService service.XrayService
clientService service.ClientService
xrayService service.XrayService
settingService service.SettingService
}
func NewGroupController(g *gin.RouterGroup) *GroupController {
@@ -22,6 +27,8 @@ func NewGroupController(g *gin.RouterGroup) *GroupController {
func (a *GroupController) initRouter(g *gin.RouterGroup) {
g.GET("/groups", a.list)
g.GET("/groups/clientPage", a.clientPage)
g.GET("/groups/subLinks/export", a.exportSubLinks)
g.GET("/groups/:name/emails", a.emails)
g.POST("/groups/create", a.create)
g.POST("/groups/rename", a.rename)
@@ -158,8 +165,10 @@ func (a *GroupController) resetTraffic(c *gin.Context) {
}
type bulkAddToGroupRequest struct {
Emails []string `json:"emails"`
Group string `json:"group"`
Emails []string `json:"emails"`
Group string `json:"group"`
InboundID *int `json:"inboundId"`
Selection *service.GroupClientSelection `json:"selection"`
}
func (a *GroupController) bulkAdd(c *gin.Context) {
@@ -172,7 +181,21 @@ func (a *GroupController) bulkAdd(c *gin.Context) {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), common.NewError("group name is required"))
return
}
affected, err := a.clientService.AddToGroup(req.Emails, req.Group)
if (req.InboundID != nil && (*req.InboundID <= 0 || req.Selection != nil || len(req.Emails) != 0)) ||
(req.Selection != nil && len(req.Emails) != 0) {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), common.NewError("use one client scope"))
return
}
var affected int
var err error
switch {
case req.InboundID != nil:
affected, err = a.clientService.AddInboundToGroup(c.Request.Context(), *req.InboundID, req.Group)
case req.Selection != nil:
affected, err = a.clientService.ChangeGroupSelection(c.Request.Context(), req.Group, "add", *req.Selection)
default:
affected, err = a.clientService.AddToGroup(req.Emails, req.Group)
}
if err != nil {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
return
@@ -182,7 +205,9 @@ func (a *GroupController) bulkAdd(c *gin.Context) {
}
type bulkRemoveFromGroupRequest struct {
Emails []string `json:"emails"`
Emails []string `json:"emails"`
Group string `json:"group"`
Selection *service.GroupClientSelection `json:"selection"`
}
func (a *GroupController) bulkRemove(c *gin.Context) {
@@ -191,12 +216,79 @@ func (a *GroupController) bulkRemove(c *gin.Context) {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
return
}
affected, err := a.clientService.RemoveFromGroup(req.Emails)
if (req.Selection != nil && len(req.Emails) != 0) || (req.Selection == nil && req.Group != "") {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), common.NewError("use one client scope"))
return
}
var affected int
var err error
if req.Selection != nil {
affected, err = a.clientService.ChangeGroupSelection(c.Request.Context(), req.Group, "remove", *req.Selection)
} else {
affected, err = a.clientService.RemoveFromGroup(req.Emails)
}
if err != nil {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
return
}
jsonObj(c, gin.H{"affected": affected}, nil)
a.xrayService.SetToNeedRestart()
notifyClientsChanged()
if affected > 0 {
a.xrayService.SetToNeedRestart()
notifyClientsChanged()
}
}
func (a *GroupController) clientPage(c *gin.Context) {
var params service.GroupClientPageParams
if err := c.ShouldBindQuery(&params); err != nil {
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
return
}
page, err := a.clientService.ListGroupClients(c.Request.Context(), params)
jsonObj(c, page, err)
}
func (a *GroupController) exportSubLinks(c *gin.Context) {
links, err := a.settingService.GetPanelSubscriptionLinks(c.Request.Host)
if err != nil {
c.String(http.StatusInternalServerError, "%s", I18nWeb(c, "somethingWentWrong"))
return
}
group := strings.TrimSpace(c.Query("group"))
if !links.Enable || links.URI == "" || group == "" {
c.String(http.StatusBadRequest, "group and enabled subscriptions are required")
return
}
c.Header("Content-Type", "text/plain; charset=utf-8")
c.Header("Content-Disposition", `attachment; filename="sub-links.txt"`)
c.Header("Cache-Control", "no-store")
out := bufio.NewWriter(c.Writer)
first := true
err = a.clientService.WalkGroupSubscriptionClients(c.Request.Context(), group, func(email, subID string) error {
if !first {
if err := out.WriteByte('\n'); err != nil {
return err
}
}
first = false
if _, err := fmt.Fprintf(out, "%s\t%s%s", email, links.URI, subID); err != nil {
return err
}
if links.JSONEnable && links.JSONURI != "" {
_, err := fmt.Fprintf(out, "\t%s%s", links.JSONURI, subID)
return err
}
return nil
})
if err == nil {
err = out.Flush()
}
if err != nil && c.Request.Context().Err() == nil {
logger.Warning("group subscription export failed:", err)
if !c.Writer.Written() {
c.Header("Content-Disposition", "")
c.String(http.StatusInternalServerError, "%s", I18nWeb(c, "somethingWentWrong"))
}
}
}