More Accurate Routing for Chinese and Overseas IPs with BGP

More Accurate Routing for Chinese and Overseas IPs with BGP

BGP-based routing for Chinese and overseas IPs makes transparent proxying more efficient and accurate. Marking overseas destinations with FakeIP lets the main router route traffic more intelligently for smoother connectivity. The sing-box DNS configuration is also refined to handle queries more flexibly and efficiently, improving the overall network experience.

After two rounds of tinkering—Using Debian as a Side Router and Routing Transparent Proxy Traffic with FakeIP—my home transparent proxy was basically usable. The FakeIP approach marks overseas IPs with FakeIP, which the main router identifies to route traffic. The sing-box DNS module was configured as follows:

This DNS routing rule uses rule sets: if a domain is not in geosite-geolocation-!cn, and either the domain is in geosite-cn or geosite-category-companies@cn, or its resolved IP is in geoip-cn, the traffic is treated as within China and receives a RealIP. Otherwise, it receives a FakeIP.

This is a very crude test. The domain rule sets cover only relatively common domains, for a start. The geoip-cn IP rule set is based on MaxMind’s GeoLite2 database and ultimately WHOIS data. Most of the time, that only tells you which organization registered an IP, not where the IP is actually used. It is especially inaccurate for CN-IP.

I happened to learn about BGP recently. Here is a bit of background, borrowed from an explainer:

Border Gateway Protocol (BGP) is a routing protocol for exchanging Network Layer Reachability Information (NLRI) between routing domains. Because each domain is controlled by a separate administrative organization, routing domains are commonly called Autonomous Systems (AS). Today’s Internet is a large network of interconnected autonomous systems. As the de facto standard external routing protocol for the Internet, BGP is widely used between Internet Service Providers (ISPs).

In BGP, routes into China are announced by Chinese ASes. Collecting the IP lists announced by all Chinese ASes should therefore give us a more accurate CN-IP list.

Given how China’s network is organized, and according to Wikipedia, only the three major carriers, the education network, and the science and technology network can establish BGP sessions directly with the global Internet.

Plenty of tutorials explain how to run your own AS and obtain a complete BGP table. Being lazy and happy to borrow someone else’s work, though, I found that GitHub already has several BGP-based CN-IP lists. This post uses this project: https://github.com/gaoyifan/china-operator-ip/blob/ip-lists/china.txt

With the list ready, it is code time!

The comments cover everything, so I will not add much explanation.

If your router runs OpenWrt, install bash, ipset, iptables, and the other dependencies separately. OpenWrt’s default shell is ash, which cannot run this script.

bash
opkg update
opkg install bash
opkg install curl
opkg install ipset
opkg install iptables

The CN-IP list updates once a day. Schedule the script to run daily, and add it to startup as well.