Skip to content

Commit

Permalink
Xray update (#1467)
Browse files Browse the repository at this point in the history
* feat(xray-core): Add heartbeatPeriod variable support

* feat(xray-core): Add keepAlivePeriod support
  • Loading branch information
ImMohammad20000 authored Nov 30, 2024
1 parent deb5802 commit 3f8f4af
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
49 changes: 43 additions & 6 deletions app/subscription/v2ray.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
MUX_TEMPLATE,
USER_AGENT_TEMPLATE,
V2RAY_SETTINGS_TEMPLATE,
V2RAY_SUBSCRIPTION_TEMPLATE
V2RAY_SUBSCRIPTION_TEMPLATE,
)


Expand Down Expand Up @@ -78,6 +78,8 @@ def add(self, remark: str, address: str, inbound: dict, settings: dict):
mode=inbound.get("mode", "auto"),
extra=inbound.get("extra", {}),
noGRPCHeader=inbound.get("noGRPCHeader", False),
heartbeatPeriod=inbound.get("heartbeatPeriod", 0),
keepAlivePeriod=inbound.get("keepAlivePeriod", 0),
)

elif inbound["protocol"] == "vless":
Expand Down Expand Up @@ -108,6 +110,8 @@ def add(self, remark: str, address: str, inbound: dict, settings: dict):
mode=inbound.get("mode", "auto"),
extra=inbound.get("extra", {}),
noGRPCHeader=inbound.get("noGRPCHeader", False),
heartbeatPeriod=inbound.get("heartbeatPeriod", 0),
keepAlivePeriod=inbound.get("keepAlivePeriod", 0),
)

elif inbound["protocol"] == "trojan":
Expand Down Expand Up @@ -138,6 +142,8 @@ def add(self, remark: str, address: str, inbound: dict, settings: dict):
mode=inbound.get("mode", "auto"),
extra=inbound.get("extra", {}),
noGRPCHeader=inbound.get("noGRPCHeader", False),
heartbeatPeriod=inbound.get("heartbeatPeriod", 0),
keepAlivePeriod=inbound.get("keepAlivePeriod", 0),
)

elif inbound["protocol"] == "shadowsocks":
Expand Down Expand Up @@ -180,7 +186,9 @@ def vmess(
x_padding_bytes: str = "100-1000",
mode: str = "auto",
extra: dict = {},
noGRPCHeader: bool = False
noGRPCHeader: bool = False,
heartbeatPeriod: int = 0,
keepAlivePeriod: int = 0,
):
payload = {
"add": address,
Expand Down Expand Up @@ -233,6 +241,12 @@ def vmess(
if extra:
payload["extra"] = extra
payload["noGRPCHeader"] = int(noGRPCHeader)
payload["keepAlivePeriod"] = keepAlivePeriod

elif net == "ws":
if heartbeatPeriod:
payload["heartbeatPeriod"] = heartbeatPeriod

return (
"vmess://"
+ base64.b64encode(
Expand Down Expand Up @@ -268,6 +282,8 @@ def vless(cls,
mode: str = "auto",
extra: dict = {},
noGRPCHeader: bool = False,
heartbeatPeriod: int = 0,
keepAlivePeriod: int = 0,
):

payload = {
Expand Down Expand Up @@ -301,11 +317,16 @@ def vless(cls,
if extra:
payload["extra"] = json.dumps(extra)
payload["noGRPCHeader"] = int(noGRPCHeader)
payload["keepAlivePeriod"] = keepAlivePeriod

elif net == 'kcp':
payload['seed'] = path
payload["host"] = host

elif net == "ws":
if heartbeatPeriod:
payload["heartbeatPeriod"] = heartbeatPeriod

else:
payload["path"] = path
payload["host"] = host
Expand Down Expand Up @@ -362,7 +383,9 @@ def trojan(cls,
x_padding_bytes: str = "100-1000",
mode: str = "auto",
extra: dict = {},
noGRPCHeader: bool = False
noGRPCHeader: bool = False,
heartbeatPeriod: int = 0,
keepAlivePeriod: int = 0,
):

payload = {
Expand Down Expand Up @@ -392,6 +415,7 @@ def trojan(cls,
if extra:
payload["extra"] = json.dumps(extra)
payload["noGRPCHeader"] = int(noGRPCHeader)
payload["keepAlivePeriod"] = keepAlivePeriod

elif net == 'quic':
payload['key'] = path
Expand All @@ -401,6 +425,10 @@ def trojan(cls,
payload['seed'] = path
payload["host"] = host

elif net == "ws":
if heartbeatPeriod:
payload["heartbeatPeriod"] = heartbeatPeriod

else:
payload["path"] = path
payload["host"] = host
Expand Down Expand Up @@ -517,7 +545,7 @@ def reality_config(sni=None, fp=None, pbk=None, sid=None, spx=None) -> dict:

return realitySettings

def ws_config(self, path: str = "", host: str = "", random_user_agent: bool = False) -> dict:
def ws_config(self, path: str = "", host: str = "", random_user_agent: bool = False, heartbeatPeriod: int = 0) -> dict:
wsSettings = copy.deepcopy(self.settings.get("wsSettings", {}))

if "headers" not in wsSettings:
Expand All @@ -528,6 +556,8 @@ def ws_config(self, path: str = "", host: str = "", random_user_agent: bool = Fa
wsSettings["headers"]["Host"] = host
if random_user_agent:
wsSettings["headers"]["User-Agent"] = choice(self.user_agent_list)
if heartbeatPeriod:
wsSettings["heartbeatPeriod"] = heartbeatPeriod

return wsSettings

Expand Down Expand Up @@ -555,6 +585,7 @@ def splithttp_config(self, path: str = "", host: str = "", random_user_agent: bo
extra: dict = {},
mode: str = "auto",
noGRPCHeader: bool = False,
keepAlivePeriod: int = 0,
) -> dict:
config = copy.deepcopy(self.settings.get("splithttpSettings", {}))

Expand All @@ -576,6 +607,7 @@ def splithttp_config(self, path: str = "", host: str = "", random_user_agent: bo

if extra:
config["extra"] = extra
config["keepAlivePeriod"] = keepAlivePeriod
# core will ignore unknown variables

return config
Expand Down Expand Up @@ -879,11 +911,13 @@ def make_stream_setting(self,
extra: dict = {},
mode: str = "auto",
noGRPCHeader: bool = False,
heartbeatPeriod: int = 0,
keepAlivePeriod: int = 0,
) -> dict:

if net == "ws":
network_setting = self.ws_config(
path=path, host=host, random_user_agent=random_user_agent)
path=path, host=host, random_user_agent=random_user_agent, heartbeatPeriod=heartbeatPeriod)
elif net == "grpc":
network_setting = self.grpc_config(
path=path, host=host, multiMode=multiMode, random_user_agent=random_user_agent)
Expand Down Expand Up @@ -911,7 +945,8 @@ def make_stream_setting(self,
xmux=xmux,
extra=extra,
mode=mode,
noGRPCHeader=noGRPCHeader
noGRPCHeader=noGRPCHeader,
keepAlivePeriod=keepAlivePeriod,
)
else:
network_setting = {}
Expand Down Expand Up @@ -1022,6 +1057,8 @@ def add(self, remark: str, address: str, inbound: dict, settings: dict):
mode=inbound.get("mode", "auto"),
extra=inbound.get("extra", {}),
noGRPCHeader=inbound.get("noGRPCHeader", False),
heartbeatPeriod=inbound.get("heartbeatPeriod", 0),
keepAlivePeriod=inbound.get("keepAlivePeriod", 0),
)

mux_json = json.loads(self.mux_template)
Expand Down
2 changes: 2 additions & 0 deletions app/xray/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ def _resolve_inbounds(self):
if isinstance(host, str):
settings['host'] = [host]

settings["heartbeatPeriod"] = net_settings.get('heartbeatPeriod', 0)
elif net == 'grpc' or net == 'gun':
settings['header_type'] = ''
settings['path'] = net_settings.get('serviceName', '')
Expand Down Expand Up @@ -309,6 +310,7 @@ def _resolve_inbounds(self):
settings["mode"] = net_settings.get("mode", "auto")
settings["extra"] = net_settings.get("extra", {})
settings["noGRPCHeader"] = net_settings.get("noGRPCHeader", False)
settings["keepAlivePeriod"] = net_settings.get("keepAlivePeriod", 0)

elif net == 'kcp':
header = net_settings.get('header', {})
Expand Down

0 comments on commit 3f8f4af

Please sign in to comment.