refactor: перенос логики работы с конфигурациями Xray из класса XrayConfig в классы User и UserFactory
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user