update nordvpn-rofi script and dotfiles
This commit is contained in:
parent
99b1052342
commit
a6fa6bc476
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
src = builtins.fetchGit {
|
src = builtins.fetchGit {
|
||||||
url = "git@git.nirgendwo.xyz:janis/dotfiles.git";
|
url = "git@git.nirgendwo.xyz:janis/dotfiles.git";
|
||||||
rev = "1af2254ee4d278305d9a8b9d6eb28a4bfc79162c";
|
rev = "56cf6dd4efac92527f465a864b2b155833b364ee";
|
||||||
ref = "main";
|
ref = "main";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,181 +1,20 @@
|
||||||
{lib, makeWrapper, stdenv, writeShellScriptBin, jq, rofi, nordvpn, ...}: let
|
{writeShellApplication, dotfiles, bash, jq, rofi, nordvpn, coreutils, ...}:
|
||||||
nordvpn-rofi = writeShellScriptBin "nordvpn-rofi" ''
|
writeShellApplication {
|
||||||
#! /bin/env bash
|
name = "nordvpn-rofi";
|
||||||
|
|
||||||
is_vpn_active() {
|
runtimeInputs = [ jq rofi nordvpn coreutils bash ];
|
||||||
nordvpn status | grep -e "Status: Disconnected"
|
|
||||||
}
|
# shellcheck errors or warnings that should be ignored:
|
||||||
|
excludeShellChecks = [
|
||||||
prettify() {
|
"SC2015" # we use && true || false superfluously, but correctly.
|
||||||
echo "$1"|sed 's/_/ /g'
|
"SC2016" # we use single quotes specifically to avoid variable expansion.
|
||||||
}
|
];
|
||||||
|
|
||||||
main="$@"
|
# don't set errexit, as this script needs functions to be able to error out
|
||||||
is_active=$(is_vpn_active)
|
bashOptions = [
|
||||||
|
"pipefail"
|
||||||
list_countries() {
|
"nounset"
|
||||||
nordvpn countries | tr -d '\r,-' | tr -s "[:blank:]" "\n" | sed '/^\s*$/d' | sort
|
];
|
||||||
}
|
|
||||||
|
text = builtins.readFile "${dotfiles}/.local/bin/nordvpn-rofi.sh";
|
||||||
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;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue