- Add vpn.bash libraries,

- Add rx3_vpn_adm tools.
This commit is contained in:
2025-07-11 18:39:59 +02:00
parent 84d00cfd8b
commit 15678efa10
8 changed files with 429 additions and 7 deletions

180
usr/local/lib/vpn.bash Normal file
View File

@@ -0,0 +1,180 @@
<#!/bin/bash
if [[ "${VPN_BASH}" != "" ]]
then
return
else
declare -g VPN_BASH=1
fi
# Includes
#-----------------------------------------------------------------------------------------------------------------------------------
. /usr/global/lib/network.bash
# Global Variable
#-----------------------------------------------------------------------------------------------------------------------------------
declare -Ag VPN_JOB_TAB
declare -g VPN_STATUS_FILE="/var/lib/rx3-vpn.status"
if [ ! -v LOG ] || [[ "${LOG}" == "" ]]
then
export LOG=":"
fi
#--------------------------------------------------------------------------------------------------------------------------
# vpn_init
#--------------------------------------------------------------------------------------------------------------------------
vpn_init()
{
network_init
file_dir_init ${VPN_STATUS_FILE} root apache
}
#--------------------------------------------------------------------------------------------------------------------------
# vpn_deinit
#--------------------------------------------------------------------------------------------------------------------------
vpn_deinit()
{
network_deinit
}
#--------------------------------------------------------------------------------------------------------------------------
# vpn_start
#--------------------------------------------------------------------------------------------------------------------------
vpn_start()
{
local job_id=0
local job_nb
local dst_id
local config_file
local mng_port
for dst_id in ${NETWORK_DST_ID_LIST}
do
network_dst_tab_get ${dst_id}
if [[ "${dst_type}" == "1" ]]
then
config_file="${VPN_CONFIG_FILE/DEVICE/${dst_device}}"
mng_port=$((2330+$(echo ${dev} | sed -e "s/tun//")))
tab_assign VPN_JOB_TAB "${dst_id},PId" "0"
tab_assign VPN_JOB_TAB "${dst_id},Cmd" "/usr/sbin/openvpn --config ${config_file} --dev ${dst_device} --daemon"
${DEBUG} \rm -f ${config_file} 2>/dev/null
${DEBUG} sed -e "s/TARGET-CFG/${dst_config}-client.conf/" -e "s/VPN-DEVICE/${dst_device}/" -e "s/MNG-PORT/${mng_port}/" <${template_file} >${config_file}
echo >>${LOG_FILE} $(date) "Loading config: VPN[${dst_id}] Name: [${dst_name}] Cmd: [${VPN_JOB_TAB["${job_id},Cmd"]}]"
job_id=$(( ${job_id} + 1))
fi
done
job_nb=${job_id}
# Main Loop
while true
do
while [[ "${job_id}" -lt "${job_nb}" ]]
do
${DEBUG} kill -0 ${VPN_JOB_TAB["${job_id},PId"]} 2>/dev/null
rc=$?
if [[ ${VPN_JOB_TAB["${job_id},PId]"]} == 0 ]] || [[ $rc != 0 ]]
then
echo >> ${LOG_FILE} $(date) "Restarting Cmd: [${VPN_JOB_TAB["${job_id}",Cmd]}]..."
eval ${DEBUG} ${VPN_JOB_TAB["${job_id},Cmd"]}
VPN_JOB_TAB["${job_id},PId"]="$( ps aux | grep "${VPN_JOB_TAB["Cmd,${job_id}"]}" | grep -v grep | head -1 | awk '{print $2}')"
VPN_JOB_TAB["${job_id},Date"]="$(date)"
echo >> ${LOG_FILE} $(date) "PId: [${VPN_JOB_TAB["${job_id},PId"]}]!"
fi
job_id=$(( ${job_id} + 1))
done
job_id=0
>${VPN_STATUS_FILE}
while [[ ${job_id} -lt ${job_nb} ]]
do
echo >>${VPN_STATUS_FILE} "PId: [${VPN_JOB_TAB["${job_id},PId"]}] Date: [${VPN_JOB_TAB["${job_id},Date"]}] Cmd: [${VPN_JOB_TAB["${job_id},Date"]}]"
job_id=$(( ${job_id} + 1))
done
job_id=0
sleep 30
network_dst_address_refresh_all
done
}
#--------------------------------------------------------------------------------------------------------------------------
# vpn_stop
#--------------------------------------------------------------------------------------------------------------------------
vpn_stop()
{
echo >> ${LOG_FILE} $(date) "Killing daemon: [$(cat ${PID_FILE})]..."
kill -15 $(cat ${PID_FILE})
while read stat
do
echo >> ${LOG_FILE} $(date) "Killing VPN: [$stat] [$(echo "$stat" | sed -e 's/PId: \[//' -e 's/\].*//')]..."
${DEBUG} kill -15 $(echo "$stat" | sed -e 's/PId: \[//' -e 's/\].*//')
done <${VPN_STATUS_FILE}
}
#--------------------------------------------------------------------------------------------------------------------------
# vpn_status
#--------------------------------------------------------------------------------------------------------------------------
vpn_status()
{
if [ -f ${VPN_STATUS_FILE} ]
then
cat ${VPN_STATUS_FILE}
fi
}