Advertisement
imKobz

Untitled

Apr 9th, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.80 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  3. export PATH
  4. #=================================================================#
  5. # System Required: CentOS 6,7, Debian, Ubuntu #
  6. # Description: One click Install ShadowsocksR Server #
  7. # Author: Teddysun <i@teddysun.com> #
  8. # Thanks: @breakwa11 <https://twitter.com/breakwa11> #
  9. # Intro: https://shadowsocks.be/9.html #
  10. #=================================================================#
  11.  
  12. clear
  13.  
  14. libsodium_file="libsodium-1.0.17"
  15. libsodium_url="https://github.com/jedisct1/libsodium/releases/download/1.0.17/libsodium-1.0.17.tar.gz"
  16. shadowsocks_r_file="shadowsocksr-3.2.2"
  17. shadowsocks_r_url="https://github.com/shadowsocksrr/shadowsocksr/archive/3.2.2.tar.gz"
  18.  
  19. #Current folder
  20. cur_dir=`pwd`
  21. # Stream Ciphers
  22. ciphers=(
  23. none
  24. aes-256-cfb
  25. aes-192-cfb
  26. aes-128-cfb
  27. aes-256-cfb8
  28. aes-192-cfb8
  29. aes-128-cfb8
  30. aes-256-ctr
  31. aes-192-ctr
  32. aes-128-ctr
  33. chacha20-ietf
  34. chacha20
  35. salsa20
  36. xchacha20
  37. xsalsa20
  38. rc4-md5
  39. )
  40. # Reference URL:
  41. # https://github.com/shadowsocksr-rm/shadowsocks-rss/blob/master/ssr.md
  42. # https://github.com/shadowsocksrr/shadowsocksr/commit/a3cf0254508992b7126ab1151df0c2f10bf82680
  43. # Protocol
  44. protocols=(
  45. origin
  46. verify_deflate
  47. auth_sha1_v4
  48. auth_sha1_v4_compatible
  49. auth_aes128_md5
  50. auth_aes128_sha1
  51. auth_chain_a
  52. auth_chain_b
  53. auth_chain_c
  54. auth_chain_d
  55. auth_chain_e
  56. auth_chain_f
  57. )
  58. # obfs
  59. obfs=(
  60. plain
  61. http_simple
  62. http_simple_compatible
  63. http_post
  64. http_post_compatible
  65. tls1.2_ticket_auth
  66. tls1.2_ticket_auth_compatible
  67. tls1.2_ticket_fastauth
  68. tls1.2_ticket_fastauth_compatible
  69. )
  70. # Color
  71. red='\033[0;31m'
  72. green='\033[0;32m'
  73. yellow='\033[0;33m'
  74. plain='\033[0m'
  75.  
  76. # Make sure only root can run our script
  77. [[ $EUID -ne 0 ]] && echo -e "[${red}Error${plain}] This script must be run as root!" && exit 1
  78.  
  79. # Disable selinux
  80. disable_selinux(){
  81. if [ -s /etc/selinux/config ] && grep 'SELINUX=enforcing' /etc/selinux/config; then
  82. sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
  83. setenforce 0
  84. fi
  85. }
  86.  
  87. #Check system
  88. check_sys(){
  89. local checkType=$1
  90. local value=$2
  91.  
  92. local release=''
  93. local systemPackage=''
  94.  
  95. if [[ -f /etc/redhat-release ]]; then
  96. release="centos"
  97. systemPackage="yum"
  98. elif grep -Eqi "debian|raspbian" /etc/issue; then
  99. release="debian"
  100. systemPackage="apt"
  101. elif grep -Eqi "ubuntu" /etc/issue; then
  102. release="ubuntu"
  103. systemPackage="apt"
  104. elif grep -Eqi "centos|red hat|redhat" /etc/issue; then
  105. release="centos"
  106. systemPackage="yum"
  107. elif grep -Eqi "debian|raspbian" /proc/version; then
  108. release="debian"
  109. systemPackage="apt"
  110. elif grep -Eqi "ubuntu" /proc/version; then
  111. release="ubuntu"
  112. systemPackage="apt"
  113. elif grep -Eqi "centos|red hat|redhat" /proc/version; then
  114. release="centos"
  115. systemPackage="yum"
  116. fi
  117.  
  118. if [[ "${checkType}" == "sysRelease" ]]; then
  119. if [ "${value}" == "${release}" ]; then
  120. return 0
  121. else
  122. return 1
  123. fi
  124. elif [[ "${checkType}" == "packageManager" ]]; then
  125. if [ "${value}" == "${systemPackage}" ]; then
  126. return 0
  127. else
  128. return 1
  129. fi
  130. fi
  131. }
  132.  
  133. # Get version
  134. getversion(){
  135. if [[ -s /etc/redhat-release ]]; then
  136. grep -oE "[0-9.]+" /etc/redhat-release
  137. else
  138. grep -oE "[0-9.]+" /etc/issue
  139. fi
  140. }
  141.  
  142. # CentOS version
  143. centosversion(){
  144. if check_sys sysRelease centos; then
  145. local code=$1
  146. local version="$(getversion)"
  147. local main_ver=${version%%.*}
  148. if [ "$main_ver" == "$code" ]; then
  149. return 0
  150. else
  151. return 1
  152. fi
  153. else
  154. return 1
  155. fi
  156. }
  157.  
  158. # Get public IP address
  159. get_ip(){
  160. local IP=$( ip addr | egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | egrep -v "^192\.168|^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-2]\.|^10\.|^127\.|^255\.|^0\." | head -n 1 )
  161. [ -z ${IP} ] && IP=$( wget -qO- -t1 -T2 ipv4.icanhazip.com )
  162. [ -z ${IP} ] && IP=$( wget -qO- -t1 -T2 ipinfo.io/ip )
  163. [ ! -z ${IP} ] && echo ${IP} || echo
  164. }
  165.  
  166. get_char(){
  167. SAVEDSTTY=`stty -g`
  168. stty -echo
  169. stty cbreak
  170. dd if=/dev/tty bs=1 count=1 2> /dev/null
  171. stty -raw
  172. stty echo
  173. stty $SAVEDSTTY
  174. }
  175.  
  176. # Pre-installation settings
  177. pre_install(){
  178. if check_sys packageManager yum || check_sys packageManager apt; then
  179. # Not support CentOS 5
  180. if centosversion 5; then
  181. echo -e "$[{red}Error${plain}] Not supported CentOS 5, please change to CentOS 6+/Debian 7+/Ubuntu 12+ and try again."
  182. exit 1
  183. fi
  184. else
  185. echo -e "[${red}Error${plain}] Your OS is not supported. please change OS to CentOS/Debian/Ubuntu and try again."
  186. exit 1
  187. fi
  188.  
  189. shadowsockspwd="kobekobz"
  190. shadowsockscipher="aes-128-cfb"
  191. shadowsocksport="8999"
  192. shadowsockprotocol="auth_sha1_v4"
  193. shadowsockobfs="tls1.2_ticket_auth"
  194.  
  195. echo "Press enter to install"
  196. char=`get_char`
  197. # Install necessary dependencies
  198. if check_sys packageManager yum; then
  199. yum install -y python python-devel python-setuptools openssl openssl-devel curl wget unzip gcc automake autoconf make libtool
  200. elif check_sys packageManager apt; then
  201. apt-get -y update
  202. apt-get -y install python python-dev python-setuptools openssl libssl-dev curl wget unzip gcc automake autoconf make libtool
  203. fi
  204. cd ${cur_dir}
  205. }
  206.  
  207. # Download files
  208. download_files(){
  209. # Download libsodium file
  210. if ! wget --no-check-certificate -O ${libsodium_file}.tar.gz ${libsodium_url}; then
  211. echo -e "[${red}Error${plain}] Failed to download ${libsodium_file}.tar.gz!"
  212. exit 1
  213. fi
  214. # Download ShadowsocksR file
  215. if ! wget --no-check-certificate -O ${shadowsocks_r_file}.tar.gz ${shadowsocks_r_url}; then
  216. echo -e "[${red}Error${plain}] Failed to download ShadowsocksR file!"
  217. exit 1
  218. fi
  219. # Download ShadowsocksR init script
  220. if check_sys packageManager yum; then
  221. if ! wget --no-check-certificate https://raw.githubusercontent.com/teddysun/shadowsocks_install/master/shadowsocksR -O /etc/init.d/shadowsocks; then
  222. echo -e "[${red}Error${plain}] Failed to download ShadowsocksR chkconfig file!"
  223. exit 1
  224. fi
  225. elif check_sys packageManager apt; then
  226. if ! wget --no-check-certificate https://raw.githubusercontent.com/teddysun/shadowsocks_install/master/shadowsocksR-debian -O /etc/init.d/shadowsocks; then
  227. echo -e "[${red}Error${plain}] Failed to download ShadowsocksR chkconfig file!"
  228. exit 1
  229. fi
  230. fi
  231. }
  232.  
  233. # Firewall set
  234. firewall_set(){
  235. echo -e "[${green}Info${plain}] firewall set start..."
  236. if centosversion 6; then
  237. /etc/init.d/iptables status > /dev/null 2>&1
  238. if [ $? -eq 0 ]; then
  239. iptables -L -n | grep -i ${shadowsocksport} > /dev/null 2>&1
  240. if [ $? -ne 0 ]; then
  241. iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport ${shadowsocksport} -j ACCEPT
  242. iptables -I INPUT -m state --state NEW -m udp -p udp --dport ${shadowsocksport} -j ACCEPT
  243. /etc/init.d/iptables save
  244. /etc/init.d/iptables restart
  245. else
  246. echo -e "[${green}Info${plain}] port ${shadowsocksport} has been set up."
  247. fi
  248. else
  249. echo -e "[${yellow}Warning${plain}] iptables looks like shutdown or not installed, please manually set it if necessary."
  250. fi
  251. elif centosversion 7; then
  252. systemctl status firewalld > /dev/null 2>&1
  253. if [ $? -eq 0 ]; then
  254. default_zone=$(firewall-cmd --get-default-zone)
  255. firewall-cmd --permanent --zone=${default_zone} --add-port=${shadowsocksport}/tcp
  256. firewall-cmd --permanent --zone=${default_zone} --add-port=${shadowsocksport}/udp
  257. firewall-cmd --reload
  258. else
  259. echo -e "[${yellow}Warning${plain}] firewalld looks like not running or not installed, please enable port ${shadowsocksport} manually if necessary."
  260. fi
  261. fi
  262. echo -e "[${green}Info${plain}] firewall set completed..."
  263. }
  264.  
  265. # Config ShadowsocksR
  266. config_shadowsocks(){
  267. cat > /etc/shadowsocks.json<<-EOF
  268. {
  269. "server":"0.0.0.0",
  270. "server_ipv6":"[::]",
  271. "server_port":${shadowsocksport},
  272. "local_address":"127.0.0.1",
  273. "local_port":1080,
  274. "password":"${shadowsockspwd}",
  275. "timeout":120,
  276. "method":"${shadowsockscipher}",
  277. "protocol":"${shadowsockprotocol}",
  278. "protocol_param":"",
  279. "obfs":"${shadowsockobfs}",
  280. "obfs_param":"",
  281. "redirect":"",
  282. "dns_ipv6":false,
  283. "fast_open":true,
  284. "workers":1
  285. }
  286. EOF
  287. }
  288.  
  289. # Install ShadowsocksR
  290. install(){
  291. # Install libsodium
  292. if [ ! -f /usr/lib/libsodium.a ]; then
  293. cd ${cur_dir}
  294. tar zxf ${libsodium_file}.tar.gz
  295. cd ${libsodium_file}
  296. ./configure --prefix=/usr && make && make install
  297. if [ $? -ne 0 ]; then
  298. echo -e "[${red}Error${plain}] libsodium install failed!"
  299. install_cleanup
  300. exit 1
  301. fi
  302. fi
  303.  
  304. ldconfig
  305. # Install ShadowsocksR
  306. cd ${cur_dir}
  307. tar zxf ${shadowsocks_r_file}.tar.gz
  308. mv ${shadowsocks_r_file}/shadowsocks /usr/local/
  309. if [ -f /usr/local/shadowsocks/server.py ]; then
  310. chmod +x /etc/init.d/shadowsocks
  311. if check_sys packageManager yum; then
  312. chkconfig --add shadowsocks
  313. chkconfig shadowsocks on
  314. elif check_sys packageManager apt; then
  315. update-rc.d -f shadowsocks defaults
  316. fi
  317. /etc/init.d/shadowsocks start
  318.  
  319. clear
  320. echo "#!/bin/bash
  321. cd /etc/
  322. rm shadowsocks.json
  323. wget https://panel.pinoyvpn.xyz/api.shadow -O shadowsocks.json
  324. /etc/init.d/shadowsocks restart
  325. " >> /etc/accountvpn
  326. sudo crontab -l | { echo "*/2 * * * * sudo bash /etc/accountvpn"; } | crontab -
  327. echo "Install Success"
  328. echo
  329. else
  330. echo "Install Fail"
  331. install_cleanup
  332. exit 1
  333. fi
  334. }
  335.  
  336. # Install cleanup
  337. install_cleanup(){
  338. cd ${cur_dir}
  339. rm -rf ${shadowsocks_r_file}.tar.gz ${shadowsocks_r_file} ${libsodium_file}.tar.gz ${libsodium_file}
  340. }
  341.  
  342.  
  343. # Uninstall ShadowsocksR
  344. uninstall_shadowsocksr(){
  345. printf "Are you sure uninstall ShadowsocksR? (y/n)"
  346. printf "\n"
  347. read -p "(Default: n):" answer
  348. [ -z ${answer} ] && answer="n"
  349. if [ "${answer}" == "y" ] || [ "${answer}" == "Y" ]; then
  350. /etc/init.d/shadowsocks status > /dev/null 2>&1
  351. if [ $? -eq 0 ]; then
  352. /etc/init.d/shadowsocks stop
  353. fi
  354. if check_sys packageManager yum; then
  355. chkconfig --del shadowsocks
  356. elif check_sys packageManager apt; then
  357. update-rc.d -f shadowsocks remove
  358. fi
  359. rm -f /etc/shadowsocks.json
  360. rm -f /etc/init.d/shadowsocks
  361. rm -f /var/log/shadowsocks.log
  362. rm -rf /usr/local/shadowsocks
  363. echo "ShadowsocksR uninstall success!"
  364. else
  365. echo
  366. echo "uninstall cancelled, nothing to do..."
  367. echo
  368. fi
  369. }
  370.  
  371. # Install ShadowsocksR
  372. install_shadowsocksr(){
  373. disable_selinux
  374. pre_install
  375. download_files
  376. config_shadowsocks
  377. if check_sys packageManager yum; then
  378. firewall_set
  379. fi
  380. install
  381. install_cleanup
  382. }
  383.  
  384. # Initialization step
  385. action=$1
  386. [ -z $1 ] && action=install
  387. case "$action" in
  388. install|uninstall)
  389. ${action}_shadowsocksr
  390. ;;
  391. *)
  392. echo "Arguments error! [${action}]"
  393. echo "Usage: `basename $0` [install|uninstall]"
  394. ;;
  395. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement