2020-09-13 19:33:39 +08:00
|
|
|
|
#! /bin/bash
|
2020-08-03 15:12:16 +08:00
|
|
|
|
# Copyright (C) Juewuy
|
|
|
|
|
|
2020-10-25 13:48:22 +08:00
|
|
|
|
echo='echo -e' && [ -n "$(echo -e|grep e)" ] && echo=echo
|
2020-10-11 21:30:20 +08:00
|
|
|
|
[ -z "$1" ] && test=0 || test=$1
|
2020-09-21 23:51:03 +08:00
|
|
|
|
|
2020-08-03 15:12:16 +08:00
|
|
|
|
echo "***********************************************"
|
|
|
|
|
echo "** 欢迎使用 **"
|
2020-09-13 19:33:39 +08:00
|
|
|
|
echo "** ShellClash **"
|
2020-08-03 15:12:16 +08:00
|
|
|
|
echo "** by Juewuy **"
|
|
|
|
|
echo "***********************************************"
|
2020-09-21 23:51:03 +08:00
|
|
|
|
|
2020-10-11 21:30:20 +08:00
|
|
|
|
#检查root权限
|
2020-10-22 20:53:22 +08:00
|
|
|
|
if [ "$USER" != "root" ];then
|
|
|
|
|
echo 当前用户:$USER
|
|
|
|
|
$echo "\033[31m请尽量使用root用户执行安装!\033[0m"
|
|
|
|
|
echo -----------------------------------------------
|
2020-10-27 09:40:58 +08:00
|
|
|
|
read -p "仍要安装?可能会产生大量未知错误!(1/0) > " res
|
2020-10-22 20:53:22 +08:00
|
|
|
|
[ "$res" != "1" ] && exit
|
|
|
|
|
fi
|
2020-10-24 21:13:06 +08:00
|
|
|
|
webget(){
|
|
|
|
|
#参数【$1】代表下载目录,【$2】代表在线地址
|
|
|
|
|
#参数【$3】代表输出显示,【$4】不启用重定向
|
|
|
|
|
if curl --version > /dev/null 2>&1;then
|
|
|
|
|
[ "$3" = "echooff" ] && progress='-s' || progress='-#'
|
|
|
|
|
[ -z "$4" ] && redirect='-L' || redirect=''
|
|
|
|
|
result=$(curl -w %{http_code} --connect-timeout 5 $progress $redirect -ko $1 $2)
|
|
|
|
|
else
|
|
|
|
|
[ "$3" = "echooff" ] && progress='-q' || progress='-q --show-progress'
|
|
|
|
|
[ "$3" = "echoon" ] && progress=''
|
|
|
|
|
[ -z "$4" ] && redirect='' || redirect='--max-redirect=0'
|
|
|
|
|
wget -Y on $progress $redirect --no-check-certificate --timeout=5 -O $1 $2
|
|
|
|
|
[ $? -eq 0 ] && result="200"
|
|
|
|
|
fi
|
|
|
|
|
}
|
2020-10-11 21:30:20 +08:00
|
|
|
|
#检查更新
|
2020-09-17 14:50:29 +08:00
|
|
|
|
url="https://cdn.jsdelivr.net/gh/juewuy/ShellClash"
|
2020-10-14 17:22:13 +08:00
|
|
|
|
if [ "$test" -gt 0 ];then
|
2020-10-25 13:48:22 +08:00
|
|
|
|
url="https://cdn.jsdelivr.net/gh/juewuy/ShellClash@master"
|
2020-12-08 23:25:57 +08:00
|
|
|
|
[ "$test" -eq 2 ] && url="http://192.168.31.31:8080/ShellClash"
|
2020-10-11 21:30:20 +08:00
|
|
|
|
[ "$test" -eq 3 ] && url="http://192.168.123.90:8080/clash-for-Miwifi"
|
2020-09-21 23:51:03 +08:00
|
|
|
|
else
|
2020-11-07 12:08:31 +08:00
|
|
|
|
webget /tmp/clashrelease $url@master/bin/release_version echoon rediroff 2>/tmp/clashrelease
|
2020-12-03 13:06:40 +08:00
|
|
|
|
[ "$result" = "200" ] && release_new=$(cat /tmp/clashrelease | head -1)
|
2020-10-24 21:13:06 +08:00
|
|
|
|
[ -z "$release_new" ] && release_new=master
|
|
|
|
|
url=$url@$release_new
|
2020-09-21 23:51:03 +08:00
|
|
|
|
fi
|
2020-10-24 21:13:06 +08:00
|
|
|
|
webget /tmp/clashversion $url/bin/version echooff
|
2020-10-25 10:01:58 +08:00
|
|
|
|
[ "$result" = "200" ] && versionsh=$(cat /tmp/clashversion | grep "versionsh" | awk -F "=" '{print $2}')
|
2020-10-24 21:13:06 +08:00
|
|
|
|
[ -z "$release_new" ] && release_new=$versionsh
|
|
|
|
|
rm -rf /tmp/clashversion
|
2020-10-25 10:01:58 +08:00
|
|
|
|
rm -rf /tmp/clashrelease
|
2020-09-17 14:50:29 +08:00
|
|
|
|
[ -z "$release_new" ] && echo "无法连接服务器!" && exit
|
2020-10-24 21:13:06 +08:00
|
|
|
|
|
|
|
|
|
tarurl=$url/bin/clashfm.tar.gz
|
|
|
|
|
|
2020-09-21 23:51:03 +08:00
|
|
|
|
gettar(){
|
2020-10-24 21:13:06 +08:00
|
|
|
|
webget /tmp/clashfm.tar.gz $tarurl
|
2020-09-21 23:51:03 +08:00
|
|
|
|
[ "$result" != "200" ] && echo "文件下载失败!" && exit 1
|
|
|
|
|
#解压
|
2020-10-06 17:56:17 +08:00
|
|
|
|
echo -----------------------------------------------
|
2020-09-21 23:51:03 +08:00
|
|
|
|
echo 开始解压文件!
|
|
|
|
|
mkdir -p $clashdir > /dev/null
|
|
|
|
|
tar -zxvf '/tmp/clashfm.tar.gz' -C $clashdir/
|
2020-10-28 13:49:45 +08:00
|
|
|
|
[ $? -ne 0 ] && echo "文件解压失败!" && rm -rf /tmp/clashfm.tar.gz && exit 1
|
2020-09-21 23:51:03 +08:00
|
|
|
|
#初始化文件目录
|
|
|
|
|
[ -f "$clashdir/mark" ] || echo '#标识clash运行状态的文件,不明勿动!' > $clashdir/mark
|
|
|
|
|
#判断系统类型写入不同的启动文件
|
|
|
|
|
if [ -f /etc/rc.common ];then
|
|
|
|
|
#设为init.d方式启动
|
|
|
|
|
mv $clashdir/clashservice /etc/init.d/clash
|
|
|
|
|
chmod 777 /etc/init.d/clash
|
|
|
|
|
else
|
2020-10-22 20:53:22 +08:00
|
|
|
|
[ -w /etc/systemd/system ] && sysdir=/etc/systemd/system
|
|
|
|
|
[ -w /usr/lib/systemd/system ] && sysdir=/usr/lib/systemd/system
|
2020-09-21 23:51:03 +08:00
|
|
|
|
if [ -n "$sysdir" ];then
|
|
|
|
|
#设为systemd方式启动
|
|
|
|
|
mv $clashdir/clash.service $sysdir/clash.service
|
|
|
|
|
sed -i "s%/etc/clash%$clashdir%g" $sysdir/clash.service
|
|
|
|
|
systemctl daemon-reload
|
|
|
|
|
else
|
|
|
|
|
#设为保守模式启动
|
|
|
|
|
sed -i '/start_old=*/'d $clashdir/mark
|
2020-10-27 09:42:12 +08:00
|
|
|
|
echo start_old=已开启 >> $clashdir/mark
|
2020-09-21 23:51:03 +08:00
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
#修饰文件及版本号
|
|
|
|
|
shtype=sh && [ -n "$(ls -l /bin/sh|grep -o dash)" ] && shtype=bash
|
|
|
|
|
sed -i "s%#!/bin/sh%#!/bin/$shtype%g" $clashdir/start.sh
|
|
|
|
|
chmod 777 $clashdir/start.sh
|
|
|
|
|
sed -i '/versionsh_l=*/'d $clashdir/mark
|
2020-10-27 09:42:12 +08:00
|
|
|
|
echo versionsh_l=$release_new >> $clashdir/mark
|
2020-09-21 23:51:03 +08:00
|
|
|
|
#设置环境变量
|
2020-10-24 21:13:06 +08:00
|
|
|
|
[ -w ~/.bashrc ] && profile=~/.bashrc
|
2020-10-22 20:53:22 +08:00
|
|
|
|
[ -w /etc/profile ] && profile=/etc/profile
|
|
|
|
|
sed -i '/alias clash=*/'d $profile
|
|
|
|
|
echo "alias clash=\"$shtype $clashdir/clash.sh\"" >> $profile #设置快捷命令环境变量
|
|
|
|
|
sed -i '/export clashdir=*/'d $profile
|
|
|
|
|
echo "export clashdir=\"$clashdir\"" >> $profile #设置clash路径环境变量
|
2020-09-21 23:51:03 +08:00
|
|
|
|
#删除临时文件
|
|
|
|
|
rm -rf /tmp/clashfm.tar.gz
|
|
|
|
|
rm -rf $clashdir/clashservice
|
|
|
|
|
rm -rf $clashdir/clash.service
|
|
|
|
|
}
|
2020-10-06 17:56:17 +08:00
|
|
|
|
#下载及安装
|
|
|
|
|
install(){
|
|
|
|
|
echo -----------------------------------------------
|
|
|
|
|
echo 开始从服务器获取安装文件!
|
|
|
|
|
echo -----------------------------------------------
|
|
|
|
|
gettar
|
|
|
|
|
echo -----------------------------------------------
|
|
|
|
|
echo ShellClash 已经安装成功!
|
|
|
|
|
echo -----------------------------------------------
|
|
|
|
|
$echo "\033[33m输入\033[30;47m clash \033[0;33m命令即可管理!!!\033[0m"
|
|
|
|
|
echo -----------------------------------------------
|
|
|
|
|
}
|
|
|
|
|
setdir(){
|
2020-10-22 20:53:22 +08:00
|
|
|
|
echo -----------------------------------------------
|
2020-12-11 22:17:29 +08:00
|
|
|
|
$echo "\033[33m安装ShellClash至少需要预留约1MB的磁盘空间\033[0m"
|
2020-10-22 20:53:22 +08:00
|
|
|
|
$echo " 1 在\033[32m/etc目录\033[0m下安装(适合路由设备)"
|
|
|
|
|
$echo " 2 在\033[32m/usr/share目录\033[0m下安装(适合大多数设备)"
|
|
|
|
|
$echo " 3 在\033[32m当前用户目录\033[0m下安装(适合非root用户)"
|
|
|
|
|
$echo " 4 手动设置安装目录"
|
|
|
|
|
$echo " 0 退出安装"
|
2020-10-06 17:56:17 +08:00
|
|
|
|
echo -----------------------------------------------
|
2020-08-03 15:12:16 +08:00
|
|
|
|
read -p "请输入相应数字 > " num
|
2020-09-21 23:51:03 +08:00
|
|
|
|
#设置目录
|
2020-08-03 15:12:16 +08:00
|
|
|
|
if [ -z $num ];then
|
|
|
|
|
echo 安装已取消
|
|
|
|
|
exit;
|
2020-09-13 19:33:39 +08:00
|
|
|
|
elif [ "$num" = "1" ];then
|
2020-08-03 15:35:45 +08:00
|
|
|
|
dir=/etc
|
2020-09-13 19:33:39 +08:00
|
|
|
|
elif [ "$num" = "2" ];then
|
2020-10-22 20:53:22 +08:00
|
|
|
|
dir=/usr/share
|
|
|
|
|
elif [ "$num" = "3" ];then
|
2020-10-24 21:13:06 +08:00
|
|
|
|
dir=~/.local/share
|
|
|
|
|
mkdir -p ~/.config/systemd/user
|
2020-10-22 20:53:22 +08:00
|
|
|
|
elif [ "$num" = "4" ];then
|
2020-10-06 17:56:17 +08:00
|
|
|
|
echo -----------------------------------------------
|
2020-08-03 15:12:16 +08:00
|
|
|
|
echo '可用路径 剩余空间:'
|
2020-10-22 20:53:22 +08:00
|
|
|
|
df -h | awk '{print $6,$4}'| sed 1d
|
2020-08-03 15:35:45 +08:00
|
|
|
|
echo '路径是必须带 / 的格式,写入虚拟内存(/tmp,/sys,..)的文件会在重启后消失!!!'
|
2020-08-03 15:12:16 +08:00
|
|
|
|
read -p "请输入自定义路径 > " dir
|
2020-10-06 17:56:17 +08:00
|
|
|
|
if [ -z "$dir" ];then
|
2020-10-22 20:53:22 +08:00
|
|
|
|
$echo "\033[31m路径错误!请重新设置!\033[0m"
|
|
|
|
|
setdir
|
2020-08-03 15:12:16 +08:00
|
|
|
|
fi
|
|
|
|
|
else
|
2020-10-06 17:56:17 +08:00
|
|
|
|
echo 安装已取消!!!
|
2020-08-03 15:12:16 +08:00
|
|
|
|
exit;
|
|
|
|
|
fi
|
2020-10-28 13:49:45 +08:00
|
|
|
|
if [ ! -w $dir ];then
|
|
|
|
|
$echo "\033[31m没有$dir目录写入权限!请重新设置!\033[0m" && sleep 1 && setdir
|
|
|
|
|
else
|
|
|
|
|
echo 目标目录磁盘剩余:$(df -h $dir | awk '{print $4}' | sed 1d )
|
|
|
|
|
read -p "确认安装?(1/0) > " res
|
|
|
|
|
[ "$res" = "1" ] && clashdir=$dir/clash || setdir
|
|
|
|
|
fi
|
2020-10-06 17:56:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-21 23:51:03 +08:00
|
|
|
|
#输出
|
2020-10-06 17:56:17 +08:00
|
|
|
|
$echo "最新版本:\033[32m$release_new\033[0m"
|
|
|
|
|
echo -----------------------------------------------
|
|
|
|
|
$echo "\033[44m如遇问题请加TG群反馈:\033[42;30m t.me/clashfm \033[0m"
|
|
|
|
|
$echo "\033[37m支持各种基于openwrt的路由器设备"
|
|
|
|
|
$echo "\033[33m支持Debian、Centos等标准Linux系统\033[0m"
|
2020-08-04 11:04:00 +08:00
|
|
|
|
|
2020-10-06 17:56:17 +08:00
|
|
|
|
if [ -n "$clashdir" ];then
|
|
|
|
|
echo -----------------------------------------------
|
|
|
|
|
$echo "检测到旧的安装目录\033[36m$clashdir\033[0m,是否覆盖安装?"
|
|
|
|
|
$echo "\033[32m覆盖安装时不会移除配置文件!\033[0m"
|
|
|
|
|
read -p "覆盖安装/卸载旧版本?(1/0) > " res
|
|
|
|
|
if [ "$res" = "1" ];then
|
|
|
|
|
install
|
|
|
|
|
elif [ "$res" = "0" ];then
|
|
|
|
|
rm -rf $clashdir
|
|
|
|
|
echo -----------------------------------------------
|
|
|
|
|
$echo "\033[31m 旧版本文件已卸载!\033[0m"
|
|
|
|
|
setdir
|
2020-10-25 16:31:08 +08:00
|
|
|
|
install
|
2020-10-22 20:53:22 +08:00
|
|
|
|
elif [ "$res" = "9" ];then
|
|
|
|
|
echo 测试模式,变更安装位置
|
|
|
|
|
setdir
|
2020-10-25 16:31:08 +08:00
|
|
|
|
install
|
2020-10-06 17:56:17 +08:00
|
|
|
|
else
|
|
|
|
|
$echo "\033[31m输入错误!已取消安装!\033[0m"
|
|
|
|
|
exit;
|
|
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
setdir
|
2020-10-25 23:17:17 +08:00
|
|
|
|
install
|
2020-10-06 17:56:17 +08:00
|
|
|
|
fi
|