SagerNet / SagerNet/sing-box

Tun入站启用auto_route和auto_redirect时,建议加入额外的exclude和include规则用于绕过sing-box路由/匹配并被路由

Open
#3,277 3 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Go
Stars
38.1k
Forks
4.6k
Avg merge
19d 15h
Merged PRs (30d)
1

Description

Tun入站启用auto_route和auto_redirect后,建议可以加入 exclude_routing_markinclude_routing_mark 以实现进一步细分的绕过核心/指定被路由到核心

1. 配置 exclude_routing_mark
    chain prerouting {
        type nat hook prerouting priority dstnat + 1; policy accept;
        meta mark $exclude_routing_mark return
        .........
	}

目的是在属于linux的主路由系统中,用户可以先提前为局域网中不需要被路由的流量打上 exclude_routing_mark ,然后经过上述运行sing-box后自动生成的nftables规则配置绕过

2. 配置 include_routing_mark

如果仅仅是在 chain prerouting 的起始位置插入

meta mark $include_routing_mark meta l4proto { tcp, udp } th dport 53 dnat ip to 172.19.0.2
meta mark $include_routing_mark meta l4proto tcp redirect to :$PORT // $PORT为运行sing-box后,Tun入站随机生成的端口
meta mark $include_routing_mark meta mark set 0x00002023 ct mark set meta mark counter return

弊端是include_routing_mark的流量虽然被路由,但route_exclude_address_set等无法生效。

或许可以参考如下优化,把chain拆分+跳转的方式配置

    chain prerouting {
        type nat hook prerouting priority dstnat + 1; policy accept;
        iifname "tun0" counter return
        meta mark $include_routing_mark meta l4proto { tcp, udp } th dport 53 jump prerouting_dns
        meta mark $include_routing_mark jump prerouting_main
        iifname "br-lan" counter return        // 通过已有选项"exclude_interface": ["br-lan"]配置
        meta l4proto { tcp, udp } th dport 53 counter jump prerouting_dns
        jump prerouting_main
    }

    chain prerouting_dns {
        ip saddr @inet4_local_address_set counter dnat ip to 172.19.0.2    // Tun入站的v4地址
        ip6 saddr @inet6_local_address_set counter dnat ip6 to fdfe:dcba:9876::2    // Tun入站的v6地址
    }

    chain prerouting_main {
        ip daddr @inet4_local_address_set counter return
        ip6 daddr @inet6_local_address_set counter return
        ip daddr @inet4_route_exclude_address_set counter return
        ip6 daddr @inet6_route_exclude_address_set counter return
        meta l4proto tcp counter redirect to :$PORT    // $PORT为运行sing-box后,Tun入站随机生成的端口
        meta mark set 0x00002023 ct mark set meta mark counter return
    }

目的是在属于linux的主路由系统中,比如openwrt,局域网设备通过 br-lan 接口接入主路由,然后Tun入站启用auto_route和auto_redirect,并且配置

"exclude_interface": [
  "br-lan"
]

运行sing-box后,即可让局域网内的所有设备都绕过sing-box路由了。但用户可以自定义提前把,需要被路由到sing-box的流量打上 include_routing_mark ,然后sing-box运行后,通过自动生成的以上三行 meta mark $include_routing_mark 匹配后,即可简单让局域网中所有设备都绕过sing-box路由,仅限 include_routing_mark 的流量,被路由到sing-box

  • exclude_routing_markinclude_routing_mark 应该互为冲突项

如此,可以让sing-box tun入站的自动路由和自动重定向更加强大,比其他同类项目更方便配置,同时仅仅是配置绕过/路由 meta mark 的流量,如果不配置,则是原有的nftables规则。可以避免过多侵入nftables

以上建议的原型,是下面3楼帖子的内容,感谢二楼的提醒,感觉确实存在过多侵入nftables的情况,所以改成以上建议。望开发者多加考虑

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how Tun inbound auto_route and auto_redirect generate nftables rules, especially the existing exclude_interface and route_exclude_address_set behavior. Define how exclude_routing_mark and include_routing_mark interact and conflict, then verify that configured marks produce the requested routing or bypass behavior without changing the existing unconfigured rules.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, linux
Domain
networking, operating-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.