Commit e55aaa92c0c3e900da0716be30e4da5778eaad65

Authored by clangcn
1 parent 8135b57e
Exists in master

new install shell

install_ngrok.sh 0 โ†’ 100644
... ... @@ -0,0 +1,289 @@
  1 +#!/bin/bash
  2 +#===============================================================================================
  3 +# System Required: CentOS Debian or Ubuntu (32bit/64bit)
  4 +# Description: Install Ngrok for CentOS Debian or Ubuntu
  5 +# Author: Clang <admin@clangcn.com>
  6 +# Intro: http://clang.cn
  7 +#===============================================================================================
  8 +PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  9 +export PATH
  10 +shell_run_start=`date "+%Y-%m-%d %H:%M:%S"` #shell run start time
  11 +version="V1.0"
  12 +str_ngrok_dir="/usr/local/ngrok"
  13 +# Check if user is root
  14 +function rootness(){
  15 + if [[ $EUID -ne 0 ]]; then
  16 + echo "Error:This script must be run as root!" 1>&2
  17 + exit 1
  18 + fi
  19 +}
  20 +function get_char(){
  21 + SAVEDSTTY=`stty -g`
  22 + stty -echo
  23 + stty cbreak
  24 + dd if=/dev/tty bs=1 count=1 2> /dev/null
  25 + stty -raw
  26 + stty echo
  27 + stty $SAVEDSTTY
  28 +}
  29 +function fun_clangcn.com(){
  30 +echo ""
  31 +echo "#######################################################################"
  32 +echo "# install Ngrok ${version} for Debian/Ubuntu/CentOS Linux Server"
  33 +echo "# Intro: http://clang.cn/blog/"
  34 +echo "#"
  35 +echo "# Author: Clang <admin@clangcn.com>"
  36 +echo "# version:${version}"
  37 +echo "#######################################################################"
  38 +echo ""
  39 +}
  40 +# Check OS
  41 +function checkos(){
  42 + if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
  43 + OS=CentOS
  44 + elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
  45 + OS=Debian
  46 + elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
  47 + OS=Ubuntu
  48 + else
  49 + echo "Not support OS, Please reinstall OS and retry!"
  50 + exit 1
  51 + fi
  52 +}
  53 +# Get version
  54 +function getversion(){
  55 + if [[ -s /etc/redhat-release ]];then
  56 + grep -oE "[0-9.]+" /etc/redhat-release
  57 + else
  58 + grep -oE "[0-9.]+" /etc/issue
  59 + fi
  60 +}
  61 +# CentOS version
  62 +function centosversion(){
  63 + local code=$1
  64 + local version="`getversion`"
  65 + local main_ver=${version%%.*}
  66 + if [ $main_ver == $code ];then
  67 + return 0
  68 + else
  69 + return 1
  70 + fi
  71 +}
  72 +# Check OS bit
  73 +function check_os_bit(){
  74 + if [[ `getconf WORD_BIT` = '32' && `getconf LONG_BIT` = '64' ]] ; then
  75 + Is_64bit='y'
  76 + else
  77 + Is_64bit='n'
  78 + fi
  79 +}
  80 +function check_centosversion(){
  81 +if centosversion 5; then
  82 + echo "Not support CentOS 5.x, please change to CentOS 6,7 or Debian or Ubuntu and try again."
  83 + exit 1
  84 +fi
  85 +}
  86 +# Disable selinux
  87 +function disable_selinux(){
  88 + if [ -s /etc/selinux/config ] && grep 'SELINUX=enforcing' /etc/selinux/config; then
  89 + sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
  90 + setenforce 0
  91 + fi
  92 +}
  93 +function fun_set_ngrok_user_env(){
  94 + str_single_user=""
  95 + echo -e "\033[33mSetting script environment, single-user or multi-user?\033[0m"
  96 + read -p "(single-user please input: y,multi-user input N,Default [no]):" str_single_user
  97 + case "${str_single_user}" in
  98 + y|Y|Yes|YES|yes|yES|yEs|YeS|yeS)
  99 + echo "You will set single-user!"
  100 + str_single_user="y"
  101 + ;;
  102 + n|N|No|NO|no|nO)
  103 + echo "You will set multi-user!"
  104 + str_single_user="n"
  105 + ;;
  106 + *)
  107 + echo "You will set multi-user!"
  108 + str_single_user="n"
  109 + esac
  110 + fun_set_ngrok_domain
  111 +}
  112 +function fun_set_ngrok_domain(){
  113 + # Set ngrok domain
  114 + NGROK_DOMAIN=""
  115 + read -p "Please input domain for Ngrok(e.g.:ngrok.clang.cn):" NGROK_DOMAIN
  116 + check_input
  117 +}
  118 +function fun_randstr(){
  119 + index=0
  120 + strRandomPass=""
  121 + for i in {a..z}; do arr[index]=$i; index=`expr ${index} + 1`; done
  122 + for i in {A..Z}; do arr[index]=$i; index=`expr ${index} + 1`; done
  123 + for i in {0..9}; do arr[index]=$i; index=`expr ${index} + 1`; done
  124 + for i in {1..16}; do strRandomPass="$strRandomPass${arr[$RANDOM%$index]}"; done
  125 + echo $strRandomPass
  126 +}
  127 +function fun_set_ngrok_pass(){
  128 + # Set ngrok pass
  129 + ngrokpass=`fun_randstr`
  130 + read -p "Please input password for Ngrok(Default Password: ${ngrokpass}):" ngrok_pass
  131 + if [ "${ngrok_pass}" = "" ]; then
  132 + ngrok_pass="${ngrokpass}"
  133 + fi
  134 +}
  135 +function check_input(){
  136 + # check ngrok domain
  137 + if [ "$NGROK_DOMAIN" = "" ]; then
  138 + echo -e "Your input is empty,please input again..."
  139 + fun_set_ngrok_domain
  140 + else
  141 + echo -e "Your domain: \033[41;37m "${NGROK_DOMAIN}" \033[0m."
  142 + fun_set_ngrok_pass
  143 + fi
  144 + # check ngrok pass
  145 + if [ "$ngrok_pass" = "" ]; then
  146 + echo -e "Your input is empty,please input again..."
  147 + fun_set_ngrok_pass
  148 + else
  149 + echo -e "Your ngrok pass: \033[41;37m "${ngrok_pass}" \033[0m."
  150 + echo -e "\033[32m \033[05mPress any key to start...or Press Ctrl+c to cancel\033[0m"
  151 + char=`get_char`
  152 + pre_install
  153 + fi
  154 +}
  155 +function pre_install(){
  156 + echo "install ngrok,please wait..."
  157 + if [ "${OS}" == 'CentOS' ]; then
  158 + #yum -y update
  159 + yum -y install unzip nano net-tools zlib-devel openssl-devel perl hg cpio expat-devel gettext-devel curl curl-devel perl-ExtUtils-MakeMaker wget gcc gcc-c++
  160 + else
  161 + apt-get update -y
  162 + apt-get install -y wget build-essential mercurial nano curl openssl libcurl4-openssl-dev
  163 + fi
  164 + mkdir -p ${str_ngrok_dir}
  165 + cd ${str_ngrok_dir}
  166 + # Download shadowsocks chkconfig file
  167 + if [ "${Is_64bit}" == 'y' ] ; then
  168 + if [ ! -s ${str_ngrok_dir}/bin/ngrokd ]; then
  169 + if ! wget --no-check-certificate https://github.com/clangcn/ngrok-one-key-install/raw/master/ngrokd/ngrokd.x86_64 -O ${str_ngrok_dir}/bin/ngrokd; then
  170 + echo "Failed to download ngrokd.x86_64 file!"
  171 + exit 1
  172 + fi
  173 + fi
  174 + else
  175 + if [ ! -s ${str_ngrok_dir}/bin/ngrokd ]; then
  176 + if ! wget --no-check-certificate https://github.com/clangcn/ngrok-one-key-install/raw/master/ngrokd/ngrokd.x86 -O ${str_ngrok_dir}/bin/ngrokd; then
  177 + echo "Failed to download ngrokd.x86 file!"
  178 + exit 1
  179 + fi
  180 + fi
  181 + fi
  182 + [ ! -x ${str_ngrok_dir}/bin/ngrokd ] && chmod 755 ${str_ngrok_dir}/bin/ngrokd
  183 + cd /usr/local/ngrok
  184 + openssl genrsa -out rootCA.key 2048
  185 + openssl req -x509 -new -nodes -key rootCA.key -subj "/CN=$NGROK_DOMAIN" -days 5000 -out rootCA.pem
  186 + openssl genrsa -out server.key 2048
  187 + openssl req -new -key server.key -subj "/CN=$NGROK_DOMAIN" -out server.csr
  188 + openssl x509 -req -in server.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out server.crt -days 5000
  189 +
  190 + if [ -s ${str_ngrok_dir}/bin/ngrokd ]; then
  191 + config_runshell_ngrok
  192 + clear
  193 + fun_clangcn.com
  194 + echo "Install Ngrok completed! enjoy it."
  195 + echo "========================================================================="
  196 + echo "On key install Ngrok ${version} for Debian/Ubuntu/CentOS Linux Server"
  197 + echo "========================================================================="
  198 + echo ""
  199 + echo "For more information please visit http://clang.cn/"
  200 + echo ""
  201 + echo -e "ngrok status manage: \033[45;37m/etc/init.d/ngrokd\033[0m {\033[40;31mstart\033[0m|\033[40;32mstop\033[0m|\033[40;33mrestart\033[0m|\033[40;34mconfig\033[0m|\033[40;35madduser\033[0m|\033[40;36minfo\033[0m}"
  202 + echo -e "Your Domain: \033[32m\033[01m${NGROK_DOMAIN}\033[0m"
  203 + echo -e "Ngrok password: \033[32m\033[01m${ngrok_pass}\033[0m"
  204 + echo -e "http_port: \033[32m\033[01m80\033[0m"
  205 + echo -e "https_port: \033[32m\033[01m443\033[0m"
  206 + echo -e "remote_port: \033[32m\033[01m4443\033[0m"
  207 + echo -e "Config file: \033[32m\033[01m${str_ngrok_dir}/.ngrok_config.sh\033[0m"
  208 + echo ""
  209 + echo "========================================================================="
  210 + else
  211 + echo ""
  212 + echo "Sorry,Failed to install Ngrok!"
  213 + echo "You can download /root/ngrok_install.log from your server,and mail ngrok_install.log to me."
  214 + exit 1
  215 + fi
  216 +
  217 + shell_run_end=`date "+%Y-%m-%d %H:%M:%S"` #shell run end time
  218 + time_distance=$(expr $(date +%s -d "$shell_run_end") - $(date +%s -d "$shell_run_start"));
  219 + hour_distance=$(expr ${time_distance} / 3600) ;
  220 + hour_remainder=$(expr ${time_distance} % 3600) ;
  221 + min_distance=$(expr ${hour_remainder} / 60) ;
  222 + min_remainder=$(expr ${hour_remainder} % 60) ;
  223 + echo -e "Shell run time is \033[32m \033[01m${hour_distance} hour ${min_distance} min ${min_remainder} sec\033[0m"
  224 +}
  225 +function config_runshell_ngrok(){
  226 +if [ "${str_single_user}" == 'y' ] ; then
  227 +cat > ${str_ngrok_dir}/.ngrok_config.sh <<EOF
  228 +#!/bin/bash
  229 +# -------------config START-------------
  230 +dns="${NGROK_DOMAIN}"
  231 +pass="${ngrok_pass}"
  232 +http_port=80
  233 +https_port=443
  234 +remote_port=4443
  235 +srtCRT=server.crt
  236 +strKey=server.key
  237 +loglevel="INFO"
  238 +SingleUser="y"
  239 +# -------------config END-------------
  240 +EOF
  241 +else
  242 +cat > ${str_ngrok_dir}/.ngrok_config.sh <<EOF
  243 +#!/bin/bash
  244 +# -------------config START-------------
  245 +dns="${NGROK_DOMAIN}"
  246 +pass="${ngrok_pass}"
  247 +http_port=80
  248 +https_port=443
  249 +remote_port=4443
  250 +srtCRT=server.crt
  251 +strKey=server.key
  252 +loglevel="INFO"
  253 +SingleUser="n"
  254 +# -------------config END-------------
  255 +EOF
  256 +fi
  257 +
  258 +if ! wget --no-check-certificate https://github.com/clangcn/ngrok-one-key-install/raw/master/ngrokd.init -O /etc/init.d/ngrokd; then
  259 + echo "Failed to download ngrokd.init file!"
  260 + exit 1
  261 +fi
  262 +[ ! -x ${str_ngrok_dir}/.ngrok_config.sh ] && chmod 500 ${str_ngrok_dir}/.ngrok_config.sh
  263 +[ ! -x /etc/init.d/ngrokd ] && chmod 755 /etc/init.d/ngrokd
  264 +if [ "${OS}" == 'CentOS' ]; then
  265 + if [ -s /etc/init.d/ngrokd ]; then
  266 + chmod +x /etc/init.d/ngrokd
  267 + chkconfig --add ngrokd
  268 + fi
  269 +else
  270 + if [ -s /etc/init.d/ngrokd ]; then
  271 + chmod +x /etc/init.d/ngrokd
  272 + update-rc.d -f ngrokd defaults
  273 + fi
  274 +fi
  275 +/etc/init.d/ngrokd start
  276 +}
  277 +function fun_install_ngrok(){
  278 + checkos
  279 + check_centosversion
  280 + check_os_bit
  281 + disable_selinux
  282 + fun_set_ngrok_user_env
  283 +}
  284 +clear
  285 +fun_clangcn.com
  286 +rootness
  287 +rm -f /root/ngrok_install.log
  288 +fun_install_ngrok 2>&1 | tee /root/ngrok_install.log
  289 +exit 0
... ...
ngrokd/ngrokd.x86 0 โ†’ 100755
No preview for this file type
ngrokd/ngrokd.x86_64 0 โ†’ 100755
No preview for this file type