本文为了个人记录@wan使用过程中的简单操作
- 生成wireguard 客户端的配置文件
- 修改配置文件
- 设置开机脚本
- #(6.14)新增一键搞
利用wg-easy容器作为wireguard服务端进行管理
添加客户端后下载配置文件格式为:
[Interface]
PrivateKey = 0D9YGlpQ9LVCe7/TTJS7qRE2YU/mCJI6CaGuaOYhpFY=
Address = 10.0.2.11/24
DNS = 1.1.1.1
[Peer]
PublicKey = p6+PuE28RoYmuYf1CsT0R8fmd0N4ZJ19UD3x8KJO2o=
PresharedKey = AUA+3T+h98vyjWJsUhUKqotYPravGaABYORZNb9ZFak=
AllowedIPs = 10.0.2.0/24
PersistentKeepalive = 0
Endpoint = cal.srun.top:52888
@wan中自带wg 命令可以直接开启隧道,配置文件格式需要更新:
去掉Address、DNS 行,同时设置保活时间PersistentKeepalive=30。完整格式:
[Interface]
PrivateKey = 0D9YGlpQ9LVCe7/TTJS7qRE2YU/mCJI6CaGuaOYhpFY=
#Address = 10.0.2.11/24
#DNS = 1.1.1.1
[Peer]
PublicKey = p6+PuE28RoYmuYf1CsT0R8fmd0N4ZJ19UD3x8KJO2o=
PresharedKey = AUA+3T+h98vyjWJsUhUKqotYPravGaABYORZNb9ZFak=
AllowedIPs = 10.0.2.0/24
PersistentKeepalive = 30
Endpoint = cal.srun.top:52888
创建etc下目录,mkdir /etc/wireguard,创建文件vi /etc/wireguard/wg.conf,写入上面配置。
可直接利用配置文件开启隧道:(修改地址为配置文件中的地址)
ip link add dev wg type wireguard
ip address add dev wg 10.0.2.100/24
wg setconf wg /etc/wireguard/wg.conf
ip link set up dev wg
开机启动项:创建 vi /etc/init.d/wg
#!/bin/sh /etc/rc.common
START=99
start() {
echo "Starting My Daemon..."
ip link add dev wg type wireguard
ip address add dev wg 10.0.2.11/24
wg setconf wg /etc/wireguard/wg.conf
ip link set up dev wg
}
stop() {
echo "Stopping My Daemon..."
ip link del dev wg type wireguard
}
restart() {
echo "Restarting My Daemon..."
stop
sleep 1
start
wg addconf wg /etc/wireguard/wg.conf
}
添加运行权限,设置开机启动:
chmod +x /etc/init.d/wg
/etc/init.d/wg enable
可通过 cat /etc/rc.d/S99wg 检查是否有这个文件,有就表示开机启动设置成功。
可通过/etc/init.d/wg start|stop|restart 对接口进行管理。
重启设备后,也能自己连wireguard了。
只要设备有网,就能通过隧道进入 https://ip:18081 进行管理。
一键通
思路:运行脚本,直接全选复制粘贴下载的配置文件能自动修改生成并处理完毕。
#可补充添加wireguard接口到/etc/config/network中,也可不用,反正开机启动运行没问题
cat <<EOF > /tmp/wg.sh
#!/bin/bash
mkdir /etc/wireguard
echo "请输入wireguard配置:(CTRL+D结束)(不知道为什么我输入两次才下一步,那就两次)"
WG_CONFIG=\$(cat)
#提取Address的值到变量
ADDRESS=\$(echo "\$WG_CONFIG" | grep 'Address' | cut -d ' ' -f 3)
# 打印出Address的值
#echo "Address的值是:\$ADDRESS"
# 注释掉Address和DNS的行
WG_CONFIG=\$(echo "\$WG_CONFIG" | sed 's/^Address/#Address/g' | sed 's/^DNS/#DNS/g')
# 把Keepalive改为30
WG_CONFIG=\$(echo "\$WG_CONFIG" | sed 's/PersistentKeepalive = 0/PersistentKeepalive = 30/g')
echo "\$WG_CONFIG" > /etc/wireguard/wg.conf
cat <<EOF1 > /etc/init.d/wg
#!/bin/sh /etc/rc.common
START=99
start() {
echo "WG启动!!!"
ip link add dev wg type wireguard
ip address add dev wg \$ADDRESS
wg setconf wg /etc/wireguard/wg.conf
ip link set up dev wg
}
stop() {
echo "关了关了..."
ip link del dev wg type wireguard
}
restart() {
echo "关关开开~"
stop
echo "say hello ~"
start
wg addconf wg /etc/wireguard/wg.conf
}
EOF1
chmod +x /etc/init.d/wg
/etc/init.d/wg enable
/etc/init.d/wg restart
#键盘敲击CTRL+D结束,并需手动输入 sh /tmp/wg.sh
EOF
sh /tmp/wg.sh