segfault/sfbin/vpn_wg2status.sh

161 lines
4.0 KiB
Bash
Raw Normal View History

2022-09-28 09:49:46 +00:00
#! /bin/bash
2022-10-08 08:30:59 +00:00
# CONTEXT: VPN context. Called when WG goes UP or DOWN
2022-09-28 09:49:46 +00:00
2022-10-08 08:30:59 +00:00
# PARAMETERS: [output filename] [up/down] [interface]
2022-09-28 09:49:46 +00:00
2022-10-08 08:30:59 +00:00
# NOTE:
# POST_UP has all the set enviornment variables but
# PRE/POST_DOWN is started with all environment variables emptied.
# Is this a WireGuard bug?
# Solution: Save the important variables during POST_UP
if [[ -f /dev/shm/env.txt ]]; then
source /dev/shm/env.txt
else
echo -e "SF_DEBUG=\"${SF_DEBUG}\"\n\
SF_REDIS_AUTH=\"${SF_REDIS_AUTH}\"\n\
2023-04-26 17:57:44 +00:00
IS_REDIRECTS_DNS=\"${IS_REDIRECTS_DNS}\"\n\
2022-10-08 08:30:59 +00:00
PROVIDER=\"${PROVIDER}\"\n" >/dev/shm/env.txt
fi
source /sf/bin/funcs.sh
2022-12-16 23:31:12 +00:00
source /sf/bin/funcs_redis.sh
2022-09-28 09:49:46 +00:00
# From all files update the VPN status file
create_vpn_status()
{
local loc
local exit_ip
2022-10-08 08:30:59 +00:00
local geoip
local provider
2022-09-28 09:49:46 +00:00
for f in "${DSTDIR}"/status-*.log; do
[[ ! -f "${f}" ]] && break
# shellcheck disable=SC1090
source "${f}"
2022-10-08 08:30:59 +00:00
provider+="'${SFVPN_PROVIDER}' "
exit_ip+="'${SFVPN_EXIT_IP}' "
geoip+="'${SFVPN_GEOIP}' "
2022-09-28 09:49:46 +00:00
done
# Delete vpn_status unless there is at least 1 VPN
2022-10-08 08:30:59 +00:00
if [[ -z $geoip ]]; then
2022-09-28 09:49:46 +00:00
rm -f "/config/guest/vpn_status"
return
fi
echo -en "\
IS_VPN_CONNECTED=1\n\
2022-10-08 08:30:59 +00:00
VPN_GEOIP=(${geoip})\n\
VPN_PROVIDER=(${provider})\n\
VPN_EXIT_IP=(${exit_ip})\n" >"/config/guest/vpn_status"
2022-09-28 09:49:46 +00:00
}
2022-10-08 08:30:59 +00:00
down()
2022-09-28 09:49:46 +00:00
{
2022-10-08 08:30:59 +00:00
# NOTE: DEBUGF wont work because stderr is closed during
# WireGuard PRE_DOWN/POST_DOWN
2022-09-28 09:49:46 +00:00
[[ -f "${LOGFNAME}" ]] && rm -f "${LOGFNAME}"
create_vpn_status
2022-10-08 08:30:59 +00:00
ip route del 10.11.0.0/16 via "${SF_ROUTER_IP}" 2>/dev/null
/sf/bin/rportfw.sh fw_delall
2022-12-16 23:31:12 +00:00
red RPUSH portd:cmd "vpndown ${PROVIDER}"
2022-10-08 08:30:59 +00:00
[[ "${PROVIDER,,}" == "cryptostorm" ]] && curl -fsSL --retry 1 --max-time 5 http://10.31.33.7/fwd -ddelallfwd=1
true
2022-09-28 09:49:46 +00:00
}
2022-10-08 08:30:59 +00:00
up()
2022-09-28 09:49:46 +00:00
{
local t
local geo
local exit_ip
2022-09-28 17:16:00 +00:00
local ep_ip
2022-09-28 09:49:46 +00:00
t="$(wg show "${DEV:-wg0}" endpoints)" && {
t="${t##*[[:space:]]}"
2022-09-28 17:16:00 +00:00
ep_ip="${t%:*}"
2022-09-28 09:49:46 +00:00
2022-10-08 08:30:59 +00:00
# First extract Geo Information from wg0.conf file before
# asking the cloud.
str=$(grep '# GEOIP=' "/etc/wireguard/wg0.conf")
geo="${str:8}"
[[ -z $geo ]] && geo=$(curl -fsSL --retry 3 --max-time 15 https://ipinfo.io 2>/dev/null) && {
local city
local geo
2022-09-28 17:16:00 +00:00
t=$(echo "$geo" | jq '.country | select(. != null)')
2022-10-08 08:30:59 +00:00
country="${t//[^[:alnum:].-_ \/]}"
2022-09-28 17:16:00 +00:00
t=$(echo "$geo" | jq '.city | select(. != null)')
2022-10-08 08:30:59 +00:00
city="${t//[^[:alnum:].-_ \/]}"
2022-09-28 17:16:00 +00:00
t=$(echo "$geo" | jq '.ip | select(. != null)')
2023-03-26 05:40:47 +00:00
unset geo
2022-10-08 08:30:59 +00:00
exit_ip="${t//[^0-9.]}"
2023-03-26 05:40:47 +00:00
[[ -n $city || -n $country ]] && geo="${city}/${country}"
2022-09-28 09:49:46 +00:00
}
2022-10-08 08:30:59 +00:00
# [[ -z $geo ]] && {
# Query local DB for info
# }
2022-09-28 17:16:00 +00:00
[[ -z $exit_ip ]] && exit_ip=$(curl -fsSL --max-time 15 ifconfig.me 2>/dev/null)
2022-09-28 09:49:46 +00:00
} # wg show
2022-09-28 17:16:00 +00:00
if [[ -z $ep_ip ]]; then
2022-09-28 09:49:46 +00:00
rm -f "${LOGFNAME}"
else
2022-10-08 08:30:59 +00:00
local myip
myip=$(ip addr show | grep inet | grep -F 172.20.0.)
myip="${myip#*inet }"
myip="${myip%%/*}"
2022-09-28 09:49:46 +00:00
echo -en "\
2023-04-26 17:57:44 +00:00
SFVPN_IS_REDIRECTS_DNS=\"${IS_REDIRECTS_DNS}\"\n\
2022-10-08 08:30:59 +00:00
SFVPN_MY_IP=\"${myip}\"\n\
SFVPN_EXEC_TS=\"$(date -u +%s)\"\n\
2022-09-28 17:16:00 +00:00
SFVPN_ENDPOINT_IP=\"${ep_ip}\"\n\
2022-10-08 08:30:59 +00:00
SFVPN_GEOIP=\"${geo:-Artemis}\"\n\
SFVPN_PROVIDER=\"${PROVIDER}\"
2022-09-28 17:16:00 +00:00
SFVPN_EXIT_IP=\"${exit_ip:-333.1.2.3}\"\n" >"${LOGFNAME}"
2022-09-28 09:49:46 +00:00
fi
create_vpn_status
2022-10-08 08:30:59 +00:00
2023-03-25 20:28:41 +00:00
# Old cryptostorm containers set a network route to default IP.
2023-03-26 05:40:47 +00:00
# Remove; We need to route to SF_ROUTER_IP instead.
2023-03-25 20:28:41 +00:00
ip route del 10.11.0.0/24 2>/dev/null
2022-10-08 08:30:59 +00:00
ip route add 10.11.0.0/16 via "${SF_ROUTER_IP}" 2>/dev/null
# Delete all old port forwards.
[[ "${PROVIDER,,}" == "cryptostorm" ]] && curl -fsSL --retry 3 --max-time 10 http://10.31.33.7/fwd -ddelallfwd=1
2022-12-16 23:31:12 +00:00
red RPUSH portd:cmd "vpnup ${PROVIDER}"
2022-10-08 08:30:59 +00:00
true
2022-09-28 09:49:46 +00:00
}
[[ -z $2 ]] && exit 254
2022-10-08 08:30:59 +00:00
export REDISCLI_AUTH="${SF_REDIS_AUTH}"
SF_ROUTER_IP="172.20.0.2"
2022-09-28 09:49:46 +00:00
LOGFNAME="$1"
OP="$2"
DEV="${3:-wg0}"
DSTDIR="$(dirname "${LOGFNAME}")"
[[ ! -d "${DSTDIR}" ]] && { umask 077; mkdir -p "${DSTDIR}"; }
2022-10-08 08:30:59 +00:00
[[ "$OP" == "down" ]] && { down; exit; }
2022-09-28 09:49:46 +00:00
source /check_vpn.sh
wait_for_handshake "${DEV}" || { echo -e "Handshake did not complete"; exit 255; }
2022-10-11 05:02:08 +00:00
check_vpn "${PROVIDER}" "${DEV}" || { echo -e "VPN Check failed"; exit 255; }
2022-09-28 09:49:46 +00:00
2022-10-08 08:30:59 +00:00
[[ "$OP" == "up" ]] && { up; exit; }
2022-09-28 09:49:46 +00:00
echo >&2 "OP=${OP}"
2022-10-08 08:30:59 +00:00
echo >&2 "Usage: [output filename] [up/pdown] [interface] <mullvad/cryptostorm>"
2022-09-28 09:49:46 +00:00
exit 255