From aa5eb74d75d5a10d937a505b7e47be6a9c9530f7 Mon Sep 17 00:00:00 2001 From: Machka Pasla <161734431+machka-pasla@users.noreply.github.com> Date: Sun, 29 Jun 2025 00:52:27 +0300 Subject: [PATCH] Rename user traffic env vars --- .env.example | 12 ++++---- README.md | 3 +- bot/services/referral_service.py | 3 +- bot/services/subscription_service.py | 42 +++++++++++++++++++++------- config/settings.py | 20 ++++++++----- 5 files changed, 54 insertions(+), 26 deletions(-) diff --git a/.env.example b/.env.example index dbe84f0..cc5bb03 100644 --- a/.env.example +++ b/.env.example @@ -78,13 +78,13 @@ REFEREE_BONUS_DAYS_12_MONTHS=15 PANEL_API_URL=http://your_panel_api_url/api PANEL_API_KEY=your_panel_api_key -# Default settings for NEW panel users -PANEL_USER_DEFAULT_EXPIRE_DAYS=1 -PANEL_USER_DEFAULT_TRAFFIC_BYTES=0 -PANEL_USER_DEFAULT_TRAFFIC_STRATEGY="NO_RESET" +# User traffic limits (applied for all users) +# 0 means unlimited +USER_TRAFFIC_LIMIT_GB=0 +USER_TRAFFIC_STRATEGY="NO_RESET" -# Default Inbounds for Panel Users (Optional, comma-separated UUIDs) -PANEL_USER_DEFAULT_INBOUND_UUIDS=uuid1,uuid2,uuid3 +# Default Inbounds for Users (Optional, comma-separated UUIDs) +USER_INBOUND_UUIDS=uuid1,uuid2,uuid3 # Trial Settings TRIAL_ENABLED=True diff --git a/README.md b/README.md index 2efaccb..b74d089 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,8 @@ This Telegram bot is designed to automate the sale and management of subscriptio * **Panel API Settings:** * `PANEL_API_URL`: Full URL to your Remnawave panel's API (e.g., `http://remnawave:3000/api` or `https://panel.yourdomain.com/api`). * `PANEL_API_KEY`: API Key for authenticating with the Remnawave panel. - * `PANEL_USER_DEFAULT_INBOUND_UUIDS`: (Optional) Comma-separated list of inbound UUIDs from your panel to assign to users. If empty, `activateAllInbounds: true` (panel default) is used for new users. + * `USER_INBOUND_UUIDS`: (Optional) Comma-separated list of inbound UUIDs from your panel to assign to users. If empty, `activateAllInbounds: true` (panel default) is used for new users. + * `USER_TRAFFIC_LIMIT_GB` and `USER_TRAFFIC_STRATEGY`: Default traffic limit in gigabytes (0 for unlimited) and the reset strategy applied when updating users on the panel. * `TRIAL_ENABLED`, `TRIAL_DURATION_DAYS`, `TRIAL_TRAFFIC_LIMIT_GB`: Settings for the trial period. * `WEB_SERVER_HOST`, `WEB_SERVER_PORT`: Host and port for the bot's internal webhook server. * `LOGS_PAGE_SIZE`: For admin panel log pagination. diff --git a/bot/services/referral_service.py b/bot/services/referral_service.py index 6254c18..30681d8 100644 --- a/bot/services/referral_service.py +++ b/bot/services/referral_service.py @@ -137,8 +137,7 @@ class ReferralService: "status_from_panel": "ACTIVE_BONUS", "traffic_limit_bytes": - self.settings. - PANEL_USER_DEFAULT_TRAFFIC_BYTES, + self.settings.user_traffic_limit_bytes, } try: await subscription_dal.deactivate_other_active_subscriptions( diff --git a/bot/services/subscription_service.py b/bot/services/subscription_service.py index f1341ba..f681e71 100644 --- a/bot/services/subscription_service.py +++ b/bot/services/subscription_service.py @@ -99,6 +99,26 @@ class SubscriptionService: logging.warning( f"Local panel_uuid {current_local_panel_uuid} for TG user {user_id} also not found on panel. User might be deleted from panel or UUID desynced." ) + logging.info( + f"Creating new panel user '{panel_username_on_panel_standard}' for TG user {user_id}." + ) + creation_response = await self.panel_service.create_panel_user( + username_on_panel=panel_username_on_panel_standard, + telegram_id=user_id, + specific_inbound_uuids=self.settings.parsed_user_inbound_uuids, + default_traffic_limit_bytes=self.settings.user_traffic_limit_bytes, + default_traffic_limit_strategy=self.settings.USER_TRAFFIC_STRATEGY, + ) + if ( + creation_response + and not creation_response.get("error") + and creation_response.get("response") + ): + panel_user_obj_from_api = creation_response.get("response") + panel_user_created_or_linked_now = True + else: + await self._notify_admin_panel_user_creation_failed(user_id) + return None, None, None, False else: @@ -108,7 +128,9 @@ class SubscriptionService: creation_response = await self.panel_service.create_panel_user( username_on_panel=panel_username_on_panel_standard, telegram_id=user_id, - specific_inbound_uuids=self.settings.parsed_default_panel_user_inbound_uuids, + specific_inbound_uuids=self.settings.parsed_user_inbound_uuids, + default_traffic_limit_bytes=self.settings.user_traffic_limit_bytes, + default_traffic_limit_strategy=self.settings.USER_TRAFFIC_STRATEGY, ) if ( creation_response @@ -333,11 +355,11 @@ class SubscriptionService: ), "status": "ACTIVE", "trafficLimitBytes": self.settings.trial_traffic_limit_bytes, - "trafficLimitStrategy": self.settings.PANEL_USER_DEFAULT_TRAFFIC_STRATEGY, + "trafficLimitStrategy": self.settings.USER_TRAFFIC_STRATEGY, } - if self.settings.parsed_default_panel_user_inbound_uuids: + if self.settings.parsed_user_inbound_uuids: panel_update_payload["activeUserInbounds"] = ( - self.settings.parsed_default_panel_user_inbound_uuids + self.settings.parsed_user_inbound_uuids ) elif panel_user_created_now: panel_update_payload["activateAllInbounds"] = True @@ -460,7 +482,7 @@ class SubscriptionService: "duration_months": months, "is_active": True, "status_from_panel": "ACTIVE", - "traffic_limit_bytes": self.settings.PANEL_USER_DEFAULT_TRAFFIC_BYTES, + "traffic_limit_bytes": self.settings.user_traffic_limit_bytes, "provider": provider, "skip_notifications": provider == "tribute", } @@ -481,12 +503,12 @@ class SubscriptionService: "+00:00", "Z" ), "status": "ACTIVE", - "trafficLimitBytes": self.settings.PANEL_USER_DEFAULT_TRAFFIC_BYTES, - "trafficLimitStrategy": self.settings.PANEL_USER_DEFAULT_TRAFFIC_STRATEGY, + "trafficLimitBytes": self.settings.user_traffic_limit_bytes, + "trafficLimitStrategy": self.settings.USER_TRAFFIC_STRATEGY, } - if self.settings.parsed_default_panel_user_inbound_uuids: + if self.settings.parsed_user_inbound_uuids: panel_update_payload["activeUserInbounds"] = ( - self.settings.parsed_default_panel_user_inbound_uuids + self.settings.parsed_user_inbound_uuids ) elif panel_user_created_now: panel_update_payload["activateAllInbounds"] = True @@ -554,7 +576,7 @@ class SubscriptionService: "duration_months": 0, "is_active": True, "status_from_panel": "ACTIVE_BONUS", - "traffic_limit_bytes": self.settings.PANEL_USER_DEFAULT_TRAFFIC_BYTES, + "traffic_limit_bytes": self.settings.user_traffic_limit_bytes, } await subscription_dal.deactivate_other_active_subscriptions( session, panel_uuid, panel_sub_uuid diff --git a/config/settings.py b/config/settings.py index b3ab200..9c0d339 100644 --- a/config/settings.py +++ b/config/settings.py @@ -86,10 +86,9 @@ class Settings(BaseSettings): PANEL_API_URL: Optional[str] = None PANEL_API_KEY: Optional[str] = None - PANEL_USER_DEFAULT_EXPIRE_DAYS: int = Field(default=1) - PANEL_USER_DEFAULT_TRAFFIC_BYTES: int = Field(default=0) - PANEL_USER_DEFAULT_TRAFFIC_STRATEGY: str = Field(default="NO_RESET") - PANEL_USER_DEFAULT_INBOUND_UUIDS: Optional[str] = Field( + USER_TRAFFIC_LIMIT_GB: Optional[float] = Field(default=0.0) + USER_TRAFFIC_STRATEGY: str = Field(default="NO_RESET") + USER_INBOUND_UUIDS: Optional[str] = Field( default=None, description= "Comma-separated UUIDs of inbounds to activate for new panel users") @@ -139,11 +138,18 @@ class Settings(BaseSettings): @computed_field @property - def parsed_default_panel_user_inbound_uuids(self) -> Optional[List[str]]: - if self.PANEL_USER_DEFAULT_INBOUND_UUIDS: + def user_traffic_limit_bytes(self) -> int: + if self.USER_TRAFFIC_LIMIT_GB is None or self.USER_TRAFFIC_LIMIT_GB <= 0: + return 0 + return int(self.USER_TRAFFIC_LIMIT_GB * (1024**3)) + + @computed_field + @property + def parsed_user_inbound_uuids(self) -> Optional[List[str]]: + if self.USER_INBOUND_UUIDS: return [ uuid.strip() - for uuid in self.PANEL_USER_DEFAULT_INBOUND_UUIDS.split(',') + for uuid in self.USER_INBOUND_UUIDS.split(',') if uuid.strip() ] return None