在一些網絡中經常會有針對個別域名走某個互聯網出口的需求,可能購買該域名訪問權限時綁定了公司的出口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%,
以上,祝你成功