1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
| #!/bin/bash
RED="\033[91m" GREEN="\033[92m" YELLOW="\033[93m" BLUE="\033[94m" PLAIN='\033[0m'
colorEcho() { echo -e "${1}${@:2}${PLAIN}" }
check_v4_v6(){ v6=$(curl -s6m8 api64.ipify.org -k) v4=$(curl -s4m8 api64.ipify.org -k) if [[ $v4 = "" ]]; then colorEcho $RED " 检查到您的VPS不支持IPv4,请更换VPS" exit 1 fi IP=$v4 }
colorEcho $YELLOW "正在检查VPS的IP配置环境, 请稍等..."
check_v4_v6
archAffix(){ case "$(uname -m)" in i686|i386) echo 'i686-unknown-linux-musl.tar.xz' ;; x86_64|amd64) echo 'x86_64-unknown-linux-gnu.tar.xz' ;; *armv7*) echo 'armv7-unknown-linux-gnueabihf.tar.xz' ;; *mips64el*) echo 'mips64el-unknown-linux-gnuabi64.tar.xz' ;; *mipsel*) echo 'mipsel-unknown-linux-gnu.tar.xz' ;; *mips*) echo 'mips-unknown-linux-gnu.tar.xz' ;; *) colorEcho $RED " 不支持的CPU架构!" exit 1 ;; esac return 0 }
SS_VERSION="v1.22.0" SS_DOWNLOAD_LINK="https://github.com/shadowsocks/shadowsocks-rust/releases/download/$SS_VERSION/shadowsocks-$SS_VERSION.$(archAffix)"
SS_CONFIG_FILE="/etc/shadowsocks/config.json" SS_SERVICE_FILE="/etc/systemd/system/shadowsocks-rust.service" OS=$(hostnamectl | grep -i system | cut -d: -f2)
check_system(){ colorEcho $BLUE " 当前系统:$OS" if [[ $(id -u) -ne "0" ]]; then colorEcho $RED "请以root身份执行该脚本" exit 1 fi if ! res=$(which systemctl 2>/dev/null); then colorEcho $RED "系统版本过低,请升级到最新版本" exit 1 fi }
setFirewall(){ systemctl status firewalld > /dev/null 2>&1 if [[ $? -eq 0 ]]; then firewall-cmd --permanent --add-port=${PORT}/tcp firewall-cmd --permanent --add-port=${PORT}/udp firewall-cmd --reload else nl=$(iptables -nL | nl | grep FORWARD | awk '{print $1}') if [[ "$nl" != "3" ]]; then iptables -I INPUT -p tcp --dport ${PORT} -j ACCEPT iptables -I INPUT -p udp --dport ${PORT} -j ACCEPT fi fi }
getData(){ read -p " 请设置密码(不输入则随机生成): " PASSWORD [[ -z "$PASSWORD" ]] && PASSWORD=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1) echo "" colorEcho $BLUE " 密码: $PASSWORD" echo ""
while true do read -p " 请设置SS的端口号[1025-65535]: " PORT [[ -z $PORT ]] && PORT="12345" if [[ "${PORT:0:1}" = "0" ]]; then echo -e " ${RED}端口不能以0开头${PLAIN}" exit 1 fi expr $PORT + 0 &>/dev/null if [[ $? -eq 0 ]]; then if [[ $PORT -ge 1025 ]] && [[ $PORT -le 65535 ]]; then echo "" colorEcho $BLUE " 端口号: $PORT" echo "" break else colorEcho $RED " 输入错误,端口号为1025-65535的数字" fi else colorEcho $RED " 输入错误,端口号为1025-65535的数字" fi done
colorEcho $RED " 请选择加密方式: " echo " 1)aes-256-gcm" echo " 2)aes-192-gcm" echo " 3)aes-128-gcm" echo " 4)chacha20-ietf" echo " 5)chacha20-ietf-poly1305" echo " 6)xchacha20-ietf-poly1305" read -p " 请选择(默认aes-256-gcm): " answer if [[ -z $answer ]]; then METHOD="aes-256-gcm" else case $answer in 1) METHOD="aes-256-gcm" ;; 2) METHOD="aes-192-gcm" ;; 3) METHOD="aes-128-gcm" ;; 4) METHOD="chacha20-ietf" ;; 5) METHOD="chacha20-ietf-poly1305" ;; 6) METHOD="xchacha20-ietf-poly1305" ;; *) colorEcho $RED " 无效的选择,使用默认的aes-256-gcm" METHOD="aes-256-gcm" ;; esac fi echo "" colorEcho $BLUE "加密方式: $METHOD" echo "" }
install(){ getData rm -rf /tmp/shadowsocks-rust mkdir -p /tmp/shadowsocks-rust colorEcho $BLUE " 下载shadowsocks-rust: $SS_DOWNLOAD_LINK" curl -sSL -H "Cache-Control: no-cache" -o /tmp/shadowsocks-rust/shadowsocks.tar.xz $SS_DOWNLOAD_LINK if [[ $? -ne 0 ]]; then colorEcho $RED " 下载shadowsocks-rust失败,请检查服务器网络设置" exit 1 fi
colorEcho $BLUE " 安装shadowsocks-rust" mkdir -p '/usr/bin/shadowsocks' '/etc/shadowsocks' && \ tar xf /tmp/shadowsocks-rust/shadowsocks.tar.xz -C /usr/bin/shadowsocks chmod +x '/usr/bin/shadowsocks/ssserver' || { colorEcho $RED " ssserver可执行文件安装失败,请检查权限设置" exit 1 }
colorEcho $BLUE " 配置${SS_CONFIG_FILE}" cat > $SS_CONFIG_FILE <<EOF { "servers": [ { "server": "0.0.0.0", "server_port": ${PORT}, "password": "${PASSWORD}", "method": "${METHOD}", "nameserver": "8.8.8.8", "mode": "tcp_and_udp", "timeout": 300, "fast_open": false } ] } EOF
colorEcho $BLUE " 配置${SS_SERVICE_FILE}" cat > $SS_SERVICE_FILE <<-EOF [Unit] Description=Shadowsocks-Rust Server After=network-online.target Wants=network-online.target
[Service] Type=simple ExecStart=/usr/bin/shadowsocks/ssserver -c /etc/shadowsocks/config.json StandardOutput=journal StandardError=journal SyslogIdentifier=ssserver Restart=always RestartSec=5s StartLimitInterval=100 StartLimitBurst=10
[Install] WantedBy=multi-user.target EOF
setFirewall colorEcho $BLUE " 启动Shadowsocks服务..." systemctl daemon-reload systemctl enable shadowsocks-rust systemctl restart shadowsocks-rust sleep 2 res=$(netstat -tupln | grep -v grep | grep 'ssserver') if [[ $res = "" ]]; then colorEcho $RED " 启动失败,请检查端口是否被占用!" exit 1 else colorEcho $GREEN " 启动成功" info fi }
uninstall(){ echo "" read -p " 确定卸载吗? (y/n) " answer [[ -z $answer ]] && answer="n"
if [[ $answer == "y" ]] || [[ $answer == "Y" ]]; then systemctl stop shadowsocks-rust && systemctl disable shadowsocks-rust rm -rf $SS_SERVICE_FILE rm -rf /usr/bin/shadowsocks rm -rf /etc/shadowsocks colorEcho $GREEN " 卸载成功" fi }
ss_status(){ if [[ ! -f /usr/bin/shadowsocks/ssserver ]]; then echo 0 return fi if [[ ! -f $SS_SERVICE_FILE ]]; then echo 0 return fi res=$(netstat -tupln | grep -v grep | grep 'ssserver') if [[ $res = "" ]]; then echo 1 else echo 2 fi }
ss_status_result(){ res=$(ss_status) case $res in 0) colorEcho $RED " 未安装" ;; 1) colorEcho $RED " 未启动" ;; 2) colorEcho $GREEN " 已启动" ;; *) colorEcho $RED " 未知错误" ;; esac }
info(){ res=$(ss_status) if [[ $res -eq 0 ]]; then colorEcho $RED " 未安装" exit 1 fi
status=$(ss_status_result) port=$(grep server_port $SS_CONFIG_FILE | cut -d: -f2 | tr -d \",' ') password=$(grep password $SS_CONFIG_FILE | cut -d: -f2 | tr -d \",' ') method=$(grep method $SS_CONFIG_FILE | cut -d: -f2 | tr -d \",' ')
echo "" echo -e " ${BLUE}运行状态${PLAIN}:${status}" echo -e " ${BLUE}配置文件:${PLAIN}${RED}$SS_CONFIG_FILE${PLAIN}" echo "" echo -e " ${RED}配置信息 ${PLAIN}" echo -e " ${BLUE}IP(address): ${PLAIN}${RED}${IP}${PLAIN}" echo -e " ${BLUE}端口(port): ${PLAIN}${RED}${port}${PLAIN}" echo -e " ${BLUE}密码(password): ${PLAIN}${RED}${password}${PLAIN}" echo -e " ${BLUE}加密方式(method): ${PLAIN}${RED}${method}${PLAIN}" }
start(){ res=$(ss_status) if [[ $res -eq 0 ]]; then colorEcho $RED " Shadowsocks未安装,请先安装!" return fi systemctl restart shadowsocks-rust res=$(netstat -tupln | grep -v grep | grep 'ssserver') if [[ $res = "" ]]; then colorEcho $RED " 启动失败,请检查端口是否被占用!" else colorEcho $GREEN " 启动成功" fi }
stop(){ res=$(ss_status) if [[ $res -eq 0 ]]; then colorEcho $RED " Shadowsocks未安装,请先安装!" return fi systemctl stop shadowsocks-rust colorEcho $GREEN " 停止成功" }
restart(){ res=$(ss_status) if [[ $res -eq 0 ]]; then colorEcho $RED " Shadowsocks未安装,请先安装!" return fi stop start }
log(){ res=$(ss_status) if [[ $res -eq 0 ]]; then colorEcho $RED " Shadowsocks未安装,请先安装!" return fi journalctl -xen -u shadowsocks-rust --no-pager }
menu(){ clear colorEcho $BLUE "==================================================" colorEcho $BLUE " Shadowsocks一键安装 " colorEcho $BLUE " 脚本基于shadowsocks-rust " colorEcho $BLUE "==================================================" colorEcho $GREEN " 1.安装 " colorEcho $GREEN " 2.卸载 " colorEcho $BLUE "==================================================" colorEcho $GREEN " 3.启动 " colorEcho $GREEN " 4.停止 " colorEcho $GREEN " 5.重启 " colorEcho $BLUE "==================================================" colorEcho $GREEN " 6.查看配置 " colorEcho $GREEN " 7.查看日志 " colorEcho $BLUE "==================================================" colorEcho $GREEN " 0.退出 " colorEcho $BLUE "==================================================" echo -n " 当前状态:" ss_status_result echo ""
read -p " 请选择操作[0-7]: " answer case $answer in 1) install ;; 2) uninstall ;; 3) start ;; 4) stop ;; 5) restart ;; 6) info ;; 7) log ;; 0) exit 0 ;; *) colorEcho $RED " 请输入正确的数字[0-3]" exit 1 ;; esac }
check_system action=$1 [[ -z $1 ]] && action=menu case "$action" in menu|install|uninstall|info|start|stop|restart|log) $action ;; *) colorEcho $RED " 参数错误!" colorEcho $BLUE " 用法:$(basename $0) [menu|install|uninstall|info|start|stop|restart|log]" ;; esac
|