[PR #5326] [MERGED] feat(notifications): event bus architecture with Telegram and SMTP subscribers #5849

Closed
opened 2026-11-02 04:04:31 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/5326
Author: @Ssentiago
Created: 6/14/2026
Status: ✅ Merged
Merged: 6/15/2026
Merged by: @MHSanaei

Base: main ← Head: feat/notifications-eventbus


📝 Commits (6)

  • 6e593af feat(notifications): event bus architecture with Telegram and SMTP subscribers
  • 67e443e Merge branch 'main' into feat/notifications-eventbus
  • c8a1ad5 fix
  • d436374 fix(notifications): repair crash/CPU alerts, harden secrets, add node alerts
  • d611784 fix(settings): use antd Space orientation instead of deprecated direction
  • f4d0b93 i18n(notifications): translate the notification feature into all locales

📊 Changes

48 files changed (+3852 additions, -139 deletions)

View changed files

📝 frontend/public/openapi.json (+181 -10)
➕ frontend/src/components/ui/EventBusCheckboxes.tsx (+147 -0)
📝 frontend/src/components/ui/index.ts (+1 -0)
📝 frontend/src/generated/examples.ts (+21 -2)
📝 frontend/src/generated/schemas.ts (+112 -10)
📝 frontend/src/generated/types.ts (+21 -2)
📝 frontend/src/generated/zod.ts (+21 -2)
📝 frontend/src/layouts/AppSidebar.tsx (+2 -0)
📝 frontend/src/models/setting.ts (+11 -2)
📝 frontend/src/pages/api-docs/endpoints.ts (+12 -0)
➕ frontend/src/pages/settings/EmailTab.tsx (+137 -0)
📝 frontend/src/pages/settings/SettingsPage.tsx (+3 -1)
📝 frontend/src/pages/settings/TelegramTab.tsx (+45 -12)
📝 frontend/src/schemas/setting.ts (+1 -1)
➕ internal/eventbus/bus.go (+123 -0)
➕ internal/eventbus/bus_test.go (+199 -0)
➕ internal/eventbus/events.go (+64 -0)
➕ internal/eventbus/filter.go (+33 -0)
📝 internal/web/controller/setting.go (+61 -2)
📝 internal/web/entity/entity.go (+22 -10)

...and 28 more files

📄 Description

Closes #5188

Summary

Add event bus notification system for 3x-ui panel. Replaces hardcoded Telegram bot notifications with a flexible pub/sub architecture that supports multiple notification channels (Telegram + Email/SMTP).

Why

Previously, notification logic was scattered as direct SendMsgToTgbotAdmins() calls in separate files (check_cpu_usage.go, tgbot_report.go). Adding a new notification channel (like email) would require modifying every one of those call sites. The event bus decouples publishers from channels — a new subscriber receives all events with zero changes to existing code.

What changed

  • Event bus — buffered pub/sub with fan-out, panic recovery, and rate limiting
  • Telegram subscriber — migrated from direct calls to event bus subscriber
  • Email subscriber — new SMTP channel with TLS/STARTTLS support and stage-by-stage diagnostics
  • 5 event types — outbound health, xray crash, CPU threshold, login attempts
  • Frontend — event selection checkboxes with inline CPU threshold input
  • i18n — all 13 locales updated (en-US and ru-RU with full translations, other 11 locales receive English defaults so translators can see what needs translating)

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

Added files

Event Bus Core

File Description
internal/eventbus/bus.go Event bus with buffered channel, fan-out, panic recovery. Subscribe/Unsubscribe/Publish pattern.
internal/eventbus/bus_test.go 7 unit tests: publish/subscribe, multiple subscribers, unsubscribe, panic recovery, buffer full, zero timestamp.
internal/eventbus/events.go Event type constants (outbound.down/up, xray.crash, cpu.high, login.attempt) and data structs (OutboundHealthData, LoginEventData).
internal/eventbus/filter.go Shared RateLimiter (1 min cooldown) and XrayStateTracker (prevents duplicate crash notifications).

Email Notifications

File Description
internal/web/service/email/email.go SMTP service with TLS/STARTTLS support, 30s timeout, TestConnection() with stage-by-stage testing (connect/auth/send), classifySMTPError() for human-readable errors.
internal/web/service/email/subscriber.go Email event bus subscriber with rate limiting, Xray state tracking, login events bypass rate limiter.
internal/web/service/email/ratelimiter_test.go 3 unit tests for rate limiter: allow, cooldown, per-type.

Telegram Notifications

File Description
internal/web/service/tgbot/tgbot_event.go Telegram event bus subscriber with HTML formatting, rate limiting, Xray state tracking.

Frontend

File Description
frontend/src/components/ui/EventBusCheckboxes.tsx Reusable collapsible event group checkboxes with inline CPU threshold input.
frontend/src/pages/settings/EmailTab.tsx Email settings tab with SMTP configuration, encryption selector (none/STARTTLS/TLS), test button with stage reporting.

Modified files

Backend - Event Publishing

File Changes
internal/xray/process.go Added OnCrash func(err error) callback field; calls OnCrash when Xray process fails unexpectedly.
internal/web/web.go Initializes event bus; registers email and Telegram subscribers; wires Xray crash callback.
internal/web/service/xray_metrics.go Publishes EventOutboundDown/Up when outbound transitions between alive↔dead states.
internal/web/job/check_cpu_usage.go Changed from direct SendMsgToTgbotAdmins() to EventBus.Publish(EventCPUHigh). Removed tgbot dependency, uses locale.I18n directly.
internal/web/job/check_xray_running_job.go Added EventBus global variable.
internal/web/service/tgbot/tgbot_report.go Modified UserLoginNotify() to publish EventLoginAttempt to event bus instead of direct Telegram send.
internal/web/service/tgbot/tgbot.go Added EventBus global variable.
internal/web/service/tgbot/tgbot_send.go Added TestConnection() method for Telegram bot testing.

Backend - Settings & API

File Changes
internal/web/entity/entity.go Removed TgBotLoginNotify; added TgEnabledEvents, SmtpEnable, SmtpHost, SmtpPort, SmtpUsername, SmtpPassword, SmtpTo, SmtpEncryptionType, SmtpEnabledEvents, SmtpCpu.
internal/web/service/setting.go Added tgEnabledEvents (default: "login.attempt,cpu.high"), smtpEnabledEvents (default: "login.attempt,cpu.high"), smtpCpu, smtpEncryptionType to defaults; added getters/setters.
internal/web/controller/setting.go Added testSmtp (POST /setting/testSmtp) and testTgBot (POST /setting/testTgBot) endpoints with i18n.

Frontend - Settings

File Changes
frontend/src/models/setting.ts Removed tgBotLoginNotify; added smtpEnable, smtpHost, smtpPort, smtpUsername, smtpPassword, smtpTo, smtpEncryptionType, smtpEnabledEvents, smtpCpu, tgEnabledEvents.
frontend/src/pages/settings/TelegramTab.tsx Replaced individual login/CPU toggles with EventBusCheckboxes; added test button.
frontend/src/pages/settings/SettingsPage.tsx Added EmailTab route.
frontend/src/layouts/AppSidebar.tsx Added "Email" menu item in settings.
frontend/src/components/ui/index.ts Exported EventBusCheckboxes component.
frontend/src/pages/api-docs/endpoints.ts Added testSmtp and testTgBot endpoint documentation.

Generated files

File Changes
frontend/public/openapi.json Added testSmtp/testTgBot paths and new SMTP/TG settings schemas.
frontend/src/generated/types.ts Added smtpCpu, smtpEnabledEvents, smtpEncryptionType, tgEnabledEvents types.
frontend/src/generated/zod.ts Added Zod schemas for new fields.
frontend/src/generated/schemas.ts Added JSON schema definitions for new fields.
frontend/src/generated/examples.ts Updated examples with new fields.

i18n (all 13 locale files)

File Changes
internal/web/translation/en-US.json Added ~50 keys for events, SMTP, email, UI labels.
internal/web/translation/ru-RU.json Added ~50 keys with Russian translations.
internal/web/translation/*.json (11 other locales) Added ~50 keys with English defaults for translators.

How was this tested?

  1. Telegram notifications:

    • Enable Telegram bot in Settings → Telegram Bot
    • Enable events in "Telegram Event Notifications" checkboxes
    • Login to panel → verify login notification received
    • Kill Xray process → verify crash notification received
    • Check CPU threshold → verify notification when exceeded
  2. Email notifications:

    • Configure SMTP in Settings → Email (host, port, username, password, encryption)
    • Click "Test" button → verify connection test with stage reporting
    • Enable events in "Email Event Notifications" checkboxes
    • Login to panel → verify login notification received via email
  3. Outbound health notifications:

    • Configure outbound with observatory enabled
    • Disable outbound connection → verify "down" notification
    • Re-enable outbound → verify "up" notification
    • Verify rate limiting (1 notification per minute per outbound)
  4. Xray crash notifications:

    • Kill Xray process → verify crash notification with error message
    • Verify state tracking (only one notification per crash, no duplicates)
    • Restart Xray → verify system recovers
  5. Rate limiting:

    • Trigger outbound down/up rapidly → verify only one notification per minute
    • Verify login notifications bypass rate limiter (success after failure from same IP)
    • Verify CPU notifications rate limited (1 per minute)
  6. Unit tests:

    • Event bus: 7 tests (publish/subscribe, multiple subscribers, unsubscribe, panic recovery, buffer full)
    • Rate limiter: 3 tests (allow, cooldown, per-type)
    • All existing project tests pass

Screenshots / recordings

Two tabs in settings - Telegram Bot and Email:
image

Telegram bot notification settings section:
image

Email settings section:
image

Email notification settings section:
image

Breaking changes

None. The notification system is backward compatible:

  • Login and CPU notifications are enabled by default in both tgEnabledEvents and smtpEnabledEvents
  • Outbound and Xray crash notifications are opt-in via settings
  • New features (Email, event checkboxes) are opt-in via settings

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass.
  • I updated the Wiki / README / API docs if user-facing behavior changed.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/5326 **Author:** [@Ssentiago](https://github.com/Ssentiago) **Created:** 6/14/2026 **Status:** ✅ Merged **Merged:** 6/15/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `feat/notifications-eventbus` --- ### 📝 Commits (6) - [`6e593af`](https://github.com/MHSanaei/3x-ui/commit/6e593af6e366380c5f1698c30c70b7b99b3a4146) feat(notifications): event bus architecture with Telegram and SMTP subscribers - [`67e443e`](https://github.com/MHSanaei/3x-ui/commit/67e443eace7c8b108653d461a6f84a02e0803b31) Merge branch 'main' into feat/notifications-eventbus - [`c8a1ad5`](https://github.com/MHSanaei/3x-ui/commit/c8a1ad507e9f8ce81c809c146728b4196ec5a5f4) fix - [`d436374`](https://github.com/MHSanaei/3x-ui/commit/d436374c900dc997113bbf0bc45e3e7cb8524f7b) fix(notifications): repair crash/CPU alerts, harden secrets, add node alerts - [`d611784`](https://github.com/MHSanaei/3x-ui/commit/d6117845636e300701944f3377a4e842f804054e) fix(settings): use antd Space orientation instead of deprecated direction - [`f4d0b93`](https://github.com/MHSanaei/3x-ui/commit/f4d0b931e7b3e5e50113dcbd4fabf6ced736310f) i18n(notifications): translate the notification feature into all locales ### 📊 Changes **48 files changed** (+3852 additions, -139 deletions) <details> <summary>View changed files</summary> 📝 `frontend/public/openapi.json` (+181 -10) ➕ `frontend/src/components/ui/EventBusCheckboxes.tsx` (+147 -0) 📝 `frontend/src/components/ui/index.ts` (+1 -0) 📝 `frontend/src/generated/examples.ts` (+21 -2) 📝 `frontend/src/generated/schemas.ts` (+112 -10) 📝 `frontend/src/generated/types.ts` (+21 -2) 📝 `frontend/src/generated/zod.ts` (+21 -2) 📝 `frontend/src/layouts/AppSidebar.tsx` (+2 -0) 📝 `frontend/src/models/setting.ts` (+11 -2) 📝 `frontend/src/pages/api-docs/endpoints.ts` (+12 -0) ➕ `frontend/src/pages/settings/EmailTab.tsx` (+137 -0) 📝 `frontend/src/pages/settings/SettingsPage.tsx` (+3 -1) 📝 `frontend/src/pages/settings/TelegramTab.tsx` (+45 -12) 📝 `frontend/src/schemas/setting.ts` (+1 -1) ➕ `internal/eventbus/bus.go` (+123 -0) ➕ `internal/eventbus/bus_test.go` (+199 -0) ➕ `internal/eventbus/events.go` (+64 -0) ➕ `internal/eventbus/filter.go` (+33 -0) 📝 `internal/web/controller/setting.go` (+61 -2) 📝 `internal/web/entity/entity.go` (+22 -10) _...and 28 more files_ </details> ### 📄 Description Closes #5188 ## Summary Add event bus notification system for 3x-ui panel. Replaces hardcoded Telegram bot notifications with a flexible pub/sub architecture that supports multiple notification channels (Telegram + Email/SMTP). ## Why Previously, notification logic was scattered as direct `SendMsgToTgbotAdmins()` calls in separate files (`check_cpu_usage.go`, `tgbot_report.go`). Adding a new notification channel (like email) would require modifying every one of those call sites. The event bus decouples publishers from channels — a new subscriber receives all events with zero changes to existing code. ## What changed - **Event bus** — buffered pub/sub with fan-out, panic recovery, and rate limiting - **Telegram subscriber** — migrated from direct calls to event bus subscriber - **Email subscriber** — new SMTP channel with TLS/STARTTLS support and stage-by-stage diagnostics - **5 event types** — outbound health, xray crash, CPU threshold, login attempts - **Frontend** — event selection checkboxes with inline CPU threshold input - **i18n** — all 13 locales updated (en-US and ru-RU with full translations, other 11 locales receive English defaults so translators can see what needs translating) ## Type of change - [ ] Bug fix - [x] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [x] Frontend (UI / panel pages) - [x] Backend (API endpoints, login, settings) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [x] Telegram bot ## Added files ### Event Bus Core | File | Description | |------|-------------| | `internal/eventbus/bus.go` | Event bus with buffered channel, fan-out, panic recovery. Subscribe/Unsubscribe/Publish pattern. | | `internal/eventbus/bus_test.go` | 7 unit tests: publish/subscribe, multiple subscribers, unsubscribe, panic recovery, buffer full, zero timestamp. | | `internal/eventbus/events.go` | Event type constants (outbound.down/up, xray.crash, cpu.high, login.attempt) and data structs (OutboundHealthData, LoginEventData). | | `internal/eventbus/filter.go` | Shared RateLimiter (1 min cooldown) and XrayStateTracker (prevents duplicate crash notifications). | ### Email Notifications | File | Description | |------|-------------| | `internal/web/service/email/email.go` | SMTP service with TLS/STARTTLS support, 30s timeout, TestConnection() with stage-by-stage testing (connect/auth/send), classifySMTPError() for human-readable errors. | | `internal/web/service/email/subscriber.go` | Email event bus subscriber with rate limiting, Xray state tracking, login events bypass rate limiter. | | `internal/web/service/email/ratelimiter_test.go` | 3 unit tests for rate limiter: allow, cooldown, per-type. | ### Telegram Notifications | File | Description | |------|-------------| | `internal/web/service/tgbot/tgbot_event.go` | Telegram event bus subscriber with HTML formatting, rate limiting, Xray state tracking. | ### Frontend | File | Description | |------|-------------| | `frontend/src/components/ui/EventBusCheckboxes.tsx` | Reusable collapsible event group checkboxes with inline CPU threshold input. | | `frontend/src/pages/settings/EmailTab.tsx` | Email settings tab with SMTP configuration, encryption selector (none/STARTTLS/TLS), test button with stage reporting. | ## Modified files ### Backend - Event Publishing | File | Changes | |------|---------| | `internal/xray/process.go` | Added `OnCrash func(err error)` callback field; calls OnCrash when Xray process fails unexpectedly. | | `internal/web/web.go` | Initializes event bus; registers email and Telegram subscribers; wires Xray crash callback. | | `internal/web/service/xray_metrics.go` | Publishes EventOutboundDown/Up when outbound transitions between alive↔dead states. | | `internal/web/job/check_cpu_usage.go` | Changed from direct `SendMsgToTgbotAdmins()` to `EventBus.Publish(EventCPUHigh)`. Removed tgbot dependency, uses locale.I18n directly. | | `internal/web/job/check_xray_running_job.go` | Added `EventBus` global variable. | | `internal/web/service/tgbot/tgbot_report.go` | Modified `UserLoginNotify()` to publish EventLoginAttempt to event bus instead of direct Telegram send. | | `internal/web/service/tgbot/tgbot.go` | Added `EventBus` global variable. | | `internal/web/service/tgbot/tgbot_send.go` | Added `TestConnection()` method for Telegram bot testing. | ### Backend - Settings & API | File | Changes | |------|---------| | `internal/web/entity/entity.go` | Removed TgBotLoginNotify; added TgEnabledEvents, SmtpEnable, SmtpHost, SmtpPort, SmtpUsername, SmtpPassword, SmtpTo, SmtpEncryptionType, SmtpEnabledEvents, SmtpCpu. | | `internal/web/service/setting.go` | Added tgEnabledEvents (default: "login.attempt,cpu.high"), smtpEnabledEvents (default: "login.attempt,cpu.high"), smtpCpu, smtpEncryptionType to defaults; added getters/setters. | | `internal/web/controller/setting.go` | Added testSmtp (POST /setting/testSmtp) and testTgBot (POST /setting/testTgBot) endpoints with i18n. | ### Frontend - Settings | File | Changes | |------|---------| | `frontend/src/models/setting.ts` | Removed tgBotLoginNotify; added smtpEnable, smtpHost, smtpPort, smtpUsername, smtpPassword, smtpTo, smtpEncryptionType, smtpEnabledEvents, smtpCpu, tgEnabledEvents. | | `frontend/src/pages/settings/TelegramTab.tsx` | Replaced individual login/CPU toggles with EventBusCheckboxes; added test button. | | `frontend/src/pages/settings/SettingsPage.tsx` | Added EmailTab route. | | `frontend/src/layouts/AppSidebar.tsx` | Added "Email" menu item in settings. | | `frontend/src/components/ui/index.ts` | Exported EventBusCheckboxes component. | | `frontend/src/pages/api-docs/endpoints.ts` | Added testSmtp and testTgBot endpoint documentation. | ### Generated files | File | Changes | |------|---------| | `frontend/public/openapi.json` | Added testSmtp/testTgBot paths and new SMTP/TG settings schemas. | | `frontend/src/generated/types.ts` | Added smtpCpu, smtpEnabledEvents, smtpEncryptionType, tgEnabledEvents types. | | `frontend/src/generated/zod.ts` | Added Zod schemas for new fields. | | `frontend/src/generated/schemas.ts` | Added JSON schema definitions for new fields. | | `frontend/src/generated/examples.ts` | Updated examples with new fields. | ### i18n (all 13 locale files) | File | Changes | |------|---------| | `internal/web/translation/en-US.json` | Added ~50 keys for events, SMTP, email, UI labels. | | `internal/web/translation/ru-RU.json` | Added ~50 keys with Russian translations. | | `internal/web/translation/*.json` (11 other locales) | Added ~50 keys with English defaults for translators. | ## How was this tested? 1. **Telegram notifications:** - Enable Telegram bot in Settings → Telegram Bot - Enable events in "Telegram Event Notifications" checkboxes - Login to panel → verify login notification received - Kill Xray process → verify crash notification received - Check CPU threshold → verify notification when exceeded 2. **Email notifications:** - Configure SMTP in Settings → Email (host, port, username, password, encryption) - Click "Test" button → verify connection test with stage reporting - Enable events in "Email Event Notifications" checkboxes - Login to panel → verify login notification received via email 3. **Outbound health notifications:** - Configure outbound with observatory enabled - Disable outbound connection → verify "down" notification - Re-enable outbound → verify "up" notification - Verify rate limiting (1 notification per minute per outbound) 4. **Xray crash notifications:** - Kill Xray process → verify crash notification with error message - Verify state tracking (only one notification per crash, no duplicates) - Restart Xray → verify system recovers 5. **Rate limiting:** - Trigger outbound down/up rapidly → verify only one notification per minute - Verify login notifications bypass rate limiter (success after failure from same IP) - Verify CPU notifications rate limited (1 per minute) 6. **Unit tests:** - Event bus: 7 tests (publish/subscribe, multiple subscribers, unsubscribe, panic recovery, buffer full) - Rate limiter: 3 tests (allow, cooldown, per-type) - All existing project tests pass ## Screenshots / recordings Two tabs in settings - Telegram Bot and Email: <img width="230" height="282" alt="image" src="https://github.com/user-attachments/assets/c930dbd0-9175-4425-8321-32fc97bdb15b" /> Telegram bot notification settings section: <img width="1239" height="701" alt="image" src="https://github.com/user-attachments/assets/8dd791bf-eb3f-47eb-9d69-d245bc8d50f1" /> Email settings section: <img width="1280" height="695" alt="image" src="https://github.com/user-attachments/assets/4f88ece3-c6df-4281-91f7-45d0c0dc59ca" /> Email notification settings section: <img width="1248" height="440" alt="image" src="https://github.com/user-attachments/assets/2b294542-a3d7-4933-af89-3cedde3e9b79" /> ## Breaking changes None. The notification system is backward compatible: - Login and CPU notifications are enabled by default in both tgEnabledEvents and smtpEnabledEvents - Outbound and Xray crash notifications are opt-in via settings - New features (Email, event checkboxes) are opt-in via settings ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [x] `go build ./...` and the test suite pass locally. - [x] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. - [x] I updated the Wiki / README / API docs if user-facing behavior changed. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:04:31 +07:00
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#5849
No description provided.