Skip to content

Commit 7e4333f

Browse files
committed
feat: 支持VPN账号固定IP地址
1 parent e18a76e commit 7e4333f

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

build/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM alpine:3.20.3
22

3-
RUN apk add --no-cache easy-rsa openvpn iptables iptables-legacy bash supervisor curl
3+
RUN apk add --no-cache easy-rsa openvpn iptables iptables-legacy bash supervisor curl sqlite
44
RUN ln -s /usr/share/easy-rsa/easyrsa /usr/local/bin
55
#RUN mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2
66

build/docker-entrypoint.sh

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ tls-server
5252
tls-version-min 1.2
5353
tls-cipher TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256
5454
auth-user-pass-verify /usr/lib/openvpn/plugins/openvpn-auth via-env
55-
client-disconnect "/usr/bin/docker-entrypoint.sh addhistory"
55+
client-disconnect /usr/bin/docker-entrypoint.sh
56+
client-connect /usr/bin/docker-entrypoint.sh
5657
script-security 3
5758
status $OVPN_DATA/openvpn-status.log
5859
client-config-dir $OVPN_DATA/ccd
@@ -305,6 +306,12 @@ $(cat $OVPN_DATA/pki/tc.key)
305306
EOF
306307
}
307308

309+
check_config(){
310+
config=$OVPN_DATA/server.conf
311+
grep -q "^client-connect" $config || echo "client-connect /usr/bin/docker-entrypoint.sh" >> $config
312+
grep -q "^client-disconnect" $config || echo "client-disconnect /usr/bin/docker-entrypoint.sh" >> $config
313+
}
314+
308315
add_history(){
309316
#https://build.openvpn.net/man/openvpn-2.6/openvpn.8.html#environmental-variables
310317
data="vip=$ifconfig_pool_remote_ip&rip=$trusted_ip&common_name=$common_name&username=$username&bytes_received=$bytes_received&bytes_sent=$bytes_sent&time_unix=$time_unix&time_duration=$time_duration"
@@ -313,8 +320,27 @@ add_history(){
313320
[ $status -ne 200 ] && echo "[CLIENT-DISCONNECT] $0:$LINENO 保存历史记录出错,请检查!" || true
314321
}
315322

323+
client_disconnect(){
324+
set +e
325+
add_history
326+
[ $? -ne 0 ] && echo "[CLIENT-DISCONNECT] $0:$LINENO 保存历史记录出错,请检查!"
327+
set -e
328+
}
329+
330+
client_connect(){
331+
#set static ip
332+
cc_file="$1"
333+
sql="SELECT ip_addr FROM user WHERE username='$username'"
334+
ipaddr=$(sqlite3 $ovpn_data/ovpn.db "$sql")
335+
echo "ifconfig-push $ipaddr $ifconfig_netmask" > $cc_file
336+
}
337+
338+
339+
316340
################################################################################################
317341

342+
343+
318344
if [ "$1" == "--init" ]; then
319345
mkdir -p $OVPN_DATA/ccd
320346

@@ -341,34 +367,39 @@ case $1 in
341367

342368
$(genclient $2 $3 "$4")
343369
exit 0
344-
;;
370+
;;
345371
"auth")
346372
$(auth $2)
347373
exit 0
348-
;;
374+
;;
349375
"renewcert")
350376
renew_cert
351377
exit 0
352-
;;
353-
"addhistory")
354-
set +e
355-
add_history
356-
[ $? -ne 0 ] && echo "[CLIENT-DISCONNECT] $0:$LINENO 保存历史记录出错,请检查!"
357-
set -e
358-
359-
exit 0
360-
;;
378+
;;
361379
"/usr/sbin/openvpn")
362380
[[ "$ENV_UPDATE_CONFIG" == "true" ]] && update_config
381+
check_config
363382
run_server
364-
;;
383+
;;
365384
"/usr/bin/supervisord")
366385
if [ ! -e $OVPN_DATA/.vars ]; then
367386
echo "请执行命令docker-compose run --rm openvpn --init进行初始化配置!"
368387
exit 1
369388
fi
370389
/usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
371-
;;
390+
;;
391+
esac
392+
393+
394+
case "$script_type" in
395+
client-connect)
396+
client_connect "$@"
397+
exit 0
398+
;;
399+
client-disconnect)
400+
client_disconnect "$@"
401+
exit 0
402+
;;
372403
esac
373404

374405
exec "$@"

0 commit comments

Comments
 (0)