How kereal/rs8kvn_bot works
Purpose and Entry Points
This Go service distributes VPN subscriptions through a Telegram bot and HTTP endpoints. It manages users, plans, orders, trials, referrals, subscriptions, and synchronization to VPN nodes. Process startup and lifecycle wiring are in cmd/bot/main.go and cmd/bot/lifecycle.go; the built binary runs as ./cmd/bot.
Composition Root and Runtime
Configuration, logging, database migration, HTTP server, Telegram handlers, workers, and shutdown are assembled at startup. The runtime is container-oriented (Dockerfile, docker-compose.yml) and exposes HTTP on port 8880. Configuration is environment-driven through internal/config/config.go; operational liveness/readiness and metrics are part of the same process.
Telegram Interaction Layer
internal/bot/handler.go routes Telegram updates into command, callback, menu, admin, referral, and subscription flows. It is the user-facing control plane: it creates or displays subscriptions, emits QR/import links, administers broadcasts, and exposes referral actions. Delivery and UI formatting are separated into internal/bot/message_sender.go, internal/bot/menu.go, and internal/bot/subscription_handler.go.
Domain Service Layer
Business workflows sit behind bot and HTTP transports. Subscription lifecycle, traffic, reminders, and node synchronization are handled by internal/service/subscription.go, internal/service/subscription_traffic.go, and internal/service/sync.go. Order and product/plan behavior is represented by internal/service/order.go. This layer coordinates durable database state with remote VPN-panel state rather than letting transports call panels directly.
Persistence and State Model
PostgreSQL-style relational persistence is abstracted by internal/database/service.go, with repositories for subscriptions, nodes, plans/products, orders, invites, and trials. The embedded migration sequence establishes the architectural model: subscriptions map to plans and multiple nodes, while orders/products support commercial flows; see internal/database/migrations/017_create_orders.up.sql, internal/database/migrations/018_create_subscription_nodes.up.sql, and internal/database/subscription_nodes.go. Backup creation/rotation is provided by internal/backup/backup.go.
VPN Node Boundary
internal/vpn/client.go defines the multi-node client abstraction. Concrete integrations include 3x-ui, proxman, and direct fetch sources in internal/vpn/threex_ui.go, internal/vpn/proxman.go, and internal/vpn/fetch.go. Sync services provision/update/delete remote clients per assigned node; failures and remote availability are therefore isolated at this adapter boundary.
HTTP Delivery and Subscription Aggregation
internal/web/web.go exposes health, readiness, invite/trial, metrics, payment callback stub, and subscription routes. The subscription server in internal/subserver/service.go and internal/subserver/subscription_handler.go validates /sub/{subID}, caches responses, fetches all active assigned nodes, merges protocol outputs, adds profile/device headers, and records access data. Its node fetch boundary is implemented by internal/subserver/fetch.go.
Background Operations and Observability
Periodic workers execute backup, expiry, trial cleanup, reminder, and pending subscription-sync work via internal/scheduler/subscription_sync_worker.go, internal/scheduler/subscription_expire_worker.go, and internal/scheduler/backup.go. Prometheus instrumentation is exposed through internal/metrics/metrics.go; external error/performance reporting is Sentry. In-memory caches and rate limiting protect recurring reads and public trial routes (internal/bot/referral_cache.go, internal/ratelimiter/per_user.go).
Main entry points: service.go Relational persistence · main.go Startup & lifecycle · handler.go Telegram update router · web.go HTTP endpoints
Components 18
Runtime
- Startup & lifecycle · Assembles process dependencies and shutdown.
cmd/bot/main.go - Environment config
internal/config/config.go
Delivery
- Telegram update router · Routes commands, callbacks, menus, admin, referral, and subscription actions.
internal/bot/handler.go - Bot UI delivery · Formats menus and subscription messages.
internal/bot/message_sender.go - HTTP endpoints · Serves health, readiness, invites, trials, metrics, callbacks, and subscription routes.
internal/web/web.go - Subscription server · Validates public subscriptions and composes assigned-node outputs.
internal/subserver/service.go
Domain workflows
- Subscription lifecycle · Coordinates lifecycle, traffic, reminders, and durable state.
internal/service/subscription.go - Orders & plans
internal/service/order.go - Node synchronization · Reconciles subscription clients across assigned VPN nodes.
internal/service/sync.go
State and adapters
- Relational persistence · Repository boundary for subscriptions, nodes, plans, orders, invites, and trials.
internal/database/service.go - Subscription-node assignments · Maps subscriptions to multiple VPN nodes.
internal/database/subscription_nodes.go - VPN client abstraction · Isolates remote node provisioning and availability failures.
internal/vpn/client.go - VPN panel adapters · 3x-ui, proxman, and direct-fetch integrations.
internal/vpn/threex_ui.go - Node subscription fetch
internal/subserver/fetch.go
Background operations
- Periodic workers · Runs expiry, trial cleanup, reminders, backup, and pending sync work.
internal/scheduler/subscription_sync_worker.go - Database backup rotation
internal/backup/backup.go - Metrics & reporting · Prometheus metrics and Sentry reporting.
internal/metrics/metrics.go - Public route protection · Protects recurring reads and public trial routes.
internal/ratelimiter/per_user.go
Connections 24
- Startup & lifecycle → Environment config: loads
- Startup & lifecycle → Telegram update router: wires
- Startup & lifecycle → HTTP endpoints: serves :8880
- Startup & lifecycle → Periodic workers: starts
- Startup & lifecycle → Metrics & reporting: initializes
- Telegram update router → Bot UI delivery: renders via
- Telegram update router → Subscription lifecycle: subscription actions
- Telegram update router → Orders & plans: commercial actions
- HTTP endpoints → Subscription server: subscription route
- HTTP endpoints → Public route protection: guards public routes
- Subscription server → Relational persistence: validates subscription
- Subscription server → Node subscription fetch: fetches active nodes
- Node subscription fetch → VPN panel adapters: retrieves node output
- Subscription lifecycle → Relational persistence: reads and writes
- Orders & plans → Relational persistence: persists orders and plans
- Node synchronization → Relational persistence: reads assignments
- Node synchronization → VPN client abstraction: provisions clients
- VPN client abstraction → VPN panel adapters: implemented by
- Relational persistence → Subscription-node assignments: stores mapping
- Periodic workers → Subscription lifecycle: expiry and reminders
- Periodic workers → Node synchronization: pending sync
- Periodic workers → Database backup rotation: runs
- Database backup rotation → Relational persistence: backs up
- HTTP endpoints → Metrics & reporting: exposes