From 839be83f0edbe47780a0e6b9edcd7bf4cfc31b64 Mon Sep 17 00:00:00 2001 From: Flay2fx <158324614+fx505@users.noreply.github.com> Date: Sat, 3 Feb 2024 21:15:46 +0330 Subject: [PATCH] Singbox mux (#778) * add max early data max early data for ws in sing-box sub * fix host in http transport * add mux in singbox sub --- app/templates/singbox/mux_config.json | 5 +++++ app/utils/share.py | 21 ++++++++++++++++++--- config.py | 1 + 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 app/templates/singbox/mux_config.json diff --git a/app/templates/singbox/mux_config.json b/app/templates/singbox/mux_config.json new file mode 100644 index 000000000..89e4d5269 --- /dev/null +++ b/app/templates/singbox/mux_config.json @@ -0,0 +1,5 @@ +{ + "enabled": false, + "protocol": "smux", + "max_streams": 8 +} \ No newline at end of file diff --git a/app/utils/share.py b/app/utils/share.py index afd55cd68..2fd237b04 100644 --- a/app/utils/share.py +++ b/app/utils/share.py @@ -18,7 +18,7 @@ if TYPE_CHECKING: from app.models.user import UserResponse -from config import CLASH_SUBSCRIPTION_TEMPLATE, SINGBOX_SUBSCRIPTION_TEMPLATE +from config import CLASH_SUBSCRIPTION_TEMPLATE, SINGBOX_SUBSCRIPTION_TEMPLATE, SINGBOX_MUX_CONFIGURATION SERVER_IP = get_public_ip() @@ -576,6 +576,8 @@ class SingBoxConfiguration(str): def __init__(self): template = render_template(SINGBOX_SUBSCRIPTION_TEMPLATE) self.config = json.loads(template) + mux_template = render_template(SINGBOX_MUX_CONFIGURATION) + self.mux_config = json.loads(mux_template) def add_outbound(self, outbound_data): self.config["outbounds"].append(outbound_data) @@ -651,7 +653,7 @@ def transport_config(transport_type='', if method: transport_config['method'] = method if host: - transport_config['headers'] = {'Host': host} + transport_config["host"] = [host] if idle_timeout: transport_config['idle_timeout'] = idle_timeout if ping_timeout: @@ -714,10 +716,21 @@ def make_outbound(self, net = 'http' if net in ['http', 'ws', 'quic', 'grpc']: + max_early_data = None + early_data_header_name = None + + if "?ed=" in path: + path, max_early_data = path.split("?ed=") + max_early_data, = max_early_data.split("/") + max_early_data = int(max_early_data) + early_data_header_name = "Sec-WebSocket-Protocol" + config['transport'] = self.transport_config( transport_type=net, host=host, - path=path + path=path, + max_early_data=max_early_data, + early_data_header_name=early_data_header_name ) else: config["network"]: net @@ -726,6 +739,8 @@ def make_outbound(self, config['tls'] = self.tls_config(sni=sni, fp=fp, tls=tls, pbk=pbk, sid=sid, alpn=alpn, ais=ais) + + config['multiplex'] = self.mux_config return config diff --git a/config.py b/config.py index b2cee4e51..0ad5937d9 100755 --- a/config.py +++ b/config.py @@ -48,6 +48,7 @@ SUBSCRIPTION_PAGE_TEMPLATE = config("SUBSCRIPTION_PAGE_TEMPLATE", default="subscription/index.html") HOME_PAGE_TEMPLATE = config("HOME_PAGE_TEMPLATE", default="home/index.html") SINGBOX_SUBSCRIPTION_TEMPLATE = config("SINGBOX_SUBSCRIPTION_TEMPLATE", default="singbox/default.json") +SINGBOX_MUX_CONFIGURATION = config("SINGBOX_MUX_CONFIGURATION", default="singbox/mux_config.json") # USERNAME: PASSWORD