@@ -52,7 +52,8 @@ tls-server
5252tls-version-min 1.2
5353tls-cipher TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256
5454auth-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
5657script-security 3
5758status $OVPN_DATA /openvpn-status.log
5859client-config-dir $OVPN_DATA /ccd
@@ -305,6 +306,12 @@ $(cat $OVPN_DATA/pki/tc.key)
305306EOF
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+
308315add_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+
318344if [ " $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+ ;;
372403esac
373404
374405exec " $@ "
0 commit comments