-
Notifications
You must be signed in to change notification settings - Fork 4
/
iSH-Tools-Setup-CN.sh
239 lines (226 loc) · 9.46 KB
/
iSH-Tools-Setup-CN.sh
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
#!/bin/sh
# Moded by lurenJBD 2024.07.09
# iSH-Tools by lurenJBD 2020.10.17
########### Variable ###########
HOST="${HOST:="www.baidu.com"}"
NAMESERVER="${NAMESERVER:="223.5.5.5"}"
Github_Url="${Github_Url:="https://github.com"}"
Mirror_Url="${Mirror_Url:="https://mirror.ghproxy.com/https://github.com"}"
Mirror_Repo="${Mirror_Repo:="http://mirrors.tuna.tsinghua.edu.cn"}" # 默认替换的镜像源链接
Bypass_Check="${Bypass_Check:=0}" # 1:跳过网络&地区检查 Net:只跳过网络检查 Loc:只跳过地区检查
inite_repo="wget ncurses openrc bash"
RED='\033[0;31m'
GREEN='\033[32m'
YELLOW='\033[0;33m'
CYAN='\033[0;36m'
PLAIN='\033[0m'
echo_ERROR="echo -e "${RED}ERROR${PLAIN}""
echo_OK="echo -e "${GREEN}OK${PLAIN}""
########### Function ###########
# 提示打印函数
printf_tips() {
local level=$1 context=$2 end_line=$3
case $level in
info) printf "${CYAN}%-7s${PLAIN} %-8s $end_line" "INFO" "$context";;
warning) printf "${YELLOW}%-7s${PLAIN} %-8s $end_line" "WARNING" "$context";;
error) printf "${RED}%-7s${PLAIN} %-8s $end_line" "ERROR" "$context";;
esac
}
# 检查网络状况
check_connection() {
[[ $enable_check_connection -eq 0 ]] && return 0
ping_host() {
ping -4 -c 1 -w 1 -A $1 &>/dev/null && ping_success=true
}
nslookup_addr() {
ipv4_addresses=$(nslookup $1 $2 2>/dev/null | awk '/^Address: / { split($2, parts, ":"); if (parts[1] ~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) print parts[1] }')
}
DNSADDR=$(grep '^nameserver' /etc/resolv.conf 2>/dev/null | head -n 1 | awk '{print $2}')
if [ ! -n "$DNSADDR" ]; then
printf_tips warning "未设置DNS地址,自动指定DNS为${NAMESERVER}" "\n"
echo "nameserver ${NAMESERVER}" > /etc/resolv.conf
DNSADDR=$NAMESERVER
fi
printf_tips info "正在检查网络状况..."
ping_success=false
if which nslookup >/dev/null 2>&1 ; then
nslookup_addr $HOST $DNSADDR
if [ ! -n "$ipv4_addresses" ]; then
printf_tips warning "DNS解析失败,修改DNS为${NAMESERVER}再次尝试" "\n"
nslookup_addr $HOST $NAMESERVER
echo "nameserver ${NAMESERVER}" > /etc/resolv.conf
fi
for ip in $ipv4_addresses; do
ping_host $ip
[ "$ping_success" = "true" ] && break
done
else
ping_host $HOST
fi
[ "$ping_success" = "false" ] && $echo_ERROR && return 1
$echo_OK
}
# 检查所属地区,决定是否使用镜像站
check_location() {
[[ $enable_check_location -eq 0 ]] && return 0
location=$(wget -T10 -qO- https://cf-ns.com/cdn-cgi/trace 2>/dev/null | awk -F'=' '/^loc=/{print $2}')
if [ -z "$location" ]; then
location=$(wget -T10 -qO- https://myip.ipip.net/ 2>/dev/null | awk -F ':' '{print $3}' | awk -F ' ' '{print $1}')
fi
if [[ "$location" == "CN" || "$location" == "中国" ]]; then
printf_tips info "根据当前网络环境,建议临时更换镜像源并使用GitHub镜像站" "\n"
read -p "[*] 是否要使用镜像源? [Y/N]" user_choice && printf '\033[A' && printf '\r\033[K'
case $user_choice in
[yY])
Github_Url=$Mirror_Url
mv /etc/apk/repositories /etc/apk/repositories-tmp.bak
echo "${Mirror_Repo}/alpine/${alpine_version}/main" >> /etc/apk/repositories
echo "${Mirror_Repo}/alpine/${alpine_version}/community" >> /etc/apk/repositories
;;
[nN]|*)
printf_tips warning "已选择不使用镜像源,如果访问缓慢可以再次运行本工具修改" "\n"
;;
esac
fi
}
# 计时器
run_timer() {
sleep 0.5
local command_name=$1
local command_pid=$(ps | grep "$command_name" | grep -v grep | awk '{print $1}')
local timeout=$2
local timeout_tip=$3
local execution_time=0
wait $command_pid 2>/dev/null
while kill -0 $command_pid 2>/dev/null; do
sleep 1
let execution_time+=1
if [ "$execution_time" -gt "$timeout" ]; then
printf "\n${YELLOW}%-7s${PLAIN} %-8s" "WARNING" "超过${timeout}s未完成安装,${timeout_tip}"
break
fi
done
}
# 安装&检查软件包
set_apk_repo() {
run_timer "apk add" 30 "可能是源下载太慢,建议使用镜像源" &
apk add -q ${inite_repo}
printf '\r\033[A' && printf_tips info "正在安装需要的软件包..."
if [ $? = 0 ]; then
sed -i "s#::sysinit:/sbin/openrc sysinit#::sysinit:/sbin/openrc#g" /etc/inittab
echo inited_repo=\"$inite_repo\" >>/etc/iSH-Tools/tools_inited
$echo_OK
else
$echo_ERROR
printf_tips error "安装软件包失败,请检查网络并重试" "\n" && exit 1
fi
}
# 脚本退出前执行
shutdown() {
printf '\r\033[K'
if [ -e /etc/apk/repositories-tmp.bak ]; then
mv /etc/apk/repositories-tmp.bak /etc/apk/repositories
fi
}
trap shutdown EXIT
# 初始化并安装最新版iSH-Tools
run_main() {
# 获取运行环境信息
ish_type=$(cat /proc/ish/version 2>/dev/null | awk '{print $1}')
ish_ver=$(cat /proc/ish/version 2>/dev/null | awk '{print $2 " " $3}')
if [[ -z "$ish_ver" ]]; then
ish_ver=$(sed 's/.* iSH \([0-9]\.[0-9]\.[0-9]\) (\([0-9]\{1,\}\)) \(.*\)/\1 (\2)/' /proc/version)
fi
if ! [[ "$ish_type" =~ "iSH" ]]; then
printf_tips error "未知的iSH版本,脚本尚未支持" "\n" && exit 1
fi
alpine_version=$(awk -F. '{if ($1 == 3) print "v3."$2}' /etc/alpine-release)
if [[ -z "$alpine_version" ]]; then
printf_tips error "非alpine系统,脚本不支持运行" "\n" && exit 1
fi
# 2.X版本的旧配置文件清理
if [ -e /opt/iSH-VNC/VNC_installed ]; then
sed -i 's/^installed_DE=\(.*\)/installed_DE="\1"/' /opt/iSH-VNC/VNC_installed
source /opt/iSH-VNC/VNC_installed && echo installed_apk_repo=\"$installed_DE\" > /etc/iSH-Tools/VNC_installed
source /opt/iSH-VNC/VNC_installed_name && echo installed_apk_name=$installed_DE_name >> /etc/iSH-Tools/VNC_installed
rm -rf /opt/iSH-VNC
fi
enable_check_connection=1; enable_check_location=1;
case $Bypass_Check in
All|true|1) enable_check_connection=0; enable_check_location=0 ;;
Net|network) enable_check_connection=0 ;;
Loc|location) enable_check_location=0 ;;
None|false|0) ;; # Defaults
*) unset Bypass_Check ;;
esac
# 进行网络连接检查
check_connection || { printf_tips error "无网络,无法安装/更新 iSH-Tools" "\n" && exit 1; }
check_location
printf_tips info "正在获取 iSH-Tools 最新版本号..."
lastest_version=$(wget -T10 -qO- ${Github_Url}/lurenJBD/iSH-Tools/raw/main/lastest_version 2>/dev/null | awk -F'=' '/^lastest_version=/{print $2}')
installed_tip="安装"
if [[ -z "$lastest_version" ]]; then
$echo_ERROR && printf_tips error "获取 iSH-Tools 最新版本号失败,请检查网络并重试" "\n" && exit 1
else
$echo_OK
fi
# 安装基础依赖包
if [ ! -e /etc/iSH-Tools/tools_inited ];then
mkdir -p /etc/iSH-Tools
printf_tips info "正在安装需要的软件包..." "\n"
set_apk_repo
else
source /etc/iSH-Tools/tools_inited
if [[ -n "$inited_version" ]]; then
printf_tips info "检测到已安装 iSH-Tools $inited_version 版本" "\n"
if [ $(echo "$lastest_version > $inited_version" | bc) -eq 1 ]; then
printf_tips info "检查到新版本 ${lastest_version}" "\n"
rm -f /usr/local/bin/iSH-Tools && sed -i '/inited_version/d' /etc/iSH-Tools/tools_inited
installed_tip="更新"
for word in $inite_repo; do
if ! echo "$inited_repo" | grep -q "$word"; then
need_inite_repo=1
break
fi
done
if [ "$need_inite_repo" = 1 ]; then
rm -f /etc/iSH-Tools/tools_inited
set_apk_repo
fi
fi
if [ $(echo "$lastest_version < $inited_version" | bc) -eq 1 ]; then
printf_tips info "远端最新版本为${lastest_version}, 低于已安装版本, 疑似数据错乱, 脚本自动退出" "\n"
return 1
fi
fi
fi
# 下载 工具文件
if [ ! -f "/etc/iSH-Tools/get_local_ip" ]; then
printf_tips info "正在下载 iSH-Tools 工具文件..."
wget -T15 -qO /etc/iSH-Tools/get_local_ip ${Github_Url}/lurenJBD/iSH-Tools/releases/download/Tools/get_local_ip
if [ $? = 0 ]; then
chmod +x /etc/iSH-Tools/get_local_ip
$echo_OK
else
rm -f /etc/iSH-Tools/get_local_ip
$echo_ERROR
fi
fi
# 下载 iSH-Tools
if [ ! -e /usr/local/bin/iSH-Tools ];then
printf_tips info "正在${installed_tip} iSH-Tools ..."
wget -T15 -qO /usr/local/bin/iSH-Tools ${Github_Url}/lurenJBD/iSH-Tools/raw/main/iSH-Tools-CN.sh
if [ $? = 0 ]; then
echo inited_version=$lastest_version >>/etc/iSH-Tools/tools_inited
chmod +x /usr/local/bin/iSH-Tools
$echo_OK
else
rm -f /usr/local/bin/iSH-Tools
$echo_ERROR
printf_tips error "下载 iSH-Tools 失败,请检查网络并重试" "\n" && exit 1
fi
fi
printf_tips info "${installed_tip} iSH-Tools ${lastest_version} 成功,输入 iSH-Tools 开始使用" "\n"
}
########### Main ###########
run_main