欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

脚本学习 1

程序员文章站 2022-06-03 18:58:38
!/usr/bin/env bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH =============================================== ......

#!/usr/bin/env bash
path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export path
#=================================================================#
#   system required:  centos, debian, ubuntu                      #
#   description: one click install ***-go server          #
#   author: teddysun <i@teddysun.com>                             #
#   thanks: @cyfdecyf <https://twitter.com/cyfdecyf>              #
#   intro:  https://teddysun.com/392.html                         #
#==================================================================

clear
echo
echo "#############################################################"
echo "# one click install ***-go server                   #"
echo "# intro: https://teddysun.com/392.html                      #"
echo "# author: teddysun <i@teddysun.com>                         #"
echo "# github: https://github.com/*/*-go     #"
echo "#############################################################"
echo

# current folder
cur_dir=`pwd`
# stream ciphers
ciphers=(
aes-256-cfb
aes-192-cfb
aes-128-cfb
aes-256-ctr
aes-192-ctr
aes-128-ctr
chacha20-ietf
chacha20
salsa20
rc4-md5
)
# color
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'

# make sure only root can run our script
[[ $euid -ne 0 ]] && echo -e "[${red}error${plain}] this script must be run as root!" && exit 1

#check system
check_sys(){
    local checktype=$1
    local value=$2

    local release=''
    local systempackage=''

    if [[ -f /etc/redhat-release ]]; then
        release="centos"
        systempackage="yum"
    elif grep -eqi "debian" /etc/issue; then
        release="debian"
        systempackage="apt"
    elif grep -eqi "ubuntu" /etc/issue; then
        release="ubuntu"
        systempackage="apt"
    elif grep -eqi "centos|red hat|redhat" /etc/issue; then
        release="centos"
        systempackage="yum"
    elif grep -eqi "debian|raspbian" /proc/version; then
        release="debian"
        systempackage="apt"
    elif grep -eqi "ubuntu" /proc/version; then
        release="ubuntu"
        systempackage="apt"
    elif grep -eqi "centos|red hat|redhat" /proc/version; then
        release="centos"
        systempackage="yum"
    fi

    if [[ "${checktype}" == "sysrelease" ]]; then
        if [ "${value}" == "${release}" ]; then
            return 0
        else
            return 1
        fi
    elif [[ "${checktype}" == "packagemanager" ]]; then
        if [ "${value}" == "${systempackage}" ]; then
            return 0
        else
            return 1
        fi
    fi
}

# get version
getversion(){
    if [[ -s /etc/redhat-release ]]; then
        grep -oe  "[0-9.]+" /etc/redhat-release
    else
        grep -oe  "[0-9.]+" /etc/issue
    fi
}

# centos version
centosversion(){
    if check_sys sysrelease centos; then
        local code=$1
        local version="$(getversion)"
        local main_ver=${version%%.*}
        if [ "$main_ver" == "$code" ]; then
            return 0
        else
            return 1
        fi
    else
        return 1
    fi
}

# is 64bit or not
is_64bit(){
    if [ `getconf word_bit` = '32' ] && [ `getconf long_bit` = '64' ] ; then
        return 0
    else
        return 1
    fi
}

# disable selinux
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
}

get_ip(){
    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 )
    [ -z ${ip} ] && ip=$( wget -qo- -t1 -t2 ipv4.icanhazip.com )
    [ -z ${ip} ] && ip=$( wget -qo- -t1 -t2 ipinfo.io/ip )
    [ ! -z ${ip} ] && echo ${ip} || echo
}

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
}

# pre-installation settings
pre_install(){
    if ! check_sys packagemanager yum && ! check_sys packagemanager apt; then
        echo -e "$[{red}error${plain}] your os is not supported. please change os to centos/debian/ubuntu and try again."
        exit 1
    fi
    # set *-go config password
    echo "please enter password for *-go:"
    read -p "(default password: teddysun.com):" *pwd
    [ -z "${*pwd}" ] && *pwd="teddysun.com"
    echo
    echo "---------------------------"
    echo "password = ${*pwd}"
    echo "---------------------------"
    echo
    # set *-go config port
    while true
    do
    dport=$(shuf -i 9000-19999 -n 1)
    echo -e "please enter a port for *-go [1-65535]"
    read -p "(default port: ${dport}):" *port
    [ -z "${*port}" ] && *port=${dport}
    expr ${*port} + 1 &>/dev/null
    if [ $? -eq 0 ]; then
        if [ ${*port} -ge 1 ] && [ ${*port} -le 65535 ] && [ ${*port:0:1} != 0 ]; then
            echo
            echo "---------------------------"
            echo "port = ${*port}"
            echo "---------------------------"
            echo
            break
        fi
    fi
    echo -e "[${red}error${plain}] please enter a correct number [1-65535]"
    done

    # set * config stream ciphers
    while true
    do
    echo -e "please select stream cipher for *-go:"
    for ((i=1;i<=${#ciphers[@]};i++ )); do
        hint="${ciphers[$i-1]}"
        echo -e "${green}${i}${plain}) ${hint}"
    done
    read -p "which cipher you'd select(default: ${ciphers[0]}):" pick
    [ -z "$pick" ] && pick=1
    expr ${pick} + 1 &>/dev/null
    if [ $? -ne 0 ]; then
        echo -e "[${red}error${plain}] please enter a number"
        continue
    fi
    if [[ "$pick" -lt 1 || "$pick" -gt ${#ciphers[@]} ]]; then
        echo -e "[${red}error${plain}] please enter a number between 1 and ${#ciphers[@]}"
        continue
    fi
    *cipher=${ciphers[$pick-1]}
    echo
    echo "---------------------------"
    echo "cipher = ${*cipher}"
    echo "---------------------------"
    echo
    break
    done

    echo
    echo "press any key to start...or press ctrl+c to cancel"
    char=`get_char`
    #install necessary dependencies
    if check_sys packagemanager yum; then
        yum install -y wget unzip gzip curl nss
    elif check_sys packagemanager apt; then
        apt-get -y update
        apt-get install -y wget unzip gzip curl libnss3
    fi
    echo

}

# download *-go
download_files(){
    cd ${cur_dir}
    if is_64bit; then
        if ! wget --no-check-certificate -c https://dl.lamp.sh/*/*-server-linux64-1.2.2.gz; then
            echo -e "[${red}error${plain}] failed to download *-server-linux64-1.2.2.gz"
            exit 1
        fi
        gzip -d *-server-linux64-1.2.2.gz
        if [ $? -ne 0 ]; then
            echo -e "[${red}error${plain}] decompress *-server-linux64-1.2.2.gz failed"
            exit 1
        fi
        mv -f *-server-linux64-1.2.2 /usr/bin/*-server
    else
        if ! wget --no-check-certificate -c https://dl.lamp.sh/*/*-server-linux32-1.2.2.gz; then
            echo -e "[${red}error${plain}] failed to download *-server-linux32-1.2.2.gz"
            exit 1
        fi
        gzip -d *-server-linux32-1.2.2.gz
        if [ $? -ne 0 ]; then
            echo -e "[${red}error${plain}] decompress *-server-linux32-1.2.2.gz failed"
            exit 1
        fi
        mv -f *-server-linux32-1.2.2 /usr/bin/*-server
    fi

    # download start script
    if check_sys packagemanager yum; then
        if ! wget --no-check-certificate -o /etc/init.d/* https://raw.githubusercontent.com/teddysun/*_install/master/*-go; then
            echo -e "[${red}error${plain}] failed to download *-go auto start script!"
            exit 1
        fi
    elif check_sys packagemanager apt; then
        if ! wget --no-check-certificate -o /etc/init.d/* https://raw.githubusercontent.com/teddysun/*_install/master/*-go-debian; then
            echo -e "[${red}error${plain}] failed to download *-go auto start script!"
            exit 1
        fi
    fi
}

# config *
config_*(){
    if [ ! -d /etc/* ]; then
        mkdir -p /etc/*
    fi
    cat > /etc/*/config.json<<-eof
{
    "server":"0.0.0.0",
    "server_port":${*port},
    "local_port":1080,
    "password":"${*pwd}",
    "method":"${*cipher}",
    "timeout":300
}
eof
}

# firewall set
firewall_set(){
    echo -e "[${green}info${plain}] firewall set start..."
    if centosversion 6; then
        /etc/init.d/iptables status > /dev/null 2>&1
        if [ $? -eq 0 ]; then
            iptables -l -n | grep -i ${*port} > /dev/null 2>&1
            if [ $? -ne 0 ]; then
                iptables -i input -m state --state new -m tcp -p tcp --dport ${*port} -j accept
                iptables -i input -m state --state new -m udp -p udp --dport ${*port} -j accept
                /etc/init.d/iptables save
                /etc/init.d/iptables restart
            else
                echo -e "[${green}info${plain}] port ${*port} has been set up."
            fi
        else
            echo -e "[${yellow}warning${plain}] iptables looks like shutdown or not installed, please manually set it if necessary."
        fi
    elif centosversion 7; then
        systemctl status firewalld > /dev/null 2>&1
        if [ $? -eq 0 ]; then
            default_zone=$(firewall-cmd --get-default-zone)
            firewall-cmd --permanent --zone=${default_zone} --add-port=${*port}/tcp
            firewall-cmd --permanent --zone=${default_zone} --add-port=${*port}/udp
            firewall-cmd --reload
        else
            echo -e "[${yellow}warning${plain}] firewalld looks like not running or not installed, please enable port ${*port} manually if necessary."
        fi
    fi
    echo -e "[${green}info${plain}] firewall set completed..."
}

# install ***-go
install(){

    if [ -f /usr/bin/*-server ]; then
        echo "***-go server install success!"
        chmod +x /usr/bin/*-server
        chmod +x /etc/init.d/*

        if check_sys packagemanager yum; then
            chkconfig --add *
            chkconfig * on
        elif check_sys packagemanager apt; then
            update-rc.d -f * defaults
        fi

        /etc/init.d/* start
        if [ $? -ne 0 ]; then
            echo -e "[${red}error${plain}] ***-go server start failed!"
        fi
    else
        echo
        echo -e "[${red}error${plain}] ***-go server install failed!"
        exit 1
    fi

    clear
    echo
    echo -e "congratulations, ***-go server install completed!"
    echo -e "your server ip        : \033[41;37m $(get_ip) \033[0m"
    echo -e "your server port      : \033[41;37m ${*port} \033[0m"
    echo -e "your password         : \033[41;37m ${*pwd} \033[0m"
    echo -e "your encryption method: \033[41;37m ${*cipher} \033[0m"
    echo
    echo "welcome to visit:https://teddysun.com/392.html"
    echo "enjoy it!"
    echo
}

# uninstall ***-go
uninstall_*_go(){
    printf "are you sure uninstall *-go? (y/n) "
    printf "\n"
    read -p "(default: n):" answer
    [ -z ${answer} ] && answer="n"
    if [ "${answer}" == "y" ] || [ "${answer}" == "y" ]; then
        ps -ef | grep -v grep | grep -i "*-server" > /dev/null 2>&1
        if [ $? -eq 0 ]; then
            /etc/init.d/* stop
        fi
        if check_sys packagemanager yum; then
            chkconfig --del *
        elif check_sys packagemanager apt; then
            update-rc.d -f * remove
        fi
        # delete config file
        rm -rf /etc/*
        # delete *
        rm -f /etc/init.d/*
        rm -f /usr/bin/*-server
        echo "***-go uninstall success!"
    else
        echo
        echo "uninstall cancelled, nothing to do..."
        echo
    fi
}

# install ***-go
install_*_go(){
    disable_selinux
    pre_install
    download_files
    config_*
    if check_sys packagemanager yum; then
        firewall_set
    fi
    install
}

# initialization step
action=$1
[ -z $1 ] && action=install
case "$action" in
    install|uninstall)
        ${action}_*_go
        ;;
    *)
        echo "arguments error! [${action}]"
        echo "usage: `basename $0` [install|uninstall]"
        ;;
esac