Skip to content

Commit

Permalink
feat(xray-core): Add support for xhttp new variables (#1447)
Browse files Browse the repository at this point in the history
* feat(xray-core): Add support for xhttp new variables

* fix: noGRPCHeader value
  • Loading branch information
ImMohammad20000 authored Nov 22, 2024
1 parent 0503532 commit e599916
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
47 changes: 46 additions & 1 deletion app/subscription/v2ray.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def add(self, remark: str, address: str, inbound: dict, settings: dict):
sc_max_concurrent_posts=inbound.get('scMaxConcurrentPosts', 100),
sc_min_posts_interval_ms=inbound.get('scMinPostsIntervalMs', 30),
x_padding_bytes=inbound.get("xPaddingBytes", "100-1000"),
mode=inbound.get("mode", "auto"),
extra=inbound.get("extra", {}),
noGRPCHeader=inbound.get("noGRPCHeader", False),
)

elif inbound["protocol"] == "vless":
Expand Down Expand Up @@ -102,6 +105,9 @@ def add(self, remark: str, address: str, inbound: dict, settings: dict):
sc_max_concurrent_posts=inbound.get('scMaxConcurrentPosts', 100),
sc_min_posts_interval_ms=inbound.get('scMinPostsIntervalMs', 30),
x_padding_bytes=inbound.get("xPaddingBytes", "100-1000"),
mode=inbound.get("mode", "auto"),
extra=inbound.get("extra", {}),
noGRPCHeader=inbound.get("noGRPCHeader", False),
)

elif inbound["protocol"] == "trojan":
Expand Down Expand Up @@ -129,6 +135,9 @@ def add(self, remark: str, address: str, inbound: dict, settings: dict):
sc_max_concurrent_posts=inbound.get('scMaxConcurrentPosts', 100),
sc_min_posts_interval_ms=inbound.get('scMinPostsIntervalMs', 30),
x_padding_bytes=inbound.get("xPaddingBytes", "100-1000"),
mode=inbound.get("mode", "auto"),
extra=inbound.get("extra", {}),
noGRPCHeader=inbound.get("noGRPCHeader", False),
)

elif inbound["protocol"] == "shadowsocks":
Expand Down Expand Up @@ -169,6 +178,9 @@ def vmess(
sc_max_concurrent_posts: int = 100,
sc_min_posts_interval_ms: int = 30,
x_padding_bytes: str = "100-1000",
mode: str = "auto",
extra: dict = {},
noGRPCHeader: bool = False
):
payload = {
"add": address,
Expand Down Expand Up @@ -217,7 +229,10 @@ def vmess(
payload["scMaxConcurrentPosts"] = sc_max_concurrent_posts
payload["scMinPostsIntervalMs"] = sc_min_posts_interval_ms
payload["xPaddingBytes"] = x_padding_bytes

payload["type"] = mode
if extra:
payload["extra"] = extra
payload["noGRPCHeader"] = int(noGRPCHeader)
return (
"vmess://"
+ base64.b64encode(
Expand Down Expand Up @@ -250,6 +265,9 @@ def vless(cls,
sc_max_concurrent_posts: int = 100,
sc_min_posts_interval_ms: int = 30,
x_padding_bytes: str = "100-1000",
mode: str = "auto",
extra: dict = {},
noGRPCHeader: bool = False,
):

payload = {
Expand Down Expand Up @@ -279,6 +297,10 @@ def vless(cls,
payload["scMaxConcurrentPosts"] = sc_max_concurrent_posts
payload["scMinPostsIntervalMs"] = sc_min_posts_interval_ms
payload["xPaddingBytes"] = x_padding_bytes
payload["mode"] = mode
if extra:
payload["extra"] = json.dumps(extra)
payload["noGRPCHeader"] = int(noGRPCHeader)

elif net == 'kcp':
payload['seed'] = path
Expand Down Expand Up @@ -338,6 +360,9 @@ def trojan(cls,
sc_max_concurrent_posts: int = 100,
sc_min_posts_interval_ms: int = 30,
x_padding_bytes: str = "100-1000",
mode: str = "auto",
extra: dict = {},
noGRPCHeader: bool = False
):

payload = {
Expand All @@ -363,6 +388,10 @@ def trojan(cls,
payload["scMaxConcurrentPosts"] = sc_max_concurrent_posts
payload["scMinPostsIntervalMs"] = sc_min_posts_interval_ms
payload["xPaddingBytes"] = x_padding_bytes
payload["mode"] = mode
if extra:
payload["extra"] = json.dumps(extra)
payload["noGRPCHeader"] = int(noGRPCHeader)

elif net == 'quic':
payload['key'] = path
Expand Down Expand Up @@ -523,9 +552,13 @@ def splithttp_config(self, path: str = "", host: str = "", random_user_agent: bo
sc_min_posts_interval_ms: int = 30,
x_padding_bytes: str = "100-1000",
xmux: dict = {},
extra: dict = {},
mode: str = "auto",
noGRPCHeader: bool = False,
) -> dict:
config = copy.deepcopy(self.settings.get("splithttpSettings", {}))

config["mode"] = mode
if path:
config["path"] = path
if host:
Expand All @@ -537,9 +570,12 @@ def splithttp_config(self, path: str = "", host: str = "", random_user_agent: bo
config.setdefault("scMaxConcurrentPosts", sc_max_concurrent_posts)
config.setdefault("scMinPostsIntervalMs", sc_min_posts_interval_ms)
config.setdefault("xPaddingBytes", x_padding_bytes)
config["noGRPCHeader"] = noGRPCHeader
if xmux:
config["xmux"] = xmux

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

return config
Expand Down Expand Up @@ -840,6 +876,9 @@ def make_stream_setting(self,
sc_min_posts_interval_ms: int = 30,
x_padding_bytes: str = "100-1000",
xmux: dict = {},
extra: dict = {},
mode: str = "auto",
noGRPCHeader: bool = False,
) -> dict:

if net == "ws":
Expand Down Expand Up @@ -870,6 +909,9 @@ def make_stream_setting(self,
sc_min_posts_interval_ms=sc_min_posts_interval_ms,
x_padding_bytes=x_padding_bytes,
xmux=xmux,
extra=extra,
mode=mode,
noGRPCHeader=noGRPCHeader
)
else:
network_setting = {}
Expand Down Expand Up @@ -977,6 +1019,9 @@ def add(self, remark: str, address: str, inbound: dict, settings: dict):
sc_min_posts_interval_ms=inbound.get('scMinPostsIntervalMs', 30),
x_padding_bytes=inbound.get("xPaddingBytes", "100-1000"),
xmux=inbound.get("xmux", {}),
mode=inbound.get("mode", "auto"),
extra=inbound.get("extra", {}),
noGRPCHeader=inbound.get("noGRPCHeader", False),
)

mux_json = json.loads(self.mux_template)
Expand Down
11 changes: 8 additions & 3 deletions app/xray/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
from app.utils.crypto import get_cert_SANs
from config import DEBUG, XRAY_EXCLUDE_INBOUND_TAGS, XRAY_FALLBACKS_INBOUND_TAG

def merge_dicts(a, b): # B will override A dictionary key and values

def merge_dicts(a, b): # B will override A dictionary key and values
for key, value in b.items():
if isinstance(value, dict) and key in a and isinstance(a[key], dict):
merge_dicts(a[key], value) # Recursively merge nested dictionaries
merge_dicts(a[key], value) # Recursively merge nested dictionaries
else:
a[key] = value
return a


class XRayConfig(dict):
def __init__(self,
config: Union[dict, str, PosixPath] = {},
Expand Down Expand Up @@ -295,7 +297,7 @@ def _resolve_inbounds(self):
host = net_settings.get('host', '')
settings['host'] = [host]

elif net in ('splithttp','xhttp'):
elif net in ('splithttp', 'xhttp'):
settings['path'] = net_settings.get('path', '')
host = net_settings.get('host', '')
settings['host'] = [host]
Expand All @@ -304,6 +306,9 @@ def _resolve_inbounds(self):
settings['scMinPostsIntervalMs'] = net_settings.get('scMinPostsIntervalMs', 30)
settings['xPaddingBytes'] = net_settings.get('xPaddingBytes', "100-1000")
settings['xmux'] = net_settings.get('xmux', {})
settings["mode"] = net_settings.get("mode", "auto")
settings["extra"] = net_settings.get("extra", {})
settings["noGRPCHeader"] = net_settings.get("noGRPCHeader", False)

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

0 comments on commit e599916

Please sign in to comment.