Skip to content

Commit

Permalink
Singbox mux (#778)
Browse files Browse the repository at this point in the history
* add max early data

max early data for ws in sing-box sub

* fix host in http transport

* add mux in singbox sub
  • Loading branch information
fx505 authored Feb 3, 2024
1 parent f5e425d commit 839be83
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
5 changes: 5 additions & 0 deletions app/templates/singbox/mux_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"enabled": false,
"protocol": "smux",
"max_streams": 8
}
21 changes: 18 additions & 3 deletions app/utils/share.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 839be83

Please sign in to comment.