Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Interactive helper script to build a new config file for Shadowsocks
#!/bin/bash
#
# Interactive helper script to build a new config file for Shadowsocks
# Modifiable vars/defaults
SERVER="0.0.0.0"
LOCAL_PORT=1080
TIMEOUT=600
METHOD="chacha20"
PORTMIN=8000
PORTMAX=8388
SS_CONFIG_DIR="/etc/shadowsocks/"
# check if the script is run by root
if [[ $EUID -ne 0 ]]; then
echo "Error: This script must be run as root" 1>&2
exit 1
fi
ANS=''
while [[ "$ANS" != "Y" && "$ANS" != "n" ]] ; do
read -p "This script will generate a new configuration file in $SS_CONFIG_DIR, continue [Y/n]? " ANS
if [ "$ANS" == "n" ] ; then
echo -e "Abort.\n"
exit 1
fi
done
SERVER_PORT=''
# Thanks http://gamon.webfactional.com/regexnumericrangegenerator/
re='^8[0-2][0-9]{2}|83[0-7][0-9]|838[0-8]$'
read -p "Server port [${PORTMIN}-${PORTMAX}]: " SERVER_PORT
while ! [[ "$SERVER_PORT" =~ $re ]]; do
echo "Your number : '$SERVER_PORT'"
echo "Error. Make sure you're specifying a port number within the range."
read -p "Server port [${PORTMIN}-${PORTMAX}]: " SERVER_PORT
done
SS_PASSWORD=''
while [[ "$SS_PASSWORD" == "" ]]; do
read -p "Password: " SS_PASSWORD
done
SS_CONFIG_PATH="$SS_CONFIG_DIR/config_$SERVER_PORT.json"
if [[ -f $SS_CONFIG_PATH ]]; then
ANS=''
while [[ "$ANS" != "Y" && "$ANS" != "n" ]] ; do
read -p "$SS_CONFIG_PATH already exists, overwrite [Y/n]? " ANS
if [ "$ANS" == "n" ] ; then
echo -e "Abort.\n"
exit 1
fi
done
fi
# Generate config file
cat > $SS_CONFIG_PATH <<EOF
{
"server":"$SERVER",
"server_port":$SERVER_PORT,
"local_port":$LOCAL_PORT,
"password":"$SS_PASSWORD",
"timeout":$TIMEOUT,
"method":"$METHOD"
}
EOF
if [[ -f $SS_CONFIG_PATH ]]; then
echo "New configuration file generated : $SS_CONFIG_PATH"
else
echo "Something went wrong..."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment