From 6a931fb9ed66f8eb28c275c1d99b91d60257cdbe Mon Sep 17 00:00:00 2001 From: Mario Date: Mon, 9 Oct 2023 17:54:24 +0800 Subject: [PATCH] Update shadowsocks-rust.sh --- shadowsocks-rust.sh | 609 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 608 insertions(+), 1 deletion(-) diff --git a/shadowsocks-rust.sh b/shadowsocks-rust.sh index 7091e15..0516fc3 100644 --- a/shadowsocks-rust.sh +++ b/shadowsocks-rust.sh @@ -7,7 +7,7 @@ export PATH # Description: shadowsocks-rust的一键安装脚本,可能是全网唯一支持纯ipv6网络vps的脚本 #================================================= -sh_ver="1.16.2" +sh_ver="v1.16.2" filepath=$(cd "$(dirname "$0")"; pwd) file_1=$(echo -e "${filepath}"|awk -F "$0" '{print $1}') FOLDER="/etc/shadowsocks-rust" @@ -15,3 +15,610 @@ FILE="/usr/local/bin/shadowsocks-rust" CONF="/etc/shadowsocks-rust/config.json" Now_ver_File="/etc/shadowsocks-rust/ver.txt" Local="/etc/sysctl.d/local.conf" + +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" && Yellow_font_prefix="\033[0;33m" +Info="${Green_font_prefix}[信息]${Font_color_suffix}" +Error="${Red_font_prefix}[错误]${Font_color_suffix}" +Tip="${Yellow_font_prefix}[注意]${Font_color_suffix}" + +check_root(){ + [[ $EUID != 0 ]] && echo -e "${Error} 当前非ROOT账号(或没有ROOT权限),无法继续操作,请更换ROOT账号或使用 ${Green_background_prefix}sudo su${Font_color_suffix} 命令获取临时ROOT权限(执行后可能会提示输入当前账号的密码)。" && exit 1 +} + +check_sys(){ + if [[ -f /etc/redhat-release ]]; then + release="centos" + elif cat /etc/issue | grep -q -E -i "debian"; then + release="debian" + elif cat /etc/issue | grep -q -E -i "ubuntu"; then + release="ubuntu" + elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then + release="centos" + elif cat /proc/version | grep -q -E -i "debian"; then + release="debian" + elif cat /proc/version | grep -q -E -i "ubuntu"; then + release="ubuntu" + elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; then + release="centos" + fi +} + +sysArch() { + uname=$(uname -m) + if [[ "$uname" == "i686" ]] || [[ "$uname" == "i386" ]]; then + arch="i686" + elif [[ "$uname" == *"armv7"* ]] || [[ "$uname" == "armv6l" ]]; then + arch="arm" + elif [[ "$uname" == *"armv8"* ]] || [[ "$uname" == "aarch64" ]]; then + arch="aarch64" + else + arch="x86_64" + fi +} + +#开启系统 TCP Fast Open +enable_systfo() { + kernel=$(uname -r | awk -F . '{print $1}') + if [ "$kernel" -ge 3 ]; then + echo 3 >/proc/sys/net/ipv4/tcp_fastopen + [[ ! -e $Local ]] && echo "fs.file-max = 51200 +net.core.rmem_max = 67108864 +net.core.wmem_max = 67108864 +net.core.rmem_default = 65536 +net.core.wmem_default = 65536 +net.core.netdev_max_backlog = 4096 +net.core.somaxconn = 4096 +net.ipv4.tcp_syncookies = 1 +net.ipv4.tcp_tw_reuse = 1 +net.ipv4.tcp_tw_recycle = 0 +net.ipv4.tcp_fin_timeout = 30 +net.ipv4.tcp_keepalive_time = 1200 +net.ipv4.ip_local_port_range = 10000 65000 +net.ipv4.tcp_max_syn_backlog = 4096 +net.ipv4.tcp_max_tw_buckets = 5000 +net.ipv4.tcp_fastopen = 3 +net.ipv4.tcp_rmem = 4096 87380 67108864 +net.ipv4.tcp_wmem = 4096 65536 67108864 +net.ipv4.tcp_mtu_probing = 1 +net.core.default_qdisc=fq +net.ipv4.tcp_congestion_control = bbr" >>/etc/sysctl.d/local.conf && sysctl --system >/dev/null 2>&1 + else + echo -e "$Error系统内核版本过低,无法支持 TCP Fast Open !" + fi +} + +check_installed_status(){ + [[ ! -e ${FILE} ]] && echo -e "${Error} Shadowsocks Rust 没有安装,请检查!" && exit 1 +} + +check_status(){ + status=`systemctl status shadowsocks-rust | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1` +} + +check_ver_comparison(){ + now_ver=$(cat ${Now_ver_File}) + if [[ "${now_ver}" != "${sh_ver}" ]]; then + echo -e "${Info} 发现 Shadowsocks Rust 已有新版本 [ ${sh_ver} ],旧版本 [ ${now_ver} ]" + read -e -p "是否更新 ? [Y/n]:" yn + [[ -z "${yn}" ]] && yn="y" + if [[ $yn == [Yy] ]]; then + check_status + \cp "${CONF}" "/tmp/config.json" + Download + mv -f "/tmp/config.json" "${CONF}" + Restart + fi + else + echo -e "${Info} 当前 Shadowsocks Rust 已是最新版本 [ ${sh_ver} ] !" && exit 1 + fi +} + +Download() { + if [[ ! -e "${FOLDER}" ]]; then + mkdir "${FOLDER}" + fi + echo -e "${Info} 开始下载 Shadowsocks Rust ……" + wget --no-check-certificate -N "https://raw.githubusercontent.com/myeveryheart/shadowsocks-rust/main/shadowsocks-${sh_ver}.${arch}-unknown-linux-gnu.tar.xz" + if [[ ! -e "shadowsocks-${sh_ver}.${arch}-unknown-linux-gnu.tar.xz" ]]; then + echo -e "${Error} Shadowsocks Rust 下载失败!" + return 1 && exit 1 + else + tar -xvf "shadowsocks-${sh_ver}.${arch}-unknown-linux-gnu.tar.xz" + fi + if [[ ! -e "ssserver" ]]; then + echo -e "${Error} Shadowsocks Rust 解压失败 !" + echo -e "${Error} Shadowsocks Rust 安装失败 !" + return 1 && exit 1 + else + rm -rf "shadowsocks-${sh_ver}.${arch}-unknown-linux-gnu.tar.xz" + chmod +x ssserver + mv -f ssserver "${FILE}" + rm sslocal ssmanager ssservice ssurl + echo "${sh_ver}" > ${Now_ver_File} + echo -e "${Info} Shadowsocks Rust 主程序下载安装完毕!" + return 0 + fi +} + +Service(){ + echo ' +[Unit] +Description= Shadowsocks Rust Service +After=network-online.target +Wants=network-online.target systemd-networkd-wait-online.service +[Service] +LimitNOFILE=32767 +Type=simple +User=root +Restart=on-failure +RestartSec=5s +DynamicUser=true +ExecStartPre=/bin/sh -c 'ulimit -n 51200' +ExecStart=/usr/local/bin/shadowsocks-rust -c /etc/shadowsocks-rust/config.json +[Install] +WantedBy=multi-user.target' > /etc/systemd/system/shadowsocks-rust.service +systemctl enable --now shadowsocks-rust + echo -e "${Info} Shadowsocks Rust 服务配置完成!" +} + +Installation_dependency(){ + if [[ ${release} == "centos" ]]; then + yum update && yum install jq gzip wget curl unzip xz -y + else + apt-get update && apt-get install jq gzip wget curl unzip xz-utils -y + fi + \cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime +} + +Write_config(){ + cat > ${CONF}<<-EOF +{ + "server": "::", + "server_port": ${port}, + "password": "${password}", + "method": "${cipher}", + "fast_open": ${tfo}, + "mode": "tcp_and_udp", + "user":"nobody", + "timeout":300, + "nameserver":"8.8.8.8" +} +EOF +} + +Read_config(){ + [[ ! -e ${CONF} ]] && echo -e "${Error} Shadowsocks Rust 配置文件不存在!" && exit 1 + port=$(cat ${CONF}|jq -r '.server_port') + password=$(cat ${CONF}|jq -r '.password') + cipher=$(cat ${CONF}|jq -r '.method') + tfo=$(cat ${CONF}|jq -r '.fast_open') +} + +Set_port(){ + while true + do + echo -e "${Tip} 本步骤不涉及系统防火墙端口操作,请手动放行相应端口!" + echo -e "请输入 Shadowsocks Rust 端口 [1-65535]" + read -e -p "(默认:2525):" port + [[ -z "${port}" ]] && port="2525" + echo $((${port}+0)) &>/dev/null + if [[ $? -eq 0 ]]; then + if [[ ${port} -ge 1 ]] && [[ ${port} -le 65535 ]]; then + echo && echo "==================================" + echo -e "端口:${Red_background_prefix} ${port} ${Font_color_suffix}" + echo "==================================" && echo + break + else + echo "输入错误, 请输入正确的端口。" + fi + else + echo "输入错误, 请输入正确的端口。" + fi + done +} + +Set_tfo(){ + echo -e "是否开启 TCP Fast Open ? +================================== +${Green_font_prefix} 1.${Font_color_suffix} 开启 ${Green_font_prefix} 2.${Font_color_suffix} 关闭 +==================================" + read -e -p "(默认:1.开启):" tfo + [[ -z "${tfo}" ]] && tfo="1" + if [[ ${tfo} == "1" ]]; then + tfo=true + enable_systfo + else + tfo=false + fi + echo && echo "==================================" + echo -e "TCP Fast Open 开启状态:${Red_background_prefix} ${tfo} ${Font_color_suffix}" + echo "==================================" && echo +} + +Set_password(){ + echo "请输入 Shadowsocks Rust 密码 [0-9][a-z][A-Z]" + read -e -p "(默认:随机生成):" password + [[ -z "${password}" ]] && password=$(tr -dc A-Za-z0-9