ROS 通过域名进行数据包分流

公司可能会有多条互联网出口链路;一些自己购买的域名或资源有白名单,只允许公司的某一特定出口可以访问,可终端所有流量使用的另外一条宽带,造成无法访问。
未解决此类问题,我们需要针对访问的域名或IP,配置PBR,使终端请求这个目标资源时,使用特定出口进行访问,达到正常访问的目的。

 

这里举例说明:
当前公司有多个互联网出口,并且有专线上网线路。想针对访问openai域名的流量使用专线进行访问,其余走国内本地互联网出口。

openai域名列表可参考以下链接:

https://github.com/v2fly/domain-list-community/blob/master/data/openai

https://github.com/blackmatrix7/ios_rule_script/blob/master/rule/Shadowrocket/OpenAI/OpenAI_Resolve.list

 

 

RouterOs配置

1、配置ros的上网条件,ip,路由,nat,上级dns

  针对需要使用设备出口上网的,需要对出口配置NAT。

命令: /ip firewall nat add chain=srcnat action=masquerade out-interface=TO-Overs

 

 

2、启用ros的DNS服务器功能,并配置可以同时解析国内外域名的DNS

命令: /ip dns set allow-remote-requests=yes servers=*.*.*.*

 

 

3、创建script用来捕获dns缓存里关于该域名的所有IP地址信息,并保存到地址列表。

:global mydns openai.com
:global mylist openai-route
:global temp1
:global temp2
:foreach i in=[/ip dns cache find where name ~ $mydns] do={
  :set temp1 [/ip dns cache get $i name];
  :if ([/ip dns cache get $i type]="A") do={
    :set temp2 [/ip dns cache get $i data];
  } else={
    :set temp2 [:resolve $temp1];
  };
  :if ([:len [/ip firewall address-list find where list=$mylist address=$temp2]] = 0) do={
    /ip firewall address-list add list=$mylist address=$temp2 comment=$temp1 timeout=1d;
  };
}


##注解:在/ip dns cach缓存内,查找ttwstatic.com相关的解析记录,并将查询结果添加到policy-dst-route 地址表(地址表内的IP有效时间为1天,可自己按照业务更改有效时长)

 

4.配置本地地址列表,便于后期增删对那些来源生效。

命令: /ip firewall  address-list add list=test_wifi address=192.168.10.0/24

 

5.配置mangel规则:

命令: /ip firewall mangle add chain=prerouting action=route passthrough=yes route-dst=10.10.15.133 src-address-list=test_wifi dst-address-list=openai-route log=no

 


##注解:

规则内容:对来源地址在test_wifi列表内,同时访问的目标地址在openai-route列表内的数据包,设置下一跳地址为10.10.15.133(10.10.15.133特定出口的对端直连地址)。即来源是192.168.10.0/24且是访问openai域名的,设置下一跳是10.10.15.133

 

6.配置周期性任务scheduler

   将刚刚创建的script加入到scheduler,并设置执行间隔时间。     

 ##建议执行间隔设置5分钟,可根据域名的TTL时间进行设定。

 

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

因openai的访问会涉及多个一级域名,故在配置脚本时需要配置多个,一个脚本对应一个一级域名。

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

至此配置已经结束,可以同时使用双线路域名分流。

祝您成功!

 

posted @ 2024-01-09 11:35  Hope·  阅读(219)  评论(0编辑  收藏  举报
分享到: