Skip to content

Instantly share code, notes, and snippets.

@cyanife
Last active April 7, 2016 06:52
Show Gist options
  • Save cyanife/60ad336969abe5eb0a1ac085f2d50fd7 to your computer and use it in GitHub Desktop.
Save cyanife/60ad336969abe5eb0a1ac085f2d50fd7 to your computer and use it in GitHub Desktop.
[SHELL]CentOS_SS-libev_install_script
#!/bin/bash
#===============================================================================================
# System Required: CentOS6.x (32bit/64bit)
# Description: Install Shadowsocks-libev server for CentOS 6.X
# Author: Cyanife <cyanife@gmail.com>
# Note: Modified from tennfy<admin@tennfy.com>'s debian script, thanks!
#===============================================================================================
clear
echo "###################################################################"
echo "# Install Shadowsocks-libev server for CentOS 6.x #"
echo "#"
echo "# Author: Cyanife <cyanife@gmail.com> #"
echo "#"
echo "# Modified from tennfy<admin@tennfy.com>'s debian script, thanks! #"
echo "###################################################################"
echo ""
################################## helper functions ##################################
function die() {
echo "ERROR: $1" 1>&2
exit 1
}
# Get CentOS version
function getversion() {
if [[ -s /etc/redhat-release ]]
then
grep -oP "\d(?=\.\d)" /etc/redhat-release
else
grep -oP "\d(?=\.\d)" /etc/issue
fi
}
# Do some sanity checking.
function check_sanity() {
if [ $(/usr/bin/id -u) != "0" ]
then
die "Must be run by root user"
fi
if [[ `getversion` -ne 6 ]]
then
die "Distribution is not supported"
fi
}
# Disable selinux
function disable_selinux() {
if [ -s /etc/selinux/config ] && grep 'SELINUX=enforcing' /etc/selinux/config
then
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
fi
}
# Confirm function
function confirm() {
read -p "$(eval echo $1) [y/n] " -n 1 -r REPLY
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
return 1
else
return 0
fi
}
# Confirm action function
function confirm_action() {
while
eval "$1"
confirm "$2"
do
continue
done
}
############################### install function ##################################
function install_shadowsocks_cyanife() {
# Check install status
if [ -s /usr/local/bin/ss-server ]
then
die "shadowsocks-libev has been installed, try update."
fi
cd $HOME
disable_selinux
# Install dependencies
yum update
yum install -y git gcc automake make autoconf expat-devel gettext-devel
yum install -y libtool libevent openssl-devel zlib-devel
# Download source code
git clone https://github.com/madeye/shadowsocks-libev.git
# Compile install
cd shadowsocks-libev
./autogen.sh
./configure --prefix=/usr
make && make install
mkdir -p /etc/shadowsocks-libev
cp ./rpm/SOURCES/etc/init.d/shadowsocks-libev /etc/init.d/shadowsocks-libev
chmod +x /etc/init.d/shadowsocks-libev
# Get IP address
IP=`curl -s checkip.dyndns.com | cut -d' ' -f 6 | cut -d'<' -f 1`
if [ -z $IP ]; then
IP=`curl -s ifconfig.me/ip`
fi
# Config setting
echo "#############################################################"
echo "#"
echo "# Please input your shadowsocks server_port and password"
echo "#"
echo "#############################################################"
echo ""
confirm_action 'read -p "Input server_port(443 is suggested): " serverport' \
'The port is $serverport'
confirm_action 'read -p "Input password: " shadowsockspwd' \
'The password is $shadowsockspwd'
# Config shadowsocks
cat > /etc/shadowsocks-libev/config.json<<-EOF
{
"server":"${IP}",
"server_port":${serverport},
"local_port":1080,
"password":"${shadowsockspwd}",
"timeout":600,
"method":"rc4-md5"
}
EOF
# Add startup
chkconfig --add shadowsocks-libev
chkconfig shadowsocks-libev on
# Change iptables
iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport ${serverport} -j ACCEPT
iptables -I INPUT -m state --state NEW -m udp -p udp --dport ${serverport} -j ACCEPT
iptables -A INPUT -p tcp --syn --dport ${serverport} -m connlimit --connlimit-above 32 -j REJECT --reject-with tcp-reset
/etc/init.d/iptables save
/etc/init.d/iptables restart
# Start shadowsocks
/etc/init.d/shadowsocks-libev start
#install successfully
echo ""
echo "Congratulations, shadowsocks-libev install completed!"
echo -e "Your Server IP: \033[32m${IP}\033[0m"
echo -e "Your Server Port: \033[32m${serverport}\033[0m"
echo -e "Your Password: \033[32m${shadowsockspwd}\033[0m"
echo -e "Your Local Port: 1080"
echo -e "Your Encryption Method: rc4-md5"
}
############################### uninstall function ##################################
function uninstall_shadowsocks_cyanife(){
# Confirmation
if confirm "Are you sure uninstall shadowsocks-libev?"
then
die "Cancelled."
fi
# Change pwd to shadowsocks-libev
cd $HOME
cd shadowsocks-libev
# Stop shadowsocks-libev
/etc/init.d/shadowsocks-libev stop
# Clean iptables
serverport=$(grep -Po '"server_port":.*?[^\\],' /etc/Shadowsocks-libev/config.json | awk 'BEGIN{FS=":"}{print $2}' | sed 's/"\(.*\)",/\1/g')
iptables-save | sed "/^.*state NEW.*port $serverport.*$/d" | sed "/^.*port $serverport.*connlimit.*$/d" | iptables-restore
/etc/init.d/iptables save
/etc/init.d/iptables restart
# Remove startup
chkconfig --del shadowsocks-libev
# Uninstall shadowsocks-libev
make uninstall
make clean
cd ..
rm -rf shadowsocks-libev
# Delete files
rm -rf /etc/shadowsocks-libev
rm -rf /etc/init.d/shadowsocks-libev
echo "Shadowsocks-libev has been uninstalled successfully!"
}
############################### Update function ##################################
function update_shadowsocks_cyanife(){
uninstall_shadowsocks_cyanife
install_shadowsocks_cyanife
echo "update Shadowsocks-libev successfully!"
}
############################### Initialization ##################################
check_sanity
action=$1
[ -z $1 ] && action=install
case "$action" in
install)
install_shadowsocks_cyanife
;;
uninstall)
uninstall_shadowsocks_cyanife
;;
update)
update_shadowsocks_cyanife
;;
*)
echo "Arguments error! [${action} ]"
echo "Usage: `basename $0` {install|uninstall|update}"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment