Files
bananarchy/overlay/rootfs/usr/bin/gs-warp-simple.sh
T

52 lines
1.6 KiB
Bash
Executable File

#!/bin/sh
DEFAULT_CFG="client.ovpn"
if [ ! -f "$DEFAULT_CFG" ]; then
if [ -f "${HOME:-/home/user}/.config/gs-warp/client.ovpn" ]; then
DEFAULT_CFG="${HOME:-/home/user}/.config/gs-warp/client.ovpn"
elif [ -f "/etc/gs-warp/client.ovpn" ]; then
DEFAULT_CFG="/etc/gs-warp/client.ovpn"
fi
fi
OVPNCFG="${1:-$DEFAULT_CFG}"
if [ ! -f "$OVPNCFG" ]; then
echo "Error: Configuration file '$OVPNCFG' not found."
exit 1
fi
if [ -z "${GS_SECRET:-}" ] && [ -f "${HOME:-/home/user}/.config/bananarchy/privacy.conf" ]; then
GS_SECRET="$(awk -F= '/^GSWARP_SECRET=/{print substr($0,15)}' "${HOME:-/home/user}/.config/bananarchy/privacy.conf" | tail -n 1)"
fi
if [ -z "${GS_SECRET:-}" ]; then
GS_SECRET="$(md5sum "$OVPNCFG" | cut -d ' ' -f 1)"
fi
if [ "$(id -u)" -eq 0 ]; then
SUDO=""
elif command -v doas >/dev/null 2>&1; then
SUDO="doas"
else
SUDO="sudo"
fi
if [ ! -f /tmp/gswarp-gs.pid ]; then
printf "Creating GS tunnel... "
gs-netcat -D -p 1194 -s "$GS_SECRET" 2>/dev/null && \
pgrep -f "gs-netcat -D -p 1194" | head -n 1 > /tmp/gswarp-gs.pid
echo "OK (PID $(cat /tmp/gswarp-gs.pid))"
printf "Connecting to OpenVPN... "
$SUDO openvpn --daemon gs-warp --config "$OVPNCFG" --writepid /tmp/gswarp-ov.pid
sleep 0.2
echo "OK (PID $(cat /tmp/gswarp-ov.pid 2>/dev/null))"
else
printf "Disconnecting OpenVPN... "
$SUDO kill "$(cat /tmp/gswarp-ov.pid 2>/dev/null)" 2>/dev/null || true
$SUDO rm -f /tmp/gswarp-ov.pid
echo "OK"
printf "Destroying GS tunnel... "
$SUDO kill "$(cat /tmp/gswarp-gs.pid 2>/dev/null)" 2>/dev/null || true
$SUDO rm -f /tmp/gswarp-gs.pid
echo "OK"
fi