From 16616a5039cd259a23487ba58cffc06ccb5cd21e Mon Sep 17 00:00:00 2001 From: David Schroeder Date: Sat, 25 Mar 2023 10:12:33 -0500 Subject: [PATCH] Update mm-scripts.sh --- mm-scripts.sh | 124 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 97 insertions(+), 27 deletions(-) diff --git a/mm-scripts.sh b/mm-scripts.sh index 202a231..beaa083 100755 --- a/mm-scripts.sh +++ b/mm-scripts.sh @@ -31,6 +31,8 @@ IFS=, SERVICES_CHECK=(${SERVICESCHECK}) unset IFS +cw_spc=38 + MMSTART(){ [ $VERBOSE = true ] && chktmstart=`date +%s` if [ "${1}" = "start" ] || [ "${1}" = "restart" ] || [ "${1}" = "" ]; then @@ -59,7 +61,12 @@ MMSTART(){ echo OUT=$(/sbin/ip link | grep "${VPN_INTERFACE}") if [ ${#OUT} = 0 ]; then - echo -en "${idsCL[LightCyan]}Setting up the VPN Tunnel config :${idsCL[Default]}" + + msg="Setting up the VPN Tunnel config" + c=0; spc=''; spc1=$((${cw_spc}-${#msg})) + until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done + echo -en "${idsCL[LightCyan]}${msg}${spc}: ${idsCL[Default]}" + privkey=$(/usr/bin/wg genkey) reqpublickey=$(echo "${privkey}" | /usr/bin/wg pubkey) reqpublickey="$(php -r 'echo rawurlencode($argv[1]);' -- "$reqpublickey")" @@ -90,20 +97,24 @@ AllowedIPs = 0.0.0.0/0 Endpoint = ${vpn_server_address}:${vpn_server_port} PersistentKeepalive = 25" >> /etc/wireguard/${VPN_INTERFACE}.conf - echo -e "${idsCL[Green]} done${idsCL[Default]}" + echo -e "${idsCL[Green]}done${idsCL[Default]}" + msg="Starting the VPN Tunnel" - echo -en "${idsCL[LightCyan]}Starting the VPN Tunnel :${idsCL[Default]}" "${VPN_START[@]}" >/dev/null 2>&1 sleep 5s else - echo -en "${idsCL[Green]}The VPN Tunnel is already running :${idsCL[Default]}" + msg="The VPN Tunnel is already running" fi else - echo -en "${idsCL[LightCyan]}Verifying the VPN Tunnel :${idsCL[Default]}" + msg="Verifying the VPN Tunnel" mmsv='Verifying' fi + c=0; spc=''; spc1=$((${cw_spc}-${#msg})) + until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done + echo -en "${idsCL[LightCyan]}${msg}${spc}: ${idsCL[Default]}" + [ $VERBOSE = true ] && echo "$(date) - ${mmsv} VPN..." >> $logfile @@ -144,7 +155,7 @@ PersistentKeepalive = 25" >> /etc/wireguard/${VPN_INTERFACE}.conf else if [ ${GOFORSTART} = true ] || [ "${1}" == "check" ]; then - echo -e "${idsCL[Green]} done${idsCL[Default]}" + echo -e "${idsCL[Green]}done${idsCL[Default]}" fi zerogw=$(/sbin/ip route show | grep 0.0.0.0 | cut -d' ' -f 3) @@ -154,20 +165,29 @@ PersistentKeepalive = 25" >> /etc/wireguard/${VPN_INTERFACE}.conf # if [ "${defaultgw}" != "${VPN_INTERFACE}" ]; then if [ "${zerogw}" != "${defaultgw}" ] || [ "${defaultgw}" != "${VPN_INTERFACE}" ]; then echo "$(date) - Adding VPN default routes" >> $logfile - echo -en "${idsCL[LightCyan]}Adding VPN default routes :${idsCL[Default]}" + + msg="Adding VPN default routes" + c=0; spc=''; spc1=$((${cw_spc}-${#msg})) + until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done + echo -en "${idsCL[LightCyan]}${msg}${spc}: ${idsCL[Default]}" + /bin/ip route del default /bin/ip route add default dev ${VPN_INTERFACE} /bin/ip route add 0.0.0.0/1 dev ${VPN_INTERFACE} /bin/ip route add 128.0.0.0/1 dev ${VPN_INTERFACE} /bin/ip route add ${vpn_server_address}/32 via 10.10.0.1 dev eth0 - echo -e "${idsCL[Green]} done${idsCL[Default]}" + + echo -e "${idsCL[Green]}done${idsCL[Default]}" zerogw=$(/sbin/ip route show | grep 0.0.0.0 | cut -d' ' -f 3) defaultgw=$(/sbin/ip route show | grep default | cut -d' ' -f 3) fi + msg="Verifying default network routes" + c=0; spc=''; spc1=$((${cw_spc}-${#msg})) + until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done + echo -en "${idsCL[LightCyan]}${msg}${spc}: ${idsCL[Default]}" - echo -en "${idsCL[LightCyan]}Verifying default network routes :${idsCL[Default]}" checked=false cc=0 until [ "${checked}" = "" ]; do @@ -192,11 +212,13 @@ PersistentKeepalive = 25" >> /etc/wireguard/${VPN_INTERFACE}.conf zerogw=$(/sbin/ip route show | grep 0.0.0.0 | cut -d' ' -f 3) defaultgw=$(/sbin/ip route show | grep default | cut -d' ' -f 3) done - echo -e "${idsCL[Green]} done${idsCL[Default]}" - - - echo -en "${idsCL[LightCyan]}Verifying traffic routing through VPN :${idsCL[Default]}" + echo -e "${idsCL[Green]}done${idsCL[Default]}" + msg="Verifying traffic routing through VPN" + c=0; spc=''; spc1=$((${cw_spc}-${#msg})) + until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done + echo -en "${idsCL[LightCyan]}${msg}${spc}: ${idsCL[Default]}" + scip="" mmip="" cc=0 @@ -205,6 +227,7 @@ PersistentKeepalive = 25" >> /etc/wireguard/${VPN_INTERFACE}.conf # mmip=$(/usr/bin/dig +short myip.opendns.com @resolver1.opendns.com) mmip=$(/usr/bin/dig +short +time=2 myip.opendns.com @208.67.222.222) if [ ${cc} -eq 10 ]; then + echo echo -e "${idsCL[Red]}Cannot get an WAN address, restarting system!${idsCL[Default]}" echo "$(date) - Cannot get an WAN address, restarting system! scip(${scip}) mmip(${mmip})" >> $logfile [ $VERBOSE = true ] && echo "$(date) - Default Routes: zerogw(${zerogw}) defaultgw(${defaultgw})" >> $logfile @@ -222,9 +245,10 @@ PersistentKeepalive = 25" >> /etc/wireguard/${VPN_INTERFACE}.conf sleep 1s done if [ "${scip}" != "${mmip}" ]; then - echo -e "${idsCL[Green]} done${idsCL[Default]}" + echo -e "${idsCL[Green]}done${idsCL[Default]}" [ $VERBOSE = true ] && echo "$(date) - WAN Address: ${mmip} - GW Address: ${defaultgw}" >> $logfile else + echo echo -e "${idsCL[Red]}Traffic is not routing through VPN, restarting system!${idsCL[Default]}" echo "$(date) - Traffic is not routing through VPN, restarting system! scip(${scip}) mmip(${mmip})" >> $logfile [ $VERBOSE = true ] && echo "$(date) - Default Routes: zerogw(${zerogw}) defaultgw(${defaultgw})" >> $logfile @@ -239,8 +263,11 @@ PersistentKeepalive = 25" >> /etc/wireguard/${VPN_INTERFACE}.conf exit 1 fi + msg="Verifying Plex Media is Mounted" + c=0; spc=''; spc1=$((${cw_spc}-${#msg})) + until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done + echo -en "${idsCL[LightCyan]}${msg}${spc}: ${idsCL[Default]}" - echo -en "${idsCL[LightCyan]}Verifying Plex Media is Mounted : ${idsCL[Default]}" ### CHECK FOR PLEX MOUNT files=$(shopt -s nullglob dotglob; echo ${PLEX_MOUNT}/*) if (( ! ${#files} )); then @@ -288,11 +315,18 @@ PersistentKeepalive = 25" >> /etc/wireguard/${VPN_INTERFACE}.conf [ "${1}" == "start" ] && rm -f ${MMFOLDER}/mm.noservices if [ "${2}" != "vpn" ] && [ ! -f ${MMFOLDER}/mm.noservices ] && [ ! -f ${MMFOLDER}/mm.plexmount.fail ]; then - echo -en "${idsCL[LightCyan]}${mmsv} Services :${idsCL[Default]}" + + msg="${mmsv} Services" + c=0; spc=''; spc1=$((${cw_spc}-${#msg})) + until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done + echo -en "${idsCL[LightCyan]}${msg}${spc}: ${idsCL[Default]}" + for srvc in "${SERVICES_CHECK[@]}"; do /bin/systemctl start ${srvc} done - echo -e "${idsCL[Green]} done${idsCL[Default]}" + + echo -e "${idsCL[Green]}done${idsCL[Default]}" + else echo -e "${idsCL[LightYellow]}Skipping ${mmsv} Services ${idsCL[Default]}" fi @@ -403,8 +437,8 @@ MMSTATUS(){ statusArray+=(${SERVICES_CHECK[@]}) echo for srvc in "${statusArray[@]}"; do - c=0; cw=38 spc='' - spc1=$((${cw}-${#NM_SERVICES[${srvc}]})) + c=0; spc='' + spc1=$((${cw_spc}-${#NM_SERVICES[${srvc}]})) until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done echo -en " ${NM_SERVICES[${srvc}]}$spc: " @@ -426,8 +460,18 @@ MMSTATUS(){ if [[ ${srvc} == *"vpn"* ]] || [[ ${srvc} == *"wireguard"* ]]; then zerogw=$(/sbin/ip route show | grep 0.0.0.0 | cut -d' ' -f 3) defaultgw=$(/sbin/ip route show | grep default | cut -d' ' -f 3) - echo -e " |- 0.0.0.0 gateway : ${idsCL[Green]}$zerogw${idsCL[Default]}" - echo -en " |- DEFAULT gateway : " + + msg=" |- 0.0.0.0 Gateway" + c=0; spc=''; spc1=$((${cw_spc}-${#msg})) + until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done + echo -en "${idsCL[LightCyan]}${msg}${spc}: ${idsCL[Default]}" + echo -e "${idsCL[Green]}$zerogw${idsCL[Default]}" + + msg=" |- DEFAULT Gateway" + c=0; spc=''; spc1=$((${cw_spc}-${#msg})) + until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done + echo -en "${idsCL[LightCyan]}${msg}${spc}: ${idsCL[Default]}" + if [ "${zerogw}" == "${defaultgw}" ] && [ "${zerogw}" == "${VPN_INTERFACE}" ]; then echo -e "${idsCL[Green]}$defaultgw${idsCL[Default]}" else @@ -443,12 +487,27 @@ MMSTATUS(){ echo -e "${idsCL[LightCyan]}Starting system back up...${idsCL[Default]}" echo MMSTART - - echo -e " ${NM_SERVICES[wireguard]} : ${idsCL[Green]}Running${idsCL[Default]}" + + msg=" ${NM_SERVICES[wireguard]}" + c=0; spc=''; spc1=$((${cw_spc}-${#msg})) + until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done + echo -en "${idsCL[LightCyan]}${msg}${spc}: ${idsCL[Default]}" + echo -e "${idsCL[Green]}Running${idsCL[Default]}" + zerogw=$(/sbin/ip route show | grep 0.0.0.0 | cut -d' ' -f 3) defaultgw=$(/sbin/ip route show | grep default | cut -d' ' -f 3) - echo -e " |- 0.0.0.0 gateway : ${idsCL[Green]}$zerogw${idsCL[Default]}" - echo -en " |- DEFAULT gateway : " + + msg=" |- 0.0.0.0 Gateway" + c=0; spc=''; spc1=$((${cw_spc}-${#msg})) + until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done + echo -en "${idsCL[LightCyan]}${msg}${spc}: ${idsCL[Default]}" + echo -e "${idsCL[Green]}$zerogw${idsCL[Default]}" + + msg=" |- DEFAULT Gateway" + c=0; spc=''; spc1=$((${cw_spc}-${#msg})) + until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done + echo -en "${idsCL[LightCyan]}${msg}${spc}: ${idsCL[Default]}" + if [ "${zerogw}" == "${defaultgw}" ] && [ "${zerogw}" == "${VPN_INTERFACE}" ]; then echo -e "${idsCL[Green]}$defaultgw${idsCL[Default]}" else @@ -463,7 +522,11 @@ MMSTATUS(){ fi fi - echo -en " |- Verify routing through VPN : " + msg=" |- Verify routing through VPN" + c=0; spc=''; spc1=$((${cw_spc}-${#msg})) + until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done + echo -en "${idsCL[LightCyan]}${msg}${spc}: ${idsCL[Default]}" + scip=$(/usr/bin/dig +short sc1.scity.us @10.10.10.5 -p 5053) # mmip=$(/usr/bin/dig +short myip.opendns.com @resolver1.opendns.com) mmip=$(/usr/bin/dig +short +time=2 myip.opendns.com @208.67.222.222) @@ -506,7 +569,14 @@ GETVPNINFO(){ # # echo -e "${idsCL[Green]}VPN Connection Info${idsCL[Default]}" # echo - # echo -e "${idsCL[LightCyan]}Server Network : ${idsCL[Green]}${VPN_SERVER}${idsCL[Default]}" + # + # msg="Server Network" + # c=0; spc=''; spc1=$((${cw_spc}-${#msg})) + # until [ $c = ${spc1} ]; do spc="${spc} "; c=`expr $c + 1`; done + # echo -en "${idsCL[LightCyan]}${msg}${spc}: ${idsCL[Default]}" + # + # echo -e "${idsCL[Green]}${VPN_SERVER}${idsCL[Default]}" + # # # echo ${JSON} | jq