Code Monkey home page Code Monkey logo

chnroute's Introduction

chnroute

持续更新的** IP 地址列表和 gfwlist 域名列表

用于帮助 RouterOS 配置科学上网

1,更新规则

**的所有 IP 网段来自 这个网页。被 gfw 污染的域名列表来自开源项目 gfwlist。运行下面的命令来获取最新的列表,并生成相应的 RouterOS 规则导入脚本:

# 更新列表并生成 RouterOS 规则导入脚本
./generate.sh

同时在生成规则前也可以将域名加入 exlucde_list.txt 和 include_list.txt 列表来手动 剔除/加入 被特殊解析的域名。

2,** IP 网段列表的导入和使用

**的 IP 网段可以帮助配置流量分流。上网时,请求的 dst IP 地址为非 CN 且非 LAN 地址列表的,可以标记路由走科学上网的路线。

2.1,可以使用以下脚本将 CN 和 LAN 的 IP 网段列表导入 RouterOS:

/system script
add dont-require-permissions=no name=cn owner=admin policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="/tool fetch url=https://raw.githubusercontent.com/ruijzhan/chnroute/master/CN.rsc\r\
    \nimport file-name=CN.rsc\r\
    \nfile remove CN.rsc\r\
    \n\r\
    \n/tool fetch url=https://raw.githubusercontent.com/ruijzhan/chnroute/master/LAN.rsc\r\
    \nimport file-name=LAN.rsc\r\
    \nfile remove LAN.rsc"

2.2,科学上网规则的配置:

在 PREROUTING 链中,配置目标地址为非 CN 的连接,JUMP 到一个自定义链中。在自定义链中的第一条规则,配置目标地址为 LAN 的连接直接 RETURN。在余下的规则中,指定协议以及目标端口号进行路由标记 (mark routing) 进行科学上网。

3,使用 gfwlist 避免域名解析污染,同时优化解析和线路速度

开源项目 chinadns 的工作原理是同时将一个域名的解析请求发往国内和未被污染的国外的 DNS 上游服务器进行解析。根据返回的结果决定是使用国内服务器的结果还是国外服务器的结果。这样做虽然可以解决域名污染的问题,并尽量使用 CDN 友好的解析结果。但是由于解析任何域名都必须等待国外 DNS 服务器的结果,所以通常一次解析要耗时 300 ms 以上。

gfwlist 维护了一个较为齐全的被 gfw 污染的域名的列表,而 RouterOS 又提供了指定匹配正则表达式的域名用指定的上游 DNS 服务器进行解析。将两者结合起来可以实现未被污染的域名直接用速度较快的国内 DNS 服务器解析,被污染的域名用速度较慢的,但无污染的国外 DNS 服务器解析。

3.1,在 RouterOS 中用环境变量指定无污染 DNS 服务器

可以使用 VPN 或者指定 8.8.8.8 这类流量科学上网的规则,在 RouterOS 上得到一个无污染的 DNS 服务器IP,然后将其设置为 RouerOS 的全局变量。由于每次 RouterOS 重启后环境变量会丢失,可以用 scheduler 在每次开机的时候设置。以 DNS 服务器 192.168.9.1 为例:

/system scheduler
add name=envs on-event="{\r\
    \n  :global dnsserver 192.168.9.1;\r\
    \n}" policy=read,write,policy,test start-time=startup

达到以下效果:

[admin@RouterBoard] > system/script/environment/print 
Columns: NAME, VALUE
#  NAME       VALUE       
0  dnsserver  192.168.9.1 

3.2,导入 gfwlist 列表到 RouterOS 中:

可以用下面的命令将脚本导入 RouterOS:

/system script
add dont-require-permissions=no name=gfwlist owner=admin policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=\
    "{\r\
    \n    /tool fetch url=https://raw.githubusercontent.com/ruijzhan/chnroute/master/gfwlist.rsc\r\
    \n    /import file-name=gfwlist.rsc\r\
    \n    /file remove gfwlist.rsc\r\
    \n    :log warning \"gfwlist\D3\F2\C3\FB\B5\BC\C8\EB\CD\EA\B3\C9\"\r\
    \n}"

注意:由于几千条规则会占用 DNS 的缓存,所以需要将 DNS 缓存的大小设置为 20560KiB 或者更大:

/ip/dns/set cache-size=20560KiB

用命令查看导入的解析规则:

/ip/dns/static/print

此时满足规则中正则表达式的域名,都会走指定的例如 192.168.9.1 这样干净且查询缓慢的服务器解析。而其他绝大多少没有被污染的域名,依然走 223.5.5.5 这类国内服务器解析。此时的整个 dns 的设置为:

[admin@RouterBoard] > /ip/dns/print 
                      servers: 223.5.5.5,223.6.6.6
              dynamic-servers: 
               use-doh-server: 
              verify-doh-cert: no
        allow-remote-requests: yes
          max-udp-packet-size: 4096
         query-server-timeout: 2s
          query-total-timeout: 10s
       max-concurrent-queries: 100
  max-concurrent-tcp-sessions: 20
                   cache-size: 20560KiB
                cache-max-ttl: 1w
                   cache-used: 16957KiB

chnroute's People

Contributors

ruijzhan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.