ss.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.cn
  4. #
  5. # Notes: OneinStack for CentOS/RedHat 6+ Debian 7+ and Ubuntu 12+
  6. # Install SS Server
  7. #
  8. # Project home page:
  9. # https://oneinstack.com
  10. # https://github.com/oneinstack/oneinstack
  11. export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
  12. clear
  13. printf "
  14. #######################################################################
  15. # OneinStack for CentOS/RedHat 6+ Debian 7+ and Ubuntu 12+ #
  16. # Install SS Server #
  17. # For more information please visit https://oneinstack.com #
  18. #######################################################################
  19. "
  20. # Check if user is root
  21. [ $(id -u) != '0' ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }
  22. oneinstack_dir=$(dirname "`readlink -f $0`")
  23. pushd ${oneinstack_dir}/src > /dev/null
  24. . ../options.conf
  25. . ../versions.txt
  26. . ../include/color.sh
  27. . ../include/check_os.sh
  28. . ../include/download.sh
  29. . ../include/python.sh
  30. PUBLIC_IPADDR=$(../include/get_public_ipaddr.py)
  31. [ "${CentOS_ver}" == '5' ] && { echo "${CWARNING}SS only support CentOS6,7 or Debian or Ubuntu! ${CEND}"; exit 1; }
  32. Check_SS() {
  33. [ -f /usr/local/bin/ss-server ] && ss_option=1
  34. [ -f ${python_install_dir}/bin/ssserver ] && ss_option=2
  35. }
  36. AddUser_SS() {
  37. while :; do echo
  38. read -e -p "Please input password for SS: " SS_password
  39. [ -n "$(echo ${SS_password} | grep '[+|&]')" ] && { echo "${CWARNING}input error,not contain a plus sign (+) and & ${CEND}"; continue; }
  40. (( ${#SS_password} >= 5 )) && break || echo "${CWARNING}SS password least 5 characters! ${CEND}"
  41. done
  42. }
  43. Iptables_set() {
  44. if [ -e '/etc/sysconfig/iptables' ]; then
  45. SS_Already_port=$(grep -oE '9[0-9][0-9][0-9]' /etc/sysconfig/iptables | head -n 1)
  46. elif [ -e '/etc/iptables/rules.v4' ]; then
  47. SS_Already_port=$(grep -oE '9[0-9][0-9][0-9]' /etc/iptables/rules.v4 | head -n 1)
  48. elif [ -e '/etc/iptables.up.rules' ]; then
  49. SS_Already_port=$(grep -oE '9[0-9][0-9][0-9]' /etc/iptables.up.rules | head -n 1)
  50. fi
  51. if [ -n "${SS_Already_port}" ]; then
  52. let SS_Default_port="${SS_Already_port}+1"
  53. else
  54. SS_Default_port=9001
  55. fi
  56. while :; do echo
  57. read -e -p "Please input SS port(Default: ${SS_Default_port}): " SS_port
  58. SS_port=${SS_port:-${SS_Default_port}}
  59. if [ ${SS_port} -ge 1 >/dev/null 2>&1 -a ${SS_port} -le 65535 >/dev/null 2>&1 ]; then
  60. [ -z "$(netstat -tpln | grep :${SS_port}$)" ] && break || echo "${CWARNING}This port is already used! ${CEND}"
  61. else
  62. echo "${CWARNING}input error! Input range: 1~65535${CEND}"
  63. fi
  64. done
  65. if [ "${PM}" == 'yum' ]; then
  66. if [ -n "`grep 'dport 80 ' /etc/sysconfig/iptables`" ] && [ -z "$(grep -E ${SS_port} /etc/sysconfig/iptables)" ]; then
  67. iptables -I INPUT 4 -p udp -m state --state NEW -m udp --dport ${SS_port} -j ACCEPT
  68. iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport ${SS_port} -j ACCEPT
  69. service iptables save
  70. /bin/cp /etc/sysconfig/{iptables,ip6tables}
  71. sed -i 's@icmp@icmpv6@g' /etc/sysconfig/ip6tables
  72. ip6tables-restore < /etc/sysconfig/ip6tables
  73. service ip6tables save
  74. fi
  75. elif [ "${PM}" == 'apt-get' ]; then
  76. if [ -e '/etc/iptables/rules.v4' ]; then
  77. if [ -n "`grep 'dport 80 ' /etc/iptables/rules.v4`" ] && [ -z "$(grep -E ${SS_port} /etc/iptables/rules.v4)" ]; then
  78. iptables -I INPUT 4 -p udp -m state --state NEW -m udp --dport ${SS_port} -j ACCEPT
  79. iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport ${SS_port} -j ACCEPT
  80. iptables-save > /etc/iptables/rules.v4
  81. /bin/cp /etc/iptables/rules.v{4,6}
  82. sed -i 's@icmp@icmpv6@g' /etc/iptables/rules.v6
  83. ip6tables-restore < /etc/iptables/rules.v6
  84. ip6tables-save > /etc/iptables/rules.v6
  85. fi
  86. elif [ -e '/etc/iptables.up.rules' ]; then
  87. if [ -n "`grep 'dport 80 ' /etc/iptables.up.rules`" ] && [ -z "$(grep -E ${SS_port} /etc/iptables.up.rules)" ]; then
  88. iptables -I INPUT 4 -p udp -m state --state NEW -m udp --dport ${SS_port} -j ACCEPT
  89. iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport ${SS_port} -j ACCEPT
  90. iptables-save > /etc/iptables.up.rules
  91. fi
  92. fi
  93. fi
  94. }
  95. Def_parameter() {
  96. while :; do echo
  97. echo "Please select SS server version:"
  98. echo -e "\t${CMSG}1${CEND}. Install SS-libev"
  99. echo -e "\t${CMSG}2${CEND}. Install SS-python"
  100. read -e -p "Please input a number:(Default 1 press Enter) " ss_option
  101. ss_option=${ss_option:-1}
  102. if [[ ! "${ss_option}" =~ ^[1-2]$ ]]; then
  103. echo "${CWARNING}input error! Please only input number 1~2${CEND}"
  104. else
  105. break
  106. fi
  107. done
  108. AddUser_SS
  109. Iptables_set
  110. if [ "${PM}" == 'yum' ]; then
  111. pkgList="wget unzip openssl-devel gcc swig autoconf libtool libevent automake make curl curl-devel zlib-devel perl perl-devel cpio expat-devel gettext-devel git asciidoc xmlto c-ares-devel pcre-devel udns-devel libev-devel"
  112. for Package in ${pkgList}; do
  113. yum -y install ${Package}
  114. done
  115. elif [ "${PM}" == 'apt-get' ]; then
  116. apt-get -y update
  117. pkgList="curl wget unzip gcc swig automake make perl cpio git libudns-dev libev-dev gettext build-essential autoconf libtool libpcre3-dev asciidoc xmlto libc-ares-dev"
  118. for Package in ${pkgList}; do
  119. apt-get -y install $Package
  120. done
  121. fi
  122. }
  123. Install_SS-python() {
  124. Install_Python
  125. ${python_install_dir}/bin/pip install M2Crypto
  126. ${python_install_dir}/bin/pip install greenlet
  127. ${python_install_dir}/bin/pip install gevent
  128. ${python_install_dir}/bin/pip install shadowsocks
  129. if [ -f ${python_install_dir}/bin/ssserver ]; then
  130. /bin/cp ../init.d/SS-python-init /etc/init.d/shadowsocks
  131. chmod +x /etc/init.d/shadowsocks
  132. sed -i "s@SS_bin=.*@SS_bin=${python_install_dir}/bin/ssserver@" /etc/init.d/shadowsocks
  133. [ "${PM}" == 'yum' ] && { chkconfig --add shadowsocks; chkconfig shadowsocks on; }
  134. [ "${PM}" == 'apt-get' ] && update-rc.d shadowsocks defaults
  135. else
  136. echo
  137. echo "${CQUESTION}SS-python install failed! Please visit https://oneinstack.com${CEND}"
  138. exit 1
  139. fi
  140. }
  141. Install_SS-libev() {
  142. src_url=http://mirrors.linuxeye.com/oneinstack/src/shadowsocks-libev-3.2.3.tar.gz && Download_src
  143. src_url=http://mirrors.linuxeye.com/oneinstack/src/libsodium-${libsodium_ver}.tar.gz && Download_src
  144. src_url=http://mirrors.linuxeye.com/oneinstack/src/mbedtls-2.16.0-apache.tgz && Download_src
  145. if [ ! -e "/usr/local/lib/libsodium.la" ]; then
  146. tar xzf libsodium-${libsodium_ver}.tar.gz
  147. pushd libsodium-${libsodium_ver} > /dev/null
  148. ./configure --disable-dependency-tracking --enable-minimal
  149. make -j ${THREAD} && make install
  150. popd > /dev/null
  151. rm -rf libsodium-${libsodium_ver}
  152. fi
  153. tar xzf mbedtls-2.16.0-apache.tgz
  154. pushd mbedtls-2.16.0 > /dev/null
  155. make SHARED=1 CFLAGS=-fPIC
  156. make DESTDIR=/usr install
  157. popd > /dev/null
  158. tar xzf shadowsocks-libev-3.2.3.tar.gz
  159. pushd shadowsocks-libev-3.2.3 > /dev/null
  160. make clean
  161. ./configure
  162. make -j ${THREAD} && make install
  163. popd > /dev/null
  164. [ -z "`grep /usr/local/lib /etc/ld.so.conf.d/*.conf`" ] && echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
  165. ldconfig
  166. if [ -f /usr/local/bin/ss-server ]; then
  167. if [ "${PM}" == 'yum' ]; then
  168. /bin/cp ../init.d/SS-libev-init-CentOS /etc/init.d/shadowsocks
  169. chkconfig --add shadowsocks
  170. chkconfig shadowsocks on
  171. elif [ "${PM}" == 'apt-get' ]; then
  172. /bin/cp ../init.d/SS-libev-init-Ubuntu /etc/init.d/shadowsocks
  173. update-rc.d shadowsocks defaults
  174. fi
  175. else
  176. echo
  177. echo "${CQUESTION}SS-libev install failed! Please visit https://oneinstack.com${CEND}"
  178. exit 1
  179. fi
  180. }
  181. Uninstall_SS() {
  182. while :; do echo
  183. read -e -p "Do you want to uninstall SS? [y/n]: " SS_yn
  184. if [[ ! "${SS_yn}" =~ ^[y,n]$ ]]; then
  185. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  186. else
  187. break
  188. fi
  189. done
  190. if [ "${SS_yn}" == 'y' ]; then
  191. [ -n "$(ps -ef | grep -v grep | grep -iE "ssserver|ss-server")" ] && /etc/init.d/shadowsocks stop
  192. [ "${PM}" == 'yum' ] && chkconfig --del shadowsocks
  193. [ "${PM}" == 'apt-get' ] && update-rc.d -f shadowsocks remove
  194. rm -rf /etc/shadowsocks /var/run/shadowsocks.pid /etc/init.d/shadowsocks
  195. if [ "${ss_option}" == '1' ]; then
  196. rm -f /usr/local/bin/{ss-local,ss-tunnel,ss-server,ss-manager,ss-redir}
  197. rm -f /usr/local/lib/libshadowsocks.*
  198. rm -f /usr/local/include/shadowsocks.h
  199. rm -f /usr/local/lib/pkgconfig/shadowsocks-libev.pc
  200. rm -f /usr/local/share/man/man1/{ss-local.1,ss-tunnel.1,ss-server.1,ss-manager.1,ss-redir.1,shadowsocks.8}
  201. if [ $? -eq 0 ]; then
  202. echo "${CSUCCESS}SS-libev uninstall successful! ${CEND}"
  203. else
  204. echo "${CFAILURE}SS-libev uninstall failed! ${CEND}"
  205. fi
  206. elif [ "${ss_option}" == '2' ]; then
  207. ${python_install_dir}/bin/pip uninstall -y shadowsocks
  208. if [ $? -eq 0 ]; then
  209. echo "${CSUCCESS}SS-python uninstall successful! ${CEND}"
  210. else
  211. echo "${CFAILURE}SS-python uninstall failed! ${CEND}"
  212. fi
  213. fi
  214. fi
  215. }
  216. Config_SS() {
  217. [ ! -d "/etc/shadowsocks" ] && mkdir /etc/shadowsocks
  218. [ "${ss_option}" == '1' ] && cat > /etc/shadowsocks/config.json << EOF
  219. {
  220. "server":"0.0.0.0",
  221. "server_port":${SS_port},
  222. "local_port":1080,
  223. "password":"${SS_password}",
  224. "timeout":300,
  225. "method":"aes-256-cfb"
  226. }
  227. EOF
  228. [ "${ss_option}" == '2' ] && cat > /etc/shadowsocks/config.json << EOF
  229. {
  230. "server":"0.0.0.0",
  231. "local_address":"127.0.0.1",
  232. "local_port":1080,
  233. "port_password":{
  234. "${SS_port}":"${SS_password}"
  235. },
  236. "timeout":300,
  237. "method":"aes-256-cfb",
  238. "fast_open":false
  239. }
  240. EOF
  241. }
  242. AddUser_Config_SS() {
  243. [ ! -e /etc/shadowsocks/config.json ] && { echo "${CFAILURE}SS is not installed! ${CEND}"; exit 1; }
  244. [ -z "$(grep \"${SS_port}\" /etc/shadowsocks/config.json)" ] && sed -i "s@\"port_password\":{@\"port_password\":{\n\t\"${SS_port}\":\"${SS_password}\",@" /etc/shadowsocks/config.json || { echo "${CWARNING}This port is already in /etc/shadowsocks/config.json${CEND}"; exit 1; }
  245. }
  246. Print_User_SS() {
  247. printf "
  248. Your Server IP: ${CMSG}${PUBLIC_IPADDR}${CEND}
  249. Your Server Port: ${CMSG}${SS_port}${CEND}
  250. Your Password: ${CMSG}${SS_password}${CEND}
  251. Your Local IP: ${CMSG}127.0.0.1${CEND}
  252. Your Local Port: ${CMSG}1080${CEND}
  253. Your Encryption Method: ${CMSG}aes-256-cfb${CEND}
  254. "
  255. }
  256. case "$1" in
  257. install)
  258. Def_parameter
  259. [ "${ss_option}" == '1' ] && Install_SS-libev
  260. [ "${ss_option}" == '2' ] && Install_SS-python
  261. Config_SS
  262. service shadowsocks start
  263. Print_User_SS
  264. ;;
  265. adduser)
  266. Check_SS
  267. if [ "${ss_option}" == '2' ]; then
  268. AddUser_SS
  269. Iptables_set
  270. AddUser_Config_SS
  271. service shadowsocks restart
  272. Print_User_SS
  273. else
  274. printf "
  275. Sorry, we have no plan to support multi port configuration. Actually you can use multiple instances instead. For example:
  276. ss-server -c /etc/shadowsocks/config1.json -f /var/run/shadowsocks-server/pid1
  277. ss-server -c /etc/shadowsocks/config2.json -f /var/run/shadowsocks-server/pid2
  278. "
  279. fi
  280. ;;
  281. uninstall)
  282. Check_SS
  283. Uninstall_SS
  284. ;;
  285. *)
  286. echo
  287. echo "Usage: ${CMSG}$0${CEND} { ${CMSG}install${CEND} | ${CMSG}adduser${CEND} | ${CMSG}uninstall${CEND} }"
  288. echo
  289. exit 1
  290. esac