1- import inspect
2-
31from PasarGuardNodeBridge import create_proxy , create_user
42from PasarGuardNodeBridge .common .service_pb2 import User as ProtoUser
53from sqlalchemy import and_ , func , select
64
75from app .db import AsyncSession
86from app .db .models import Group , ProxyInbound , User , UserStatus , inbounds_groups_association , users_groups_association
7+ from app .models .protocol import ProxyProtocol
98
10- _CREATE_PROXY_PARAMS = set ( inspect . signature ( create_proxy ). parameters )
9+ _ALL_PROXY_PROTOCOLS = frozenset ( ProxyProtocol )
1110
1211
1312def _inbounds_from_loaded_groups (user : User ) -> list [str ] | None :
@@ -30,7 +29,7 @@ def _inbounds_from_loaded_groups(user: User) -> list[str] | None:
3029 return list (tags )
3130
3231
33- async def serialize_user (user : User ) -> ProtoUser :
32+ async def serialize_user (user : User , allowed_protocols : frozenset [ ProxyProtocol ] | None = None ) -> ProtoUser :
3433 user_settings = user .proxy_settings
3534 inbounds = None
3635 status = user .__dict__ .get ("status" )
@@ -42,38 +41,52 @@ async def serialize_user(user: User) -> ProtoUser:
4241 if inbounds is None :
4342 inbounds = await user .inbounds ()
4443
45- return _serialize_user_for_node (user .id , user .username , user_settings , inbounds )
46-
47-
48- def _serialize_user_for_node (id : int , username : str , user_settings : dict , inbounds : list [str ] = None ) -> ProtoUser :
49- vmess_settings = user_settings .get ("vmess" , {})
50- vless_settings = user_settings .get ("vless" , {})
51- if vless_settings .get ("flow" ) == "xtls-rprx-vision-udp443" :
52- vless_settings ["flow" ] = "xtls-rprx-vision"
53- trojan_settings = user_settings .get ("trojan" , {})
54- shadowsocks_settings = user_settings .get ("shadowsocks" , {})
55- wireguard_settings = user_settings .get ("wireguard" , {})
56- hysteria_settings = user_settings .get ("hysteria" , {})
57- proxy_kwargs = {
58- "vmess_id" : vmess_settings .get ("id" ),
59- "vless_id" : vless_settings .get ("id" ),
60- "vless_flow" : vless_settings .get ("flow" ),
61- "trojan_password" : trojan_settings .get ("password" ),
62- "shadowsocks_password" : shadowsocks_settings .get ("password" ),
63- "shadowsocks_method" : shadowsocks_settings .get ("method" ),
64- "wireguard_public_key" : wireguard_settings .get ("public_key" ),
65- "wireguard_peer_ips" : wireguard_settings .get ("peer_ips" ) or [],
66- "hysteria_auth" : hysteria_settings .get ("auth" ),
67- }
44+ return _serialize_user_for_node (user .id , user .username , user_settings , inbounds , allowed_protocols )
45+
46+
47+ def _serialize_user_for_node (
48+ id : int ,
49+ username : str ,
50+ user_settings : dict ,
51+ inbounds : list [str ] = None ,
52+ allowed_protocols : frozenset [ProxyProtocol ] | None = None ,
53+ ) -> ProtoUser :
54+ allowed_protocols = allowed_protocols or _ALL_PROXY_PROTOCOLS
55+
56+ proxy_kwargs = {}
57+ if ProxyProtocol .vmess in allowed_protocols :
58+ proxy_kwargs ["vmess_id" ] = user_settings .get ("vmess" , {}).get ("id" )
59+ if ProxyProtocol .vless in allowed_protocols :
60+ vless_settings = dict (user_settings .get ("vless" , {}))
61+ if vless_settings .get ("flow" ) == "xtls-rprx-vision-udp443" :
62+ vless_settings ["flow" ] = "xtls-rprx-vision"
63+ proxy_kwargs ["vless_id" ] = vless_settings .get ("id" )
64+ proxy_kwargs ["vless_flow" ] = vless_settings .get ("flow" )
65+ if ProxyProtocol .trojan in allowed_protocols :
66+ proxy_kwargs ["trojan_password" ] = user_settings .get ("trojan" , {}).get ("password" )
67+ if ProxyProtocol .shadowsocks in allowed_protocols :
68+ shadowsocks_settings = user_settings .get ("shadowsocks" , {})
69+ proxy_kwargs ["shadowsocks_password" ] = shadowsocks_settings .get ("password" )
70+ proxy_kwargs ["shadowsocks_method" ] = shadowsocks_settings .get ("method" )
71+ if ProxyProtocol .wireguard in allowed_protocols :
72+ wireguard_settings = user_settings .get ("wireguard" , {})
73+ proxy_kwargs ["wireguard_public_key" ] = wireguard_settings .get ("public_key" )
74+ proxy_kwargs ["wireguard_peer_ips" ] = wireguard_settings .get ("peer_ips" ) or []
75+ if ProxyProtocol .hysteria in allowed_protocols :
76+ proxy_kwargs ["hysteria_auth" ] = user_settings .get ("hysteria" , {}).get ("auth" )
6877
6978 return create_user (
7079 f"{ id } .{ username } " ,
71- create_proxy (** { key : value for key , value in proxy_kwargs . items () if key in _CREATE_PROXY_PARAMS } ),
80+ create_proxy (** proxy_kwargs ),
7281 inbounds ,
7382 )
7483
7584
76- async def core_users (db : AsyncSession , inbound_tags : list [str ] | set [str ] | None = None ):
85+ async def core_users (
86+ db : AsyncSession ,
87+ inbound_tags : list [str ] | set [str ] | None = None ,
88+ allowed_protocols : frozenset [ProxyProtocol ] | None = None ,
89+ ):
7790 dialect = db .bind .dialect .name
7891 inbound_tags = list (dict .fromkeys (inbound_tags or []))
7992
@@ -117,11 +130,21 @@ async def core_users(db: AsyncSession, inbound_tags: list[str] | set[str] | None
117130 for row in results :
118131 inbound_tags = row .inbound_tags .split ("," ) if row .inbound_tags else []
119132 if inbound_tags :
120- bridge_users .append (_serialize_user_for_node (row .id , row .username , row .proxy_settings , inbound_tags ))
133+ bridge_users .append (
134+ _serialize_user_for_node (
135+ row .id ,
136+ row .username ,
137+ row .proxy_settings ,
138+ inbound_tags ,
139+ allowed_protocols ,
140+ )
141+ )
121142 return bridge_users
122143
123144
124- async def serialize_users_for_node (users : list [User ]) -> list [ProtoUser ]:
145+ async def serialize_users_for_node (
146+ users : list [User ], allowed_protocols : frozenset [ProxyProtocol ] | None = None
147+ ) -> list [ProtoUser ]:
125148 bridge_users : list = []
126149
127150 for user in users :
@@ -133,6 +156,8 @@ async def serialize_users_for_node(users: list[User]) -> list[ProtoUser]:
133156 else :
134157 inbounds_list = loaded_inbounds
135158
136- bridge_users .append (_serialize_user_for_node (user .id , user .username , user .proxy_settings , inbounds_list ))
159+ bridge_users .append (
160+ _serialize_user_for_node (user .id , user .username , user .proxy_settings , inbounds_list , allowed_protocols )
161+ )
137162
138163 return bridge_users
0 commit comments