chore: code clean up

This commit is contained in:
NekoRectifier 2023-12-12 20:02:01 +08:00
parent dac5fd8833
commit 607fa2afde
4 changed files with 59 additions and 36 deletions

View File

@ -1,8 +1,8 @@
# 项目介绍 # 项目介绍
本项目基于[clash-for-linux](https://github.com/wnlen/clash-for-linux)再次开发,增加了 fish 的功能支持。
此项目是通过使用开源项目[clash](https://github.com/Dreamacro/clash)作为核心程序,再结合脚本实现简单的代理功能。 此项目是通过使用开源项目[clash](https://github.com/Dreamacro/clash)作为核心程序,再结合脚本实现简单的代理功能。
主要是为了解决我们在服务器上下载GitHub等一些国外资源速度慢的问题。
# 使用须知 # 使用须知
@ -108,9 +108,6 @@ $ proxy_off
然后检查程序端口、进程以及环境变量`http_proxy|https_proxy`,若都没则说明服务正常关闭。 然后检查程序端口、进程以及环境变量`http_proxy|https_proxy`,若都没则说明服务正常关闭。
<br>
## Clash Dashboard ## Clash Dashboard
- 访问 Clash Dashboard - 访问 Clash Dashboard
@ -127,9 +124,6 @@ $ proxy_off
此 Clash Dashboard 使用的是[yacd](https://github.com/haishanh/yacd)项目详细使用方法请移步到yacd上查询。 此 Clash Dashboard 使用的是[yacd](https://github.com/haishanh/yacd)项目详细使用方法请移步到yacd上查询。
<br>
# 常见问题 # 常见问题
1. 部分Linux系统默认的 shell `/bin/sh` 被更改为 `dash`,运行脚本会出现报错(报错内容一般会有 `-en [ OK ]`)。建议使用 `bash xxx.sh` 运行脚本。 1. 部分Linux系统默认的 shell `/bin/sh` 被更改为 `dash`,运行脚本会出现报错(报错内容一般会有 `-en [ OK ]`)。建议使用 `bash xxx.sh` 运行脚本。

View File

@ -47,4 +47,4 @@ else
exitWithError "Unsupported Linux distribution" exitWithError "Unsupported Linux distribution"
fi fi
echo "CPU architecture: $CpuArch" # echo "CPU architecture: $CpuArch"

View File

@ -33,8 +33,6 @@ export Temp_Dir
# 获取 CLASH_SECRET 值,如果不存在则生成一个随机数 # 获取 CLASH_SECRET 值,如果不存在则生成一个随机数
Secret=${CLASH_SECRET:-$(openssl rand -hex 32)} Secret=${CLASH_SECRET:-$(openssl rand -hex 32)}
#################### 函数定义 #################### #################### 函数定义 ####################
# 自定义action函数实现通用action功能 # 自定义action函数实现通用action功能
@ -73,8 +71,6 @@ if_success() {
fi fi
} }
#################### 任务执行 #################### #################### 任务执行 ####################
## 获取CPU架构信息 ## 获取CPU架构信息
@ -98,7 +94,7 @@ unset NO_PROXY
# create logs folder # create logs folder
if [ -d $Server_Dir"/logs" ]; then if [ -d $Server_Dir"/logs" ]; then
echo "" echo "" > /dev/null
else else
echo -e "\nlogs 文件夹缺失,正在创建..." echo -e "\nlogs 文件夹缺失,正在创建..."
mkdir logs mkdir logs
@ -106,20 +102,17 @@ fi
Actual_Conf="$Conf_Dir/config.yaml" Actual_Conf="$Conf_Dir/config.yaml"
if [ -f $Actual_Conf ]; then
# 获取文件的修改时间(秒级时间戳) # 获取文件的修改时间(秒级时间戳)
modified_time=$(stat -c %Y "$Actual_Conf") modified_time=$(stat -c %Y "$Actual_Conf")
# 获取当前时间(秒级时间戳) # 获取当前时间(秒级时间戳)
current_time=$(date +%s) current_time=$(date +%s)
# 计算文件修改时间距离现在的时间差(秒) # 计算文件修改时间距离现在的时间差(秒)
time_diff=$((current_time - modified_time)) time_diff=$((current_time - modified_time))
# 计算24小时对应的秒数 # 计算24小时对应的秒数
tf_hours=$((24 * 60 * 60)) tf_hours=$((24 * 60 * 60))
if [ -f $Actual_Conf ]; then echo -e "正在检查先前配置文件"
echo -e "\n正在检查先前配置文件"
if [ "$time_diff" -lt "$tf_hours" ]; then if [ "$time_diff" -lt "$tf_hours" ]; then
echo -e "\n当前配置文件有效跳过更新" echo -e "\n当前配置文件有效跳过更新"
else else
@ -141,10 +134,10 @@ PID=`ps -ef | grep [c]lash-linux-a | awk '{print $2}'`
## Prevent clash instance is started again ## Prevent clash instance is started again
if [ -z "$PID" ]; then if [ -z "$PID" ]; then
echo -e "Clash isn't running" echo -e "\nClash 未在运行"
else else
echo "Running clash detected." echo -e "\n已检测到正在运行的 Clash 实例"
echo "Killing clash now. PID:"$PID echo -e "\n正在停止 Clash... PID:"$PID
sudo kill -9 $PID sudo kill -9 $PID
fi fi
@ -172,7 +165,7 @@ fi
# Output Dashboard access address and Secret # Output Dashboard access address and Secret
echo '' echo ''
echo -e "Clash Dashboard 访问地址: http://<ip>:9090/ui" echo -e "Clash Dashboard 访问地址: http://localhost:9090/ui"
echo -e "Secret: ${Secret}" echo -e "Secret: ${Secret}"
echo '' echo ''

View File

@ -1,5 +1,41 @@
#!/bin/bash #!/bin/bash
# 自定义action函数实现通用action功能
success() {
echo -en "\\033[60G[\\033[1;32m OK \\033[0;39m]\r"
return 0
}
failure() {
local rc=$?
echo -en "\\033[60G[\\033[1;31mFAILED\\033[0;39m]\r"
[ -x /bin/plymouth ] && /bin/plymouth --details
return $rc
}
action() {
local STRING rc
STRING=$1
echo -n "$STRING "
shift
"$@" && success $"$STRING" || failure $"$STRING"
rc=$?
echo
return $rc
}
# 函数,判断命令是否正常执行
if_success() {
local ReturnStatus=$3
if [ $ReturnStatus -eq 0 ]; then
action "$1" /bin/true
else
action "$2" /bin/false
exit 1
fi
}
## Clash 订阅地址检测及配置文件下载 ## Clash 订阅地址检测及配置文件下载
# 检查url是否有效 # 检查url是否有效
echo -e '\n正在检测订阅地址...' echo -e '\n正在检测订阅地址...'