Guest User

shadowsocks-go

a guest
Feb 21st, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 25.36 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. #=================================================
  6. #   System Required: CentOS/Debian/Ubuntu
  7. #   Description: Shadowsocks Golang
  8. #   Version: 1.0.0
  9. #   Author: Toyo
  10. #   Blog: https://doub.io/ss-jc67/
  11. #=================================================
  12.  
  13. sh_ver="1.0.0"
  14. filepath=$(cd "$(dirname "$0")"; pwd)
  15. file_1=$(echo -e "${filepath}"|awk -F "$0" '{print $1}')
  16. FOLDER="/usr/local/shadowsocks-go"
  17. FILE="/usr/local/shadowsocks-go/shadowsocks-go"
  18. CONF="/usr/local/shadowsocks-go/shadowsocks-go.conf"
  19. LOG="/usr/local/shadowsocks-go/shadowsocks-go.log"
  20. Now_ver_File="/usr/local/shadowsocks-go/ver.txt"
  21. Crontab_file="/usr/bin/crontab"
  22.  
  23. Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m"
  24. Info="${Green_font_prefix}[信息]${Font_color_suffix}"
  25. Error="${Red_font_prefix}[错误]${Font_color_suffix}"
  26. Tip="${Green_font_prefix}[注意]${Font_color_suffix}"
  27.  
  28. check_root(){
  29.     [[ $EUID != 0 ]] && echo -e "${Error} 当前非ROOT账号(或没有ROOT权限),无法继续操作,请更换ROOT账号或使用 ${Green_background_prefix}sudo su${Font_color_suffix} 命令获取临时ROOT权限(执行后可能会提示输入当前账号的密码)。" && exit 1
  30. }
  31. #检查系统
  32. check_sys(){
  33.     if [[ -f /etc/redhat-release ]]; then
  34.         release="centos"
  35.     elif cat /etc/issue | grep -q -E -i "debian"; then
  36.         release="debian"
  37.     elif cat /etc/issue | grep -q -E -i "ubuntu"; then
  38.         release="ubuntu"
  39.     elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then
  40.         release="centos"
  41.     elif cat /proc/version | grep -q -E -i "debian"; then
  42.         release="debian"
  43.     elif cat /proc/version | grep -q -E -i "ubuntu"; then
  44.         release="ubuntu"
  45.     elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; then
  46.         release="centos"
  47.     fi
  48.     bit=`uname -m`
  49. }
  50. check_installed_status(){
  51.     [[ ! -e ${FILE} ]] && echo -e "${Error} Shadowsocks 没有安装,请检查 !" && exit 1
  52. }
  53. check_crontab_installed_status(){
  54.     if [[ ! -e ${Crontab_file} ]]; then
  55.         echo -e "${Error} Crontab 没有安装,开始安装..."
  56.         if [[ ${release} == "centos" ]]; then
  57.             yum install crond -y
  58.         else
  59.             apt-get install cron -y
  60.         fi
  61.         if [[ ! -e ${Crontab_file} ]]; then
  62.             echo -e "${Error} Crontab 安装失败,请检查!" && exit 1
  63.         else
  64.             echo -e "${Info} Crontab 安装成功!"
  65.         fi
  66.     fi
  67. }
  68. check_pid(){
  69.     PID=$(ps -ef| grep "./shadowsocks-go "| grep -v "grep" | grep -v "init.d" |grep -v "service" |awk '{print $2}')
  70. }
  71. check_new_ver(){
  72.     new_ver=$(wget -qO- https://api.github.com/repos/shadowsocks/go-shadowsocks2/releases| grep "tag_name"| head -n 1| awk -F ":" '{print $2}'| sed 's/\"//g;s/,//g;s/ //g')
  73.     [[ -z ${new_ver} ]] && echo -e "${Error} Shadowsocks 最新版本获取失败!" && exit 1
  74.     echo -e "${Info} 检测到 Shadowsocks 最新版本为 [ ${new_ver} ]"
  75. }
  76. check_ver_comparison(){
  77.     now_ver=$(cat ${Now_ver_File})
  78.     if [[ "${now_ver}" != "${new_ver}" ]]; then
  79.         echo -e "${Info} 发现 Shadowsocks 已有新版本 [ ${new_ver} ],旧版本 [ ${now_ver} ]"
  80.         read -e -p "是否更新 ? [Y/n] :" yn
  81.         [[ -z "${yn}" ]] && yn="y"
  82.         if [[ $yn == [Yy] ]]; then
  83.             check_pid
  84.             [[ ! -z $PID ]] && kill -9 ${PID}
  85.             \cp "${CONF}" "/tmp/shadowsocks-go.conf"
  86.             rm -rf ${FOLDER}
  87.             Download
  88.             mv "/tmp/shadowsocks-go.conf" "${CONF}"
  89.             Start
  90.         fi
  91.     else
  92.         echo -e "${Info} 当前 Shadowsocks 已是最新版本 [ ${new_ver} ]" && exit 1
  93.     fi
  94. }
  95. Download(){
  96.     if [[ ! -e "${FOLDER}" ]]; then
  97.         mkdir "${FOLDER}"
  98.     else
  99.         [[ -e "${FILE}" ]] && rm -rf "${FILE}"
  100.     fi
  101.     cd "${FOLDER}"
  102.     if [[ ${bit} == "x86_64" ]]; then
  103.         wget --no-check-certificate -N "https://github.com/shadowsocks/go-shadowsocks2/releases/download/v0.0.11/shadowsocks2-linux.gz"
  104.     else
  105.         echo -e "${Error} Shadowsocks-Go版目前不支持 非64位 构架的服务器安装,请更换系统 !" && rm -rf "${FOLDER}" && exit 1
  106.     fi
  107.     [[ ! -e "shadowsocks2-linux.gz" ]] && echo -e "${Error} Shadowsocks 压缩包下载失败 !" && rm -rf "${FOLDER}" && exit 1
  108.     gzip -d "shadowsocks2-linux.gz"
  109.     [[ ! -e "shadowsocks2-linux" ]] && echo -e "${Error} Shadowsocks 压缩包解压失败 !" && rm -rf "${FOLDER}" && exit 1
  110.     mv "shadowsocks2-linux" "shadowsocks-go"
  111.     [[ ! -e "shadowsocks-go" ]] && echo -e "${Error} Shadowsocks 重命名失败 !" && rm -rf "${FOLDER}" && exit 1
  112.     chmod +x shadowsocks-go
  113.     echo "${new_ver}" > ${Now_ver_File}
  114. }
  115. Service(){
  116.     if [[ ${release} = "centos" ]]; then
  117.         if ! wget --no-check-certificate "https://raw.githubusercontent.com/ToyoDAdoubiBackup/doubi/master/service/ss_go_centos" -O /etc/init.d/ss-go; then
  118.             echo -e "${Error} Shadowsocks 服务管理脚本下载失败 !"
  119.             rm -rf "${FOLDER}"
  120.             exit 1
  121.         fi
  122.         chmod +x "/etc/init.d/ss-go"
  123.         chkconfig --add ss-go
  124.         chkconfig ss-go on
  125.     else
  126.         if ! wget --no-check-certificate "https://raw.githubusercontent.com/ToyoDAdoubiBackup/doubi/master/service/ss_go_debian" -O /etc/init.d/ss-go; then
  127.             echo -e "${Error} Shadowsocks 服务管理脚本下载失败 !"
  128.             rm -rf "${FOLDER}"
  129.             exit 1
  130.         fi
  131.         chmod +x "/etc/init.d/ss-go"
  132.         update-rc.d -f ss-go defaults
  133.     fi
  134.     echo -e "${Info} Shadowsocks 服务管理脚本下载完成 !"
  135. }
  136. Installation_dependency(){
  137.     gzip_ver=$(gzip -V)
  138.     if [[ -z ${gzip_ver} ]]; then
  139.         if [[ ${release} == "centos" ]]; then
  140.             yum update
  141.             yum install -y gzip
  142.         else
  143.             apt-get update
  144.             apt-get install -y gzip
  145.         fi
  146.     fi
  147.     \cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  148. }
  149. Write_config(){
  150.     cat > ${CONF}<<-EOF
  151. PORT = ${ss_port}
  152. PASSWORD = ${ss_password}
  153. CIPHER = ${ss_cipher}
  154. VERBOSE = ${ss_verbose}
  155. EOF
  156. }
  157. Read_config(){
  158.     [[ ! -e ${CONF} ]] && echo -e "${Error} Shadowsocks 配置文件不存在 !" && exit 1
  159.     port=$(cat ${CONF}|grep 'PORT = '|awk -F 'PORT = ' '{print $NF}')
  160.     password=$(cat ${CONF}|grep 'PASSWORD = '|awk -F 'PASSWORD = ' '{print $NF}')
  161.     cipher=$(cat ${CONF}|grep 'CIPHER = '|awk -F 'CIPHER = ' '{print $NF}')
  162.     verbose=$(cat ${CONF}|grep 'VERBOSE = '|awk -F 'VERBOSE = ' '{print $NF}')
  163. }
  164. Set_port(){
  165.     while true
  166.         do
  167.         echo -e "请输入 Shadowsocks 端口 [1-65535]"
  168.         read -e -p "(默认: 443):" ss_port
  169.         [[ -z "${ss_port}" ]] && ss_port="443"
  170.         echo $((${ss_port}+0)) &>/dev/null
  171.         if [[ $? -eq 0 ]]; then
  172.             if [[ ${ss_port} -ge 1 ]] && [[ ${ss_port} -le 65535 ]]; then
  173.                 echo && echo "========================"
  174.                 echo -e "   端口 : ${Red_background_prefix} ${ss_port} ${Font_color_suffix}"
  175.                 echo "========================" && echo
  176.                 break
  177.             else
  178.                 echo "输入错误, 请输入正确的端口。"
  179.             fi
  180.         else
  181.             echo "输入错误, 请输入正确的端口。"
  182.         fi
  183.         done
  184. }
  185. Set_password(){
  186.     echo "请输入 Shadowsocks 密码 [0-9][a-z][A-Z]"
  187.     read -e -p "(默认: 随机生成):" ss_password
  188.     [[ -z "${ss_password}" ]] && ss_password=$(date +%s%N | md5sum | head -c 16)
  189.     echo && echo "========================"
  190.     echo -e "   密码 : ${Red_background_prefix} ${ss_password} ${Font_color_suffix}"
  191.     echo "========================" && echo
  192. }
  193. Set_cipher(){
  194.     echo -e "请选择 Shadowsocks 加密方式
  195.    
  196. ${Green_font_prefix} 1.${Font_color_suffix} aes-128-cfb
  197. ${Green_font_prefix} 2.${Font_color_suffix} aes-128-ctr
  198. ${Green_font_prefix} 3.${Font_color_suffix} aes-192-cfb
  199. ${Green_font_prefix} 4.${Font_color_suffix} aes-192-ctr
  200. ${Green_font_prefix} 5.${Font_color_suffix} aes-256-cfb
  201. ${Green_font_prefix} 6.${Font_color_suffix} aes-256-ctr
  202. ${Green_font_prefix} 7.${Font_color_suffix} chacha20-ietf
  203. ${Green_font_prefix} 8.${Font_color_suffix} xchacha20
  204. ${Green_font_prefix} 9.${Font_color_suffix} aes-128-gcm            (AEAD)
  205. ${Green_font_prefix}10.${Font_color_suffix} aes-192-gcm            (AEAD)
  206. ${Green_font_prefix}11.${Font_color_suffix} aes-256-gcm            (AEAD)
  207. ${Green_font_prefix}12.${Font_color_suffix} chacha20-ietf-poly1305 (AEAD)
  208.  
  209. ${Tip} chacha20 系列加密方式无需额外安装 libsodium,Shadowsocks Go版默认集成 !" && echo
  210.     read -e -p "(默认: 12. chacha20-ietf-poly1305):" ss_cipher
  211.     [[ -z "${ss_cipher}" ]] && ss_cipher="12"
  212.     if [[ ${ss_cipher} == "1" ]]; then
  213.         ss_cipher="aes-128-cfb"
  214.     elif [[ ${ss_cipher} == "2" ]]; then
  215.         ss_cipher="aes-128-ctr"
  216.     elif [[ ${ss_cipher} == "3" ]]; then
  217.         ss_cipher="aes-192-cfb"
  218.     elif [[ ${ss_cipher} == "4" ]]; then
  219.         ss_cipher="aes-192-ctr"
  220.     elif [[ ${ss_cipher} == "5" ]]; then
  221.         ss_cipher="aes-256-cfb"
  222.     elif [[ ${ss_cipher} == "6" ]]; then
  223.         ss_cipher="aes-256-ctr"
  224.     elif [[ ${ss_cipher} == "7" ]]; then
  225.         ss_cipher="chacha20-ietf"
  226.     elif [[ ${ss_cipher} == "8" ]]; then
  227.         ss_cipher="xchacha20"
  228.     elif [[ ${ss_cipher} == "9" ]]; then
  229.         ss_cipher="aead_aes_128_gcm"
  230.     elif [[ ${ss_cipher} == "10" ]]; then
  231.         ss_cipher="aead_aes_192_gcm"
  232.     elif [[ ${ss_cipher} == "11" ]]; then
  233.         ss_cipher="aead_aes_256_gcm"
  234.     elif [[ ${ss_cipher} == "12" ]]; then
  235.         ss_cipher="aead_chacha20_poly1305"
  236.     else
  237.         ss_cipher="aead_chacha20_poly1305"
  238.     fi
  239.     echo && echo "========================"
  240.     echo -e "   加密 : ${Red_background_prefix} ${ss_cipher} ${Font_color_suffix}"
  241.     echo "========================" && echo
  242. }
  243. Set_verbose(){
  244.     echo -e "是否启用详细日志模式?[Y/n]
  245. 启用详细日志模式就可以在日志中看到链接者信息(链接时间、链接代理端口、链接者IP、链接者访问的目标域名或IP这些非敏感类信息)。"
  246.     read -e -p "(默认:N 禁用):" ss_verbose
  247.     [[ -z "${ss_verbose}" ]] && ss_verbose="N"
  248.     if [[ "${ss_verbose}" == [Yy] ]]; then
  249.         ss_verbose="YES"
  250.     else
  251.         ss_verbose="NO"
  252.     fi
  253.     echo && echo "========================"
  254.     echo -e "   详细日志模式 : ${Red_background_prefix} ${ss_verbose} ${Font_color_suffix}"
  255.     echo "========================" && echo
  256. }
  257. Set(){
  258.     check_installed_status
  259.     echo && echo -e "你要做什么?
  260. ${Green_font_prefix}1.${Font_color_suffix}  修改 端口配置
  261. ${Green_font_prefix}2.${Font_color_suffix}  修改 密码配置
  262. ${Green_font_prefix}3.${Font_color_suffix}  修改 加密配置
  263. ${Green_font_prefix}4.${Font_color_suffix}  修改 详细日志模式 配置
  264. ${Green_font_prefix}5.${Font_color_suffix}  修改 全部配置
  265. ————————————————
  266. ${Green_font_prefix}6.${Font_color_suffix}  监控 运行状态" && echo
  267.     read -e -p "(默认: 取消):" ss_modify
  268.     [[ -z "${ss_modify}" ]] && echo "已取消..." && exit 1
  269.     if [[ "${ss_modify}" == "1" ]]; then
  270.         Read_config
  271.         Set_port
  272.         ss_password=${password}
  273.         ss_cipher=${cipher}
  274.         ss_verbose=${verbose}
  275.         Write_config
  276.         Del_iptables
  277.         Add_iptables
  278.         Restart
  279.     elif [[ "${ss_modify}" == "2" ]]; then
  280.         Read_config
  281.         Set_password
  282.         ss_port=${port}
  283.         ss_cipher=${cipher}
  284.         ss_verbose=${verbose}
  285.         Write_config
  286.         Restart
  287.     elif [[ "${ss_modify}" == "3" ]]; then
  288.         Read_config
  289.         Set_cipher
  290.         ss_port=${port}
  291.         ss_password=${password}
  292.         ss_verbose=${verbose}
  293.         Write_config
  294.         Restart
  295.     elif [[ "${ss_modify}" == "4" ]]; then
  296.         Read_config
  297.         Set_verbose
  298.         ss_port=${port}
  299.         ss_password=${password}
  300.         ss_cipher=${cipher}
  301.         Write_config
  302.         Restart
  303.     elif [[ "${ss_modify}" == "5" ]]; then
  304.         Read_config
  305.         Set_port
  306.         Set_password
  307.         Set_cipher
  308.         Set_verbose
  309.         Write_config
  310.         Restart
  311.     elif [[ "${ss_modify}" == "6" ]]; then
  312.         Set_crontab_monitor
  313.     else
  314.         echo -e "${Error} 请输入正确的数字(1-6)" && exit 1
  315.     fi
  316. }
  317. Install(){
  318.     check_root
  319.     [[ -e ${FILE} ]] && echo -e "${Error} 检测到 Shadowsocks 已安装 !" && exit 1
  320.     echo -e "${Info} 开始设置 用户配置..."
  321.     Set_port
  322.     Set_password
  323.     Set_cipher
  324.     Set_verbose
  325.     echo -e "${Info} 开始安装/配置 依赖..."
  326.     Installation_dependency
  327.     echo -e "${Info} 开始下载/安装..."
  328.     check_new_ver
  329.     Download
  330.     echo -e "${Info} 开始下载/安装 服务脚本(init)..."
  331.     Service
  332.     echo -e "${Info} 开始写入 配置文件..."
  333.     Write_config
  334.     echo -e "${Info} 开始设置 iptables防火墙..."
  335.     Set_iptables
  336.     echo -e "${Info} 开始添加 iptables防火墙规则..."
  337.     Add_iptables
  338.     echo -e "${Info} 开始保存 iptables防火墙规则..."
  339.     Save_iptables
  340.     echo -e "${Info} 所有步骤 安装完毕,开始启动..."
  341.     Start
  342. }
  343. Start(){
  344.     check_installed_status
  345.     check_pid
  346.     [[ ! -z ${PID} ]] && echo -e "${Error} Shadowsocks 正在运行,请检查 !" && exit 1
  347.     /etc/init.d/ss-go start
  348.     #sleep 1s
  349.     check_pid
  350.     [[ ! -z ${PID} ]] && View
  351. }
  352. Stop(){
  353.     check_installed_status
  354.     check_pid
  355.     [[ -z ${PID} ]] && echo -e "${Error} Shadowsocks 没有运行,请检查 !" && exit 1
  356.     /etc/init.d/ss-go stop
  357. }
  358. Restart(){
  359.     check_installed_status
  360.     check_pid
  361.     [[ ! -z ${PID} ]] && /etc/init.d/ss-go stop
  362.     /etc/init.d/ss-go start
  363.     #sleep 1s
  364.     check_pid
  365.     [[ ! -z ${PID} ]] && View
  366. }
  367. Update(){
  368.     check_installed_status
  369.     check_new_ver
  370.     check_ver_comparison
  371. }
  372. Uninstall(){
  373.     check_installed_status
  374.     echo "确定要卸载 Shadowsocks ? (y/N)"
  375.     echo
  376.     read -e -p "(默认: n):" unyn
  377.     [[ -z ${unyn} ]] && unyn="n"
  378.     if [[ ${unyn} == [Yy] ]]; then
  379.         check_pid
  380.         [[ ! -z $PID ]] && kill -9 ${PID}
  381.         if [[ -e ${CONF} ]]; then
  382.             port=$(cat ${CONF}|grep 'PORT = '|awk -F 'PORT = ' '{print $NF}')
  383.             Del_iptables
  384.             Save_iptables
  385.         fi
  386.         if [[ ! -z $(crontab -l | grep "ss-go.sh monitor") ]]; then
  387.             crontab_monitor_cron_stop
  388.         fi
  389.         rm -rf "${FOLDER}"
  390.         if [[ ${release} = "centos" ]]; then
  391.             chkconfig --del ss-go
  392.         else
  393.             update-rc.d -f ss-go remove
  394.         fi
  395.         rm -rf "/etc/init.d/ss-go"
  396.         echo && echo "Shadowsocks 卸载完成 !" && echo
  397.     else
  398.         echo && echo "卸载已取消..." && echo
  399.     fi
  400. }
  401. getipv4(){
  402.     ipv4=$(wget -qO- -4 -t1 -T2 ipinfo.io/ip)
  403.     if [[ -z "${ipv4}" ]]; then
  404.         ipv4=$(wget -qO- -4 -t1 -T2 api.ip.sb/ip)
  405.         if [[ -z "${ipv4}" ]]; then
  406.             ipv4=$(wget -qO- -4 -t1 -T2 members.3322.org/dyndns/getip)
  407.             if [[ -z "${ipv4}" ]]; then
  408.                 ipv4="IPv4_Error"
  409.             fi
  410.         fi
  411.     fi
  412. }
  413. getipv6(){
  414.     ipv6=$(wget -qO- -6 -t1 -T2 ifconfig.co)
  415.     if [[ -z "${ipv6}" ]]; then
  416.         ipv6="IPv6_Error"
  417.     fi
  418. }
  419. urlsafe_base64(){
  420.     date=$(echo -n "$1"|base64|sed ':a;N;s/\n/ /g;ta'|sed 's/ //g;s/=//g;s/+/-/g;s/\//_/g')
  421.     echo -e "${date}"
  422. }
  423. ss_link_qr(){
  424.     if [[ "${ipv4}" != "IPv4_Error" ]]; then
  425.         if [[ "${cipher}" == "aead_chacha20_poly1305" ]]; then
  426.             cipher_1="chacha20-ietf-poly1305"
  427.         else
  428.             cipher_1=$(echo "${cipher}"|sed 's/aead_//g;s/_/-/g')
  429.         fi
  430.         SSbase64=$(urlsafe_base64 "${cipher_1}:${password}@${ipv4}:${port}")
  431.         SSurl="ss://${SSbase64}"
  432.         SSQRcode="http://doub.pw/qr/qr.php?text=${SSurl}"
  433.         ss_link_ipv4=" 链接  [ipv4] : ${Red_font_prefix}${SSurl}${Font_color_suffix} \n 二维码[ipv4] : ${Red_font_prefix}${SSQRcode}${Font_color_suffix}"
  434.     fi
  435.     if [[ "${ipv6}" != "IPv6_Error" ]]; then
  436.         if [[ "${cipher}" == "aead_chacha20_poly1305" ]]; then
  437.             cipher_1="chacha20-ietf-poly1305"
  438.         else
  439.             cipher_1=$(echo "${cipher}"|sed 's/aead_//g;s/_/-/g')
  440.         fi
  441.         SSbase64=$(urlsafe_base64 "${cipher_1}:${password}@${ipv6}:${port}")
  442.         SSurl="ss://${SSbase64}"
  443.         SSQRcode="http://doub.pw/qr/qr.php?text=${SSurl}"
  444.         ss_link_ipv6=" 链接  [ipv6] : ${Red_font_prefix}${SSurl}${Font_color_suffix} \n 二维码[ipv6] : ${Red_font_prefix}${SSQRcode}${Font_color_suffix}"
  445.     fi
  446. }
  447. View(){
  448.     check_installed_status
  449.     Read_config
  450.     getipv4
  451.     getipv6
  452.     ss_link_qr
  453.     if [[ "${cipher}" == "aead_chacha20_poly1305" ]]; then
  454.         cipher_2="chacha20-ietf-poly1305"
  455.     else
  456.         cipher_2=$(echo "${cipher}"|sed 's/aead_//g;s/_/-/g')
  457.     fi
  458.     clear && echo
  459.     echo -e "Shadowsocks 用户配置:"
  460.     echo -e "————————————————"
  461.     [[ "${ipv4}" != "IPv4_Error" ]] && echo -e " 地址\t: ${Green_font_prefix}${ipv4}${Font_color_suffix}"
  462.     [[ "${ipv6}" != "IPv6_Error" ]] && echo -e " 地址\t: ${Green_font_prefix}${ipv6}${Font_color_suffix}"
  463.     echo -e " 端口\t: ${Green_font_prefix}${port}${Font_color_suffix}"
  464.     echo -e " 密码\t: ${Green_font_prefix}${password}${Font_color_suffix}"
  465.     echo -e " 加密\t: ${Green_font_prefix}${cipher_2}${Font_color_suffix}"
  466.     [[ ! -z "${ss_link_ipv4}" ]] && echo -e "${ss_link_ipv4}"
  467.     [[ ! -z "${ss_link_ipv6}" ]] && echo -e "${ss_link_ipv6}"
  468.     echo
  469.     echo -e " 详细日志模式\t: ${Green_font_prefix}${verbose}${Font_color_suffix}"
  470.     echo
  471. }
  472. View_Log(){
  473.     check_installed_status
  474.     [[ ! -e ${LOG} ]] && echo -e "${Error} Shadowsocks 日志文件不存在 !" && exit 1
  475.     echo && echo -e "${Tip}${Red_font_prefix}Ctrl+C${Font_color_suffix} 终止查看日志"
  476.     echo -e "如果需要查看完整日志内容,请用 ${Red_font_prefix}cat ${LOG}${Font_color_suffix} 命令。"
  477.     echo -e "如果想要查看详细日志,请在 [7.设置 账号配置 - 4.修改 详细日志模式 配置] 开启。" && echo
  478.     tail -f ${LOG}
  479. }
  480. # 显示 连接信息
  481. View_user_connection_info_1(){
  482.     format_1=$1
  483.     Read_config
  484.     user_IP=$(ss state connected sport = :${port} -tn|sed '1d'|awk '{print $NF}'|awk -F ':' '{print $(NF-1)}'|sort -u)
  485.     if [[ -z ${user_IP} ]]; then
  486.         user_IP_total="0"
  487.         echo -e "端口: ${Green_font_prefix}"${port}"${Font_color_suffix}\t 链接IP总数: ${Green_font_prefix}"${user_IP_total}"${Font_color_suffix}\t 当前链接IP: "
  488.     else
  489.         user_IP_total=$(echo -e "${user_IP}"|wc -l)
  490.         if [[ ${format_1} == "IP_address" ]]; then
  491.             echo -e "端口: ${Green_font_prefix}"${port}"${Font_color_suffix}\t 链接IP总数: ${Green_font_prefix}"${user_IP_total}"${Font_color_suffix}\t 当前链接IP: "
  492.             get_IP_address
  493.             echo
  494.         else
  495.             user_IP=$(echo -e "\n${user_IP}")
  496.             echo -e "端口: ${Green_font_prefix}"${user_port}"${Font_color_suffix}\t 链接IP总数: ${Green_font_prefix}"${user_IP_total}"${Font_color_suffix}\t 当前链接IP: ${Green_font_prefix}${user_IP}${Font_color_suffix}\n"
  497.         fi
  498.     fi
  499.     user_IP=""
  500. }
  501. View_user_connection_info(){
  502.     check_installed_status
  503.     echo && echo -e "请选择要显示的格式:
  504. ${Green_font_prefix}1.${Font_color_suffix} 显示 IP 格式
  505. ${Green_font_prefix}2.${Font_color_suffix} 显示 IP+IP归属地 格式" && echo
  506.     read -e -p "(默认: 1):" connection_info
  507.     [[ -z "${connection_info}" ]] && connection_info="1"
  508.     if [[ "${connection_info}" == "1" ]]; then
  509.         View_user_connection_info_1
  510.     elif [[ "${connection_info}" == "2" ]]; then
  511.         echo -e "${Tip} 检测IP归属地(ipip.net),如果IP较多,可能时间会比较长..."
  512.         View_user_connection_info_1 "IP_address"
  513.     else
  514.         echo -e "${Error} 请输入正确的数字(1-2)" && exit 1
  515.     fi
  516. }
  517. get_IP_address(){
  518.     if [[ ! -z ${user_IP} ]]; then
  519.         for((integer_1 = ${user_IP_total}; integer_1 >= 1; integer_1--))
  520.         do
  521.             IP=$(echo "${user_IP}" |sed -n "$integer_1"p)
  522.             IP_address=$(wget -qO- -t1 -T2 http://freeapi.ipip.net/${IP}|sed 's/\"//g;s/,//g;s/\[//g;s/\]//g')
  523.             echo -e "${Green_font_prefix}${IP}${Font_color_suffix} (${IP_address})"
  524.             sleep 1s
  525.         done
  526.     fi
  527. }
  528. Set_crontab_monitor(){
  529.     check_crontab_installed_status
  530.     crontab_monitor_status=$(crontab -l|grep "ss-go.sh monitor")
  531.     if [[ -z "${crontab_monitor_status}" ]]; then
  532.         echo && echo -e "当前监控模式: ${Red_font_prefix}未开启${Font_color_suffix}" && echo
  533.         echo -e "确定要开启 ${Green_font_prefix}Shadowsocks 服务端运行状态监控${Font_color_suffix} 功能吗?(当进程关闭则自动启动 Shadowsocks 服务端)[Y/n]"
  534.         read -e -p "(默认: y):" crontab_monitor_status_ny
  535.         [[ -z "${crontab_monitor_status_ny}" ]] && crontab_monitor_status_ny="y"
  536.         if [[ ${crontab_monitor_status_ny} == [Yy] ]]; then
  537.             crontab_monitor_cron_start
  538.         else
  539.             echo && echo "  已取消..." && echo
  540.         fi
  541.     else
  542.         echo && echo -e "当前监控模式: ${Green_font_prefix}已开启${Font_color_suffix}" && echo
  543.         echo -e "确定要关闭 ${Red_font_prefix}Shadowsocks 服务端运行状态监控${Font_color_suffix} 功能吗?(当进程关闭则自动启动 Shadowsocks 服务端)[y/N]"
  544.         read -e -p "(默认: n):" crontab_monitor_status_ny
  545.         [[ -z "${crontab_monitor_status_ny}" ]] && crontab_monitor_status_ny="n"
  546.         if [[ ${crontab_monitor_status_ny} == [Yy] ]]; then
  547.             crontab_monitor_cron_stop
  548.         else
  549.             echo && echo "  已取消..." && echo
  550.         fi
  551.     fi
  552. }
  553. crontab_monitor_cron_start(){
  554.     crontab -l > "$file_1/crontab.bak"
  555.     sed -i "/ss-go.sh monitor/d" "$file_1/crontab.bak"
  556.     echo -e "\n* * * * * /bin/bash $file_1/ss-go.sh monitor" >> "$file_1/crontab.bak"
  557.     crontab "$file_1/crontab.bak"
  558.     rm -r "$file_1/crontab.bak"
  559.     cron_config=$(crontab -l | grep "ss-go.sh monitor")
  560.     if [[ -z ${cron_config} ]]; then
  561.         echo -e "${Error} Shadowsocks 服务端运行状态监控功能 启动失败 !" && exit 1
  562.     else
  563.         echo -e "${Info} Shadowsocks 服务端运行状态监控功能 启动成功 !"
  564.     fi
  565. }
  566. crontab_monitor_cron_stop(){
  567.     crontab -l > "$file_1/crontab.bak"
  568.     sed -i "/ss-go.sh monitor/d" "$file_1/crontab.bak"
  569.     crontab "$file_1/crontab.bak"
  570.     rm -r "$file_1/crontab.bak"
  571.     cron_config=$(crontab -l | grep "ss-go.sh monitor")
  572.     if [[ ! -z ${cron_config} ]]; then
  573.         echo -e "${Error} Shadowsocks 服务端运行状态监控功能 停止失败 !" && exit 1
  574.     else
  575.         echo -e "${Info} Shadowsocks 服务端运行状态监控功能 停止成功 !"
  576.     fi
  577. }
  578. crontab_monitor(){
  579.     check_installed_status
  580.     check_pid
  581.     #echo "${PID}"
  582.     if [[ -z ${PID} ]]; then
  583.         echo -e "${Error} [$(date "+%Y-%m-%d %H:%M:%S %u %Z")] 检测到 Shadowsocks服务端 未运行 , 开始启动..." | tee -a ${LOG}
  584.         /etc/init.d/ss-go start
  585.         sleep 1s
  586.         check_pid
  587.         if [[ -z ${PID} ]]; then
  588.             echo -e "${Error} [$(date "+%Y-%m-%d %H:%M:%S %u %Z")] Shadowsocks服务端 启动失败..." | tee -a ${LOG}
  589.         else
  590.             echo -e "${Info} [$(date "+%Y-%m-%d %H:%M:%S %u %Z")] Shadowsocks服务端 启动成功..." | tee -a ${LOG}
  591.         fi
  592.     else
  593.         echo -e "${Info} [$(date "+%Y-%m-%d %H:%M:%S %u %Z")] Shadowsocks服务端 进程运行正常..." | tee -a ${LOG}
  594.     fi
  595. }
  596. Add_iptables(){
  597.     iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport ${ss_port} -j ACCEPT
  598.     iptables -I INPUT -m state --state NEW -m udp -p udp --dport ${ss_port} -j ACCEPT
  599.     ip6tables -I INPUT -m state --state NEW -m tcp -p tcp --dport ${ss_port} -j ACCEPT
  600.     ip6tables -I INPUT -m state --state NEW -m udp -p udp --dport ${ss_port} -j ACCEPT
  601. }
  602. Del_iptables(){
  603.     iptables -D INPUT -m state --state NEW -m tcp -p tcp --dport ${port} -j ACCEPT
  604.     iptables -D INPUT -m state --state NEW -m udp -p udp --dport ${port} -j ACCEPT
  605.     ip6tables -D INPUT -m state --state NEW -m tcp -p tcp --dport ${port} -j ACCEPT
  606.     ip6tables -D INPUT -m state --state NEW -m udp -p udp --dport ${port} -j ACCEPT
  607. }
  608. Save_iptables(){
  609.     if [[ ${release} == "centos" ]]; then
  610.         service iptables save
  611.         service ip6tables save
  612.     else
  613.         iptables-save > /etc/iptables.up.rules
  614.         ip6tables-save > /etc/ip6tables.up.rules
  615.     fi
  616. }
  617. Set_iptables(){
  618.     if [[ ${release} == "centos" ]]; then
  619.         service iptables save
  620.         service ip6tables save
  621.         chkconfig --level 2345 iptables on
  622.         chkconfig --level 2345 ip6tables on
  623.     else
  624.         iptables-save > /etc/iptables.up.rules
  625.         ip6tables-save > /etc/ip6tables.up.rules
  626.         echo -e '#!/bin/bash\n/sbin/iptables-restore < /etc/iptables.up.rules\n/sbin/ip6tables-restore < /etc/ip6tables.up.rules' > /etc/network/if-pre-up.d/iptables
  627.         chmod +x /etc/network/if-pre-up.d/iptables
  628.     fi
  629. }
  630. Update_Shell(){
  631.     sh_new_ver=$(wget --no-check-certificate -qO- -t1 -T3 "https://raw.githubusercontent.com/ToyoDAdoubiBackup/doubi/master/ss-go.sh"|grep 'sh_ver="'|awk -F "=" '{print $NF}'|sed 's/\"//g'|head -1) && sh_new_type="github"
  632.     [[ -z ${sh_new_ver} ]] && echo -e "${Error} 无法链接到 Github !" && exit 0
  633.     if [[ -e "/etc/init.d/ss-go" ]]; then
  634.         rm -rf /etc/init.d/ss-go
  635.         Service
  636.     fi
  637.     wget -N --no-check-certificate "https://raw.githubusercontent.com/ToyoDAdoubiBackup/doubi/master/ss-go.sh" && chmod +x ss-go.sh
  638.     echo -e "脚本已更新为最新版本[ ${sh_new_ver} ] !(注意:因为更新方式为直接覆盖当前运行的脚本,所以可能下面会提示一些报错,无视即可)" && exit 0
  639. }
  640. check_sys
  641. action=$1
  642. if [[ "${action}" == "monitor" ]]; then
  643.     crontab_monitor
  644. else
  645.     echo && echo -e "  Shadowsocks-Go 一键管理脚本 ${Red_font_prefix}[v${sh_ver}]${Font_color_suffix}
  646.  ---- Toyo | doub.io/ss-jc67 ----
  647.  
  648. ${Green_font_prefix} 0.${Font_color_suffix} 升级脚本
  649. ————————————
  650. ${Green_font_prefix} 1.${Font_color_suffix} 安装 Shadowsocks
  651. ${Green_font_prefix} 2.${Font_color_suffix} 更新 Shadowsocks
  652. ${Green_font_prefix} 3.${Font_color_suffix} 卸载 Shadowsocks
  653. ————————————
  654. ${Green_font_prefix} 4.${Font_color_suffix} 启动 Shadowsocks
  655. ${Green_font_prefix} 5.${Font_color_suffix} 停止 Shadowsocks
  656. ${Green_font_prefix} 6.${Font_color_suffix} 重启 Shadowsocks
  657. ————————————
  658. ${Green_font_prefix} 7.${Font_color_suffix} 设置 账号配置
  659. ${Green_font_prefix} 8.${Font_color_suffix} 查看 账号信息
  660. ${Green_font_prefix} 9.${Font_color_suffix} 查看 日志信息
  661. ${Green_font_prefix}10.${Font_color_suffix} 查看 链接信息
  662. ————————————" && echo
  663.     if [[ -e ${FILE} ]]; then
  664.         check_pid
  665.         if [[ ! -z "${PID}" ]]; then
  666.             echo -e " 当前状态: ${Green_font_prefix}已安装${Font_color_suffix}${Green_font_prefix}已启动${Font_color_suffix}"
  667.         else
  668.             echo -e " 当前状态: ${Green_font_prefix}已安装${Font_color_suffix}${Red_font_prefix}未启动${Font_color_suffix}"
  669.         fi
  670.     else
  671.         echo -e " 当前状态: ${Red_font_prefix}未安装${Font_color_suffix}"
  672.     fi
  673.     echo
  674.     read -e -p " 请输入数字 [0-10]:" num
  675.     case "$num" in
  676.         0)
  677.         Update_Shell
  678.         ;;
  679.         1)
  680.         Install
  681.         ;;
  682.         2)
  683.         Update
  684.         ;;
  685.         3)
  686.         Uninstall
  687.         ;;
  688.         4)
  689.         Start
  690.         ;;
  691.         5)
  692.         Stop
  693.         ;;
  694.         6)
  695.         Restart
  696.         ;;
  697.         7)
  698.         Set
  699.         ;;
  700.         8)
  701.         View
  702.         ;;
  703.         9)
  704.         View_Log
  705.         ;;
  706.         10)
  707.         View_user_connection_info
  708.         ;;
  709.         *)
  710.         echo "请输入正确数字 [0-10]"
  711.         ;;
  712.     esac
  713. fi
Add Comment
Please, Sign In to add comment