Skip to content

Commit

Permalink
chore(subscription): customizeable config detail for v2ray- json and …
Browse files Browse the repository at this point in the history
…sing-box
  • Loading branch information
M03ED committed Jul 22, 2024
1 parent d19f567 commit 68a7936
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 144 deletions.
146 changes: 96 additions & 50 deletions app/subscription/singbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from config import (
SINGBOX_SUBSCRIPTION_TEMPLATE,
SINGBOX_SETTINGS_TEMPLATE,
MUX_TEMPLATE,
USER_AGENT_TEMPLATE,
GRPC_USER_AGENT_TEMPLATE,
Expand Down Expand Up @@ -33,6 +34,11 @@ def __init__(self):
else:
self.grpc_user_agent_data = []

temp_settings = render_template(SINGBOX_SETTINGS_TEMPLATE)
self.settings = json.loads(temp_settings)

del temp_user_agent_data, user_agent_data, temp_grpc_user_agent_data, grpc_user_agent_data, temp_settings

def add_outbound(self, outbound_data):
self.config["outbounds"].append(outbound_data)

Expand Down Expand Up @@ -88,75 +94,115 @@ def tls_config(sni=None, fp=None, tls=None, pbk=None,

return config

def http_config(self, host='', path='', random_user_agent: bool = False):
config = self.settings.get("httpSettings", {
"idle_timeout": "15s",
"ping_timeout": "15s",
"method": "GET",
"headers": {}
})
if "headers" not in config:
config["headers"] = {}

config["host"] = []
if path:
config["path"] = path
if host:
config["host"] = [host]
if random_user_agent:
config["headers"]["User-Agent"] = choice(self.user_agent_list)

return config

def ws_config(self, host='', path='', random_user_agent: bool = False,
max_early_data=None, early_data_header_name=None):
config = self.settings.get("wsSettings", {
"headers": {}
})
if "headers" not in config:
config["headers"] = {}

if path:
config["path"] = path
if host:
config["headers"] = {"Host": host}
if random_user_agent:
config["headers"]["User-Agent"] = choice(self.user_agent_list)
if max_early_data is not None:
config["max_early_data"] = max_early_data
if early_data_header_name:
config["early_data_header_name"] = early_data_header_name

return config

def grpc_config(self, path='', random_user_agent: bool = False):
config = self.settings.get("grpcSettings", {
"headers": {}
})
if "headers" not in config:
config["headers"] = {}

if path:
config["service_name"] = path
if random_user_agent:
config["headers"]["User-Agent"] = choice(self.grpc_user_agent_data)

return config

def httpupgrade_config(self, host='', path='', random_user_agent: bool = False):
config = self.settings.get("httpupgradeSettings", {
"headers": {}
})
if "headers" not in config:
config["headers"] = {}

config["host"] = host
if path:
config["path"] = path
if random_user_agent:
config["headers"]["User-Agent"] = choice(self.user_agent_list)

return config

def transport_config(self,
transport_type='',
host='',
path='',
method='',
idle_timeout="15s",
ping_timeout="15s",
max_early_data=None,
early_data_header_name=None,
permit_without_stream=False,
random_user_agent: bool = False):

transport_config = {}

if transport_type:
transport_config['type'] = transport_type

if transport_type == "http":
transport_config['host'] = []
if path:
transport_config['path'] = path
if method:
transport_config['method'] = method
if host or random_user_agent:
transport_config['headers'] = {}
if host:
transport_config["host"] = [host]
if random_user_agent:
transport_config['headers']['User-Agent'] = choice(self.user_agent_list)
if idle_timeout:
transport_config['idle_timeout'] = idle_timeout
if ping_timeout:
transport_config['ping_timeout'] = ping_timeout
transport_config = self.http_config(
host=host,
path=path,
random_user_agent=random_user_agent
)

elif transport_type == "ws":
if path:
transport_config['path'] = path
if host or random_user_agent:
transport_config['headers'] = {}
if host:
transport_config['headers'] = {'Host': host}
if random_user_agent:
transport_config['headers']['User-Agent'] = choice(self.user_agent_list)
if max_early_data is not None:
transport_config['max_early_data'] = max_early_data
if early_data_header_name:
transport_config['early_data_header_name'] = early_data_header_name
transport_config = self.ws_config(
host=host,
path=path,
random_user_agent=random_user_agent,
max_early_data=max_early_data,
early_data_header_name=early_data_header_name
)

elif transport_type == "grpc":
if path:
transport_config['service_name'] = path
if idle_timeout:
transport_config['idle_timeout'] = idle_timeout
if ping_timeout:
transport_config['ping_timeout'] = ping_timeout
if permit_without_stream:
transport_config['permit_without_stream'] = permit_without_stream
if random_user_agent:
transport_config['headers'] = {}
transport_config['headers']['User-Agent'] = choice(self.grpc_user_agent_data)
transport_config = self.grpc_config(
path=path,
random_user_agent=random_user_agent)

elif transport_type == "httpupgrade":
transport_config['host'] = host
if path:
transport_config['path'] = path
if random_user_agent:
transport_config['headers'] = {}
transport_config['headers']['User-Agent'] = choice(self.user_agent_list)
transport_config = self.httpupgrade_config(
host=host,
path=path,
random_user_agent=random_user_agent)

transport_config['type'] = transport_type
return transport_config

def make_outbound(self,
Expand Down
Loading

0 comments on commit 68a7936

Please sign in to comment.