182 lines
4.4 KiB
Nix
182 lines
4.4 KiB
Nix
{lib, makeWrapper, stdenv, writeShellScriptBin, jq, rofi, nordvpn, ...}: let
|
|
nordvpn-rofi = writeShellScriptBin "nordvpn-rofi" ''
|
|
#! /bin/env bash
|
|
|
|
is_vpn_active() {
|
|
nordvpn status | grep -e "Status: Disconnected"
|
|
}
|
|
|
|
prettify() {
|
|
echo "$1"|sed 's/_/ /g'
|
|
}
|
|
|
|
main="$@"
|
|
is_active=$(is_vpn_active)
|
|
|
|
list_countries() {
|
|
nordvpn countries | tr -d '\r,-' | tr -s "[:blank:]" "\n" | sed '/^\s*$/d' | sort
|
|
}
|
|
|
|
list_cities() {
|
|
nordvpn cities "$1" | tr -d '\r,-' | tr -s "[:blank:]" "\n" | sed '/^\s*$/d' | sort
|
|
}
|
|
|
|
list_countries_with_cities() {
|
|
IFS=$'\n'
|
|
local countries
|
|
countries=( $(list_countries) )
|
|
local with_cities
|
|
for c in "''${countries[@]}"
|
|
do
|
|
with_cities+=($c)
|
|
with_cities+=($(list_cities $c))
|
|
done
|
|
|
|
echo $with_cities
|
|
}
|
|
|
|
print_country() {
|
|
echo -en "$(prettify $1)\0info\x1f$1\n"
|
|
}
|
|
|
|
# $1 -> city, $2 -> country
|
|
print_city() {
|
|
echo -en "$(prettify $1)\0meta\x1f$2\x1finfo\x1f$1\n"
|
|
}
|
|
|
|
print_countries() {
|
|
local countries
|
|
IFS=$'\n'
|
|
countries=( $(list_countries) )
|
|
|
|
for cunt in "''${countries[@]}"
|
|
do
|
|
print_country $cunt
|
|
done
|
|
}
|
|
|
|
print_all() {
|
|
local countries
|
|
IFS=$'\n'
|
|
countries=( $(list_countries) )
|
|
|
|
for c in "''${countries[@]}"
|
|
do
|
|
print_country $c
|
|
local cities=( $(list_cities $c) )
|
|
for b in "''${cities[@]}"
|
|
do
|
|
print_city $b $c
|
|
done
|
|
done
|
|
}
|
|
|
|
init() {
|
|
echo -en "\0prompt\x1fNordVPN\n"
|
|
|
|
echo -en "Show Cities\0info\x1fCITIES\n"
|
|
echo -en "Disconnect\0info\x1fDISCONNECT\n"
|
|
|
|
print_countries
|
|
}
|
|
|
|
|
|
selected() {
|
|
local countries
|
|
IFS=$'\n'
|
|
countries=( $(list_countries) )
|
|
|
|
|
|
case $ROFI_INFO in
|
|
"CITIES")
|
|
echo -en "\0prompt\x1fNordVPN\n"
|
|
|
|
print_all
|
|
;;
|
|
"DISCONNECT")
|
|
coproc( nordvpn disconnect > /dev/null 2>&1 || notify-send "NordVPN" "Failed to disconnect vpn." )
|
|
;;
|
|
*)
|
|
echo -en "\0prompt\x1fConnecting to $@..\n"
|
|
|
|
|
|
coproc ( if nordvpn connect $ROFI_INFO > /dev/null 2>&1; then notify-send "NordVPN" "Connected to <b>$ROFI_INFO</b>"; else notify-send "NordVPN" "<span foreground='red'>Failed to connect to <b>$ROFI_INFO</b></span>"; fi; pkill -SIGRTMIN+8 waybar )
|
|
exit 0
|
|
;;
|
|
esac
|
|
}
|
|
|
|
input=$@
|
|
|
|
case $input in
|
|
"--connected")
|
|
$( nordvpn status | sed -rne "s/Status: ([a-z,A-Z]*)/\1/p" | grep -qe "Connected" )
|
|
exit $?
|
|
;;
|
|
"--status")
|
|
if $( nordvpn status | sed -rne "s/Status: ([a-z,A-Z]*)/\1/p" | grep -qe "Connected" );
|
|
then
|
|
echo "$( nordvpn status | sed -nr -e 's/Country: ([a-z,A-Z]*)/\1/p' -e 's/City: ([a-z,A-Z]*)/\1/p' | paste - - -d / )"
|
|
else
|
|
echo "Disconnected"
|
|
fi
|
|
|
|
exit 0
|
|
;;
|
|
"--status-json")
|
|
vpn_status=$(nordvpn status | sed -Ee 's/.*(Status:.*)/\1/' | sed -n '/Status.*/,$p')
|
|
tooltip=$(echo "$vpn_status" | sed -Ee \
|
|
's/Transfer: (.*) received, (.*) sent/Transer: \1 \2 /' | \
|
|
sed -Ee 's/(.*): (.*)/<b>\1:<\/b> \2/')
|
|
|
|
(echo "$vpn_status" | sed -rne "s/Status: ([a-z,A-Z]*)/\1/p" | grep -qe "Connected")
|
|
|
|
if [[ $? == 0 ]];
|
|
then
|
|
text="$( echo "$vpn_status" | \
|
|
sed -nr -e 's/Country: ([a-z,A-Z]*)/\1/p' -e 's/City: ([a-z,A-Z]*)/\1/p' | \
|
|
paste - - -d / )"
|
|
class="connected"
|
|
jq -c -j -n --arg text "$text" --arg tooltip "$tooltip" \
|
|
--arg class "$class" '{$text, $tooltip, $class}'
|
|
else
|
|
text="Disconnected"
|
|
class="disconnected"
|
|
jq -c -j -n --arg text "$text" --arg tooltip "$tooltip" \
|
|
--arg class "$class" '{$text, $tooltip, $class}'
|
|
fi
|
|
|
|
exit 0
|
|
;;
|
|
esac
|
|
|
|
case $ROFI_RETV in
|
|
0) init ;;
|
|
1) selected $input ;;
|
|
esac
|
|
'';
|
|
in stdenv.mkDerivation {
|
|
pname = "nordvpn-rofi";
|
|
version = "1.0";
|
|
buildInputs = [ jq rofi nordvpn ];
|
|
nativeBuildInputs = [ makeWrapper ];
|
|
|
|
src = "${nordvpn-rofi}";
|
|
|
|
installPhase = ''
|
|
mkdir -p $out/bin
|
|
cp ${nordvpn-rofi}/bin/nordvpn-rofi $out/bin/nordvpn-rofi
|
|
chmod +x $out/bin/nordvpn-rofi
|
|
wrapProgram $out/bin/nordvpn-rofi \
|
|
--prefix PATH : ${jq}/bin \
|
|
--prefix PATH : ${rofi}/bin \
|
|
--prefix PATH : ${nordvpn}/bin
|
|
'';
|
|
|
|
meta = {
|
|
description = "A utility script for NordVPN and Rofi";
|
|
license = lib.licenses.mit;
|
|
platforms = lib.platforms.linux;
|
|
};
|
|
}
|