fix(tgbot): look up a Telegram account's clients through the indexed client table (#6887)

Every bot usage command and the hourly report resolved a Telegram ID by
expanding every inbound's settings.clients, re-parsing the matched inbounds and
looking up each client again by email. With thousands of clients this ran
thousands of times per report and showed up as high PostgreSQL CPU, and an
account whose lookup came back empty was retried once per client carrying it.

The lookup now uses the normalized clients table and its Telegram-ID index in
one query per account, and the hourly pass checks each account once. Bot output
and thresholds are unchanged; link callbacks now authorize against the current
client records instead of possibly stale settings JSON.
This commit is contained in:
Hamed
2026-10-24 07:25:19 +03:30
committed by GitHub
parent d2a0216e01
commit 7d45456feb
9 changed files with 378 additions and 85 deletions
@@ -9,6 +9,7 @@ import (
"github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"github.com/mhsanaei/3x-ui/v3/internal/web/global"
"github.com/mhsanaei/3x-ui/v3/internal/web/service"
"github.com/mhsanaei/3x-ui/v3/internal/xray"
"github.com/mymmrac/telego"
@@ -19,8 +20,8 @@ const (
ownerMail = "owner@x"
)
// newLinksCallbackTgbot seeds one inbound whose settings bind email to
// ownerTgID, the traffic row the ownership lookup joins on, and a mocked API.
// newLinksCallbackTgbot seeds synchronized client records, inbound settings,
// traffic and a mocked Telegram API for the owner's link callbacks.
func newLinksCallbackTgbot(t *testing.T, email string) (*Tgbot, func(string) int) {
t.Helper()
mock, calls := staleButtonServer(t, map[string]any{
@@ -47,6 +48,13 @@ func newLinksCallbackTgbot(t *testing.T, email string) (*Tgbot, func(string) int
if err := database.GetDB().Create(inbound).Error; err != nil {
t.Fatalf("seed inbound: %v", err)
}
clients, err := (&service.InboundService{}).GetClients(inbound)
if err != nil {
t.Fatal(err)
}
if err := (&service.ClientService{}).SyncInbound(nil, inbound.Id, clients); err != nil {
t.Fatal(err)
}
if err := database.GetDB().Create(&xray.ClientTraffic{
InboundId: inbound.Id,
Email: email,
@@ -101,6 +109,25 @@ func TestClientLinkCallbackServesOwnClient(t *testing.T) {
}
}
func TestClientOwnershipUsesNormalizedTelegramID(t *testing.T) {
tb, _ := newLinksCallbackTgbot(t, ownerMail)
db := database.GetDB()
if err := db.Model(&model.ClientRecord{}).Where("email = ?", ownerMail).Update("tg_id", ownerTgID+1).Error; err != nil {
t.Fatal(err)
}
if tb.clientOwnedByTgUser(ownerTgID, ownerMail) {
t.Fatal("stale settings JSON granted ownership to the previous Telegram user")
}
if !tb.clientOwnedByTgUser(ownerTgID+1, ownerMail) {
t.Fatal("the normalized Telegram owner was denied access")
}
for _, id := range []int64{0, -1, ownerTgID + 2} {
if tb.clientOwnedByTgUser(id, ownerMail) {
t.Fatalf("unbound Telegram ID %d received ownership", id)
}
}
}
// Regression test: a payload past 64 chars arrives as its hash, so an email long
// enough to be hashed must still be decoded and served to its owner.
func TestHashedLinkCallbackServesOwnClient(t *testing.T) {