仅允许WireGuard私有网络访问的网站托管
你好,我是无能。将admin页面公开到公共网络上不太好,但在我的情况下,目前只允许WireGuard的私有IP访问admin页面。这是设置方法。
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范围内的IP
- 查询内部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自身Admin页面的设置。通过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规则,IP限制仍然有效,而不会被识别为WireGuard服务器的公共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终究是一个虚拟网卡,所以在WireGuard上的接口是wg0。在这种情况下,由于流量通过wg0,如果proxy_pass是10.1.0.20:3001,那么数据包将通过这个虚拟网卡。
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上的虚拟网卡内部完成,因此可以进行这种私有通信。
最后
这就是全部内容,您觉得如何?实际上,我个人不喜欢GNU/Linux的规则写法,所以BSD系统的pf在这方面非常方便...那么,下次再见。请多关照。