僅允許WireGuard私有網路的網站主機
您好,我是無能。管理頁面公開在網路上並不是很好,但在我的情況下,目前只允許WireGuard的私有IP訪問管理頁面。這就是設定方法。
WireGuard端設定
也就是說,目前正在使用多個客戶端。
客戶端
在這種情況下,對於wg0,由於AllowedIPs = 0.0.0.0/0, ::/0,所有流量都將通過WireGuard網路。
但是,在這種情況下,伺服器端需要為10.1.0.22設定通過伺服器端NAT的規則。因為如果沒有這樣做,這個IP將無法離開WireGuard的私有網路。
簡要總結如下:
- wg0
- 通過所有網路
- 查詢內部DNS
- 如果不在WireGuard伺服器端設定NAT規則,則無法出網
- wg1
- 僅路由WireGuard的私有IP
- 僅通過10.1.0.0/24的CIDR範圍內
- 查詢內部DNS
- 僅路由WireGuard的私有IP
wg0.conf:
[Interface]
PrivateKey = Client Private Key
Address = 10.1.0.22/24
DNS = 10.1.0.1
[Peer]
PublicKey = Server PubKey
AllowedIPs = 0.0.0.0/0, ::/0
Endpoint = WireGuard Server Endpoint:51820
PreSharedKey = PreSharedKey
wg1.conf:
[Interface]
PrivateKey = Client Private Key
Address = 10.1.0.21/24
DNS = 10.1.0.1
[Peer]
PublicKey = Server PubKey
AllowedIPs = 10.1.0.0/24
Endpoint = WireGuard Server Endpoint:51820
PreSharedKey = PreSharedKey
伺服器端
[Interface]
ListenPort = 51820
Address = 10.1.0.1/24
PrivateKey = Server PrivKey
~~~
[Peer]
PublicKey = Client PubKey
PreSharedKey = PreSharedKey
AllowedIPs = 10.1.0.21/32
PersistentKeepalive = 25
[Peer]
PublicKey = Client PubKey
PreSharedKey = PreSharedKey
AllowedIPs = 10.1.0.22/32
PersistentKeepalive = 25
結果如下。
WireGuard伺服器端的NAT規則
這是BSD系統的特權,我們將使用pf來設定NAT規則。
# 變數定義
wireguard_clients="{ 10.1.0.4, 10.1.0.22 }"
wanint="vtnet0"
wg_ports="{51820}"
# 將來自 $wireguard_clients 的流量,改寫為 WAN 介面($wanint)的 IP 後發送到網際網路
# NAPT設定
nat on $wanint inet from $wireguard_clients to any -> $wanint
# 允許從外部到 WAN 側的 WireGuard 連線(UDP 51820)
pass in on $wanint proto udp from any to $wanint port $wg_ports
這樣一來,WireGuard的私有IP本身就能夠通過WireGuard伺服器端的NAT。
Nginx設定
a.soulminingrig.com是此網站LumeCMS管理頁面的設定。
使用allow 10.1.0.0/16;僅允許WireGuard網路,其他則deny all;。
server {
listen 80;
server_name a.soulminingrig.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name a.soulminingrig.com;
client_max_body_size 500M;
location / {
allow 10.1.0.0/16;
deny all;
proxy_pass http://10.1.0.20:3001/;
include proxy_headers.conf;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504 http_403 http_404;
proxy_connect_timeout 3s;
proxy_read_timeout 15s;
}
include common_error_pages.conf;
include ssl_common.conf;
ssl_certificate /usr/local/etc/letsencrypt/live/a.soulminingrig.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /usr/local/etc/letsencrypt/live/a.soulminingrig.com/privkey.pem; # managed by Certbot
}
為什麼設定了NAT規則,卻沒有被識別為WireGuard伺服器端的公共IP,仍然可以進行IP限制?
(自行想像的問題)
NAT規則中是這樣寫的:
nat on $wanint inet from $wireguard_clients to any -> $wanint
也就是說,on $wanint表示NAT僅適用於通過WAN介面的封包。
這有點複雜,為了更容易理解,讓我們執行ifconfig看看。
vtnet0: flags=1008843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST,LOWER_UP> metric 0 mtu 1500
options=4c079b<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,VLAN_HWCSUM,TSO4,TSO6,LRO,VLAN_HWTSO,LINKSTATE,TXCSUM_IPV6>
ether aa
inet aa netmask 0xfffffe00 broadcast aa
inet6 aa prefixlen 64 scopeid 0x1
inet6 aa prefixlen 64
media: Ethernet autoselect (10Gbase-T <full-duplex>)
status: active
nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>
lo0: flags=1008049<UP,LOOPBACK,RUNNING,MULTICAST,LOWER_UP> metric 0 mtu 16384
options=680003<RXCSUM,TXCSUM,LINKSTATE,RXCSUM_IPV6,TXCSUM_IPV6>
inet 127.0.0.1 netmask 0xff000000
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x2
groups: lo
nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>
pflog0: flags=0 metric 0 mtu 33152
options=0
groups: pflog
wg0: flags=10080c1<UP,RUNNING,NOARP,MULTICAST,LOWER_UP> metric 0 mtu 1420
options=80000<LINKSTATE>
inet 10.1.0.1 netmask 0xffffff00
groups: wg
nd6 options=109<PERFORMNUD,IFDISABLED,NO_DAD>
是的,WireGuard終究是一個虛擬NIC,所以在WireGuard上的介面是wg0。
在這種情況下,由於它通過wg0,如果proxy_pass是10.1.0.20:3001,那麼封包將通過這個虛擬NIC。
10.1.0.22 -> 符合allow 10.1.0.0/16 (10.1.0.0 ~ 10.1.255.255) 的規則 -> Nginx的proxy_pass是10.1.0.20,因此在私有網路內完成
在此期間,所有操作都在wg0上的虛擬NIC內完成,因此可以進行這種私有通訊。
結語
以上就是我的說明,您覺得如何?
實際上,我個人不喜歡GNU/Linux的規則寫法,所以BSD系統的pf在這方面來說非常方便...
那麼下次再見。請多指教。