nstart/nstart.zsh
2026-09-09 20:31:37 +03:00

150 lines
3.7 KiB
Bash
Executable file

#!/bin/zsh
show_help() {
print -P -- \
"%BUsage:%b nstart
Quickly connect to a NetworkManager profile and start Xray.
nstart is intended for fast interactive use, replacing repetitive commands
such as %F{cyan}nmcli c u wifi%f followed by manually starting Xray.
%BConfiguration:%b
%F{green}XRAY_CONFIG_FILE%f
Path to the Xray configuration file.
%F{green}DEFAULT_CONNECTIONS_NM%f
Preferred NetworkManager connections.
typeset -xT DEFAULT_CONNECTIONS_NM nmcli_def_conns
nmcli_def_conns=( wifi home work )
%BBehavior:%b
nstart first offers connections from DEFAULT_CONNECTIONS_NM.
If none is selected, it lets you choose from all profiles found by
%F{cyan}nmcli connection show%f.
After NetworkManager connects successfully, Xray is started with:
%F{cyan}xray run -c \"\$XRAY_CONFIG_FILE\"%f
%BContributing:%b
Bugs, patches and improvements are welcome:
%F{blue}https://git.vpnpickup.com/artobyte/nstart%f"
}
spinner() {
local pid=$1
local text=$2
local -a frames=( '⠋' '⠙' '⠹' '⠸' '⠼' '⠴' '⠦' '⠧' '⠇' '⠏' )
local i=1
while kill -0 "$pid" 2>/dev/null; do
printf "\r%s %s" "${frames[$i]}" "$text"
(( i = i % $#frames + 1 ))
sleep 0.08
done
}
print_numbered_list() {
local i
local -a items=( "${(@P)1}" )
for (( i=1; i <= ${#items}; i++ )); do
items[$i]="%B[$i]%b$items[$i]"
done
print -P -C 4 "${items[@]}"
}
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
show_help
exit 0
fi
connections=( ${(f)"$(nmcli --terse --fields NAME connection show)"} )
typeset -T DEFAULT_CONNECTIONS_NM default_con
if [[ "$default_con" == "" ]]; then
#if you can't export zsh array to environment, replace this fallback values
default_con=(home wg0 work wifi iPhone)
fi
for (( i=$#default_con; i>=1; i-- )); do
local flag=false
for con in $connections; do
if [[ "$default_con[$i]" == "$con" ]]; then
flag=true
break
fi
done
if [[ "$flag" == "false" ]]; then
default_con[$i]=()
fi
done
print -n "Found"
for (( i=1; i <= $#default_con; i++ )); do
print -nP " %B[$i]%b$default_con[$i]"
done
print " from default connections"
connection_number=0
connection_promt=" Connect to [1]? Otherwise enter connection's number (1-$#default_con)
or 'n' for choosing from all connections [1/n]: "
read -r "connection_number?${connection_promt}"
connection_to_run="None"
if [[ -z "$connection_number" ]]; then
connection_to_run=$default_con[1]
elif (( connection_number )); then
connection_to_run=$default_con[$connection_number]
elif [[ "$connection_number" == "n" ]]; then
print "Select connections from below:"
print_numbered_list connections
read -r "connection_number_?
Enter a number: "
connection_to_run=$connections[$connection_number_]
else
connection_to_run=$connection_number
fi
nmcli c u "$connection_to_run" 1> /dev/null 2>&1 &
nmcli_pid=$!
spinner "$nmcli_pid" "Connecting to '$connection_to_run'..."
if wait "$nmcli_pid"; then
print -P "\r%E%F{green}✔%f Connected to '$connection_to_run'"
else
print -P "\r%F{red}✘%fFailed to connect to '$connection_to_run'"
fi
## Starting xray proxy
#checking if xray running
if pgrep xray 1>/dev/null; then
print "Xray is running already. No need in duplication."
exit 0
fi
#
if [[ -n "$XRAY_CONFIG_FILE" ]]; then
print "\$XRAY_CONFIG_FILE is defined. Using this."
else
print "\$XRAY_CONFIG_FILE is not defined. Write path to xray's config file:"
read -t 60 XRAY_CONFIG_FILE
if [[ ! -n "$XRAY_CONFIG_FILE" ]]; then
print -P "Timeout for promting XRAY_CONFIG_FILE.\nScript exiting."
exit 1
fi
fi
print -n "Starting quietly xray with config '$XRAY_CONFIG_FILE'..."
xray run -c $XRAY_CONFIG_FILE 1>/dev/null 2>/dev/null &
print -P "\r%E%B%F{green}✔%f%b xray has started"
exit 0