mirror of
https://github.com/hackerschoice/segfault.git
synced 2024-06-30 18:51:22 +00:00
79 lines
2.4 KiB
Plaintext
Executable File
79 lines
2.4 KiB
Plaintext
Executable File
#!/usr/bin/with-contenv bash
|
|
|
|
if [[ -z ${PUBLIC_KEY} || -z ${END_POINT} ]]; then
|
|
echo "[$(date -Iseconds)] Finding the best server..."
|
|
recommendations=$(curl --retry 3 -LsS "https://api.nordvpn.com/v1/servers/recommendations?&filters\[servers_technologies\]\[identifier\]=wireguard_udp&limit=1&${QUERY}")
|
|
server=$(jq -r '.[0] | del(.services, .technologies)' <<< "${recommendations}")
|
|
if [[ -z ${server} ]]; then
|
|
echo "[$(date -Iseconds)] Unable to select a server ¯\_(⊙︿⊙)_/¯"
|
|
sleep 1
|
|
exit 250
|
|
fi
|
|
#echo "[$(date -Iseconds)] Using server: ${server}"
|
|
if [[ -z ${PUBLIC_KEY} ]]; then
|
|
PUBLIC_KEY=$(jq -r '.[0].technologies[] | select( .identifier == "wireguard_udp" ) | .metadata[] | select( .name == "public_key" ) | .value' <<< "${recommendations}")
|
|
fi
|
|
if [[ -z ${END_POINT} ]]; then
|
|
END_POINT=$(jq -r '.[0].hostname' <<< "${recommendations}"):51820
|
|
fi
|
|
fi
|
|
|
|
[[ -z "${PRIVATE_KEY}" ]] && [[ -f "${PRIVATE_KEY_FILE}" ]] && PRIVATE_KEY="$(head -n 1 "${PRIVATE_KEY_FILE}")"
|
|
|
|
( umask 077 && { cat >/etc/wireguard/wg0.conf <<-EOF
|
|
[Interface]
|
|
PrivateKey = ${PRIVATE_KEY}
|
|
ListenPort = ${LISTEN_PORT:-51820}
|
|
Address = ${ADDRESS:-10.5.0.2/32}
|
|
DNS = ${DNS}
|
|
Table = ${TABLE}
|
|
PreUp = ${PRE_UP}
|
|
PostUp = ${POST_UP}
|
|
PreDown = ${PRE_DOWN}
|
|
PostDown = ${POST_DOWN}
|
|
|
|
[Peer]
|
|
Endpoint = ${END_POINT}
|
|
PublicKey = ${PUBLIC_KEY}
|
|
AllowedIPs = ${ALLOWED_IPS:-0.0.0.0/0}
|
|
PersistentKeepalive = ${PERSISTENT_KEEP_ALIVE:-25}
|
|
EOF
|
|
} && sync )
|
|
|
|
echo "[$(date -Iseconds)] Connecting..."
|
|
wg-quick up wg0
|
|
echo "[$(date -Iseconds)] Connected! \(ᵔᵕᵔ)/"
|
|
|
|
if [[ -n ${RECONNECT} ]]; then
|
|
echo "[$(date -Iseconds)] Reconnecting in ${RECONNECT} seconds"
|
|
re=$RECONNECT
|
|
fi
|
|
n=0
|
|
while :; do
|
|
last=$(wg show wg0 latest-handshakes 2>/dev/null)
|
|
[ -z "$last" ] && break
|
|
|
|
hs_age=$(( $(date -u +%s) - $(echo "$last" | awk '{print $2}') ))
|
|
if [ $hs_age -le 180 ]; then
|
|
[ $n -gt 0 ] && echo "[$(date -Iseconds)] Re-Handshake complete!"
|
|
n=0
|
|
else
|
|
n=$((n+1))
|
|
echo -e "[$(date -Iseconds)] Strike #${n}: No handshake for $hs_age sec..."
|
|
[ $n -gt 3 ] && break
|
|
fi
|
|
sleep 10
|
|
[[ -n $RECONNECT ]] && {
|
|
re=$((re - 10))
|
|
[ $re -le 0 ] && { echo "RECONNECTING..."; sleep 5; exit; }
|
|
}
|
|
done
|
|
|
|
echo "[$(date -Iseconds)] RESTARTING..."
|
|
sleep 5
|
|
exit
|
|
if [[ -n ${RECONNECT} ]]; then
|
|
echo "[$(date -Iseconds)] Reconnecting in ${RECONNECT} seconds"
|
|
fi
|
|
sleep "${RECONNECT:-infinity}"
|