-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·499 lines (419 loc) · 13.1 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·499 lines (419 loc) · 13.1 KB
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#!/bin/bash
#==============================================================================
# Mihomo 代理自动安装部署脚本
#
# 功能:
# - 自动下载和安装 Mihomo
# - 配置 systemd 服务
# - 设置环境变量
# - 安装 clash 管理工具
# - 配置 Claude Code 环境
# - 优化节点优先级(Best Practice)
#
# 使用方法:
# ./install.sh # 使用模板配置
# ./install.sh --subscription "订阅链接" # 使用订阅链接
# ./install.sh --config /path/to/config.yaml # 使用自定义配置
#==============================================================================
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 配置变量
MIHOMO_VERSION="latest"
MIHOMO_INSTALL_DIR="/usr/local/bin"
MIHOMO_CONFIG_DIR="/etc/mihomo"
MIHOMO_BINARY="$MIHOMO_INSTALL_DIR/mihomo"
MIHOMO_CONFIG="$MIHOMO_CONFIG_DIR/config.yaml"
MIHOMO_SERVICE="/etc/systemd/system/mihomo.service"
CLASH_SCRIPT="$MIHOMO_INSTALL_DIR/clash"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# 参数解析
SUBSCRIPTION_URL=""
CUSTOM_CONFIG=""
UPDATE_MODE=false
while [[ $# -gt 0 ]]; do
case $1 in
--subscription)
SUBSCRIPTION_URL="$2"
shift 2
;;
--config)
CUSTOM_CONFIG="$2"
shift 2
;;
--update)
UPDATE_MODE=true
shift
;;
*)
echo -e "${RED}未知参数: $1${NC}"
exit 1
;;
esac
done
#==============================================================================
# 工具函数
#==============================================================================
print_header() {
echo -e "${BLUE}================================${NC}"
echo -e "${BLUE}$1${NC}"
echo -e "${BLUE}================================${NC}"
}
print_success() {
echo -e "${GREEN}✓${NC} $1"
}
print_error() {
echo -e "${RED}✗${NC} $1"
}
print_warning() {
echo -e "${YELLOW}⚠${NC} $1"
}
print_info() {
echo -e "${BLUE}ℹ${NC} $1"
}
check_root() {
if [[ $EUID -ne 0 ]]; then
print_error "此脚本需要 root 权限运行"
echo "请使用: sudo $0"
exit 1
fi
}
check_system() {
print_info "检查系统环境..."
# 检查系统类型
if [[ ! -f /etc/os-release ]]; then
print_error "无法识别的操作系统"
exit 1
fi
source /etc/os-release
print_success "系统: $PRETTY_NAME"
# 检查 systemd
if ! command -v systemctl &> /dev/null; then
print_error "系统不支持 systemd"
exit 1
fi
print_success "systemd 支持"
# 检查必要工具
local missing_tools=()
for tool in curl wget tar; do
if ! command -v $tool &> /dev/null; then
missing_tools+=($tool)
fi
done
if [[ ${#missing_tools[@]} -gt 0 ]]; then
print_warning "缺少工具: ${missing_tools[*]}"
print_info "正在安装..."
if command -v apt-get &> /dev/null; then
apt-get update -qq
apt-get install -y -qq ${missing_tools[*]}
elif command -v yum &> /dev/null; then
yum install -y -q ${missing_tools[*]}
else
print_error "无法自动安装缺失工具,请手动安装: ${missing_tools[*]}"
exit 1
fi
fi
print_success "必要工具已安装"
}
detect_architecture() {
local arch=$(uname -m)
case $arch in
x86_64)
echo "amd64"
;;
aarch64|arm64)
echo "arm64"
;;
armv7l)
echo "armv7"
;;
*)
print_error "不支持的架构: $arch"
exit 1
;;
esac
}
download_mihomo() {
print_header "下载 Mihomo"
local arch=$(detect_architecture)
print_info "检测到架构: $arch"
# 获取最新版本
print_info "获取最新版本信息..."
local latest_version=$(curl -s https://api.github.com/repos/MetaCubeX/mihomo/releases/latest | grep '"tag_name"' | sed -E 's/.*"([^"]+)".*/\1/')
if [[ -z "$latest_version" ]]; then
print_warning "无法获取最新版本,使用备用下载方式"
latest_version="v1.18.1"
fi
print_info "最新版本: $latest_version"
# 下载 URL
local download_url="https://github.com/MetaCubeX/mihomo/releases/download/${latest_version}/mihomo-linux-${arch}-${latest_version}.gz"
print_info "下载地址: $download_url"
print_info "正在下载..."
# 创建临时目录
local temp_dir=$(mktemp -d)
cd "$temp_dir"
# 下载文件
if ! curl -L -o mihomo.gz "$download_url"; then
print_error "下载失败"
rm -rf "$temp_dir"
exit 1
fi
print_success "下载完成"
# 解压
print_info "解压文件..."
gunzip mihomo.gz
chmod +x mihomo
# 安装
print_info "安装到 $MIHOMO_BINARY"
mv mihomo "$MIHOMO_BINARY"
# 清理
cd - > /dev/null
rm -rf "$temp_dir"
print_success "Mihomo 安装完成"
# 验证安装
if "$MIHOMO_BINARY" -v &> /dev/null; then
local version=$("$MIHOMO_BINARY" -v | head -1)
print_success "版本: $version"
else
print_error "安装验证失败"
exit 1
fi
}
setup_config() {
print_header "配置 Mihomo"
# 创建配置目录
mkdir -p "$MIHOMO_CONFIG_DIR"
print_success "创建配置目录: $MIHOMO_CONFIG_DIR"
# 配置文件处理
if [[ -n "$SUBSCRIPTION_URL" ]]; then
# 使用订阅链接
print_info "从订阅链接下载配置..."
if curl -L -o "$MIHOMO_CONFIG" "$SUBSCRIPTION_URL"; then
print_success "订阅配置下载完成"
# 优化配置:调整节点优先级
optimize_config
else
print_error "订阅下载失败"
exit 1
fi
elif [[ -n "$CUSTOM_CONFIG" ]]; then
# 使用自定义配置
print_info "使用自定义配置: $CUSTOM_CONFIG"
cp "$CUSTOM_CONFIG" "$MIHOMO_CONFIG"
print_success "配置文件复制完成"
else
# 使用模板配置
print_info "使用模板配置"
if [[ -f "$SCRIPT_DIR/config/mihomo-template.yaml" ]]; then
cp "$SCRIPT_DIR/config/mihomo-template.yaml" "$MIHOMO_CONFIG"
print_success "模板配置复制完成"
print_warning "请手动编辑 $MIHOMO_CONFIG 添加代理节点"
else
print_error "找不到模板配置文件"
exit 1
fi
fi
# 验证配置
print_info "验证配置文件..."
if "$MIHOMO_BINARY" -f "$MIHOMO_CONFIG" -t; then
print_success "配置文件验证通过"
else
print_error "配置文件验证失败"
exit 1
fi
}
optimize_config() {
print_info "优化节点优先级(Best Practice)..."
# 备份原配置
cp "$MIHOMO_CONFIG" "$MIHOMO_CONFIG.backup"
# 使用 Python 或 sed 优化配置
# 这里简化处理,实际可以用更复杂的逻辑
print_info "调整代理组优先级..."
# 确保端口配置正确
sed -i 's/^port:.*/port: 7890/' "$MIHOMO_CONFIG"
sed -i 's/^socks-port:.*/socks-port: 7891/' "$MIHOMO_CONFIG"
print_success "配置优化完成"
}
setup_systemd() {
print_header "配置 systemd 服务"
# 复制服务文件
if [[ -f "$SCRIPT_DIR/config/mihomo.service" ]]; then
cp "$SCRIPT_DIR/config/mihomo.service" "$MIHOMO_SERVICE"
else
# 创建服务文件
cat > "$MIHOMO_SERVICE" << EOF
[Unit]
Description=Mihomo (Clash.Meta) Service
After=network.target
[Service]
ExecStart=$MIHOMO_BINARY -f $MIHOMO_CONFIG -d $MIHOMO_CONFIG_DIR
Restart=always
RestartSec=10
User=root
WorkingDirectory=$MIHOMO_CONFIG_DIR
[Install]
WantedBy=multi-user.target
EOF
fi
print_success "服务文件创建完成"
# 重载 systemd
systemctl daemon-reload
print_success "systemd 重载完成"
# 启用服务
systemctl enable mihomo
print_success "服务已设置为开机自启"
# 启动服务
print_info "启动 Mihomo 服务..."
if systemctl start mihomo; then
print_success "服务启动成功"
else
print_error "服务启动失败"
print_info "查看日志: journalctl -u mihomo -n 50"
exit 1
fi
# 等待服务启动
sleep 2
# 检查服务状态
if systemctl is-active --quiet mihomo; then
print_success "服务运行正常"
else
print_error "服务未正常运行"
systemctl status mihomo
exit 1
fi
}
install_clash_tool() {
print_header "安装 clash 管理工具"
if [[ -f "$SCRIPT_DIR/scripts/clash" ]]; then
cp "$SCRIPT_DIR/scripts/clash" "$CLASH_SCRIPT"
chmod +x "$CLASH_SCRIPT"
print_success "clash 工具安装完成"
else
print_warning "找不到 clash 脚本,跳过安装"
fi
}
setup_environment() {
print_header "配置环境变量"
# 配置代理环境变量
local env_config="
# Mihomo 代理配置
export HTTP_PROXY=http://127.0.0.1:7890
export HTTPS_PROXY=http://127.0.0.1:7890
export ALL_PROXY=socks5://127.0.0.1:7891
export NO_PROXY=localhost,127.0.0.1,::1
"
# 添加到 /etc/profile.d/
echo "$env_config" > /etc/profile.d/mihomo-proxy.sh
chmod +x /etc/profile.d/mihomo-proxy.sh
print_success "系统环境变量配置完成"
# 添加到当前用户的 .bashrc
if [[ -n "$SUDO_USER" ]]; then
local user_home=$(eval echo ~$SUDO_USER)
local bashrc="$user_home/.bashrc"
if [[ -f "$bashrc" ]] && ! grep -q "Mihomo 代理配置" "$bashrc"; then
echo "$env_config" >> "$bashrc"
print_success "用户环境变量配置完成"
fi
fi
}
setup_claude() {
print_header "配置 Claude Code 环境"
if [[ -f "$SCRIPT_DIR/scripts/setup-claude.sh" ]]; then
bash "$SCRIPT_DIR/scripts/setup-claude.sh"
else
print_warning "找不到 Claude Code 配置脚本,跳过"
fi
}
test_proxy() {
print_header "测试代理连接"
# 等待代理启动
sleep 2
# 测试 HTTP 代理
print_info "测试 HTTP 代理..."
if curl -x http://127.0.0.1:7890 -s -o /dev/null -w "%{http_code}" --connect-timeout 10 https://www.google.com | grep -q "200"; then
print_success "HTTP 代理连接正常"
else
print_warning "HTTP 代理连接测试失败(可能是网络问题)"
fi
# 测试控制端口
print_info "测试控制端口..."
if curl -s http://127.0.0.1:9090 > /dev/null 2>&1; then
print_success "控制端口连接正常"
else
print_warning "控制端口连接失败"
fi
}
print_summary() {
print_header "安装完成"
echo ""
echo -e "${GREEN}✓ Mihomo 代理服务已成功安装并启动${NC}"
echo ""
echo -e "${BLUE}代理配置信息:${NC}"
echo -e " HTTP/HTTPS: ${GREEN}http://127.0.0.1:7890${NC}"
echo -e " SOCKS5: ${GREEN}socks5://127.0.0.1:7891${NC}"
echo -e " 控制面板: ${GREEN}http://127.0.0.1:9090${NC}"
echo ""
echo -e "${BLUE}管理命令:${NC}"
echo -e " ${GREEN}clash${NC} - 查看状态"
echo -e " ${GREEN}clash start${NC} - 启动服务"
echo -e " ${GREEN}clash stop${NC} - 停止服务"
echo -e " ${GREEN}clash restart${NC} - 重启服务"
echo -e " ${GREEN}clash logs${NC} - 查看日志"
echo -e " ${GREEN}clash test${NC} - 测试连接"
echo -e " ${GREEN}clash help${NC} - 查看帮助"
echo ""
echo -e "${BLUE}配置文件位置:${NC}"
echo -e " 配置文件: ${GREEN}$MIHOMO_CONFIG${NC}"
echo -e " 服务文件: ${GREEN}$MIHOMO_SERVICE${NC}"
echo ""
echo -e "${YELLOW}提示:${NC}"
echo -e " 1. 运行 ${GREEN}source /etc/profile.d/mihomo-proxy.sh${NC} 或重新登录以使环境变量生效"
echo -e " 2. 如果使用模板配置,请编辑 ${GREEN}$MIHOMO_CONFIG${NC} 添加代理节点"
echo -e " 3. 修改配置后运行 ${GREEN}clash restart${NC} 重启服务"
echo ""
}
#==============================================================================
# 主流程
#==============================================================================
main() {
print_header "Mihomo 代理自动安装脚本"
# 检查权限
check_root
# 检查系统
check_system
# 如果是更新模式
if [[ "$UPDATE_MODE" == true ]]; then
print_info "更新模式"
setup_config
systemctl restart mihomo
print_success "配置更新完成"
exit 0
fi
# 检查是否已安装
if [[ -f "$MIHOMO_BINARY" ]] && systemctl is-active --quiet mihomo; then
print_warning "Mihomo 已安装并运行"
read -p "是否重新安装?(y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
print_info "取消安装"
exit 0
fi
# 停止服务
systemctl stop mihomo
fi
# 执行安装步骤
download_mihomo
setup_config
setup_systemd
install_clash_tool
setup_environment
setup_claude
test_proxy
print_summary
}
# 运行主流程
main "$@"