ss.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. showhelp() {
  32. echo
  33. echo "Usage: $0 command ...[parameters]....
  34. --help, -h Show this help message
  35. --quiet, -q quiet operation
  36. --install, -i Install
  37. --uninstall, -u Uninstall
  38. --libev shadowsocks-libev
  39. --python shadowsocks-python
  40. --adduser Add shadowsocks account
  41. --password [password] shadowsocks password
  42. --port [port] shadowsocks port
  43. "
  44. }
  45. ARG_NUM=$#
  46. TEMP=`getopt -o hqiu --long help,quiet,install,uninstall,libev,python,adduser,password:,port: -- "$@" 2>/dev/null`
  47. [ $? != 0 ] && echo "${CWARNING}ERROR: unknown argument! ${CEND}" && showhelp && exit 1
  48. eval set -- "${TEMP}"
  49. while :; do
  50. [ -z "$1" ] && break;
  51. case "$1" in
  52. -h|--help)
  53. showhelp; exit 0
  54. ;;
  55. -q|--quiet)
  56. quiet_yn=y; shift 1
  57. ;;
  58. -i|--install)
  59. install_yn=y; shift 1
  60. ;;
  61. -u|--uninstall)
  62. uninstall_yn=y; shift 1
  63. ;;
  64. --libev)
  65. libev_queit=y; ss_option=1; shift 1
  66. ;;
  67. --python)
  68. python_queit=y; ss_option=2; shift 1
  69. ;;
  70. --adduser)
  71. adduser_yn=y; shift 1
  72. ;;
  73. --password)
  74. password_queit=y; SS_password=$2; shift 2
  75. ;;
  76. --port)
  77. port_queit=y; SS_port=$2; shift 2
  78. ;;
  79. --)
  80. shift
  81. ;;
  82. *)
  83. echo "${CWARNING}ERROR: unknown argument! ${CEND}" && showhelp && exit 1
  84. ;;
  85. esac
  86. done
  87. [ "${CentOS_ver}" == '5' ] && { echo "${CWARNING}SS only support CentOS6,7 or Debian or Ubuntu! ${CEND}"; exit 1; }
  88. Check_SS() {
  89. [ -f /usr/local/bin/ss-server ] && ss_option=1
  90. [ -f ${python_install_dir}/bin/ssserver ] && ss_option=2
  91. }
  92. AddUser_SS() {
  93. while :; do echo
  94. [ ${password_queit} != 'y' ] && read -e -p "Please input password for SS: " SS_password
  95. [ -n "$(echo ${SS_password} | grep '[+|&]')" ] && { echo "${CWARNING}input error,not contain a plus sign (+) and & ${CEND}"; continue; }
  96. (( ${#SS_password} >= 5 )) && break || echo "${CWARNING}SS password least 5 characters! ${CEND}"
  97. done
  98. }
  99. Iptables_set() {
  100. if [ -e '/etc/sysconfig/iptables' ]; then
  101. SS_Already_port=$(grep -oE '9[0-9][0-9][0-9]' /etc/sysconfig/iptables | head -n 1)
  102. elif [ -e '/etc/iptables/rules.v4' ]; then
  103. SS_Already_port=$(grep -oE '9[0-9][0-9][0-9]' /etc/iptables/rules.v4 | head -n 1)
  104. elif [ -e '/etc/iptables.up.rules' ]; then
  105. SS_Already_port=$(grep -oE '9[0-9][0-9][0-9]' /etc/iptables.up.rules | head -n 1)
  106. fi
  107. if [ -n "${SS_Already_port}" ]; then
  108. let SS_Default_port="${SS_Already_port}+1"
  109. else
  110. SS_Default_port=9001
  111. fi
  112. while :; do echo
  113. [ ${port_queit} != 'y' ] && read -e -p "Please input SS port(Default: ${SS_Default_port}): " SS_port
  114. SS_port=${SS_port:-${SS_Default_port}}
  115. if [ ${SS_port} -ge 1 >/dev/null 2>&1 -a ${SS_port} -le 65535 >/dev/null 2>&1 ]; then
  116. [ -z "$(netstat -tpln | grep :${SS_port}$)" ] && break || echo "${CWARNING}This port is already used! ${CEND}"
  117. else
  118. echo "${CWARNING}input error! Input range: 1~65535${CEND}"
  119. fi
  120. done
  121. if [ "${PM}" == 'yum' ]; then
  122. if [ -n "`grep 'dport 80 ' /etc/sysconfig/iptables`" ] && [ -z "$(grep -E ${SS_port} /etc/sysconfig/iptables)" ]; then
  123. iptables -I INPUT 4 -p udp -m state --state NEW -m udp --dport ${SS_port} -j ACCEPT
  124. iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport ${SS_port} -j ACCEPT
  125. service iptables save
  126. ip6tables -I INPUT 4 -p udp -m state --state NEW -m udp --dport ${SS_port} -j ACCEPT
  127. ip6tables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport ${SS_port} -j ACCEPT
  128. service ip6tables save
  129. fi
  130. elif [ "${PM}" == 'apt-get' ]; then
  131. if [ -e '/etc/iptables/rules.v4' ]; then
  132. if [ -n "`grep 'dport 80 ' /etc/iptables/rules.v4`" ] && [ -z "$(grep -E ${SS_port} /etc/iptables/rules.v4)" ]; then
  133. iptables -I INPUT 4 -p udp -m state --state NEW -m udp --dport ${SS_port} -j ACCEPT
  134. iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport ${SS_port} -j ACCEPT
  135. iptables-save > /etc/iptables/rules.v4
  136. ip6tables -I INPUT 4 -p udp -m state --state NEW -m udp --dport ${SS_port} -j ACCEPT
  137. ip6tables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport ${SS_port} -j ACCEPT
  138. ip6tables-save > /etc/iptables/rules.v6
  139. fi
  140. elif [ -e '/etc/iptables.up.rules' ]; then
  141. if [ -n "`grep 'dport 80 ' /etc/iptables.up.rules`" ] && [ -z "$(grep -E ${SS_port} /etc/iptables.up.rules)" ]; then
  142. iptables -I INPUT 4 -p udp -m state --state NEW -m udp --dport ${SS_port} -j ACCEPT
  143. iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport ${SS_port} -j ACCEPT
  144. iptables-save > /etc/iptables.up.rules
  145. fi
  146. fi
  147. fi
  148. }
  149. Def_parameter() {
  150. while :; do echo
  151. if [ "${libev_queit}" != 'y' -a "${python_queit}" != 'y' ]; then
  152. echo "Please select SS server version:"
  153. echo -e "\t${CMSG}1${CEND}. Install SS-libev"
  154. echo -e "\t${CMSG}2${CEND}. Install SS-python"
  155. read -e -p "Please input a number:(Default 1 press Enter) " ss_option
  156. fi
  157. ss_option=${ss_option:-1}
  158. if [[ ! "${ss_option}" =~ ^[1-2]$ ]]; then
  159. echo "${CWARNING}input error! Please only input number 1~2${CEND}"
  160. else
  161. break
  162. fi
  163. done
  164. AddUser_SS
  165. Iptables_set
  166. if [ "${PM}" == 'yum' ]; then
  167. 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"
  168. for Package in ${pkgList}; do
  169. yum -y install ${Package}
  170. done
  171. elif [ "${PM}" == 'apt-get' ]; then
  172. apt-get -y update
  173. 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"
  174. for Package in ${pkgList}; do
  175. apt-get -y install $Package
  176. done
  177. fi
  178. }
  179. Install_SS-python() {
  180. Install_Python
  181. ${python_install_dir}/bin/pip install M2Crypto
  182. ${python_install_dir}/bin/pip install greenlet
  183. ${python_install_dir}/bin/pip install gevent
  184. ${python_install_dir}/bin/pip install shadowsocks
  185. if [ -f ${python_install_dir}/bin/ssserver ]; then
  186. sed -i 's@libcrypto.EVP_CIPHER_CTX_cleanup@libcrypto.EVP_CIPHER_CTX_reset@g' ${python_install_dir}/lib/python3.6/site-packages/shadowsocks/crypto/openssl.py
  187. if [ -e /bin/systemctl ]; then
  188. /bin/cp ../init.d/SS-python.service /lib/systemd/system/shadowsocks.service
  189. sed -i "s@/usr/local/python@${python_install_dir}@g" /lib/systemd/system/shadowsocks.service
  190. systemctl enable shadowsocks
  191. else
  192. /bin/cp ../init.d/SS-python-init /etc/init.d/shadowsocks
  193. sed -i "s@SS_bin=.*@SS_bin=${python_install_dir}/bin/ssserver@" /etc/init.d/shadowsocks
  194. chmod +x /etc/init.d/shadowsocks
  195. [ "${PM}" == 'yum' ] && { chkconfig --add shadowsocks; chkconfig shadowsocks on; }
  196. [ "${PM}" == 'apt-get' ] && update-rc.d shadowsocks defaults
  197. fi
  198. else
  199. echo
  200. echo "${CQUESTION}SS-python install failed! Please visit https://oneinstack.com${CEND}"
  201. exit 1
  202. fi
  203. }
  204. Install_SS-libev() {
  205. src_url=http://mirrors.linuxeye.com/oneinstack/src/shadowsocks-libev-3.2.3.tar.gz && Download_src
  206. src_url=http://mirrors.linuxeye.com/oneinstack/src/libsodium-${libsodium_ver}.tar.gz && Download_src
  207. src_url=http://mirrors.linuxeye.com/oneinstack/src/mbedtls-2.16.0-apache.tgz && Download_src
  208. if [ ! -e "/usr/local/lib/libsodium.la" ]; then
  209. tar xzf libsodium-${libsodium_ver}.tar.gz
  210. pushd libsodium-${libsodium_ver} > /dev/null
  211. ./configure --disable-dependency-tracking --enable-minimal
  212. make -j ${THREAD} && make install
  213. popd > /dev/null
  214. rm -rf libsodium-${libsodium_ver}
  215. fi
  216. tar xzf mbedtls-2.16.0-apache.tgz
  217. pushd mbedtls-2.16.0 > /dev/null
  218. make SHARED=1 CFLAGS=-fPIC
  219. make DESTDIR=/usr install
  220. popd > /dev/null
  221. tar xzf shadowsocks-libev-3.2.3.tar.gz
  222. pushd shadowsocks-libev-3.2.3 > /dev/null
  223. make clean
  224. ./configure
  225. make -j ${THREAD} && make install
  226. popd > /dev/null
  227. [ -z "`grep /usr/local/lib /etc/ld.so.conf.d/*.conf`" ] && echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
  228. ldconfig
  229. if [ -f /usr/local/bin/ss-server ]; then
  230. if [ -e /bin/systemctl ]; then
  231. /bin/cp ../init.d/SS-libev.service /lib/systemd/system/shadowsocks.service
  232. systemctl enable shadowsocks
  233. else
  234. if [ "${PM}" == 'yum' ]; then
  235. /bin/cp ../init.d/SS-libev-init-CentOS /etc/init.d/shadowsocks
  236. chkconfig --add shadowsocks
  237. chkconfig shadowsocks on
  238. elif [ "${PM}" == 'apt-get' ]; then
  239. /bin/cp ../init.d/SS-libev-init-Ubuntu /etc/init.d/shadowsocks
  240. update-rc.d shadowsocks defaults
  241. fi
  242. fi
  243. else
  244. echo
  245. echo "${CQUESTION}SS-libev install failed! Please visit https://oneinstack.com${CEND}"
  246. exit 1
  247. fi
  248. }
  249. Uninstall_SS() {
  250. while :; do echo
  251. [ "${quiet_yn}" != 'y' ] && read -e -p "Do you want to uninstall SS? [y/n]: " uninstall_yn
  252. if [[ ! "${uninstall_yn}" =~ ^[y,n]$ ]]; then
  253. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  254. else
  255. break
  256. fi
  257. done
  258. if [ "${uninstall_yn}" == 'y' ]; then
  259. [ -n "$(ps -ef | grep -v grep | grep -iE "ssserver|ss-server")" ] && service shadowsocks stop
  260. [ -e /lib/systemd/system/shadowsocks.service ] && { systemctl disable shadowsocks; rm -f /lib/systemd/system/shadowsocks.service; }
  261. [ "${PM}" == 'yum' ] && chkconfig --del shadowsocks
  262. [ "${PM}" == 'apt-get' ] && update-rc.d -f shadowsocks remove
  263. rm -rf /etc/shadowsocks /var/run/shadowsocks.pid /etc/init.d/shadowsocks
  264. if [ "${ss_option}" == '1' ]; then
  265. rm -f /usr/local/bin/{ss-local,ss-tunnel,ss-server,ss-manager,ss-redir}
  266. rm -f /usr/local/lib/libshadowsocks.*
  267. rm -f /usr/local/include/shadowsocks.h
  268. rm -f /usr/local/lib/pkgconfig/shadowsocks-libev.pc
  269. rm -f /usr/local/share/man/man1/{ss-local.1,ss-tunnel.1,ss-server.1,ss-manager.1,ss-redir.1,shadowsocks.8}
  270. if [ $? -eq 0 ]; then
  271. echo "${CSUCCESS}SS-libev uninstall successful! ${CEND}"
  272. else
  273. echo "${CFAILURE}SS-libev uninstall failed! ${CEND}"
  274. fi
  275. elif [ "${ss_option}" == '2' ]; then
  276. ${python_install_dir}/bin/pip uninstall -y shadowsocks
  277. if [ $? -eq 0 ]; then
  278. echo "${CSUCCESS}SS-python uninstall successful! ${CEND}"
  279. else
  280. echo "${CFAILURE}SS-python uninstall failed! ${CEND}"
  281. fi
  282. fi
  283. fi
  284. }
  285. Config_SS() {
  286. [ ! -d "/etc/shadowsocks" ] && mkdir /etc/shadowsocks
  287. [ "${ss_option}" == '1' ] && cat > /etc/shadowsocks/config.json << EOF
  288. {
  289. "server":"0.0.0.0",
  290. "server_port":${SS_port},
  291. "local_port":1080,
  292. "password":"${SS_password}",
  293. "timeout":300,
  294. "method":"aes-256-cfb"
  295. }
  296. EOF
  297. [ "${ss_option}" == '2' ] && cat > /etc/shadowsocks/config.json << EOF
  298. {
  299. "server":"0.0.0.0",
  300. "local_address":"127.0.0.1",
  301. "local_port":1080,
  302. "port_password":{
  303. "${SS_port}":"${SS_password}"
  304. },
  305. "timeout":300,
  306. "method":"aes-256-cfb",
  307. "fast_open":false
  308. }
  309. EOF
  310. }
  311. AddUser_Config_SS() {
  312. [ ! -e /etc/shadowsocks/config.json ] && { echo "${CFAILURE}SS is not installed! ${CEND}"; exit 1; }
  313. [ -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; }
  314. }
  315. Print_User_SS() {
  316. printf "
  317. Your Server IP: ${CMSG}${PUBLIC_IPADDR}${CEND}
  318. Your Server Port: ${CMSG}${SS_port}${CEND}
  319. Your Password: ${CMSG}${SS_password}${CEND}
  320. Your Local IP: ${CMSG}127.0.0.1${CEND}
  321. Your Local Port: ${CMSG}1080${CEND}
  322. Your Encryption Method: ${CMSG}aes-256-cfb${CEND}
  323. "
  324. }
  325. if [ "${install_yn}" == 'y' -o "${ARG_NUM}" == '0' ]; then
  326. Def_parameter
  327. [ "${ss_option}" == '1' ] && Install_SS-libev
  328. [ "${ss_option}" == '2' ] && Install_SS-python
  329. Config_SS
  330. service shadowsocks start
  331. Print_User_SS
  332. fi
  333. if [ "${adduser_yn}" == 'y' ]; then
  334. Check_SS
  335. if [ "${ss_option}" == '2' ]; then
  336. AddUser_SS
  337. Iptables_set
  338. AddUser_Config_SS
  339. service shadowsocks restart
  340. Print_User_SS
  341. elif [ "${ss_option}" == '2' ]; then
  342. printf "
  343. Sorry, we have no plan to support multi port configuration. Actually you can use multiple instances instead. For example:
  344. ss-server -c /etc/shadowsocks/config1.json -f /var/run/shadowsocks-server/pid1
  345. ss-server -c /etc/shadowsocks/config2.json -f /var/run/shadowsocks-server/pid2
  346. "
  347. fi
  348. fi
  349. if [ "${uninstall_yn}" == 'y' ]; then
  350. Check_SS
  351. Uninstall_SS
  352. fi