在一些网络中经常会有针对个别域名走某个互联网出口的需求,可能购买该域名访问权限时绑定了公司的出口IP地址

而在公司网络中,虽然内部网络互通,但可能不同区域有自己独立的默认路由出口,这就导致访问某个域名无法路由到有权限的IP出口出而访问失败

通常的解决方案是代理,公司内部dns系统将该域名以及相关域名解析到网内的nginx代理,以实现统一出口,但该方案有时候无法在参数上完全适配对方的站点,比如图片显示不正常样式加载失败等

因此在路由层面将该域名的所有访问通过直接nat的方式就可以达到完全兼容

 

具体如下:

前提条件:

1、全网ospf动态路由

2、作为代理的路由器在ospf系统中,并且出口配置了有权限的公网IP,或者位于配置了有权限公网IP的其他出口设备之下默认路由从这台出口设备出

3、内网有dns服务器,设置条件转发器,将要适配的域名的解析转发到本次做代理的routeros

routeros配置

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

2、启用ros的dns服务器功能

3、ospf启用静态发布

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

:global mydns 123.net
:global mylist policy-dst-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 ( [/ip f add find list=$mylist address=$temp2]="") \
do={/ip f add add list=$mylist address=$temp2 comment=$temp1 timeout=1h}; \
}

5、对捕获的IP地址添加静态路由,并发布到ospf(routing filter里加了默认discard的条目避免不必要的静态发布到网内,因此该脚本每次添加一条要发布的静态路由在filter里同步添加一条accept放行,并移动到discard之上,同时由于路由器的老bug最后移动的accept条目需要禁用再启用才能生效)

:global tempaddr
:global tempgateway 10.10.0.1
:global tempcomment 123.net
:foreach i in=[/ip f ad find list=policy-dst-route] \
do={ \
:set tempaddr [/ip f add get $i address];\
:if ([/ip rou find dst-address=($tempaddr."/32")]="")\
do={/ip rou add dst-address=$tempaddr gateway=$tempgateway comment=$tempcomment};\
:if ([/routing filter find prefix=($tempaddr)]="")\
do={
/routing fil add action=accept chain=ospf-out prefix=($tempaddr) comment=$tempcomment;\
/routing fil pr without;\
/routing f move [/routing filter find prefix=($tempaddr)] 0;\
};\
}
/routing fil pr without;/routing f dis 0;/routing f en 0

 

6、定期删除address-list里已经过期消失的路由

:global mydns 123.net
:global mylist policy-dst-route
:global temp1
:global temp2
:foreach i in=[/ip route find where comment ~ $mydns] \
do={\
:set temp1 [/ip route get $i dst-address]
:set temp2 [:pick $temp1 0 [:find $temp1 "/" ] ];\
:if ( [/ip f add find list=$mylist address=$temp2]="") \
do={/ip rou rem $i;/routing f rem [find prefix=($temp2)]};\
}

7、将456分别加入到scheduler,4-5执行间隔5秒,定期删除的可以是24小时或更长

 

至此针对某个域名的访问可以完全以路由nat的方式实现识别和分流,兼容性100%,

以上,祝你成功