Skip to content

Commit

Permalink
fix: invalid logging functions, DEBUG=1 now enables debug logs for …
Browse files Browse the repository at this point in the history
…protonvpn-cli and out container logs
  • Loading branch information
tprasadtp committed Jun 26, 2021
1 parent 585b277 commit fe04e5e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 65 deletions.
4 changes: 2 additions & 2 deletions makefiles/help.mk
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ endif


# Validate Auto Populated variables are not empty
# GIT_BRANCH is an exception as on CI systems it can be empty
# Due to shalow cloning and detached head
# GIT_BRANCH is an exception as on CI systems it can be empty,
# due to shalow cloning and detached head.
# -------------------------------------
$(call check_defined, \
BUILD_NUMBER \
Expand Down
95 changes: 61 additions & 34 deletions root/etc/cont-init.d/70-vpn-setup
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ if [[ -f /usr/local/lib/loggers/logger.sh ]]; then
# shellcheck disable=SC1091
source /usr/local/lib/loggers/logger.sh
else
echo "[ERROR ] Failed to loag logging library at/usr/local/lib/loggers/logger.sh"
echo "[ERROR ] Failed to load logging library expected at /usr/local/lib/loggers/logger.sh"
exit 1
fi

if [[ $DEBUG == "1" ]]; then
LOG_LVL="0"
fi

if [[ -z "$PROTONVPN_USERNAME" ]]; then
log_error "No Username specified!"
exit 2
Expand All @@ -31,10 +35,10 @@ if [[ -z ${PROTONVPN_CHECK_URL} ]]; then
fi

if [[ ${PROTONVPN_CHECK_URL} =~ ^https://* ]]; then
log_success "OK! Check URL is secure"
log_success "OK! Check URL is secure"
else
log_error "PROTONVPN_CHECK_URL must be secure(https)"
exit 3
log_error "PROTONVPN_CHECK_URL must be secure(https)"
exit 3
fi
# Validate healthcheck endpoint returns 200
if [[ $(curl -s -o /dev/null -w "%{http_code}" "${PROTONVPN_CHECK_URL}") == "200" ]]; then
Expand All @@ -54,22 +58,45 @@ else
fi

case ${PROTONVPN_TIER} in
0 | free | Free | FREE) log_notice "Free Plan";CFG_TIER=0;;
1 | basic | Basic | BASIC) log_notice "Basic Plan";CFG_TIER=1;;
2 | plus | Plus | PLUS) log_notice "Plus Plan";CFG_TIER=2;;
3 | visionary | Visionary | VISIONARY) log_notice "Visionary Plan";CFG_TIER=3;;
*) log_error "Invalid ProtonVPN Tier, Can only accept 0-3";exit 3;;
0 | free | Free | FREE)
log_notice "Free Plan"
CFG_TIER=0
;;
1 | basic | Basic | BASIC)
log_notice "Basic Plan"
CFG_TIER=1
;;
2 | plus | Plus | PLUS)
log_notice "Plus Plan"
CFG_TIER=2
;;
3 | visionary | Visionary | VISIONARY)
log_notice "Visionary Plan"
CFG_TIER=3
;;
*)
log_error "Invalid ProtonVPN Tier, Can only accept 0-3"
exit 3
;;
esac

case ${PROTONVPN_PROTOCOL} in
tcp | TCP) log_notice "Protocol: TCP";CFG_PROTO="tcp";;
udp | UDP) log_notice "Protocol: UDP";CFG_PROTO="udp";;
*) log_warning "Protocol: Invalid protocol specified - ${PROTONVPN_PROTOCOL}";
log_warning "Falling back to UDP";
CFG_PROTO="udp";;
tcp | TCP)
log_notice "Protocol: TCP"
CFG_PROTO="tcp"
;;
udp | UDP)
log_notice "Protocol: UDP"
CFG_PROTO="udp"
;;
*)
log_warning "Protocol: Invalid protocol specified - ${PROTONVPN_PROTOCOL}"
log_warning "Setting protocol to UDP"
CFG_PROTO="udp"
;;
esac

log_variable "PROTONVPN_SERVER"
log_debug "Connecting to" "$PROTONVPN_SERVER"

if [[ -z ${PROTONVPN_SERVER} ]]; then
log_error "Specify PROTONVPN_SERVER(${PROTONVPN_SERVER})"
Expand Down Expand Up @@ -115,7 +142,7 @@ else
log_debug "Split tunnel is disabled!(${cidrs_from_env[*]})"
fi

if [[ ${#INVALID_CIDRS[@]} -gt 0 ]];then
if [[ ${#INVALID_CIDRS[@]} -gt 0 ]]; then
log_error "Following CIDRs were invalid: ${INVALID_CIDRS[*]}"
exit 5
fi
Expand All @@ -130,7 +157,7 @@ else
fi

# Handle Split Tunnels
if [[ ${#SPLIT_TUNNEL_CIDRS[@]} -gt 0 ]];then
if [[ ${#SPLIT_TUNNEL_CIDRS[@]} -gt 0 ]]; then
log_notice "Following CIDRs will be excluded from VPN ${SPLIT_TUNNEL_CIDRS[*]}"
ENABLE_SPLIT_TUNNEL=1
else
Expand All @@ -149,23 +176,23 @@ chown root:root /root/.pvpn-cli/
# Pull Server Info
log_info "Prefetch server list"
curl --fail \
--location \
--silent \
--show-error \
--max-time 20 \
--silent \
--location \
--user-agent 'protonvpn-cli-docker' \
--header 'x-pm-appversion: LinuxVPN_2.2.6' \
--header 'x-pm-apiversion: 3' \
--header 'Accept: application/vnd.protonmail.v1+json' \
--output /root/.pvpn-cli/serverinfo.json \
https://api.protonvpn.ch/vpn/logicals
--location \
--silent \
--show-error \
--max-time 20 \
--silent \
--location \
--user-agent 'protonvpn-cli-docker' \
--header 'x-pm-appversion: LinuxVPN_2.2.6' \
--header 'x-pm-apiversion: 3' \
--header 'Accept: application/vnd.protonmail.v1+json' \
--output /root/.pvpn-cli/serverinfo.json \
https://api.protonvpn.ch/vpn/logicals

__METADATA_PULL_DATE="$(date +%s)"

log_info "Generating config file"
cat > /root/.pvpn-cli/pvpn-cli.cfg <<EOF
cat >/root/.pvpn-cli/pvpn-cli.cfg <<EOF
[USER]
# API endpoint
api_domain = https://api.protonvpn.ch
Expand All @@ -183,17 +210,17 @@ split_tunnel = ${ENABLE_SPLIT_TUNNEL}

[metadata]
last_api_pull = ${__METADATA_PULL_DATE}
last_update_check = 0
last_update_check = ${__METADATA_PULL_DATE}
EOF

if [[ ${ENABLE_SPLIT_TUNNEL} -eq 1 ]];then
if [[ ${ENABLE_SPLIT_TUNNEL} -eq 1 ]]; then
log_info "Generate split-tunnel config file"
printf "%s\n" "${SPLIT_TUNNEL_CIDRS[@]}" > /root/.pvpn-cli/split_tunnel.txt
printf "%s\n" "${SPLIT_TUNNEL_CIDRS[@]}" >/root/.pvpn-cli/split_tunnel.txt
fi

log_info "Writing credentials file"
# https://github.com/ProtonVPN/linux-cli/blob/089af8d309cf774adc31c158a62a27733ae39ee0/protonvpn_cli/utils.py#L530
cat > /root/.pvpn-cli/pvpnpass <<EOF
cat >/root/.pvpn-cli/pvpnpass <<EOF
${PROTONVPN_USERNAME}+plc
${PROTONVPN_PASSWORD}
EOF
Expand Down
43 changes: 18 additions & 25 deletions root/etc/services.d/protonvpn/run
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ if [[ $DEBUG == "1" ]]; then
export LOG_LVL=0
fi

if [[ -f /usr/local/lib/loggers/logger.bash ]]; then
if [[ -f /usr/local/lib/loggers/logger.sh ]]; then
# shellcheck disable=SC1091
source /usr/local/lib/loggers/logger.bash
source /usr/local/lib/loggers/logger.sh
else
echo "[ERROR ] Failed to loag logging library at/usr/local/lib/loggers/logger.bash"
echo "[ERROR ] Failed to load logging library expected at /usr/local/lib/loggers/logger.sh"
exit 1
fi

if [[ $DEBUG == "1" ]]; then
LOG_LVL="0"
fi

# Reset disconnect mode flag
PROTONVPN_SVC_DISCONNECTING=0
Expand All @@ -24,16 +27,15 @@ _term() {
PROTONVPN_SVC_DISCONNECTING=1
log_info "Disconnecting!"
if PVPN_DEBUG="${DEBUG:-0}" protonvpn disconnect; then
log_success "Diconnected"
log_success "Disconnected"
else
log_error "Failed to disconnect!"
fi
}

trap _term SIGTERM SIGINT

function connect_vpn()
{
function connect_vpn() {

if [[ -z ${PROTONVPN_SERVER} ]]; then
log_error "Specify PROTONVPN_SERVER(${PROTONVPN_SERVER})"
Expand Down Expand Up @@ -63,9 +65,7 @@ function connect_vpn()
fi
}


function reconnect_vpn()
{
function reconnect_vpn() {
log_warning "Reconnecting..."
if PVPN_DEBUG="${DEBUG:-0}" protonvpn r; then
log_success "Reconnection successful"
Expand All @@ -76,9 +76,7 @@ function reconnect_vpn()
fi
}


function api_check()
{
function api_check() {
COUNTRY="$(curl \
--max-time 20 \
--silent \
Expand All @@ -88,20 +86,19 @@ function api_check()
PROTONVPN_COUNTRY="$(python3 /usr/local/bin/proton-srv-country)"

if [[ $COUNTRY == "${PROTONVPN_COUNTRY}" ]]; then
log_success "Healthchecks passed"
log_success "VPN is running and healthy"
thrshold_f=0
else
log_error "Healthcheck #$(( ++thrshold_f )) Failed!"
log_error "Healthcheck #$((++thrshold_f)) Failed!"
log_error "Connected to #${COUNTRY} instead of #${PROTONVPN_COUNTRY}"
if [[ $thrshold_f -gt "${PROTONVPN_FAIL_THRESHOLD}" ]];then
if [[ $thrshold_f -gt "${PROTONVPN_FAIL_THRESHOLD}" ]]; then
log_error "Reconnecting! (${PROTONVPN_FAIL_THRESHOLD})"
reconnect_vpn
fi
fi
}

function check_vpn()
{
function check_vpn() {
# Check if openvpnv process is running.
if [[ ${PROTONVPN_SVC_DISCONNECTING} -ne 1 ]]; then
if [[ -z "$(pgrep -x openvpn)" ]]; then
Expand All @@ -115,19 +112,16 @@ function check_vpn()
fi
}


function main()
{

function main() {
re='^[0-9]+$'
if ! [[ $PROTONVPN_CHECK_INTERVAL =~ $re ]] ; then
if ! [[ $PROTONVPN_CHECK_INTERVAL =~ $re ]]; then
log_warning "PROTONVPN_CHECK_INTERVAL can only be an integer!, will default to 90 seconds"
PROTONVPN_CHECK_INTERVAL=90
else
log_info "PROTONVPN_CHECK_INTERVAL is set to #$PROTONVPN_CHECK_INTERVAL"
fi

if ! [[ $PROTONVPN_FAIL_THRESHOLD =~ $re ]] ; then
if ! [[ $PROTONVPN_FAIL_THRESHOLD =~ $re ]]; then
log_warning "PROTONVPN_FAIL_THRESHOLD can only be an integer!, will default to 3"
PROTONVPN_FAIL_THRESHOLD=3
else
Expand All @@ -147,8 +141,7 @@ function main()
fi

# Checks
while :
do
while :; do
log_debug "Is service disconnecting: ${PROTONVPN_SVC_DISCONNECTING}"
check_vpn
sleep "${PROTONVPN_CHECK_INTERVAL}" &
Expand Down
8 changes: 4 additions & 4 deletions root/usr/local/bin/healthcheck
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ fi
PROTONVPN_COUNTRY="$(python3 /usr/local/bin/proton-srv-country)"

CONNECTED_COUNTRY="$(curl \
--max-time 20 \
--silent \
--location \
"${PROTONVPN_CHECK_URL}" | jq -r "${PROTONVPN_CHECK_QUERY}")"
--max-time 20 \
--silent \
--location \
"${PROTONVPN_CHECK_URL}" | jq -r "${PROTONVPN_CHECK_QUERY}")"

if [[ ${CONNECTED_COUNTRY} == "${PROTONVPN_COUNTRY}" ]]; then
if [[ ${PROTONVPN_CHECK_SILENT} -ne 1 ]]; then
Expand Down

0 comments on commit fe04e5e

Please sign in to comment.