以前自己用 strongswan 等软件配 IPSec site-to-site 的时候,可以直接指定客户端的 IP 是 0.0.0.0,Azure 的 site-to-site IPSec VPN 必须要指定客户端 IP。
PowerShell 爱好者可以参考 https://www.hayesjupe.com/using-azure-rm-site-to-site-vpn-with-a-dynamic-ip/ 这篇文章。
我还是习惯用 Linux,于是使用 Azure CLI 来解决这个问题。
在 VPN 的 Virtual Network 里放置一台 Linux 虚拟机。按照上述连接里的说明安装软件。接下来开始操作。
1 登录
SSH 登录虚拟机后,执行命令,并按照输出,用浏览器登录 Azure 账号,完成认证。
1 2 |
$ az login To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code XXXXXXX to authenticate. |
如果是中国区 Azure,先需要设置服务器
1 2 3 4 |
$ az cloud set -n AzureChinaCloud Switched active cloud to 'AzureChinaCloud'. Use 'az login' to log in to this cloud. Use 'az account set' to set the active subscription. |
如果需要切换回海外版本
1 |
$ az cloud set -n AzureCloud |
2 添加更新服务
参考 DDNS 的玩法,让客户端定期更新IP。服务端可以简单地将数据记录到数据库、文件、或者任何地方,配置一个 cron 来执行更新检测和 Gateway IP 更新。
3 更新 Gateway IP
1 2 3 4 |
# 更新 IP $ az network local-gateway update -g RGNAME -n GWNAME --gateway-ip-address IP.ADD.RE.SS # 查看 Gateway $ az network local-gateway list -g RGNAME |
这里会有个问题,如果源 IP 和新 IP 相同,update 可能会出一个莫名其妙的404 错误。
1 |
Operation failed with status: 'Not Found'. Details: 404 Client Error: Not Found for url: https://management.chinacloudapi.cn/subscriptions/SUBSCRIPTION_ID/providers/Microsoft.Network/locations/chinanorth/operations/OPERATION_ID?api-version=2019-04-01 |
看了眼 –verbose –debug 的输出,这个 update 命令先发送了一条命令到服务端,然后轮询等待任务更新。可能这个任务服务端直接判定不需要执行,所以返回的 operation id 无效。