鸿蒙系统原生支持 设置 移动网络 VPN 添加 VPN 网络 类型 IKEv2/IPSec MSCHAPv2 等类型,我是自建了 IKEv2/IPSec MSCHAPv2 连到家里,连到家里之后流量怎么走就随你了。我的部分 docker 配置可以参考,没贴出来的部分你自己让 ai 自建你自己的就行
cat docker-compose.yml
++ cat docker-compose.yml
services:
省略
ddns-go:
image: jeessy/ddns-go:latest
container_name: ddns-go
restart: always
ports:
- "0.0.0.0:9876:9876"
volumes:
- ./ddns-go-data:/root
省略
# 9. IKEv2 / IPSec VPN 服务
ikev2-vpn:
build: ./ikev2-vpn
container_name: ikev2-vpn
restart: always
ports:
- "0.0.0.0:500:500/udp" # IKEv2 协商端口
- "0.0.0.0:4500:4500/udp" # NAT-T 端口
sysctls:
- net.ipv4.ip_forward=1 # 由 Docker 在容器 netns 初始化时设置
cap_add:
- NET_ADMIN
- NET_RAW # bridge 模式下需要,用于 raw socket 操作
volumes:
- ./ikev2-vpn/swanctl/swanctl.conf:/etc/swanctl/swanctl.conf
# 1. 把 acme.sh 自动生成的中间CA映射进容器CA目录
- ./ssl/你的域名_ecc/ca.cer:/etc/swanctl/x509ca/letsencrypt-ca.cer:ro
# 2. 直接映射 acme.sh 自动生成的完整证书链(叶子+中间CA)作为服务端证书
- ./ssl/你的域名_ecc/fullchain.cer:/etc/swanctl/x509/cert.pem:ro
# 3. 直接将私钥映射到容器内的 ecdsa 密钥目录
- ./ssl/你的域名_ecc/你的域名.key:/etc/swanctl/ecdsa/privkey.pem:ro
# 防止 acme 容器被清理
acme-holder:
image: neilpang/acme.sh:latest
container_name: acme-holder
restart: unless-stopped
entrypoint: ["sleep", "infinity"]
cat ikev2-vpn/Dockerfile
++ cat ikev2-vpn/Dockerfile
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y --no-install-recommends \
strongswan \
strongswan-swanctl \
strongswan-pki \
charon-systemd \
libcharon-extra-plugins \
libcharon-extauth-plugins \
libstrongswan-extra-plugins \
libstrongswan-standard-plugins \
iptables \
iproute2 \
ca-certificates \
curl \
&& rm -rf /var/lib/apt/lists/*
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
CMD ["/entrypoint.sh"]cat ikev2-vpn/entrypoint.sh
++ cat ikev2-vpn/entrypoint.sh
#!/bin/bash
set -evx
# 后台启动 charon
/usr/lib/ipsec/charon &
CHARON_PID=$!
# 等待 vici 接口就绪
for i in $(seq 1 30); do
if swanctl --stats >/dev/null 2>&1; then
echo "charon vici ready, loading swanctl config..."
break
fi
sleep 1
done
# 加载所有 swanctl 配置(连接、证书、密钥、pool、secrets)
swanctl --load-all
iptables 命令省略
# 保持容器前台运行
wait $CHARON_PID
cat ikev2-vpn/swanctl/swanctl.conf | sed '/eap-.*{/,/}/ { s/eap-[^{ ]*/eap-打码/; s/id = .*/id = 打码/; s/secret = .*/secret = "打码"/ }'
++ cat ikev2-vpn/swanctl/swanctl.conf
++ sed '/eap-.*{/,/}/ { s/eap-[^{ ]*/eap-打码/; s/id = .*/id = 打码/; s/secret = .*/secret = "打码"/ }'
connections {
ikev2-mschapv2 {
version = 2
proposals = default
rekey_time = 0s
fragmentation = yes
encap = yes
send_cert = always
local {
auth = pubkey
certs = cert.pem
id = 你的域名
}
remote {
auth = eap-mschapv2
eap_id = %any
}
children {
ikev2-child {
local_ts = 0.0.0.0/0
rekey_time = 0s
}
}
pools = vpn_pool
}
}
pools {
vpn_pool {
addrs = 10.10.10.2-10.10.10.254
dns = 10.10.10.1
}
}
secrets {
eap-打码 {
id = 打码
secret = "打码"
}
eap-打码 {
id = 打码
secret = "打码"
}
}
crontab -l | sed "s/Ali_Key=[^ ]*/Ali_Key=\"打码\"/; s/Ali_Secret=[^ ]*/Ali_Secret=\"打码\"/"
++ crontab -l
++ sed 's/Ali_Key=[^ ]*/Ali_Key="打码"/; s/Ali_Secret=[^ ]*/Ali_Secret="打码"/'
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
0 3 * * * /usr/bin/docker run --rm -v /opt/remote-gateway/ssl:/acme.sh -e Ali_Key="打码" -e Ali_Secret="打码" neilpang/acme.sh --cron --home /acme.sh >> /opt/remote-gateway/acme_cron.log 2>&1 && /usr/bin/docker exec nginx nginx -s reload >> /opt/remote-gateway/acme_cron.log 2>&1 && /usr/bin/docker exec ikev2-vpn swanctl --load-all >> /opt/remote-gateway/acme_cron.log 2>&1