哔哩哔哩

前言

介绍啥的我就不说了,想必对这个有兴趣的已经搜了很多文章了。
这个实现的过程就是利用GitHub上的一个项目拉取apnic 和 ipip的中国地址列表,然后取反,将列表生成bird的静态路由格式,通过OSPF把路由表传给ROS,国内IP直接通过ros出去,国外IP ROS根据路由表转发给Openwrt科学出去,从而实现国内外IP分流。
这样做的好处是,Openwrt关机不会影响国内IP的正常上网,Openwrt开机ROS自动分流。本教程没有涉及DNS的分流,至于获取无污染的DNS,大家可以建一个adguard类的DNS服务器,获取一些国外DOH的DNS。

准备

软件下载

Openwrt上我们需要一些软件所有IPK都在openwrt.org,注意选对版本和架构
routing里搜索bird
image.png
我们需要安装这些软件

packages搜索git

image.png

我们需要安装这些软件
继续搜索nano,安装nano
image.png

继续搜索make,安装make
image.png
另外固件里要有Python3和curl如果没有和也要下载安装

安装方法

用ssh工具链接Openwrt
比如要下载make
image.png

右击复制链接地址,然后在ssh里wget 链接地址
image.png
然后ls查看名字
然后okpg install 名字
image.png

注意有些应用安装提示错误的是因为要先安装别的,换换安装顺序就可以装上了

现成的openwrt

蓝奏云
这个是我自用的openwrt,里面包含了以上软件,以下的配置也都已经弄好了,但是软件比较少只有ssrp。
使用我的固件只需要设置好科学全局,和定时任务就可以了

已知问题

我的固件ssh远程无法链接,解决办法:
image.png

把关于ssh的删掉,安装这两个

GitHub项目

我们需要用到这个项目dndx/nchnroutes,GitHub里面介绍已经很全了,我这里就不重复了

我们需要把它clone到Openwrt上

image.png

git clone https://github.com/dndx/nchnroutes.git

OpenWRT

首先需要OpenWRT 以旁路由的模式运行。并且可以正常上网,打开科学上网,并且选择全局。防火墙转发全部允许

进入到刚刚clone的目录
cd nchnroutes/
然后需要编辑produce.py
输入nano produce.py
image.png
将这里改成br-lan
ctrl + x 回车保存

然后执行make
然后会生成 routes6.confroutes4.conf

打开openwrt的webui
image.png
将bird4和6的这个取消勾选,保存应用

然后我们修改/etc/bird4.conf文件
nano /etc/bird4.conf先把里面的内容删除干净,然后粘贴下面的内容

router id 10.0.0.253;

# The Kernel protocol is not a real routing protocol. Instead of communicating
# with other routers in the network, it performs synchronization of BIRD's
# routing tables with the OS kernel.
protocol kernel {
    scan time 60;
    import none;
    export all;   # Actually insert routes into the kernel routing table
}

# The Device protocol is not a real routing protocol. It doesn't generate any
# routes and it only serves as a module for getting information about network
# interfaces from the kernel. 
protocol device {
    scan time 60;
}

protocol static {
    include "routes4.conf";
}

protocol ospf {
    export all;

    area 0.0.0.0 {
        interface "*" {
            #authentication cryptographic;
            #password "foobar";
        };
    };
}

router id改成你的openwrt的IP
image.png

同理替换/etc/bird6.conf

router id 10.0.0.253;

protocol kernel {
    scan time 60;
    import none;
    export all;   # Actually insert routes into the kernel routing table
}

# The Device protocol is not a real routing protocol. It doesn't generate any
# routes and it only serves as a module for getting information about network
# interfaces from the kernel. 
protocol device {
    scan time 60;
}

protocol static {
    include "routes6.conf";
}

protocol ospf {
    export all;

    area 0.0.0.0 {
        interface "*" {
        };
    };
}

如果你不需要IPV6的分流的话,/etc/bird6.conf就不需要动

如果粘贴格式不对的话,也可以在PC上创建好文件,用文件管理替换掉源文件

然后将刚才生成的routes6.confroutes4.conf移动到/etc下面

mv routes4.conf /etc/routes4.conf
mv routes6.conf /etc/routes6.conf

image.png

点击bird4和6的restart,如果不需要ipv6分流的不需要点击bird6的restart

RouterOS

打开OSPF面板

image.png

如果你没有的话,那就是你没有安装或者没有启用routing package。

点击instance,点击+
image.png

名字随便,router ID填ros的IP

image.png

点击Areas,点击+
名字随便,instance选刚刚创建的,如图

image.png

点击interface templates,点击+

interfaces选择你内网的接口(桥接接口),Areas选择刚刚创建的

全部创建完成点击neighbors就可以看到Openwrt的router ID了
image.png
以上就是IPV4分流
同理IPV6只需要再添加一条instanceversion选择3就可以了
image.png

最后可以看到传过来的路由表了
image.png

热更新路由

我们需要修改openwrt上面那个Makefile
nano Makefile

image.png

图片上只执行ipv4的如果你需要IPV6把图片上的#删掉就可以了birdc4 configure改成/etc/init.d/bird4 reload

最后加上定时任务

crontab -e

i编辑 按Esc退出编辑,输入:wq保存退出

image.png

0 0 * * 0 make -C /root/nchnroutes

每周日0点更新

附件

route4.conf,route6.conf

如果你觉得本教程对你有帮助,请随意打赏,谢谢。

最后修改:2021 年 12 月 18 日
感谢您的支持