From 3f2ccc6f04f894507649369c12e6b8a730a5f9c9 Mon Sep 17 00:00:00 2001 From: vladimir Date: Mon, 6 Jul 2026 02:58:08 +0300 Subject: [PATCH] =?UTF-8?q?refactor:=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B8=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20=D1=81=20=D0=BA=D0=BE=D0=BD?= =?UTF-8?q?=D1=84=D0=B8=D0=B3=D1=83=D1=80=D0=B0=D1=86=D0=B8=D1=8F=D0=BC?= =?UTF-8?q?=D0=B8=20Xray=20=D0=B8=D0=B7=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D0=B0=20XrayConfig=20=D0=B2=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D1=8B=20User=20=D0=B8=20UserFactory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/xray_manager/core/user.py | 38 +++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/xray_manager/core/user.py b/src/xray_manager/core/user.py index 7bcb6a2..8eaa6a4 100644 --- a/src/xray_manager/core/user.py +++ b/src/xray_manager/core/user.py @@ -7,7 +7,7 @@ from .outbound import Outbound, OutboundFactory, OutboundSpec if TYPE_CHECKING: from .target import TargetStorage - from .xray_config import XrayConfig + from .xray_config import XrayConfig, XrayStorage @dataclass @@ -41,6 +41,18 @@ class User: self.outbounds = list(user_specs.values()) + def add_to_xray_inbounds(self, xrs: XrayStorage): + added = False + xray_inbounds = xrs.load_inbounds() + for outbound in self.outbounds: + for xray_inbound in xray_inbounds: + if outbound.matches_inbound(xray_inbound): + outbound.add_to_inbound(xray_inbound, self.username) + added = True + + if not added: + raise RuntimeError(f"Inbound not found for {outbound}") + class UserFactory: @staticmethod @@ -62,5 +74,27 @@ class UserFactory: return user @staticmethod - def empty(username: str): + def empty(username: str) -> User: return User(username, []) + + @staticmethod + def from_xray_inbound(username: str, ts: TargetStorage, xrs: XrayStorage): + user_outbounds: list[Outbound] = [] + + inbounds: list[dict] = xrs.load_inbounds() + for inbound in inbounds: + protocol = inbound.get("protocol") + + if protocol in OutboundFactory._registry: + settings = inbound.get("settings", {}) + clients = settings.get("clients", []) + + for client in clients: + email = client.get("email", "") + inbound_username, _ = Outbound.split_client_email(email) + + if inbound_username == username: + user_outbound = OutboundFactory.from_xray_inbound( + ts, client, inbound + ) + user_outbounds.append(user_outbound)