找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广告投放联系QQ68610888
查看: 21824|回复: 47

[Router OS] 分享下自用的ROS分流方案

  [复制链接]
本帖最后由 myvirus 于 2022-5-14 23:41 编辑

上次发的好多都被吃了,原文为MD格式,懒得改了~~~~

首先是一堆废话
一开始因对外网需求仅是查资料,偶尔下载点东西、看下youtube,使用的是月流量30G的小机场,一年439,够用。。
因为疫情原因比较闲,买了个netflix和disney,买的当天机场就欠费停了,续了30G,第二天又没了。实在受不了,买了台bwg GIA自建v2ray,然后就开始一直折腾
最初使用RouterOS做主路由,openwrt做旁路网关,adg home + openclash订阅小机场使用,很稳,这个一堆大神出过教程,就不啰嗦了
买了bwg GIA后自建v2ray,改了下机场的订阅配置,netflix和disney分流到gia,用了大概3天,间歇性断连,刷新网页又好了,受不了只能继续折腾
改用ss,无法秒开网页,但不会断连了,用了一周左右
然后在vps上搭建adguard home,openwrt上用smartDNS + openclash(这个教程也很多),因为是双线路分流原因,小机场我通常使用的都是hk的节点,bwg GIA在洛杉矶,我用vps的dns,反而更慢了,又还原了回去
有天看到youtube上推荐的ROS做CNIP + static dns fwd + wireguard分流,心一横,在vps上装了wireguard,按教程测了下,效果挺好
然后想到我有些个人站点在腾讯云轻量,那小水管太慢,我自己都基本不用,,干脆把vps刷成ROS系统,然后通过wg接口转发80、443端口到家里来,这样东西都在家,维护方便些。
已经稳定运行1个月左右了,正好同事也想搞,让他看视频他说不太懂原理,我和他详细说了次,就有了下面这份东西
废话结束

在vps中安装RouterOS,如果vps上有数据,建议先备份!!!!
  1. # 使用root用户登录

  2. # 更新系统,安装unzip,下载ros chr,解压
  3. apt update && apt install unzip -y && wget [url]https://download.mikrotik.com/routeros/7.2.3/chr-7.2.3.img.zip[/url] && unzip chr-7.2.3.img.zip

  4. # 挂载镜像,注:如果vps有DHCP服务,从这句到卸载镜像都可以不用执行
  5. mount -o loop,offset=33571840 chr-7.2.3.img /mnt
  6. # 注: 此处的offset可以使用 fdisk -lu chr-7.2.3.img 获取img2 -> Start获取,该镜像为65570,乘以512就是offset了

  7. # 定义变量,注意需要将eth0修改为vps的外网接口名称
  8. ADDR0=`ip addr show eth0 | grep global | cut -d' ' -f 6 | head -n 1`
  9. GATE0=`ip route list | grep default | cut -d' ' -f 3`

  10. # 在镜像中写入ROS自动运行脚本,该脚本的目的是在没有DHCP服务的环境中设置IP地址,注意修改其中的eth0接口为vps外网接口名称
  11. mkdir -p /mnt/rw && echo -e '/ip address add address=$ADDR0 interface=[/interface ethernet find where name=eth0]\n/ip route add gateway=$GATE0' > /mnt/rw/autorun.scr

  12. # 卸载镜像
  13. umount /mnt

  14. # 重新挂载所有的文件系统为只读,目的是怕其他进程同时写盘导致数据覆盖后无法启动系统
  15. echo u > /proc/sysrq-trigger

  16. # 写出镜像并重启,注意这里的/dev/vda,可以通过 fdisk -lu 查看vps磁盘设备获得
  17. dd if=chr-7.2.3.img bs=1024 of=/dev/vda && reboot

  18. # 然后使用ssh连接ROS,用户名admin,密码为空。
  19. ssh admin@VPS外网IP
复制代码


接下来配置ROS和WireGuard

ROS需要尽快修改密码。我遇到过reboot后去上厕所,一泡尿的功夫密码被改了!!!
  1. # 设置主机名称,方便查看操作的是哪个ros
  2. [admin@MikroTik] > /system identity set name=bwg-cn2-gia
  3. # 设置新密码
  4. [admin@bwg-cn2-gia] > /user set 0 password="新的密码"

  5. #禁用掉除ssh外的所有服务,并建议修改ssh默认端口。如果使用winbox的话,同理
  6. # 查看服务序号
  7. [admin@bwg-cn2-gia] > /ip service print
  8. Flags: X, I - INVALID
  9. Columns: NAME, PORT, CERTIFICATE, VRF
  10. #   NAME     PORT  CERTIFICATE  VRF
  11. 0 X telnet     23               main
  12. 1 X ftp        21                  
  13. 2 X www        80               main
  14. 3 X ssh        22               main
  15. 4 X www-ssl   443  none         main
  16. 5   api      8728               main
  17. 6   winbox   8291               main
  18. 7 X api-ssl  8729  none         main

  19. # 禁用除ssh外的所有服务,如果使用winbox,把6换成3即可,注:根据自己打印出来的顺序调整
  20. [admin@bwg-cn2-gia] > /ip service set 0,1,2,4,5,6,7 disabled=yes
  21. # 修改ssh端口
  22. [admin@bwg-cn2-gia] > /ip service set 3 port=65529
  23. # 退出
  24. [admin@bwg-cn2-gia] > quit

  25. # 使用新端口和密码重连
  26. ssh admin@VPS外网IP -p 65529
复制代码


修改DNS,这里可以使用traceroute看下距离、延迟和丢包情况。然后自己选几个最优的放到Servers里
  1. # 测试1.1.1.1
  2. [admin@bwg-cn2-gia] > /tool traceroute address=1.1.1.1
  3. Columns: ADDRESS, LOSS, SENT, LAST, AVG, BEST, WORST, STD-DEV
  4. #  ADDRESS         LOSS  SENT  LAST     AVG   BEST  WORST  STD-DEV
  5. 1  xxxxxxxxxxxxx   0%       8  13ms     19.3  11.8  45.4   10.2   
  6. 2                  100%     8  timeout                           

  7. ...
  8. 6  1.1.1.1         0%       7  1.7ms    1.7   1.7   1.8    0

  9. # 测试8.8.8.8
  10. [admin@bwg-cn2-gia] > /tool traceroute address=8.8.8.8
  11. # 测试其他....

  12. # 设置dns,开启DNS Settings里的Allow Remote Requests(允许远程请求)
  13. # 如果有访问大量不同国外站点的需求,可以将Cache Size(DNS缓存)改大点
  14. [admin@bwg-cn2-gia] > /ip dns set allow-remote-requests=yes cache-size=20480KiB servers=1.1.1.1,8.8.8.8,1.0.0.1,8.8.4.4
复制代码


在VPS和"家里"中添加WireGuard接口,并配置Peers
  1. -------------以下操作在VPS里执行
  2. # VPS添加WireGuard接口,接口名称、端口按实际情况修改
  3. [admin@bwg-cn2-gia] > /interface wireguard add listen-port=65530 mtu=1500 name=wg-home
  4. # 配置WG IP地址,这里为了方便,使用的24掩码,按需修改
  5. [admin@bwg-cn2-gia] > /ip address add interface=wg-home address=10.0.1.1/24
  6. # 查看公钥
  7. [admin@bwg-cn2-gia] > /interface wireguard print
  8. Flags: X - disabled; R - running
  9. 0  R name="wg-home" mtu=1500 listen-port=65530
  10.       private-key="...."
  11.       public-key="...."

  12. -------------以下操作在"家里"执行
  13. # 添加WireGuard接口
  14. [admin@RouterOS] > /interface wireguard add listen-port=65530 mtu=1500 name=wg-cn2-gia
  15. # 配置WG IP地址,注意这里的地址是10.0.1.2
  16. [admin@RouterOS] > /ip address add interface=wg-cn2-gia address=10.0.1.2/24
  17. # 查看公钥
  18. [admin@RouterOS] > /interface wireguard print
  19. Flags: X - disabled; R - running
  20. 0  R name="wg-cn2-gia" mtu=1500 listen-port=65530
  21.       private-key="...."
  22.       public-key="...."
  23. # 添加Peers
  24. [admin@RouterOS] > /interface wireguard peers add \
  25.    allowed-address=0.0.0.0/0 endpoint-address=VPS外网IP \
  26.    endpoint-port=65530 interface=wg-cn2-gia \
  27.     persistent-keepalive=15s public-key="VPS print命令中的public-key"


  28. -------------以下操作在VPS执行
  29. [admin@bwg-cn2-gia] > /interface wireguard peers add \
  30.    allowed-address=0.0.0.0/0 interface=wg-home \
  31.     persistent-keepalive=15s public-key="家里print命令中的public-key"

  32. # 测试是否能到达"家里"
  33. [admin@bwg-cn2-gia] > ping 10.0.1.2
  34.   SEQ HOST                                     SIZE TTL TIME       STATUS                                                                                                                           
  35.     0 10.0.1.2                                 56  64 177ms119us
  36.     1 10.0.1.2                                 56  64 176ms755us
  37.     ...
  38. # 如果不通,请查看防火墙里是否配置了禁ping
复制代码

如果"家中"有公网IP,或配置过DDNS,可以在VPS添加peers时,增加endpoint-address和endpoint-port,支持输入域名

以上步骤完成后,正常情况下,应该就可以通过"家里"访问到vps了

配置VPS防火墙

  1. # 添加防火墙过滤规则,注意修改ether1为你VPS的外网接口名称
  2. [admin@bwg-cn2-gia] > /ip firewall filter
  3. # 丢弃icmp数据包,比如ping...
  4. [admin@bwg-cn2-gia] > add action=drop chain=input disabled=yes in-interface=ether1 protocol=icmp
  5. [admin@bwg-cn2-gia] > add action=accept chain=input connection-state=established,related,untracked in-interface=ether1
  6. # 允许WireGuard端口,此处是udp协议,端口按自己的配置修改
  7. [admin@bwg-cn2-gia] > add action=accept chain=input dst-port=65530 in-interface=ether1 protocol=udp
  8. # 80、443是用来做转发的,如果不需要将个人站点代理到家中,可以不添加这两条
  9. [admin@bwg-cn2-gia] > add action=accept chain=input dst-port=80 in-interface=ether1 protocol=tcp
  10. [admin@bwg-cn2-gia] > add action=accept chain=input dst-port=443 in-interface=ether1 protocol=tcp
  11. # DNS端口
  12. [admin@bwg-cn2-gia] > add action=accept chain=input dst-port=53 in-interface=ether1 protocol=udp
  13. # 其他入站数据都丢弃
  14. [admin@bwg-cn2-gia] > add action=drop chain=input in-interface=ether1

  15. # 配置NAT
  16. [admin@bwg-cn2-gia] > /ip firewall nat
  17. # 配置IP伪装
  18. [admin@bwg-cn2-gia] > add action=masquerade chain=srcnat
  19. # 转发80、443到家里
  20. [admin@bwg-cn2-gia] > add action=dst-nat chain=dstnat dst-port=80 in-interface=ether1 protocol=tcp to-addresses=10.0.1.2 to-ports=80
  21. [admin@bwg-cn2-gia] > add action=dst-nat chain=dstnat dst-port=443 in-interface=ether1 protocol=tcp to-addresses=10.0.1.2 to-ports=443

  22. # 配置下change-mss,也就是OP中的MSS钳制
  23. [admin@bwg-cn2-gia] > /ip firewall mangle
  24. [admin@bwg-cn2-gia] > add action=change-mss chain=forward new-mss=clamp-to-pmtu passthrough=yes protocol=tcp tcp-flags=syn
  25. [admin@bwg-cn2-gia] > add action=change-mss chain=output new-mss=clamp-to-pmtu passthrough=no protocol=tcp tcp-flags=syn
复制代码

至此,VPS配置完成了,接下来的操作将在"家中"完成

导入CNIP、Static DNS FWD
  1. # 测试能否通过wg接口进行dns
  2. [admin@MikroTik] > put [resolve goog.com server=10.0.1.1]
  3. 44.238.227.49
  4. # 如果没有输出IP,请检查配置

  5. # 这里我们使用ruijzhan维护的https://github.com/ruijzhan/chnroute项目
  6. # 我修改为jsdelivr代理地址了,我这下载稍快些,
  7. # 添加CNIP导入脚本
  8. [admin@MikroTik] > /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://gcore.jsdelivr.net/gh/ruijzhan/chnroute@master/CN.rsc\r\
  9.     \nimport file-name=CN.rsc\r\
  10.     \nfile remove CN.rsc\r\
  11.     \n\r\
  12.     \n/tool fetch url=https://gcore.jsdelivr.net/gh/ruijzhan/chnroute@master/LAN.rsc\r\
  13.     \nimport file-name=LAN.rsc\r\
  14.     \nfile remove LAN.rsc"

  15. # 添加GFW URL to Static DNS脚本,注意,需要修改这里10.0.1.1为你VPS wireguard的地址,或修改为VPS外网IP
  16. [admin@MikroTik] > add dont-require-permissions=no name=gfwlist owner=admin policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=\
  17.     "{\r\
  18.     \n    :global dnsserver 10.0.1.1;\
  19.     \n    /tool fetch url=https://gcore.jsdelivr.net/gh/ruijzhan/chnroute@master/gfwlist.rsc\r\
  20.     \n    /import file-name=gfwlist.rsc\r\
  21.     \n    /file remove gfwlist.rsc\r\
  22.     \n}"

  23. # 执行CNIP导入脚本,CN列表即在该列表中的IP不进行分流,直接走"家里"的ICP线路
  24. [admin@MikroTik] > execute "cn"

  25. # 稍等一会,检查导入,导入应该有8600条左右
  26. [admin@MikroTik] > /ip firewall address-list export
  27. Columns: LIST, ADDRESS, CREATION-TIME
  28.    # LIST  ADDRESS           CREATION-TIME      
  29.    0 CN    5.182.60.0/22     may/10/2022 14:30:14
  30.    1 CN    45.3.32.0/19      may/10/2022 14:30:14
  31. ...

  32. # 添加局域网网段、wg网段、vps网段到CN列表,根据自己的情况进行修改!!!!必须加!!!
  33. # 这是我的局域网网段
  34. [admin@MikroTik] > /ip firewall address-list add list=CN address=10.0.0.0/24
  35. # WireGuard网段
  36. [admin@MikroTik] > /ip firewall address-list add list=CN address=10.0.1.0/24
  37. # VPS外网IP
  38. [admin@MikroTik] > /ip firewall address-list add list=CN address=VPS外网IP


  39. # 执行GFW URLs导入脚本,在该列表中的URL会通过WireGuard接口到VPS进行解析
  40. [admin@MikroTik] > execute "gfwlist"

  41. # 在稍等一会,检查导入,导入完成应该有6000条左右
  42. [admin@MikroTik] > /ip dns static export
  43. Columns: REGEXP, TYPE, TTL
  44.    #  REGEXP                                            TYPE  TTL
  45.    0  .*000webhost\.com$                                FWD   1d
  46.    1  .*030buy\.com$                                    FWD   1d
  47.    2  .*0rz\.tw$                                        FWD   1d
  48. ...

  49. # 测试static fwd规则是否生效
  50. [admin@MikroTik] > /put [resolve youtube.com]
  51. 142.251.40.46
  52. # 指定通过wg接口走vps进行dns请求,结果相同说明fwd规则生效了
  53. [admin@MikroTik] > /put [resolve youtube.com server=10.0.1.1]
  54. 142.251.40.46
复制代码

分流
  1. # 添加路由表
  2. [admin@MikroTik] > /routing table add fib name=vps
  3. # 添加要分流的内网IP,这里我使用1-245范围,过滤掉了部分不需要的地址,你也可以直接填10.0.0.0/24这种整个段,或单个ip的
  4. [admin@MikroTik] > /ip firewall address-list add list=bypass address=10.0.0.1-10.0.0.245
  5. # 添加路由,指定vps路由表走wireguard接口
  6. [admin@MikroTik] > /ip route add gateway=wg-cn2-gia routing-table=vps dst-address=0.0.0.0/0
  7. # 修改原地址列表在bypass中的,且目的地不是CNIP列表中的数据包走vps路由
  8. [admin@MikroTik] > /ip firewall mangle add action=mark-routing chain=prerouting dst-address-list=!CN \
  9.    new-routing-mark=vps passthrough=no src-address-list=bypass
复制代码

结束~~

分流关键点
1. ROS新增的DNS FWD,可以让符合正则规则的域名通过指定地址发送DNS请求
2. mark-routing

其他方式
1. 视频网站分流
    1. 在dns static列表中将netflix、disney等视频网站的域名修改为另一个wg地址
    2. 新建个address-list表,比如video,把netflix、disney等视频网站的IP导入
    3. 参考第7步分流的配置,dst-address-list处改为dst-address-list=video
2. 走旁路OP的ss、ssr、v2ray同理。把wireguard的部分换成旁路OP就行,旁路OP设为全局代理,省的在去过滤gfw、cnip
3. 故障切换,使用Netwatch监控WireGuard地址是否能通,如果不能,修改dns、route为另一个地址
4. 特定ip、域名走其他ICP线路

评分

参与人数 3恩山币 +4 收起 理由
roo*** + 1 你好,久仰卧龙大名,在下凤雏!.
ads*** + 2 你好,久仰卧龙大名,在下凤雏!
fanl*** + 1 很不错,有空折腾一下

查看全部评分

我的恩山、我的无线 The best wifi forum is right here.
防火强脚 本扔出来喂

点评

[attachimg]553617[/attachimg]  详情 回复 发表于 2022-5-21 00:18
之前发的被吃了。。重新编辑了  详情 回复 发表于 2022-5-15 00:39
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| | 显示全部楼层
lonei 发表于 2022-5-13 22:43
防火强脚 本扔出来喂

之前发的被吃了。。重新编辑了
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

Euserv 安装失败。。。
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

lonei 发表于 2022-5-13 22:43
防火强脚 本扔出来喂


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

感谢楼主,周末倒腾倒腾
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

请问油管上的视频链接是?
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

大佬牛逼,wg速度确实很爽,要是再分享一个定期更改wg端口的脚本就完美了

点评

我现在就是用wg,之前靠udp2raw升级补丁,现在既然ROS 7原生支持wg,那就彻底拿掉,在RB4011里定时ssh到远程vps上改wg的端口,远程成功了改本地的。 这里其实没啥,就是要给ROS的admin和远程VPS上的用户设置同一  详情 回复 发表于 2022-8-16 12:20
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

本帖最后由 mantouboji 于 2022-8-16 12:26 编辑
dvdrm 发表于 2022-8-2 11:47
大佬牛逼,wg速度确实很爽,要是再分享一个定期更改wg端口的脚本就完美了

我现在就是用wg,之前靠udp2raw伪装成TCP,现在既然ROS 7原生支持wg,那就彻底拿掉,在RB4011里定时ssh到远程vps上改wg的端口,远程成功了改本地的。

这里其实没啥,就是要给ROS的admin和远程VPS上的用户设置同一个ssh key,这样就不需要输入登录密码。VPS上设置这个用户无需口令即可sudo,就可以ssh过去sudo wg set wg0 listen-port xxx了。

然后在VPS里运行bird2,通过OSPFv2和v3把路由表从wg发过来给rb4011,这样境外的所有IPv4和v6流量都走wg。

为了防止DNS污染,另外在局域网里搞了一个OrangePi One跑chinadns-ng和dnsmasq。

目前爽歪歪。

  1. # Change both VPS and local port to avoid UDP QoS

  2. :local vps [/interface wireguard peers get [ find comment="VPS" ] endpoint-address ]
  3. :local newport [ :rndnum from=45000 to=55000 ]

  4. :put $newport
  5. :local logstr

  6. :if [ ping count=1 $vps as-value ] do={
  7.     :if [ /system/ssh-exec user=SSH_USER port=SSH_PORT address=$vps command="sudo wg set wg0 listen-port $newport " as-value ] do={
  8.         /interface/wireguard/peers/set [ find comment="VPS" ] endpoint-port=$newport
  9.         :set logstr "Change wireguard port to $newport"
  10. }
  11. }
  12. :log info $logstr
复制代码


点评

大佬请教一下每次改端口大慨可以存活多久呢?之前用的好像半天就挂了。不用伪装一下真的可以吗?  详情 回复 发表于 2023-10-8 14:04
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

小白求x86下一样的布设吗?
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

请问下,pt下载的网站基本上都是国外ip,怎么设置?在旁路由(vps)里面设置白名单吗?
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

你这个方案有一个问题,就是把DNS公开了。那些蹭DNS的会把vps的流量给用完。我就刚体验过了。

点评

在vps的ros中设置禁止外部访问53就行  详情 回复 发表于 2022-11-4 19:10
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| | 显示全部楼层
煎米茶 发表于 2022-10-17 14:01
你这个方案有一个问题,就是把DNS公开了。那些蹭DNS的会把vps的流量给用完。我就刚体验过了。

在vps的ros中设置禁止外部访问53就行
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

还有,change mss不应该是在客户端配置,怎么会在服务端配置的?
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

来自手机 | 显示全部楼层
需要聚合,只能用这个ros
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

欢迎大家光临恩山无线论坛上一条 /1 下一条

有疑问请添加管理员QQ86788181|手机版|小黑屋|Archiver|恩山无线论坛(常州市恩山计算机开发有限公司版权所有) ( 苏ICP备05084872号 )

GMT+8, 2024-4-29 09:47

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

| 江苏省互联网有害信息举报中心 举报信箱:js12377 | @jischina.com.cn 举报电话:025-88802724 本站不良内容举报信箱:68610888@qq.com 举报电话:0519-86695797

快速回复 返回顶部 返回列表