This repository has been archived by the owner on Nov 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmiclash
executable file
·443 lines (422 loc) · 15.1 KB
/
bmiclash
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
#!/bin/bash
#8Mi-ClashLCI <Powered By Clash>
# custom command
# 统一请求选择器 取自配置项 dl_engine
# bmi_downloadHub "下载地址" “文件名称 或 -” “下载引擎(可选)” "重试 boll"
bmi_debug() {
eval $1 2>./console_error.tmp || bmi_debugError $1
}
bmi_debugError() {
echo "————————————————————————————————"
bmi_echo "完了~巴比Q了~完了完了~我丢~哇焯~这都啥错误啊~我裂开了~"
bmi_echo '执行错误的指令 '"$1"
bmi_echo '执行错误的输出:'
cat ./console_error.tmp
bmi_echo "前往 https://github.com/8Mi-Tech/8Mi-ClashLCI/issues 反馈并上传截图 以供方便处理"
echo "————————————————————————————————"
rm ./console_error.tmp
exit 1
}
bmi_downloadHub() {
if [ $3 ]; then
bmi_downloadHub_EngineID=$3
else
bmi_downloadHub_EngineID="`bmi_clash_getControlConfig dl_engine`"
#echo "未设置,从配置内读取"$bmi_downloadHub_EngineID
fi
case $bmi_downloadHub_EngineID in
"axel")
axel -a -o "$2" "$1" 1>&1
bmi_downloadHubError=$?
;;
"wget")
wget --show-progress --progress=bar:force -O "$2" "$1" 1>&1
bmi_downloadHubError=$?
;;
"curl")
curl -# -o "$2" "$1" 1>&1
bmi_downloadHubError=$?
;;
"")
bmi_echo "下载引擎未设置,请到 clash_settings.config 内设置 dl_engine=[axel|wget|*curl]"
;;
*)
bmi_echo "$bmi_downloadHub_EngineID""? 这是什么"
;;
esac
if [ $4 ]; then
bmi_downloadHub_Retry=$4
else
bmi_downloadHub_Retry=false
fi
if [ $bmi_downloadHub_Retry = "true" ] | [ $bmi_downloadHubError != 0 ]; then
bmi_downloadHub "$1" "$2" "$3" "$4"
fi
}
bmi_echo() {
echo "[8Mi-ClashLCI] ""$1"
}
bmi_setSystemProxy() {
if [ $1 == true ]; then
if [ "`command -v gsettings`" ]; then
gsettings set org.gnome.system.proxy mode 'manual'
gsettings set org.gnome.system.proxy.http port 7899
gsettings set org.gnome.system.proxy.http host '127.0.0.1'
gsettings set org.gnome.system.proxy.https port 7899
gsettings set org.gnome.system.proxy.https host '127.0.0.1'
gsettings set org.gnome.system.proxy.socks port 7899
gsettings set org.gnome.system.proxy.socks host '127.0.0.1'
gsettings set org.gnome.system.proxy ignore-hosts "['localhost', '127.0.0.0/8', '::1']"
else
`pwd`/SetSystemProxy.sh default true
fi
if [ "`command -v apt`" ]; then
bmi_echo "检测到系统有APT,正在为APT设置代理"
`pwd`/SetSystemProxy.sh apt true
fi
else
if [ "`command -v gsettings`" ]; then
gsettings set org.gnome.system.proxy mode 'none'
else
`pwd`/SetSystemProxy.sh default false
fi
if [ "`command -v apt`" ]; then
bmi_echo "检测到系统有APT,正在为APT取消代理"
`pwd`/SetSystemProxy.sh apt false
fi
fi
}
bmi_setThisProxy(){
default_ip="127.0.0.1:7899"
if [ $1 == true ]; then
http_proxy="http://"$default_ip
https_proxy="http://"$default_ip
SOCKS_SERVER="socks5://"$default_ip
else
http_proxy=""
https_proxy=""
SOCKS_SERVER=""
fi
}
bmi_help_ClashLCI() {
case $1 in
"")
bmi_echo "当前运行状态 "`bmi_clashIsRunning_bool`
bmi_echo '未知的指令 不过可以看看如下帮助
用法: bmiclash [install|start|stop|reload]'
;;
"reload")
bmi_echo '重载指令帮助
用法: bmiclash reload [config|ui]
重载指令详细讲解:
- 配置文件: bmiclash reload config
- UI界面: bmiclash reload ui '
;;
"setconfig")
bmi_echo '设置配置帮助
(参数标注双引号的说明指令内需要包含双引号)
订阅链接: ./bmiclash set sub_link "*"
下载器类型: ./bmiclash set dl_engine [wget|curl|axel]
二进制文件更新频道: ./bmiclash set binary_update_channel [stable|premium]
GitHub镜像地址(结尾不要加斜杠): ./bmiclash set github_url "*"
(注:部分加速地址非专门为GitHub加速,请记得插入github.com 比如 https://github.do/https://github.com)'
;;
esac
}
bmi_clashIsRunning() {
tmux_result=`tmux ls 2>&1`
echo $tmux_result | grep "clash: " >/dev/null
if [ $? == 0 ]; then
echo 1
else
echo 0
fi
}
bmi_clashIsRunning_bool() {
tmux_result=`tmux ls 2>&1`
echo $tmux_result | grep "clash: " >/dev/null
if [ $? == 0 ]; then
echo true
else
echo false
fi
}
bmi_clash_getConfig() {
bmi_downloadHub "`bmi_clash_getControlConfig sub_link`" "config.yaml"
if [ 0 -ne $bmi_downloadHubError ]; then
if [ $bmi_clash_get_failed_count == 0 ]; then
bmi_echo "获取失败,正在重试(直到提示成功即可)"
fi
bmi_clash_get_failed_count=`(( $bmi_clash_get_failed_count + 1 ))`
bmi_clash_getConfig
else
bmi_clash_get_failed_count=0
bmi_echo "获取成功,正在检查配置文件"
fi
}
bmi_clash_addConfig() {
if [ "`grep $1 config.yaml 2>&1`" = "" ]; then
if [ $blank == 0 ]; then
echo -e "\n"$1": "$2 >>./config.yaml
blank=1
else
echo $1": "$2 >>./config.yaml
fi
bmi_echo "设置参数"\"$1\""为"\"$2\"
fi
}
bmi_startClash() {
bmi_setThisProxy false
if [ `bmi_clashIsRunning` == 1 ]; then
bmi_echo "Clash已加载,若需要重载配置,请输入 \" bmiclash reload config \"" &
exit
fi
bmi_echo "正在加载Clash中,请稍等"
env bmi_clash_tmux=true tmux new-session -s clash -n core -d
bmi_clash_get_failed_count=0
bmi_reloadClashConfig
sleep 5
if [ ! "$(ps aux | grep "./clash" | grep -v "grep")" ]; then
bmi_stopClash
bmi_echo "加载失败,请检查网络环境以及配置文件下载地址是否正确"
exit 1
fi
tmux new-window -t clash -n setting
tmux send-keys -t clash:setting '#8Mi-ClashLCI<Powered By Clash>' Enter
bmi_reloadClashUI
bmi_echo "正在调整系统设置里的系统代理参数"
bmi_setSystemProxy true
bmi_echo "系统代理参数设置成功"
}
bmi_clash_whenRunning() {
if [ $TMUX ] && [ $bmi_clash_tmux ]; then
./clash -d ./
else
bmi_echo "调试??一边玩去 (doge)"
fi
}
bmi_reloadClash() {
if [ `bmi_clashIsRunning` == 0 ]; then
bmi_echo "Clash未加载,无效操作" & exit 1
fi
bmi_echo "正在加载(或重载)Clash主程序"
tmux send-keys -t clash:core C-c " ./bmiclash start binary" Enter
bmi_echo "Clash主程序以及界面程序加载完毕(也许是)"
}
bmi_reloadClashConfig() {
if [ `bmi_clashIsRunning` == 0 ]; then
bmi_echo "Clash未加载,无效操作" & exit 1
fi
bmi_echo "开始获取订阅配置文件中 ( 若显示进度条并且提示100%则提示获取成功 ) "
bmi_setThisProxy false
bmi_clash_get_failed_count=0
bmi_clash_getConfig
blank=0
bmi_clash_addConfig "mixed-port" "7899"
bmi_clash_addConfig "allow-lan" "false"
bmi_clash_addConfig "mode" "Rule"
bmi_clash_addConfig "log-level" "info"
bmi_clash_addConfig "external-controller" "127.0.0.1:9090"
bmi_echo "设置完毕"
bmi_reloadClash
}
bmi_reloadClashUI() {
if [ `bmi_clashIsRunning` == 0 ]; then
bmi_echo "Clash未加载,无效操作" & exit 1
fi
bmi_echo "正在加载(或重载)Clash界面程序"
#tmux send-keys -t clash:setting C-c 'browser -enable-gpu-rasterization --disable-pings --media-router=0 --enable-remote-extensions --load-extension= --window-size=1280,720 --app='`grep "clash_dashboard=" clash_settings.config | sed 's/clash_dashboard=//g') Enter
chmod +x "./8Mi-ClashLUI"
tmux send-keys -t clash:setting C-c " ./8Mi-ClashLUI" Enter
bmi_echo "Clash界面程序加载完毕(或许是)"
}
bmi_stopClash() {
if [ `bmi_clashIsRunning` == 0 ]; then
bmi_echo "Clash已停止"
exit
fi
bmi_echo "正在停止Clash主程序以及界面程序"
bmi_setSystemProxy false
tmux kill-session -t clash
bmi_echo "停止完毕"
}
bmi_clash_getControlConfig() {
if [ "`grep "$1=" clash_settings.config`" ];then
grep "$1=" clash_settings.config | grep -v "#" | sed "s/$1=//g" 1>&1 2>&2
else
exit 1
fi
}
bmi_clash_setControlConfig() {
if [ `grep $1"=" clash_settings.config` ]; then
if [ `bmi_clash_getControlConfig $1 | grep "/"` ]; then
bmi_sed_symbol="#"
else
bmi_sed_symbol="/"
fi
sed -i "s"$bmi_sed_symbol$1"="`bmi_clash_getControlConfig $1`$bmi_sed_symbol$1"="$2$bmi_sed_symbol"g" clash_settings.config
else
bmi_echo "无效参数"
exit 1
fi
}
bmi_clash_upgradeBinary() {
bmi_echo "正在检查主控程序更新"
bmi_echo "二进制更新频道 "`bmi_clash_getControlConfig binary_update_channel`
case "`bmi_clash_getControlConfig binary_update_channel`" in
"premium")
bmi_clash_upgradeBinary_JSONFull=`bmi_downloadHub https://api.github.com/repos/Dreamacro/clash/releases - curl true`
bmi_clash_upgradeBinary_JSONCount=`echo $bmi_clash_upgradeBinary_JSONFull | jq -r '. | length'`
for ((i = 0; i < $bmi_clash_upgradeBinary_JSONCount; i++)); do
if [ `echo $bmi_clash_upgradeBinary_JSONFull | jq -r ".[$i].tag_name"` == "premium" ]; then
bmi_clash_upgradeBinary_listFull=`echo $bmi_clash_upgradeBinary_JSONFull | jq -r ".[$i]"`
bmi_clash_Binary_NewVersion="`echo $bmi_clash_upgradeBinary_listFull | jq -r '.name' | awk '{print $2}'`"
break
fi
done
;;
"stable")
bmi_clash_upgradeBinary_listFull=`bmi_downloadHub https://api.github.com/repos/Dreamacro/clash/releases - curl | jq -r '.[0]'`
bmi_clash_Binary_NewVersion=`echo $bmi_clash_upgradeBinary_listFull | jq -r '.tag_name'`
;;
esac
if [ "`bmi_clash_getControlConfig binary_version`" == "$bmi_clash_Binary_NewVersion" ]&&[ -e "./clash" ]; then
bmi_echo "当前版本为最新,无需再次更新." & exit 1
else
bmi_echo "最新版本为"$bmi_clash_Binary_NewVersion
fi
bmi_clash_upgradeBinary_listAssets=`echo $bmi_clash_upgradeBinary_listFull | jq -r '.assets'`
bmi_clash_upgradeBinary_listCount=0
bmi_clash_upgradeBinary_listCount=`echo $bmi_clash_upgradeBinary_listAssets | jq -r '. | length'`
for ((i = 0; i < $bmi_clash_upgradeBinary_listCount; i++)); do
if [ `echo $bmi_clash_upgradeBinary_listAssets | jq -r '.['$i'].browser_download_url' | grep "linux"` ]; then
bmi_clash_Binary_LinkList="$bmi_clash_Binary_LinkList"`echo $bmi_clash_upgradeBinary_listAssets | jq -r '.['$i'].browser_download_url' | grep "linux"``echo '\n'`
fi
done
bmi_clash_Binary_LinkList=`echo -e "$bmi_clash_Binary_LinkList"`
case "`arch`" in
"x86_64")
bmi_clash_upgradeBinary_URL=`echo "$bmi_clash_Binary_LinkList" | grep "amd64" | grep -v "v3"`
;;
esac
bmi_clash_upgradeBinary_URL="`echo $bmi_clash_upgradeBinary_URL | sed 's#https://github.com##g'`"
bmi_downloadHub "`bmi_clash_getControlConfig github_url`$bmi_clash_upgradeBinary_URL" "clash.gz"
gunzip -f clash.gz
chmod +x clash
case "`bmi_clash_getControlConfig binary_update_channel`" in
"premium")
bmi_clash_setControlConfig binary_version `echo $bmi_clash_upgradeBinary_listFull | jq -r '.name' | awk '{print $2}'`
;;
"stable")
bmi_clash_setControlConfig binary_version `echo $bmi_clash_upgradeBinary_listFull | jq -r '.tag_name'`
;;
esac
}
bmi_clash_install() {
if [ -f "./clash" ]; then
bmi_echo "已经安装,请不要重复安装,如需升级,请输入 bmiclash upgrade" & exit
fi
#自动部署前置 通过包管理器一键部署
if [ -x "`command -v apt`" ]; then
bmi_pkg_type="apt"
else if [ -x "`command -v yum`" ]; then
bmi_pkg_type="yum"
else if [ -x "`command -v dnf`" ]; then
bmi_pkg_type="dnf"
fi
fi
fi
case "$bmi_pkg_type" in
"apt")
bmi_echo "初始化APT包管理器相关"
bmi_pkg_inst_cmd="apt install git tmux jq wget curl -y"
if [ "$USER" == "root" ]; then
apt update
eval $bmi_pkg_inst_cmd
else
sudo apt update
eval "sudo "$bmi_pkg_inst_cmd
fi
;;
*)
bmi_echo "不知道怎么初始化,请在项目issues内报告您的发行版" & exit 1
;;
esac
#自动初始化控制器配置项
if [ !-f "clash_settings.config"]; then
cp clash_settings.config.example clash_settings.config
fi
bmi_clash_setControlConfig "dl_engine" "curl"
bmi_clash_setControlConfig "binary_update_channel" "premium"
bmi_clash_setControlConfig "github_url" "https://ghproxy.futils.com/https://github.com"
bmi_clash_setControlConfig "clash_dashboard" "http://clash.razord.top"
#初始化完毕后才进入正式环节
bmi_clash_upgradeBinary
}
bmi_clash_upgrade() {
bmi_echo "检查控制器更新"
git pull || bmi_echo "控制器更新失败,请检查网络..." | exit 1
bmi_clash_upgradeBinary 1>&1 2>&2
}
# run area
if [ "$1" !== "install" ]; then
if [ ! -x "`command -v git`" ]; then
bmi_echo "未安装 Git ,无法执行" &
exit 1
fi
if [ ! -x "`command -v tmux`" ]; then
bmi_echo "未安装 Tmux ,无法执行" &
exit 1
fi
if ! [ -x "`command -v $(bmi_clash_getControlConfig dl_engine) `" ]; then
bmi_echo "配置文件里读取到 dl_engine 为 `bmi_clash_getControlConfig dl_engine`, 但系统无法检测到有本指令的存在,请修改或者安装对应的包再执行本程序" & exit 1
fi
fi
case "$1" in
"install")
bmi_clash_install
;;
"start")
case "$2" in
"binary")
bmi_clash_whenRunning
;;
"")
bmi_startClash
;;
esac
;;
"stop")
bmi_stopClash
;;
"reload")
case "$2" in
"config")
bmi_reloadClashConfig
;;
"ui")
bmi_reloadClashUI
;;
"")
bmi_reloadClash
;;
*)
bmi_help_ClashLCI reload
;;
esac
;;
"set")
if [ $2 ]; then
bmi_clash_setControlConfig $2 $3
else
bmi_help_ClashLCI setconfig
exit 1
fi
;;
"upgrade")
bmi_clash_upgrade 1>&1 2>&2
;;
*)
bmi_help_ClashLCI
;;
esac