Posts tagged with “infra”

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.

A 64 MB travel router that gets through the GFW: sing-box, a Cloudflare tunnel, and one pinned IP

I travel to China with a TP-Link WR703N in my bag. It is a matchbox-sized router running LEDE 17.01, with a 400 MHz single-core MIPS CPU, about 60 MB of usable RAM, one Ethernet port and one 2.4 GHz radio. Everything on its Wi-Fi gets through the firewall: laptop, phone, tablet. No device needs a client app.

The hardware is an absurd place to run a modern proxy. Getting it to work well took most of a week. The final design is simple. The parts that made the difference were not the parts I expected.

The shape of it

LAN client
  -> iptables REDIRECT (all LAN TCP, except private ranges)
  -> redsocks 192.168.1.1:12346
  -> sing-box SOCKS 127.0.0.1:11081
  -> VLESS over WebSocket + TLS, h2mux
  -> Cloudflare edge (a pinned Hong Kong IP)
  -> cloudflared tunnel
  -> sing-box on a small VPS, listening on 127.0.0.1 only
  -> the internet

The VPS has no open proxy port. cloudflared dials out to Cloudflare, and Cloudflare forwards the WebSocket to sing-box on localhost. Days before this trip, the IP of another VPS I own was blocked. With this setup a blocked origin IP does not matter, because the client never connects to it. The client only ever sees Cloudflare.

…more

A 60MB travel router doesn't need xray — bare Shadowsocks with all-TCP DNS just works

My travel router is a LEDE 17.01 box (60MB RAM, MIPS) living behind the GFW. The "modern" stack — xray with DoH DNS and geoip routing, plus kcptun for UDP acceleration — ate ~45MB of RAM, lived on a USB stick, and kept zombifying after restarts. Meanwhile UDP on Chinese networks got worse every month: kcptun sessions died, UDP DNS relays never worked.

The fix was going backwards: bare shadowsocks-libev 3.2.1, global transparent proxy, DNS over TCP.

Global transparent proxy, no geoip — everything except private ranges rides the tunnel:

iptables -t nat -N SSRULE
for net in 0.0.0.0/8 10.0.0.0/8 127.0.0.0/8 169.254.0.0/16 \
           172.16.0.0/12 192.168.0.0/16 224.0.0.0/4 240.0.0.0/4 \
           100.64.0.0/10 <server-ip>/32; do
  iptables -t nat -A SSRULE -d $net -j RETURN   # server-IP entry avoids a redirect loop
done
iptables -t nat -A SSRULE -p tcp -j REDIRECT --to-ports 12345
iptables -t nat -A PREROUTING -i br-lan -p tcp -j SSRULE
iptables -t nat -A OUTPUT -p tcp -j SSRULE

DNS without UDP relay is the clever bit. Unpoisoned DNS has to go through the tunnel, which normally means ss-tunnel's UDP relay — which silently fails on this MIPS box: it logs "listening" but no socket ever appears in netstat. Instead, OpenWrt's tiny dns-forwarder listens on UDP locally and queries upstream over TCP:

dns-forwarder -b 127.0.0.1 -p 1053 -s 8.8.8.8:53

dnsmasq forwards to 127.0.0.1#1053 as usual; the forwarder's TCP:53 packets hit the same REDIRECT rule and ride the tunnel. You get real answers — NXDOMAIN passes through untouched, something poisoned DNS never gives you. No UDP ever leaves the router.

Total cost: 490KB of binaries, embedded in internal flash (verified by unmounting the USB stick mid-run and pulling google in 1.3s), RAM down to 28MB, procd respawns everything if it dies, and boot just works.

Sometimes the modern stack is the problem, not the solution.

Fcitx5 + Rime on a 2015 Chromebook (postmarketOS): GTK_IM_MODULE=fcitx, not fcitx5

I just got Chinese input (wubi + pinyin via Rime) working on my ASUS C100P Flip Chromebook — postmarketOS edge, armv7, 2 GB RAM, MATE on X11. Two things made it real: a minimal package set, and one environment variable half the internet gets wrong.

Install is 10 packages:

doas apk add fcitx5 fcitx5-rime fcitx5-gtk3 fcitx5-gtk4

Resist the "install everything" reflex. On armv7, fcitx5-chinese-addons doesn't exist, fcitx5-configtool needs plasma6 (never built on armhf), and the old full chain dragged in qt6-qtwebengine, which pins icu-libs-76 — on Alpine edge that froze my nodejs at Node 22 (that icu standoff is exactly why I ripped the whole thing out months ago). Rime alone covers wubi and pinyin; my schemas/dictionaries live in my dotfiles repo and get symlinked into ~/.local/share/fcitx5/rime/, so fcitx5 deploys them on first start.

Then ~/.xprofile:

export GTK_IM_MODULE=fcitx      # NOT fcitx5
export QT_IM_MODULE=fcitx       # NOT fcitx5
export XMODIFIERS="@im=fcitx5"  # yes, this one really is fcitx5

The asymmetry is not a typo. Alpine's fcitx5-gtk registers the GTK input module under the id fcitx only — the widely-documented fcitx5 alias never connects. The symptom is maddening: tray icon alive, right-click menu works, even tray "Restart" works — but fcitx5-remote stays 0 (no input context has ever connected) no matter which window has focus, and no keystroke ever reaches the daemon.

Proof in 60 seconds, using zenity as a disposable GTK app and xdotool to tap Shift into it:

export DBUS_SESSION_BUS_ADDRESS="unix:path=$(ls /tmp/dbus-* | head -1)"
GTK_IM_MODULE=fcitx5 zenity --entry & sleep 2   # fcitx5-remote → 0, dead
GTK_IM_MODULE=fcitx  zenity --entry & sleep 2   # fcitx5-remote → 1
xdotool keydown Shift_L; sleep 0.05; xdotool keyup Shift_L   # → 2: rime active

fcitx5-remote states: 0 = no client ever connected, 1 = inactive, 2 = active. A focused GTK window that never lifts you off 0 is an env problem, not a daemon problem — stop restarting fcitx5 and fix the variable.

Two traps if you debug from an ssh/agent shell. First, fcitx5 started there inherits the shell's stale DBUS_SESSION_BUS_ADDRESS and becomes an orphan daemon on a dead bus, invisible to the GUI session — take the real bus from the session process: tr '\0' '\n' < /proc/$(pgrep -n mate-session)/environ | grep ^DBUS_. Second, environment is baked at process birth: mate-terminal windows are all spawned by a factory that inherited the session's env at login, so per-window fixes don't stick — .xprofile plus one logout/login is the only global cure (mate-terminal --disable-factory buys you a single standalone window meanwhile). After that, double-tap Shift toggles Rime and the dotfiles-managed wubi86/pinyin_simp schemas come up on first deploy.

Let xrdp repair stuck keyboard and mouse state on reconnect

An xrdp reconnect can leave a virtual mouse button or key down. A stuck mouse button creates an X11 pointer grab, so every window appears unable to receive clicks even though XFCE and the taskbar are still alive. The old workaround was to click the stuck button once; a reconnect hook can repair both devices automatically without destroying the desktop session.

Keep the session alive in /etc/xrdp/sesman.ini:

KillDisconnected=false
DisconnectedTimeLimit=0
ReconnectScript=reconnectwm.sh

ReconnectScript is the important part. It runs when xrdp-sesman reconnects a client to an existing session. Put the repair command in /etc/xrdp/reconnectwm.sh:

#!/bin/sh

/usr/local/bin/rdp-input-reset >/dev/null 2>&1 || true

The helper only resets a device when X reports a key or button still down:

#!/bin/sh
set -eu

for name in xrdpMouse xrdpKeyboard; do
    id=$(xinput list --id-only "$name" 2>/dev/null || true)
    [ -n "$id" ] || continue

    if xinput query-state "$id" | grep -Eq '(key|button)\[[0-9]+\]=down'; then
        xinput disable "$id"
        xinput enable "$id"
    fi
done

Run the helper manually when the connection has not dropped:

DISPLAY=:10.0 XAUTHORITY="$HOME/.Xauthority" rdp-input-reset

This is safer than restarting xfwm4, the panel, or the whole desktop: it clears the stale XRDP input devices while preserving windows, terminals, and the session. It is not magic—the hook only runs for a reconnect to an existing session. If the connection remains open while input gets stuck, run the helper manually. KillDisconnected=true is a stronger isolation workaround, but it eventually kills the disconnected desktop, so it is the wrong trade-off for a persistent RDP session.