[GH-ISSUE #4921] مشکل محاسبه ترافیک #3439

Closed
opened 2026-10-31 20:32:27 +07:00 by bsakaguchi · 5 comments
Owner

Originally created by @libea18 on GitHub (Jun 4, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/4921

Describe the bug

مشکلی که هست:
اگر اینباند اول روی سرور مادر باشه و اینباند دوم یک نود باشه
ما بیایم کلاینت اد کنیم، موقع اد کردن کلاینت جدید، اگه attach inbound رو اول نود انتخاب کنیم بعد اینباند خود سرور، ترافیک مصرفی کلاینت روی کانفیگ سرور مادر حساب نمیشه و جلو نمیره ولی ترافیک کانفیگ نود حساب میشه.
حالا اگه موقع اتچ، اول بیایم اینباند اولی(که روی خود سرور هست) رو انتخاب کنیم، بعد اینباند نود رو، ترافیک هردو رو درست حساب میکنه.
روی آخرین نسخه 3.2.7 تست شده
اینم اضافه کنم توی حالتی که ترافیک محاسبه نمیشه، کلاینت آنلاین میخورها، ولی ترافیک جلو نمیره
اگه به کانفیگ سرور نود وصل شم توی همون ساب، درجا ترافیک نود رو میزنه و حساب میکنه.
(این در حالتیه که اول سرور نود انتخاب شده باشه توی اتچ)

How to reproduce the problem

1-clients
2-add client
3-attach inbound

Expected behavior

No response

Actual behavior

No response

Relevant logs


Screenshots

No response

3x-ui version

3.2.7

Xray-core version

No response

How did you install 3x-ui?

install.sh script

Operating system

ubuntu24.04

Which parts of the panel are affected?

No response

Browser (only if it is a UI bug)

No response

Is the panel behind a reverse proxy or CDN?

None

Before submitting

  • I searched existing issues and this bug has not been reported.
  • I am running the latest released version of 3x-ui (or have verified the bug still exists on it).
  • This bug report is written in English.
  • I have redacted any sensitive data (tokens, real domains, client UUIDs).
Originally created by @libea18 on GitHub (Jun 4, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/4921 ### Describe the bug مشکلی که هست: اگر اینباند اول روی سرور مادر باشه و اینباند دوم یک نود باشه ما بیایم کلاینت اد کنیم، موقع اد کردن کلاینت جدید، اگه attach inbound رو اول نود انتخاب کنیم بعد اینباند خود سرور، ترافیک مصرفی کلاینت روی کانفیگ سرور مادر حساب نمیشه و جلو نمیره ولی ترافیک کانفیگ نود حساب میشه. حالا اگه موقع اتچ، اول بیایم اینباند اولی(که روی خود سرور هست) رو انتخاب کنیم، بعد اینباند نود رو، ترافیک هردو رو درست حساب میکنه. روی آخرین نسخه 3.2.7 تست شده اینم اضافه کنم توی حالتی که ترافیک محاسبه نمیشه، کلاینت آنلاین میخورها، ولی ترافیک جلو نمیره اگه به کانفیگ سرور نود وصل شم توی همون ساب، درجا ترافیک نود رو میزنه و حساب میکنه. (این در حالتیه که اول سرور نود انتخاب شده باشه توی اتچ) ### How to reproduce the problem 1-clients 2-add client 3-attach inbound ### Expected behavior _No response_ ### Actual behavior _No response_ ### Relevant logs ```shell ``` ### Screenshots _No response_ ### 3x-ui version 3.2.7 ### Xray-core version _No response_ ### How did you install 3x-ui? install.sh script ### Operating system ubuntu24.04 ### Which parts of the panel are affected? _No response_ ### Browser (only if it is a UI bug) _No response_ ### Is the panel behind a reverse proxy or CDN? None ### Before submitting - [x] I searched [existing issues](../issues?q=is%3Aissue) and this bug has not been reported. - [x] I am running the latest released version of 3x-ui (or have verified the bug still exists on it). - [x] This bug report is written in English. - [x] I have redacted any sensitive data (tokens, real domains, client UUIDs).
bsakaguchi 2026-10-31 20:32:27 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@mitrios-hub commented on GitHub (Jun 4, 2026):

I wrote about this issue yesterday... Client using tunnels but traffic is zero and client is offline.
In some cases user can be online, but...

Image
<!-- gh-comment-id:4624648325 --> @mitrios-hub commented on GitHub (Jun 4, 2026): I wrote about this issue yesterday... Client using tunnels but traffic is zero and client is offline. In some cases user can be online, but... <img width="499" height="341" alt="Image" src="https://github.com/user-attachments/assets/d72b08e1-1883-46fb-bac7-2bf1bfd70fe4" />
Author
Owner

@github-actions[bot] commented on GitHub (Jun 4, 2026):

باگ تایید شده است — علت اصلی آن در کد پیدا شد.


علت باگ

در web/service/inbound.go، تابع AddClientStat (خط ۲۳۹۹) یک ردیف ClientTraffic در دیتابیس می‌سازد که هم کلاینت و هم ترافیک مصرفی او را نگه می‌دارد. این ردیف روی فیلد email یکتا (UNIQUE) است. اگر هنگام آتچ کردن، کلاینت به چند اینباند اضافه شود، فقط اولین اینباند انتخاب‌شده این ردیف را می‌سازد؛ برای اینباندهای بعدی کد زیر اجرا می‌شود:

// web/service/inbound.go:2408
return tx.Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "email"}}, DoNothing: true}).
    Create(&clientTraffic).Error

یعنی اگر ایمیل از قبل وجود داشته باشد، هیچ کاری انجام نمی‌شود و InboundId ردیف موجود همان اینباند اول باقی می‌ماند.

سپس در تابع addClientTraffic (خط ۱۸۳۱)، موقع ثبت ترافیک دریافتی از Xray، فقط ردیف‌هایی پردازش می‌شوند که متعلق به نود نباشند:

// web/service/inbound.go:1831-1833
Where("email IN (?) AND inbound_id NOT IN (?)", emails,
    tx.Model(&model.Inbound{}).Select("id").Where("node_id IS NOT NULL"))

نتیجه:

  • اگر اول نود را انتخاب کنید → InboundId ردیف = شناسه نود → در کوئری فوق فیلتر می‌شود → ترافیک سرور مادر ثبت نمی‌شود.
  • اگر اول سرور مادر را انتخاب کنید → InboundId ردیف = شناسه اینباند سرور اصلی → در کوئری گنجانده می‌شود → ترافیک هر دو درست حساب می‌شود.

این دقیقاً همان رفتاری است که گزارش داده‌اید.


راه‌حل موقت (تا رفع باگ)

هنگام اضافه کردن کلاینت با «Attach Inbound»، ابتدا اینباند سرور مادر را انتخاب کنید و بعد اینباند نود را. همان‌طور که خودتان هم متوجه شده‌اید، این ترتیب مشکل را دور می‌زند.


فایل‌های مرتبط برای رفع باگ

فایل خط موضوع
web/service/inbound.go ۲۳۹۹–۲۴۱۰ AddClientStat — باید هنگام conflict، InboundId را به سمت اینباند سرور مادر به‌روز کند
web/service/inbound.go ۱۸۳۱–۱۸۳۴ addClientTraffic — کوئری باید ردیف‌هایی با inbound_id اشتباه را هم پوشش دهد

مشکلات مشابه قبلاً در #4712 و #4725 هم گزارش شده بودند که با همین منطق OnConflict مرتبط است.

<!-- gh-comment-id:4624664991 --> @github-actions[bot] commented on GitHub (Jun 4, 2026): باگ تایید شده است — علت اصلی آن در کد پیدا شد. --- ## علت باگ در `web/service/inbound.go`، تابع `AddClientStat` (خط ۲۳۹۹) یک ردیف `ClientTraffic` در دیتابیس می‌سازد که هم کلاینت و هم ترافیک مصرفی او را نگه می‌دارد. این ردیف روی فیلد `email` یکتا (UNIQUE) است. اگر هنگام آتچ کردن، کلاینت به چند اینباند اضافه شود، فقط **اولین** اینباند انتخاب‌شده این ردیف را می‌سازد؛ برای اینباندهای بعدی کد زیر اجرا می‌شود: ```go // web/service/inbound.go:2408 return tx.Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "email"}}, DoNothing: true}). Create(&clientTraffic).Error ``` یعنی اگر ایمیل از قبل وجود داشته باشد، هیچ کاری انجام نمی‌شود و `InboundId` ردیف موجود همان اینباند اول باقی می‌ماند. سپس در تابع `addClientTraffic` (خط ۱۸۳۱)، موقع ثبت ترافیک دریافتی از Xray، فقط ردیف‌هایی پردازش می‌شوند که **متعلق به نود نباشند**: ```go // web/service/inbound.go:1831-1833 Where("email IN (?) AND inbound_id NOT IN (?)", emails, tx.Model(&model.Inbound{}).Select("id").Where("node_id IS NOT NULL")) ``` **نتیجه:** - اگر اول **نود** را انتخاب کنید → `InboundId` ردیف = شناسه نود → در کوئری فوق فیلتر می‌شود → **ترافیک سرور مادر ثبت نمی‌شود.** - اگر اول **سرور مادر** را انتخاب کنید → `InboundId` ردیف = شناسه اینباند سرور اصلی → در کوئری گنجانده می‌شود → **ترافیک هر دو درست حساب می‌شود.** این دقیقاً همان رفتاری است که گزارش داده‌اید. --- ## راه‌حل موقت (تا رفع باگ) هنگام اضافه کردن کلاینت با «Attach Inbound»، **ابتدا اینباند سرور مادر را انتخاب کنید** و بعد اینباند نود را. همان‌طور که خودتان هم متوجه شده‌اید، این ترتیب مشکل را دور می‌زند. --- ## فایل‌های مرتبط برای رفع باگ | فایل | خط | موضوع | |------|-----|--------| | `web/service/inbound.go` | ۲۳۹۹–۲۴۱۰ | `AddClientStat` — باید هنگام conflict، `InboundId` را به سمت اینباند سرور مادر به‌روز کند | | `web/service/inbound.go` | ۱۸۳۱–۱۸۳۴ | `addClientTraffic` — کوئری باید ردیف‌هایی با `inbound_id` اشتباه را هم پوشش دهد | مشکلات مشابه قبلاً در #4712 و #4725 هم گزارش شده بودند که با همین منطق `OnConflict` مرتبط است.
Author
Owner

@libea18 commented on GitHub (Jun 4, 2026):

I wrote about this issue yesterday... Client using tunnels but traffic is zero and client is offline

برای من کلاینت انلاین میشود وقتی به کانفیگ سرور مادر وصل میشه ولی ترافیک اپدیت نمیشه. به محض وصل شدن به کانفیگ نود ترافیک جلو میره (ولی ترافیک مصرفی قبلی اپدیت نمیشه)
تنها در صورتی درست میشه که اول اینباند سرور مادر رو اتچ کنم بعد سرور نود . ( حتی با ادیت کردن کلاینت درست نمیشه و تنها راهش اد کردن کلاینت جدیده)

<!-- gh-comment-id:4624684375 --> @libea18 commented on GitHub (Jun 4, 2026): > I wrote about this issue yesterday... Client using tunnels but traffic is zero and client is offline برای من کلاینت انلاین میشود وقتی به کانفیگ سرور مادر وصل میشه ولی ترافیک اپدیت نمیشه. به محض وصل شدن به کانفیگ نود ترافیک جلو میره (ولی ترافیک مصرفی قبلی اپدیت نمیشه) تنها در صورتی درست میشه که اول اینباند سرور مادر رو اتچ کنم بعد سرور نود . ( حتی با ادیت کردن کلاینت درست نمیشه و تنها راهش اد کردن کلاینت جدیده)
Author
Owner

@bjerfan commented on GitHub (Jun 4, 2026):

واسه منم همینطور هست با حذف کلی نود هم مشکل حل نمیشه متاسفانه

<!-- gh-comment-id:4625231014 --> @bjerfan commented on GitHub (Jun 4, 2026): واسه منم همینطور هست با حذف کلی نود هم مشکل حل نمیشه متاسفانه
Author
Owner

@MHSanaei commented on GitHub (Jun 4, 2026):

Fixed in e0845626.

Root cause: client_traffics is keyed by email — one shared row per client across every inbound it is attached to. The local traffic updater filtered with inbound_id NOT IN (node inbounds), so when a client was attached to both a node inbound and the mother inbound and the node inbound was attached first, the shared row carried the node inbound's id (AddClientStat uses OnConflict DoNothing and never refreshes it). The local xray's traffic for that client was then dropped entirely — the client appeared online but its usage stayed at zero. Attaching the mother inbound first worked around it, exactly as reported.

Fix: match purely by email. The reported emails come only from the local xray (which only knows local-attached clients), so the query stays correctly scoped, and this also repairs already-broken rows that re-adding the client previously worked around.

Verified with a Go unit test plus a Docker end-to-end run (repointed a client's shared row at a node inbound, drove real traffic through the mother inbound, confirmed the row accumulated).

<!-- gh-comment-id:4626553238 --> @MHSanaei commented on GitHub (Jun 4, 2026): Fixed in e0845626. **Root cause:** `client_traffics` is keyed by email — one shared row per client across every inbound it is attached to. The local traffic updater filtered with `inbound_id NOT IN (node inbounds)`, so when a client was attached to both a node inbound and the mother inbound and the **node inbound was attached first**, the shared row carried the node inbound's id (`AddClientStat` uses `OnConflict DoNothing` and never refreshes it). The local xray's traffic for that client was then dropped entirely — the client appeared online but its usage stayed at zero. Attaching the mother inbound first worked around it, exactly as reported. **Fix:** match purely by email. The reported emails come only from the local xray (which only knows local-attached clients), so the query stays correctly scoped, and this also repairs already-broken rows that re-adding the client previously worked around. Verified with a Go unit test plus a Docker end-to-end run (repointed a client's shared row at a node inbound, drove real traffic through the mother inbound, confirmed the row accumulated).
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#3439
No description provided.