Skip to content

Commit

Permalink
add additional field for ws/hu early data
Browse files Browse the repository at this point in the history
  • Loading branch information
khodedawsh committed Dec 14, 2024
1 parent 4194814 commit 48eec1b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
14 changes: 14 additions & 0 deletions v2share/_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""v2share utilities"""

from typing import Dict, Sequence, Any
from urllib.parse import parse_qs, urlsplit


def filter_dict(d: Dict[Any, Any], values: Sequence[Any]) -> dict:
Expand All @@ -11,3 +12,16 @@ def filter_dict(d: Dict[Any, Any], values: Sequence[Any]) -> dict:
:return: the filtered dictionary
"""
return dict(filter(lambda p: p[1] not in values, d.items()))


def set_path_early_data(path: str, early_data: int):
parsed = urlsplit(path)
query_params = parse_qs(parsed.query)

query_params["ed"] = [str(early_data)]

query_string = "&".join(
"&".join([f"{key}={v}" for v in values]) for key, values in query_params.items()
)

return parsed.path + "?" + query_string
7 changes: 6 additions & 1 deletion v2share/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Optional, Dict, List
from uuid import UUID

from v2share._utils import filter_dict
from v2share._utils import filter_dict, set_path_early_data
from v2share.exceptions import ProtocolNotSupportedError


Expand Down Expand Up @@ -47,6 +47,7 @@ class V2Data:
fragment_length: str = "100-200"
fragment_interval: str = "10-20"
xray_noises: Optional[List[XrayNoise]] = None
early_data: Optional[int] = None
mtu: Optional[int] = None
dns_servers: Optional[List[str]] = None
allowed_ips: Optional[List[str]] = None
Expand Down Expand Up @@ -96,6 +97,10 @@ def _apply_trojan_vless_transport(self, payload):
transport_data = {"key": self.path, "quicSecurity": self.host}
else:
transport_data = {"path": self.path, "host": self.host}
if self.transport_type in {"ws", "httpupgrade"} and transport_data["path"]:
transport_data["path"] = set_path_early_data(
transport_data["path"], self.early_data
)

payload.update(transport_data)

Expand Down
8 changes: 4 additions & 4 deletions v2share/singbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def transport_config(
path=None,
http_method=None,
headers=None,
early_data=None,
):
if headers is None:
headers = {}
Expand All @@ -137,13 +138,11 @@ def transport_config(
elif transport_type == "ws":
transport_config["headers"] = headers
if path:
if "?ed=" in path:
path, max_early_data = path.split("?ed=")
max_early_data = int(max_early_data)
if early_data:
transport_config["early_data_header_name"] = (
"Sec-WebSocket-Protocol"
)
transport_config["max_early_data"] = max_early_data
transport_config["max_early_data"] = early_data
transport_config["path"] = path
if host:
transport_config["headers"]["Host"] = host
Expand Down Expand Up @@ -182,6 +181,7 @@ def create_outbound(config: V2Data):
host=config.host,
path=config.path,
headers=config.http_headers,
early_data=config.early_data,
)

if config.tls in ("tls", "reality"):
Expand Down
20 changes: 16 additions & 4 deletions v2share/xray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from importlib import resources
from typing import List

from v2share._utils import set_path_early_data
from v2share.base import BaseConfig
from v2share.data import V2Data, XrayNoise
from v2share.exceptions import ProtocolNotSupportedError, TransportNotSupportedError
Expand Down Expand Up @@ -183,26 +184,36 @@ def reality_config(public_key, short_id, sni, fingerprint="", spiderx=""):
}

@staticmethod
def ws_config(path=None, host=None, headers=None):
def ws_config(path=None, host=None, headers=None, early_data=None):
if headers is None:
headers = {}

ws_settings = {"headers": headers}
if path:
ws_settings["path"] = path

if early_data:
ws_settings["path"] = set_path_early_data(
ws_settings.get("path", "/"), early_data
)

if host:
ws_settings["host"] = host

return ws_settings

@staticmethod
def httpupgrade_config(path=None, host=None, headers=None):
def httpupgrade_config(path=None, host=None, headers=None, early_data=None):
if headers is None:
headers = {}

httpupgrade_settings = {"headers": headers}
if path:
httpupgrade_settings["path"] = path
if early_data:
httpupgrade_settings["path"] = set_path_early_data(
httpupgrade_settings.get("path", "/"), early_data
)
if host:
httpupgrade_settings["host"] = host

Expand Down Expand Up @@ -314,6 +325,7 @@ def make_stream_settings(
grpc_multi_mode=False,
dialer_proxy=None,
headers=None,
early_data=None,
):
if headers is None:
headers = {}
Expand All @@ -323,7 +335,7 @@ def make_stream_settings(
if net:
if net in {"ws", "websocket"}:
stream_settings["wsSettings"] = XrayConfig.ws_config(
path=path, host=host, headers=headers
path=path, host=host, headers=headers, early_data=early_data
)
elif net in {"grpc", "gun"}:
stream_settings["grpcSettings"] = XrayConfig.grpc_config(
Expand All @@ -347,7 +359,7 @@ def make_stream_settings(
)
elif net == "httpupgrade":
stream_settings["httpupgradeSettings"] = XrayConfig.httpupgrade_config(
path=path, host=host, headers=headers
path=path, host=host, headers=headers, early_data=early_data
)
elif net == "splithttp":
stream_settings["splithttpSettings"] = XrayConfig.splithttp_config(
Expand Down

0 comments on commit 48eec1b

Please sign in to comment.