A Shadowrocket-style `fq on` / `fq off` for a Linux laptop, with sing-box TUN

On a trip to China, my phone had Shadowrocket: one toggle and every app worked. My Debian laptop depended on a travel router, and on any other Wi-Fi it had nothing. I wanted the same one-toggle experience, and I wanted off to leave no trace, because back home I don't need it at all.

The answer is sing-box in TUN mode, run by a systemd unit that is never enabled, plus a tiny wrapper:

#!/usr/bin/env bash
# /usr/local/bin/fq
UNIT=sing-box-fq.service
case "${1😖tatus}" in
  on)  sudo systemctl start "$UNIT" ;;
  off) sudo systemctl stop "$UNIT"
       # a crashed sing-box can leave policy rules behind
       while ip rule | grep -q 'lookup 2022'; do
         sudo ip rule del priority "$(ip rule | awk '/lookup 2022/{sub(":","",$1);print $1;exit}')"
       done ;;
  status) systemctl is-active "$UNIT"
          curl -s -m 8 -o /dev/null -w 'google %{http_code}\n' https://www.google.com/generate_204 ;;
esac
# /etc/systemd/system/sing-box-fq.service  (do NOT `systemctl enable` it)
[Service]
ExecStart=/usr/local/bin/sing-box run -c /etc/sing-box-fq/config.json -D /var/lib/sing-box-fq
StateDirectory=sing-box-fq

The config has a tun inbound with auto_route: true. Routing sends private IPs and the geosite-cn / geoip-cn rule sets direct and everything else to your VLESS/Trojan/whatever outbound. Store the rule sets as local .srs files so startup never depends on a download. fq off stops the unit, and sing-box removes its interface, its ip rules, and table 2022, so you are back to a stock network.

Three things cost me time:

ufw silently kills it. With the default (mixed/system) stack, sing-box hands TCP back to the kernel through the TUN interface. ufw's INPUT DROP drops those packets, so the TUN comes up and not a single connection gets through, with nothing in the log. Fix: sudo ufw allow in on sbfq0 (use your TUN interface name). The rule does nothing while the interface doesn't exist.

Don't exclude your LAN from the TUN. I put 192.168.0.0/16 in route_exclude_address to be safe, and that also excluded the router's DNS at 192.168.1.1. DNS then skipped sing-box, and you get poisoned answers in China. Leave private ranges out of the exclude list. sing-box's own rules still route normal LAN traffic through the main table, and only port 53 is pulled into the TUN for hijacking. Set the DNS strategy to ipv4_only if the network has no real IPv6. Otherwise apps get AAAA records and fail fast.

Tailscale bypasses your TUN on purpose. tailscaled marks its sockets with fwmark 0x80000 and has an ip rule (priority 5210) that sends them to the main table. So its control plane still goes direct and still gets blocked. Pull only its TCP (control plane and DERP, both on 443) into the TUN, and leave its UDP alone so direct WireGuard paths keep working:

ExecStartPost=-/usr/sbin/ip rule add priority 5200 fwmark 0x80000/0xff0000 ipproto tcp lookup 2022
ExecStopPost=-/usr/sbin/ip rule del priority 5200

One habit worth keeping: if you test routing changes over SSH or Tailscale, set up an automatic revert first, e.g. sudo systemd-run --on-active=180 /usr/local/bin/fq off. A bad TUN config cuts off the session you'd need to fix it.

Comments

  1. Markdown is allowed. HTML tags allowed: <strong>, <em>, <blockquote>, <code>, <pre>, <a>.