@@ -52,7 +52,8 @@ tls-server
52
52
tls-version-min 1.2
53
53
tls-cipher TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256
54
54
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
56
57
script-security 3
57
58
status $OVPN_DATA /openvpn-status.log
58
59
client-config-dir $OVPN_DATA /ccd
@@ -305,6 +306,12 @@ $(cat $OVPN_DATA/pki/tc.key)
305
306
EOF
306
307
}
307
308
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
+
308
315
add_history (){
309
316
# https://build.openvpn.net/man/openvpn-2.6/openvpn.8.html#environmental-variables
310
317
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(){
313
320
[ $status -ne 200 ] && echo " [CLIENT-DISCONNECT] $0 :$LINENO 保存历史记录出错,请检查!" || true
314
321
}
315
322
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
+
316
340
# ###############################################################################################
317
341
342
+
343
+
318
344
if [ " $1 " == " --init" ]; then
319
345
mkdir -p $OVPN_DATA /ccd
320
346
@@ -341,34 +367,39 @@ case $1 in
341
367
342
368
$( genclient $2 $3 " $4 " )
343
369
exit 0
344
- ;;
370
+ ;;
345
371
" auth" )
346
372
$( auth $2 )
347
373
exit 0
348
- ;;
374
+ ;;
349
375
" renewcert" )
350
376
renew_cert
351
377
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
+ ;;
361
379
" /usr/sbin/openvpn" )
362
380
[[ " $ENV_UPDATE_CONFIG " == " true" ]] && update_config
381
+ check_config
363
382
run_server
364
- ;;
383
+ ;;
365
384
" /usr/bin/supervisord" )
366
385
if [ ! -e $OVPN_DATA /.vars ]; then
367
386
echo " 请执行命令docker-compose run --rm openvpn --init进行初始化配置!"
368
387
exit 1
369
388
fi
370
389
/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
+ ;;
372
403
esac
373
404
374
405
exec " $@ "
0 commit comments