# 前情提要 在兩層NAT的情況要開對外服務很麻煩,每次都請營運商改路由器設定不是長久之計。 cloudflared tunnel可以解決普通的網頁等TCP based的流量,但需要自有Domain,也無法處理UDP流量 # 情境 路由器1,2都是routeros 由於路由器1在NAT內,沒有public ip,想讓特定服務從public ip存取 路由器2在有public的環境 ```mermaid graph LR; subgraph 營運商內網 subgraph 內網 伺服器<-->路由器1 end 路由器1<-->營運商路由器 end 營運商路由器<-->公網 公網<-->路由器2 subgraph 第三方 路由器1<--wireguard-->路由器2 end ``` # 作法 在另一個有外網的路由器(後面稱為 路由器2)上建立wireguard vpn,同時在路由器2上加入masquerade 來轉發封包。 這時路由器1上有兩個外網: - wan(eth1) - wireguard1 (ip = 10.1.0.2) 想要透過wireguard來讓特定的服務可以從這裡存取 內網: - 192.168.88.0/24 ## 路由器1設定 0. 加入路由器2的wireguard vpn 1. 新增dst-nat (在ip->firewall->nat) - chain=dst-nat dst-address=10.1.0.2 protocal=udp dst-port=服務的port action=dst-nat to-address=內網要開出服務的ip to-port=服務的port 2. 新增marked routing (在ip->firewall->mangle) - chain=prerouting in-interface=wireguard1 action=mark-connection new-connection-mark=wireguard(隨便設自己看得懂的) passthrough=yes(預設的沒動,不確定效果) - chain=prerouting in-interface-list=!WAN(除了eth1和wireguard1來的) connection-mark=wireguard action=mark-routing new-routing-mark=rtab-1(如果只有main,去routing->tables新增第二個table) passthrough=yes(一樣預設,不確定效果) ## 路由器2設定 1. 建立wireguard vpn供路由器1連線 2. 設定dst-nat將連到路由器2 public ip特定port的封包轉發到路由器1(to-address=10.1.0.2) 3. 設定masquerade 將路由器1回傳過來的封包(from-interface wireguard1)轉成src-ip=public-ip送回外網 # 參考 https://forum.mikrotik.com/viewtopic.php?t=152949
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up