Ngrok服务器一键安装脚本【支持用户管理】(穿透DDNS)

作者:袖梨 2022-06-30

内网穿透动态域名解析服务(简称NAT-DDNS),是一种基于动态域名服务(DDNS)和网络地址转换(NAT)的服务器内外网动态映射方法。
帮助用户实现在内网IP地址环境下也能实现动态域名解析服务,使动态IP地址实时与服务器保持交流,绑定固定域名,让上网用户输入特定域名即可访问内网主机,轻松实现用自己的主机架设WEB/FTP/OA/ERP/CRM等服务器,还可实现远程管理、远程存取等功能。
在此非常感谢koolshare的宝大对ngrok进行的二次开发。

脚本是业余爱好,英文属于文盲,写的不好,root帐号登录你的vps执行下面命令:建议先安装screen。

安装命令执行:

安装命令Shell


wget --no-check-certificate https://github.com/clangcn/ngrok-one-key-install/raw/master/ngrok_install.sh
chmod 500 ./ngrok_install.sh
./ngrok_install.sh


————————-分割线————————-

安装脚本源码:


安装脚本源码Shell


#!/bin/bash
#===============================================================================================
#   System Required:  CentOS Debian or Ubuntu (32bit/64bit)
#   Description:  Install Ngrok for CentOS Debian or Ubuntu
#   Author: Clang
#   Intro:  http://clang.cn
#===============================================================================================
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
shell_run_start=`date "+%Y-%m-%d %H:%M:%S"`   #shell run start time
version="V5.0"
str_ngrok_dir="/usr/local/ngrok"
# Check if user is root
function rootness(){
    if [[ $EUID -ne 0 ]]; then
       echo "Error:This script must be run as root!" 1>&2
       exit 1
    fi
}
function get_char(){
    SAVEDSTTY=`stty -g`
    stty -echo
    stty cbreak
    dd if=/dev/tty bs=1 count=1 2> /dev/null
    stty -raw
    stty echo
    stty $SAVEDSTTY
}
function fun_clangcn.com(){
echo ""
echo "#######################################################################"
echo "# install Ngrok ${version} for Debian/Ubuntu/CentOS Linux Server"
echo "# Intro: http://clang.cn/blog/"
echo "#"
echo "# Author: Clang "
echo "# version:${version}"
echo "#######################################################################"
echo ""
}
# Check OS
function checkos(){
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
        OS=CentOS
    elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
        OS=Debian
    elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
        OS=Ubuntu
    else
        echo "Not support OS, Please reinstall OS and retry!"
        exit 1
    fi
}
# Get version
function getversion(){
    if [[ -s /etc/redhat-release ]];then
        grep -oE  "[0-9.]+" /etc/redhat-release
    else   
        grep -oE  "[0-9.]+" /etc/issue
    fi   
}
# CentOS version
function centosversion(){
    local code=$1
    local version="`getversion`"
    local main_ver=${version%%.*}
    if [ $main_ver == $code ];then
        return 0
    else
        return 1
    fi       
}
# Check OS bit
function check_os_bit(){
    if [[ `getconf WORD_BIT` = '32' && `getconf LONG_BIT` = '64' ]] ; then
        Is_64bit='y'
    else
        Is_64bit='n'
    fi
}
function check_centosversion(){
if centosversion 5; then
    echo "Not support CentOS 5.x, please change to CentOS 6,7 or Debian or Ubuntu and try again."
    exit 1
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
}
function fun_set_ngrok_domain(){
    # Set ngrok domain
    NGROK_DOMAIN=""
    read -p "Please input domain for Ngrok(e.g.:ngrok.clang.cn):" NGROK_DOMAIN
    check_input
}
function fun_randstr(){
  index=0
  strRandomPass=""
  for i in {a..z}; do arr[index]=$i; index=`expr ${index} + 1`; done
  for i in {A..Z}; do arr[index]=$i; index=`expr ${index} + 1`; done
  for i in {0..9}; do arr[index]=$i; index=`expr ${index} + 1`; done
  for i in {1..16}; do strRandomPass="$strRandomPass${arr[$RANDOM%$index]}"; done
  echo $strRandomPass
}
function fun_set_ngrok_pass(){
    # Set ngrok pass
    ngrokpass=`fun_randstr`
    read -p "Please input password for Ngrok(Default Password: ${ngrokpass}):" ngrok_pass
    if [ "${ngrok_pass}" = "" ]; then
        ngrok_pass="${ngrokpass}"
    fi
}
function check_input(){
    # check ngrok domain
    if [ "$NGROK_DOMAIN" = "" ]; then
        echo -e "Your input is empty,please input again..."
        fun_set_ngrok_domain
    else
        echo -e "Your domain: 33[41;37m "${NGROK_DOMAIN}" 33[0m."
        fun_set_ngrok_pass
    fi
    # check ngrok pass
    if [ "$ngrok_pass" = "" ]; then
        echo -e "Your input is empty,please input again..."
        fun_set_ngrok_pass
    else
        echo -e "Your ngrok pass: 33[41;37m "${ngrok_pass}" 33[0m."
        echo -e "33[32m 33[05mPress any key to start...or Press Ctrl+c to cancel33[0m"
        char=`get_char`
        pre_install
    fi
}
function config_runshell_ngrok(){
cat > ${str_ngrok_dir}/.ngrok_config.sh < #!/bin/bash
# -------------config START-------------
dns="${NGROK_DOMAIN}"
pass="${ngrok_pass}"
http_port=80
https_port=443
remote_port=4443
srtCRT=server.crt
strKey=server.key
loglevel="INFO"
# -------------config END-------------
EOF
 
if [ ! -s /etc/init.d/ngrokd ]; then
    if ! wget --no-check-certificate https://github.com/clangcn/ngrok-one-key-install/raw/master/ngrokd.init -O /etc/init.d/ngrokd; then
        echo "Failed to download ngrokd.init file!"
        exit 1
    fi
fi
[ ! -x ${str_ngrok_dir}/.ngrok_config.sh ] && chmod 500 ${str_ngrok_dir}/.ngrok_config.sh
[ ! -x /etc/init.d/ngrokd ] && chmod 755 /etc/init.d/ngrokd
if [ "${OS}" == 'CentOS' ]; then
    if [ -s /etc/init.d/ngrokd ]; then
        chmod +x /etc/init.d/ngrokd
        chkconfig --add ngrokd
    fi
else
    if [ -s /etc/init.d/ngrokd ]; then
        chmod +x /etc/init.d/ngrokd
        update-rc.d -f ngrokd defaults
    fi
fi
 
}
function fun_install_ngrok(){
    checkos
    check_centosversion
    check_os_bit
    disable_selinux
    fun_set_ngrok_domain
}
function pre_install(){
    echo "install ngrok,please wait..."
    if [ "${OS}" == 'CentOS' ]; then
        #yum -y update
        yum -y install unzip nano screen git net-tools zlib-devel openssl-devel perl hg cpio expat-devel gettext-devel curl curl-devel perl-ExtUtils-MakeMaker wget gcc gcc-c++
    else
        apt-get update -y
        apt-get install -y wget build-essential mercurial git nano screen curl openssl libcurl4-openssl-dev
    fi
    cd /root/
    # Download shadowsocks chkconfig file
    if [ "${Is_64bit}" == 'y' ] ; then
        if [ ! -s /root/go1.6.linux-amd64.tar.gz ]; then
            if ! wget --no-check-certificate https://github.com/clangcn/golang/raw/master/go1.6/go1.6.linux-amd64.tar.gz; then
                echo "Failed to download go1.6.linux-amd64.tar.gz file!"
                exit 1
            fi
        fi
        tar zxvf go1.6.linux-amd64.tar.gz
    else
         if [ ! -s /root/go1.6.linux-386.tar.gz ]; then
            if ! wget --no-check-certificate https://github.com/clangcn/golang/raw/master/go1.6/go1.6.linux-386.tar.gz; then
                echo "Failed to download go1.6.linux-386.tar.gz file!"
                exit 1
            fi
        fi
        tar zxvf go1.6.linux-386.tar.gz
    fi
    mv go/ /usr/local/
    rm -f /usr/bin/go /usr/bin/godoc /usr/bin/gofmt
    ln -s /usr/local/go/bin/* /usr/bin/
    go version
    cd /usr/local/
    git clone https://github.com/clangcn/ngrok-1.7.git ngrok
    export GOPATH=/usr/local/ngrok/
    cd ngrok
    openssl genrsa -out rootCA.key 2048
    openssl req -x509 -new -nodes -key rootCA.key -subj "/CN=$NGROK_DOMAIN" -days 5000 -out rootCA.pem
    openssl genrsa -out server.key 2048
    openssl req -new -key server.key -subj "/CN=$NGROK_DOMAIN" -out server.csr
    openssl x509 -req -in server.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out server.crt -days 5000
    rm -f assets/client/tls/ngrokroot.crt assets/server/tls/snakeoil.crt assets/server/tls/snakeoil.key
    cp rootCA.pem assets/client/tls/ngrokroot.crt
    cp server.crt assets/server/tls/snakeoil.crt
    cp server.key assets/server/tls/snakeoil.key
    if [ "${Is_64bit}" == 'y' ] ; then
        cd /usr/local/go/src
        GOOS=linux GOARCH=amd64 ./make.bash
        cd /usr/local/ngrok/
        GOOS=linux GOARCH=amd64 make release-server
    else
        cd /usr/local/go/src
        GOOS=linux GOARCH=386 ./make.bash
        cd /usr/local/ngrok/
        GOOS=linux GOARCH=386 make release-server
    fi
    if [ -s /usr/local/ngrok/bin/ngrokd ]; then
        config_runshell_ngrok
        clear
        fun_clangcn.com
        echo "Install Ngrok completed! enjoy it."
        echo "========================================================================="
        echo "On key install Ngrok ${version} for Debian/Ubuntu/CentOS Linux Server"
        echo "========================================================================="
        echo ""
        echo "For more information please visit http://clang.cn/"
        echo ""
        echo -e "ngrok status manage: 33[45;37m/etc/init.d/ngrokd33[0m {33[40;31mstart33[0m|33[40;32mstop33[0m|33[40;33mrestart33[0m|33[40;34mconfig33[0m|33[40;35madduser33[0m|33[40;36minfo33[0m}"
        echo -e "Your Domain: 33[32m33[01m${NGROK_DOMAIN}33[0m"
        echo -e "Ngrok password: 33[32m33[01m${ngrok_pass}33[0m"
        echo -e "http_port: 33[32m33[01m8033[0m"
        echo -e "https_port: 33[32m33[01m44333[0m"
        echo -e "remote_port: 33[32m33[01m444333[0m"
        echo -e "Config file:   33[32m33[01m${str_ngrok_dir}/.ngrok_config.sh33[0m"
        echo ""
        echo "========================================================================="
    else
        echo ""
        echo "Sorry,Failed to install Ngrok!"
        echo "You can download /root/ngrok_install.log from your server,and mail ngrok_install.log to me."
        exit 1
    fi
 
    shell_run_end=`date "+%Y-%m-%d %H:%M:%S"`   #shell run end time
    time_distance=$(expr $(date +%s -d "$shell_run_end") - $(date +%s -d "$shell_run_start"));
    hour_distance=$(expr ${time_distance} / 3600) ;
    hour_remainder=$(expr ${time_distance} % 3600) ;
    min_distance=$(expr ${hour_remainder} / 60) ;
    min_remainder=$(expr ${hour_remainder} % 60) ;
    echo -e "Shell run time is 33[32m 33[01m${hour_distance} hour ${min_distance} min ${min_remainder} sec33[0m"
}
clear
fun_clangcn.com
rootness
rm -f /root/ngrok_install.log
fun_install_ngrok 2>&1 | tee /root/ngrok_install.log
exit 0

相关文章

精彩推荐