Skip to content

Commit a2fbf1e

Browse files
committed
Add route dict support
1 parent 5b3a0db commit a2fbf1e

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

singbox2proxy/base.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,7 @@ def __init__(
14671467
tun_mtu: int = 9000,
14681468
tun_auto_route: bool = True,
14691469
set_system_proxy: bool = False,
1470+
route: dict = None,
14701471
):
14711472
"""Initialize a SingBoxProxy instance.
14721473
@@ -1498,6 +1499,8 @@ def __init__(
14981499
tun_auto_route: If True, automatically configure system routing rules. Default is True.
14991500
set_system_proxy: If True, automatically configure system-wide proxy settings to use
15001501
this proxy. Settings are restored when the proxy is stopped. Default is False.
1502+
route: Optional dictionary containing sing-box routing rules. This corresponds to the
1503+
"route" section in the sing-box configuration.
15011504
15021505
Raises:
15031506
TypeError: If config is not a string or path-like object.
@@ -1518,7 +1521,7 @@ def __init__(
15181521
parsed = urllib.parse.urlparse(config)
15191522
# Treat strings that parse to a URL with a scheme and a network location as URLs.
15201523
# This handles proxy link schemes like vless://, vmess://, ss://, trojan://, etc.
1521-
# It also avoids misclassifying Windows paths like "C:\path" which have a scheme but no netloc.
1524+
# It also avoids misclassifying Windows paths like "C:\\path" which have a scheme but no netloc.
15221525
if parsed.scheme and parsed.netloc:
15231526
self.config_url = config
15241527
self.config_path = None
@@ -1546,9 +1549,10 @@ def __init__(
15461549
self.tun_stack = tun_stack
15471550
self.tun_mtu = tun_mtu
15481551
self.tun_auto_route = tun_auto_route
1552+
self.set_system_proxy = set_system_proxy
1553+
self.route = route
15491554

15501555
# System proxy configuration
1551-
self.set_system_proxy = set_system_proxy
15521556
self._system_proxy_manager = None
15531557

15541558
# Runtime state
@@ -1760,7 +1764,7 @@ def _pick_unused_port(cls, exclude_port: int | list = None) -> int:
17601764

17611765
try:
17621766
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
1763-
s.bind(("localhost", 0)) # Let OS choose a free port
1767+
s.bind(("", 0)) # Let OS choose a free port on all interfaces
17641768
_, port = s.getsockname()
17651769
if port not in exclude_port:
17661770
_allocated_ports.add(port)
@@ -2446,6 +2450,10 @@ def generate_config(self, chain_proxy=None):
24462450
"outbounds": outbounds,
24472451
}
24482452

2453+
# Add route section if provided
2454+
if self.route:
2455+
config["route"] = self.route
2456+
24492457
# Add TUN inbound if enabled
24502458
if self.tun_enabled:
24512459
tun_inbound = {

0 commit comments

Comments
 (0)