#! /bin/bash # This is shadowsocks-manager install script. # Update data: 2018-09-23 # Version: 1.2.4 PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH if [ $(id -u) != "0" ];then echo "[${red}Error!${plain}] This script must be run as root!" exit 1 elif [ `cat /etc/redhat-release |awk -F '.' '{print $1}'|awk '{print $NF}'` -ne 7 ];then echo "[${red}Error!${plain}] You have to run script on CentOS 7" exit 1 fi blank_line(){ cat</dev/null if [ $? -ne 0 ]; then echo echo -e "[${red}Error!${plain}] Please enter a number." continue fi if [[ "$pick" -lt 1 || "$pick" -gt ${#encryptions[@]} ]]; then echo echo -e "[${red}Error!${plain}] Please enter a number between 1 and ${#encryptions[@]}" continue fi ss_libev_encry=${encryptions[$pick-1]} echo echo "encryptions = ${ss_libev_encry}" break done # set admin email if [ "${ss_run}" == "webgui" ];then while :; do echo echo "Please enter email address for admin:" read -p "(For example: 123@123.com):" email_admin if [ -z ${email_admin} ];then echo echo -e "[${red}Error!${plain}] Administrator Email address can not be empty!" continue elif check_email ${email_admin};then echo echo -e "[${red}Error!${plain}] Please enter a correct email address!" else break fi done echo read -p "Please enter your email passwd or authorization code:" email_passwd echo echo "Please enter your Mail Server address:" read -p "(For example: smtp.qq.com or other):" email_smtp fi } print_conf(){ echo echo "+---------------------------------------------------------------+" echo echo -e " Your ss-libev port: ${ss_libev_port}" echo -e " Your ss-mgr port ${ssmgr_port}" echo -e " Your ss-mgr passwd ${ssmgr_passwd}" echo -e " Your user port ranges: ${port_ranges}" echo -e " Your ss-libev-encry: ${ss_libev_encry}" if [ "${ss_run}" == "webgui" ];then echo -e " Your E-amil address: ${email_admin}" echo -e " Your E-amil server: ${email_smtp}" fi echo echo "+---------------------------------------------------------------+" blank_line } create_file_conf(){ # shadowsocks-manager configuration cat > /root/.ssmgr/ss.yml < /root/.ssmgr/webgui.yml< /etc/firewalld/services/ssmgr.xml < ssmgr ssmgr service. EOF fi } check_conf(){ # check configuration information blank_line if [ ! -d /root/.ssmgr ];then mkdir /root/.ssmgr/ read -p "Do you need to configure some parameters here?(y/n)" config if [ "${config}" == "n" ] || [ "${config}" == "N" ];then return fi echo echo "Now start set up some related configuration." while :; do get_conf clear echo echo -e "[${green}Info${plain}] Please verify the configure you have entered." print_conf read -p "Are you sure to use them?(y/n):" verify if [ "${verify}" == "n" ] || [ "${verify}" == "N" ];then continue else create_file_conf break fi done else ss_libev_port=$(grep "add" /root/.ssmgr/ss.yml |awk -F ':' '{print $NF}' |head -n1) ss_libev_encry="aes-256-gcm" fi } download() { local filename=${1} local cur_dir=`pwd` if [ -s ${filename} ]; then echo echo -e "[${green}Info!${plain}] ${filename} found!" else echo echo -e "[${yellow}Warning!${plain}] ${filename} not found, download now..." wget --no-check-certificate -c -O ${1} ${2} if [ $? -eq 0 ]; then echo echo -e "[${green}Info!${plain}] ${filename} download completed..." else echo echo -e "[${red}Error!${plain}] Failed to download ${filename}, please download it to ${cur_dir} directory manually and try again." exit 1 fi fi } download_files(){ cd ${cur_dir} get_ss_version download "${shadowsocks_libev_ver}.tar.gz" "${download_link}" download "${libsodium_file}.tar.gz" "${libsodium_url}" download "${mbedtls_file}-gpl.tgz" "${mbedtls_url}" download "node-v8.11.3-linux-x64.tar.gz" "${nodejs_url}" } set_firewalld(){ local firewall_file=/etc/firewalld/zones/public.xml if systemctl status firewalld |grep -q 'active (running)'; then firewall-cmd --zone=public --add-service=ssmgr --permanent if [ "${ss_run}" == "webgui" ];then if ! grep -q '"http"' ${firewall_file} ;then firewall-cmd --zone=public --add-service=http --permanent fi firewall-cmd --reload fi if ! grep -q '"ssmgr"' ${firewall_file}; then sed -i '/dhcpv6-client/a\ ' ${firewall_file} firewall-cmd --reload fi else echo echo -e "[${yellow}Warning!${plain}] The firewalld not running." fi } install_libsodium(){ echo echo -e "[${green}Info!${plain}] Installing ${libsodium_file}" sleep 3 if [ ! -f /usr/lib/libsodium.a ]; then cd ${cur_dir} tar zxf ${libsodium_file}.tar.gz cd ${libsodium_file} ./configure --prefix=/usr && make && make install if [ $? -ne 0 ]; then echo echo -e "[${red}Error!${plain}] ${libsodium_file} install failed." exit 1 fi else echo echo -e "[${yellow}Warning!${plain}] ${libsodium_file} already installed." fi } install_mbedtls(){ echo echo -e "[${green}Info!${plain}] Installing ${mbedtls_file}" sleep 3 if [ ! -f /usr/lib/libmbedtls.a ]; then cd ${cur_dir} tar zxf ${mbedtls_file}-gpl.tgz cd ${mbedtls_file} make SHARED=1 CFLAGS=-fPIC make DESTDIR=/usr install if [ $? -ne 0 ]; then echo echo -e "[${red}Error!${plain}] The ${mbedtls_file} install failed." exit 1 fi else echo echo -e "[${yellow}Warning!${plain}] ${mbedtls_file} already installed." fi } install_nodejs(){ echo echo -e "[${green}Info!${plain}] Installing ${nodejs_file}" sleep 3 if [ -d /usr/local/node ];then if [ $(node -v |cut -b2) -ne "8" ];then mv /usr/local/node /usr/local/node.bak fi fi cd ${cur_dir} tar zxf ${nodejs_file}.tar.gz mv ${nodejs_file} /usr/local/node [ ! -s /usr/bin/node ] && ln -s /usr/local/node/bin/node /usr/bin/node [ ! -s /usr/bin/npm ] && ln -s /usr/local/node/bin/npm /usr/bin/npm if grep 'export NODE_PATH=/usr/local/node/lib/node_modules' /etc/profile ;then return else echo 'export NODE_PATH=/usr/local/node/lib/node_modules' >>/etc/profile source /etc/profile fi npm i -g npm } install_shadowsocks_libev(){ install_libsodium install_mbedtls echo echo -e "[${green}Info!${plain}] Installing ${shadowsocks_libev_ver}" sleep 3 cd ${cur_dir} tar zxf ${shadowsocks_libev_ver}.tar.gz cd ${shadowsocks_libev_ver} ./configure --disable-documentation if [ $? -ne 0 ];then echo echo -e "[${red}Error!${plain}] ${shadowsocks_libev_ver} install failed." exit 1 else make && make install if [ $? -ne 0 ];then echo echo -e "[${red}Error!${plain}] ${shadowsocks_libev_ver} install failed." exit 1 fi fi echo echo -e "[${green}Info!${plain}] ${shadowsocks_libev_ver} install success." } install_shadowsocks_manager(){ echo echo -e "[${green}Info!${plain}] Installing shadowsocks-manager..." sleep 3 npm i -g shadowsocks-manager --unsafe-perm if [ $? -eq 0 ];then echo echo -e "[${green}Info!${plain}] The shdowsocks-manager install success!" [ ! -s /usr/bin/ssmgr ] && ln -s /usr/local/node/lib/node_modules/shadowsocks-manager/bin/ssmgr /usr/bin/ssmgr else echo echo -e "[${red}Error!${plain}] The shdowsocks-manager install failed!" exit 1 fi } ssmgr_start(){ echo echo -e "[${green}Info!${plain}] Starting ssmgr..." [ ! -s /usr/lib64/libsodium.so.13 ] && ln -s /usr/lib/libsodium.so /usr/lib64/libsodium.so.13 [ ! -s /usr/lib64/libsodium.so.23 ] && ln -s /usr/lib/libsodium.so /usr/lib64/libsodium.so.23 [ ! -s /usr/lib64/libmbedcrypto.so.0 ] && ln -s /usr/lib/libmbedcrypto.so.0 /usr/lib64/libmbedcrypto.so.0 echo echo "Are you run webgui or only ss?" echo "The M server and S nodes are in the webgui option." read -p "(Default:webgui):" ss_run [ -z ${ss_run} ] && ss_run=webgui ss_run=$(echo ${ss_run} |tr [A-Z] [a-z]) check_conf screen -dmS ss-libev ss-manager -m ${ss_libev_encry} --manager-address 127.0.0.1:${ss_libev_port} sleep 1 while :; do if [ "${ss_run}" == "webgui" ] ;then screen -dmS ss ssmgr -c ss.yml sleep 1 screen -dmS webgui ssmgr -c webgui.yml break elif [ "${ss_run}" == "ss" ] ;then screen -dmS ss ssmgr -c ss.yml break else echo echo -e "[${red}Error!${plain}] Please enter webgui or ss!" fi done set_firewalld } stop_ssmgr(){ while : do pid_num=$(screen -ls |egrep "\(*\)" |awk -F '.' '{print $1}' |head -n1) kill $pid_num [ $? -ne 0 ] && break done } install(){ print_info disable_selinux install_deppak download_files install_nodejs install_shadowsocks_libev install_shadowsocks_manager ssmgr_start print_info print_conf echo -e "[${green}Info!${plain}] Thanks for your using this script." echo -e "[${green}Info!${plain}] Please visit ${ipaddr}" sleep 3 } uninstall_shadowsocks_libev(){ rm -f /etc/rc.d/init.d/ss-manager rm -f /run/systemd/generator.late/ss-manager.service rm -f /usr/share/man/man1/ss-nat.1.gz rm -f /usr/share/man/man1/ss-redir.1.gz rm -f /usr/share/man/man1/ss-local.1.gz rm -f /usr/share/man/man1/ss-manager.1.gz rm -f /usr/share/man/man1/ss-tunnel.1.gz rm -f /usr/share/man/man1/ss-server.1.gz rm -f /usr/local/bin/ss-local rm -f /usr/local/bin/ss-tunnel rm -f /usr/local/bin/ss-server rm -f /usr/local/bin/ss-manager rm -f /usr/local/bin/ss-redir rm -f /usr/local/bin/ss-nat rm -rf /usr/local/include/shadowsocks.h rm -f /usr/local/lib/pkgconfig/shadowsocks-libev.pc rm -fr /usr/share/doc/shadowsocks-libev } uninstall_shadowsocks_manager(){ npm uninstall -g shadowsocks-manager blank_line read -p "Do you need to keep the configuration file?(y/n)" keep_path if [ "${keep_path}" == "n" ] || [ "${keep_path}" == "N" ];then rm -rf /usr/lib/node_modules/shadowsocks-manager rm -rf /root/.ssmgr firewall-cmd --zone=public --remove-service=ssmgr --permanent rm -rf /etc/firewalld/services/ssmgr.xml firewall-cmd --reload fi } uninstall(){ print_info blank_line read -p "Are you sure uninstall?(y/n)" answer [ -z ${answer} ] && answer="n" if [ "${answer}" == "y" ] || [ "${answer}" == "Y" ]; then stop_ssmgr uninstall_shadowsocks_libev uninstall_shadowsocks_manager fi print_info echo echo -e "[${green}Info!${plain}] Thanks for using this script." blank_line } action=${1} [ -z ${1} ] && action=install case ${action} in install|uninstall) ${action} ;; *) print_error ;; esac