-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·726 lines (645 loc) · 25.6 KB
/
install.sh
File metadata and controls
executable file
·726 lines (645 loc) · 25.6 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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#!/bin/bash
# zero-token — 一键安装 & 启动脚本
# 用法: bash install.sh
#
# 修改说明:
# 1. 不再注册 systemd 服务,直接在终端前台运行
# 2. Chrome 不使用 headless 模式,前台打开供用户交互登录
# 3. 已安装时进入可选菜单,按需执行操作
set -e
# ── 配置 ─────────────────────────────────────────────────────
REPO_URL="https://github.com/uplusplus/zero-token.git"
INSTALL_DIR="/opt/zero-token"
MIN_NODE_VER=22
SERVER_PORT="${SERVER_PORT:-8080}"
CDP_PORT="${CDP_PORT:-9333}"
# ── 颜色 ─────────────────────────────────────────────────────
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; CYAN='\033[0;36m'; BOLD='\033[1m'; NC='\033[0m'
info() { echo -e "${CYAN}▸${NC} $*"; }
ok() { echo -e "${GREEN}✔${NC} $*"; }
warn() { echo -e "${YELLOW}⚠${NC} $*"; }
err() { echo -e "${RED}✘${NC} $*" >&2; }
die() { err "$@"; exit 1; }
echo ""
echo -e "${BOLD}┌─────────────────────────────────────┐${NC}"
echo -e "${BOLD}│ zero-token 安装程序 │${NC}"
echo -e "${BOLD}└─────────────────────────────────────┘${NC}"
echo ""
# ── Root 检查 ─────────────────────────────────────────────────
if [ "$(id -u)" -ne 0 ]; then
if command -v sudo &>/dev/null; then
warn "需要 root 权限,使用 sudo 重新执行..."
exec sudo "$0" "$@"
else
die "请以 root 身份运行此脚本"
fi
fi
# ── 检测是否已安装 ────────────────────────────────────────────
ALREADY_INSTALLED=false
if [ -d "$INSTALL_DIR" ] && [ -f "$INSTALL_DIR/dist/server.mjs" ]; then
ALREADY_INSTALLED=true
fi
# ── 已安装:可选菜单 ─────────────────────────────────────────
show_menu() {
echo -e "${BOLD}┌─────────────────────────────────────┐${NC}"
echo -e "${BOLD}│ 已安装,选择操作: │${NC}"
echo -e "${BOLD}└─────────────────────────────────────┘${NC}"
echo ""
echo -e " ${CYAN}1)${NC} 启动 Chrome 并打开 Provider 登录页"
echo -e " ${CYAN}2)${NC} 抓取登录凭据 (onboard)"
echo -e " ${CYAN}3)${NC} 启动 zero-token 服务"
echo -e " ${CYAN}4)${NC} 更新代码 & 重新构建"
echo -e " ${CYAN}5)${NC} 全部执行 (Chrome → 登录 → 抓取 → 启动)"
echo -e " ${CYAN}0)${NC} 退出"
echo ""
read -rp " 请选择 [0-5]: " MENU_CHOICE
}
if [ "$ALREADY_INSTALLED" = true ]; then
# ── 检测 Chrome(菜单需要) ────────────────────────────────
CHROME_PATH=""
CHROME_DATA_DIR="${HOME}/.zero-token/chrome-data"
# 从已安装目录检测 Chrome
detect_chrome_quick() {
local candidates=(
"/opt/google/chrome/google-chrome"
"/usr/bin/google-chrome"
"/usr/bin/google-chrome-stable"
"/usr/bin/chromium"
"/snap/bin/chromium"
)
for p in "${candidates[@]}"; do
if [ -f "$p" ]; then
echo "$p" && return
fi
done
for cmd in google-chrome google-chrome-stable chromium chromium-browser; do
local p
p=$(command -v "$cmd" 2>/dev/null) || continue
echo "$p" && return
done
echo ""
}
CHROME_PATH=$(detect_chrome_quick)
show_menu
case "$MENU_CHOICE" in
1)
# ── 启动 Chrome 并打开登录页 ──────────────────────────
if [ -z "$CHROME_PATH" ]; then
warn "未找到 Chrome/Chromium,无法启动"
exit 1
fi
CHROME_PID=""
CHROME_ARGS=(
--remote-debugging-port="$CDP_PORT"
--user-data-dir="$CHROME_DATA_DIR"
--no-first-run
--no-default-browser-check
--disable-background-networking
--disable-sync
--disable-translate
--remote-allow-origins=*
--no-sandbox
--disable-dev-shm-usage
)
if curl -sfm 2 "http://localhost:$CDP_PORT/json/version" > /dev/null 2>&1; then
ok "Chrome 已在运行 (CDP: http://localhost:$CDP_PORT),跳过启动"
else
if ss -tlnp 2>/dev/null | grep -q ":$CDP_PORT " || netstat -tlnp 2>/dev/null | grep -q ":$CDP_PORT "; then
warn "端口 $CDP_PORT 已被占用,清理残留 Chrome ..."
pkill -f "remote-debugging-port=$CDP_PORT" 2>/dev/null || true
sleep 2
fi
mkdir -p "$CHROME_DATA_DIR"
rm -f "$CHROME_DATA_DIR/SingletonLock" "$CHROME_DATA_DIR/SingletonCookie" 2>/dev/null
"$CHROME_PATH" "${CHROME_ARGS[@]}" > /dev/null 2>&1 &
CHROME_PID=$!
ok "Chrome 启动中 (PID: $CHROME_PID) ..."
info "等待 Chrome 就绪 ..."
for i in $(seq 1 15); do
if curl -sfm 4 "http://localhost:$CDP_PORT/json/version" > /dev/null 2>&1; then
break
fi
if ! kill -0 "$CHROME_PID" 2>/dev/null; then
warn "Chrome 进程已退出"
CHROME_PID=""
break
fi
sleep 1
done
fi
if curl -sfm 5 "http://localhost:$CDP_PORT/json/version" > /dev/null 2>&1; then
ok "Chrome CDP 就绪 (http://localhost:$CDP_PORT)"
PROVIDER_URLS=(
"https://chat.deepseek.com"
"https://claude.ai"
"https://kimi.com"
"https://doubao.com"
"https://xiaomimo.ai"
"https://chat.qwen.ai"
"https://chatglm.cn"
"https://chat.z.ai"
"https://perplexity.ai"
"https://chatgpt.com"
"https://gemini.google.com"
"https://grok.com"
)
info "自动打开 Provider 登录页 ..."
for url in "${PROVIDER_URLS[@]}"; do
curl -sfm 5 -X PUT "http://localhost:$CDP_PORT/json/new?$url" > /dev/null 2>&1 || true
done
ok "已打开 ${#PROVIDER_URLS[@]} 个 Provider 登录页"
else
warn "Chrome CDP 未就绪"
fi
;;
2)
# ── 抓取凭据 ─────────────────────────────────────────
if [ -f "$INSTALL_DIR/scripts/onboard.mjs" ]; then
info "抓取登录凭据 ..."
cd "$INSTALL_DIR"
node scripts/onboard.mjs --all || warn "凭据抓取失败,可手动运行: cd $INSTALL_DIR && node scripts/onboard.mjs"
else
warn "未找到 onboard.mjs"
fi
;;
3)
# ── 启动服务 ─────────────────────────────────────────
info "启动 zero-token 服务 (端口: $SERVER_PORT) ..."
echo -e " ${YELLOW}按 Ctrl+C 停止服务${NC}"
echo ""
cd "$INSTALL_DIR"
export NODE_ENV=production
export SERVER_PORT="$SERVER_PORT"
cleanup() {
echo ""
info "正在停止 ..."
ok "zero-token 已停止"
exit 0
}
trap cleanup INT TERM
node dist/server.mjs
;;
4)
# ── 更新代码 & 重新构建 ──────────────────────────────
info "更新代码 ..."
cd "$INSTALL_DIR"
if [ -d ".git" ]; then
[ -f config.yaml ] && cp config.yaml config.yaml.bak
if ! GIT_SSL_BACKEND=openssl git -c http.lowSpeedLimit=1000 -c http.lowSpeedTime=60 fetch origin main 2>&1; then
warn "fetch 失败,尝试镜像 ..."
git remote set-url origin "https://gh-proxy.com/https://github.com/uplusplus/zero-token.git"
git fetch origin main 2>&1 || warn "所有 fetch 均失败"
fi
git reset --hard origin/main 2>&1 || warn "reset 失败,保留当前版本"
[ -f config.yaml.bak ] && mv config.yaml.bak config.yaml
else
warn "非 git 安装,无法自动更新,请手动重新下载"
exit 1
fi
info "重新安装依赖 ..."
npm ci --include=dev 2>/dev/null || npm install --include=dev
ok "依赖安装完成"
# 确保 tsdown 可用
if [ ! -f "node_modules/.bin/tsdown" ]; then
warn "tsdown 未找到,重新安装 ..."
npm install tsdown@0.21.2
fi
info "重新构建 ..."
npx tsdown
ok "构建完成"
npm prune --omit=dev 2>/dev/null || true
;;
5)
# ── 全部执行 ─────────────────────────────────────────
if [ -z "$CHROME_PATH" ]; then
warn "未找到 Chrome/Chromium,无法启动"
exit 1
fi
# 启动 Chrome
CHROME_PID=""
CHROME_ARGS=(
--remote-debugging-port="$CDP_PORT"
--user-data-dir="$CHROME_DATA_DIR"
--no-first-run
--no-default-browser-check
--disable-background-networking
--disable-sync
--disable-translate
--remote-allow-origins=*
--no-sandbox
--disable-dev-shm-usage
)
if curl -sfm 3 "http://localhost:$CDP_PORT/json/version" > /dev/null 2>&1; then
ok "Chrome 已在运行 (CDP: http://localhost:$CDP_PORT),跳过启动"
else
if ss -tlnp 2>/dev/null | grep -q ":$CDP_PORT " || netstat -tlnp 2>/dev/null | grep -q ":$CDP_PORT "; then
warn "端口 $CDP_PORT 已被占用,清理残留 Chrome ..."
pkill -f "remote-debugging-port=$CDP_PORT" 2>/dev/null || true
sleep 2
fi
mkdir -p "$CHROME_DATA_DIR"
rm -f "$CHROME_DATA_DIR/SingletonLock" "$CHROME_DATA_DIR/SingletonCookie" 2>/dev/null
"$CHROME_PATH" "${CHROME_ARGS[@]}" > /dev/null 2>&1 &
CHROME_PID=$!
ok "Chrome 启动中 (PID: $CHROME_PID) ..."
info "等待 Chrome 就绪 ..."
for i in $(seq 1 15); do
if curl -sfm 4 "http://localhost:$CDP_PORT/json/version" > /dev/null 2>&1; then
break
fi
if ! kill -0 "$CHROME_PID" 2>/dev/null; then
warn "Chrome 进程已退出"
CHROME_PID=""
break
fi
sleep 1
done
fi
if curl -sfm 4 "http://localhost:$CDP_PORT/json/version" > /dev/null 2>&1; then
ok "Chrome CDP 就绪 (http://localhost:$CDP_PORT)"
PROVIDER_URLS=(
"https://chat.deepseek.com"
"https://claude.ai"
"https://kimi.com"
"https://doubao.com"
"https://xiaomimo.ai"
"https://chat.qwen.ai"
"https://chatglm.cn"
"https://chat.z.ai"
"https://perplexity.ai"
"https://chatgpt.com"
"https://gemini.google.com"
"https://grok.com"
)
info "自动打开 Provider 登录页 ..."
for url in "${PROVIDER_URLS[@]}"; do
curl -sfm 4 -X PUT "http://localhost:$CDP_PORT/json/new?$url" > /dev/null 2>&1 || true
done
ok "已打开 ${#PROVIDER_URLS[@]} 个 Provider 登录页"
echo ""
echo -e " ${YELLOW}请在各 Chrome 标签页中登录你需要的平台${NC}"
echo -e " ${YELLOW}登录完成后,按任意键继续...${NC}"
read -n 1 -s -r
echo ""
else
warn "Chrome CDP 未就绪,跳过登录步骤"
fi
# 抓取凭据
if [ -f "$INSTALL_DIR/scripts/onboard.mjs" ]; then
info "抓取登录凭据 ..."
cd "$INSTALL_DIR"
node scripts/onboard.mjs --all || warn "凭据抓取失败,可手动运行: cd $INSTALL_DIR && node scripts/onboard.mjs"
fi
# 启动服务
echo ""
info "启动 zero-token 服务 (端口: $SERVER_PORT) ..."
echo -e " ${YELLOW}按 Ctrl+C 停止服务${NC}"
echo ""
cd "$INSTALL_DIR"
export NODE_ENV=production
export SERVER_PORT="$SERVER_PORT"
cleanup() {
echo ""
info "正在停止 ..."
if [ -n "$CHROME_PID" ] && kill -0 "$CHROME_PID" 2>/dev/null; then
kill "$CHROME_PID" 2>/dev/null || true
ok "Chrome 已停止"
else
info "Chrome 非本脚本启动,保持运行"
fi
ok "zero-token 已停止"
exit 0
}
trap cleanup INT TERM
(
sleep 3
curl -sfm 4 -X PUT "http://localhost:$CDP_PORT/json/new?http://localhost:$SERVER_PORT/health" > /dev/null 2>&1 || true
) &
node dist/server.mjs
;;
0)
info "已退出"
exit 0
;;
*)
warn "无效选择"
exit 1
;;
esac
# 菜单项 1/2/4 执行完后正常退出
exit 0
else
# ── 首次安装流程 ─────────────────────────────────────────────
# ── 1. 检测 & 安装 Node.js ──────────────────────────────────
install_nodejs() {
info "安装 Node.js ${MIN_NODE_VER}.x ..."
if command -v apt-get &>/dev/null; then
apt-get update -qq
apt-get install -y -qq ca-certificates curl gnupg
mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key \
| gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg 2>/dev/null
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_${MIN_NODE_VER}.x nodistro main" \
> /etc/apt/sources.list.d/nodesource.list
apt-get update -qq
apt-get install -y -qq nodejs
elif command -v dnf &>/dev/null; then
dnf install -y "https://rpm.nodesource.com/pub_${MIN_NODE_VER}.x/nodistro/repo/nodesource-release-nodistro-1.noarch.rpm" 2>/dev/null || true
dnf install -y nodejs
elif command -v yum &>/dev/null; then
yum install -y "https://rpm.nodesource.com/pub_${MIN_NODE_VER}.x/nodistro/repo/nodesource-release-nodistro-1.noarch.rpm" 2>/dev/null || true
yum install -y nodejs
elif command -v brew &>/dev/null; then
brew install node
elif command -v pacman &>/dev/null; then
pacman -Sy --noconfirm nodejs npm
else
die "无法自动安装 Node.js,请手动安装 Node.js >= ${MIN_NODE_VER}: https://nodejs.org/"
fi
}
check_node() {
if command -v node &>/dev/null; then
local major
major=$(node -v | sed 's/v//' | cut -d. -f1)
if [ "$major" -ge "$MIN_NODE_VER" ]; then
ok "Node.js $(node -v)"
return 0
fi
warn "Node.js $(node -v) 版本过低,需要 >= ${MIN_NODE_VER}"
fi
install_nodejs
command -v node &>/dev/null || die "Node.js 安装失败"
ok "Node.js $(node -v)"
}
check_node
# ── 2. 安装 & 检测 Chromium ──────────────────────────────────
# 验证 Chrome 路径是否可用(排除损坏的 snap)
verify_chrome() {
local path="$1"
[ ! -f "$path" ] && return 1
# 排除 Ubuntu 24.04 的 snap 壳脚本(/usr/bin/chromium-browser 是个提示安装 snap 的小脚本)
if head -c 512 "$path" 2>/dev/null | grep -q "requires the chromium snap\|snap install chromium\|command_is_valid.*snap"; then
return 1
fi
# snap 包:检查 snap revision 目录是否存在
if [[ "$path" == /snap/bin/* ]]; then
local snap_name
snap_name=$(basename "$path")
local current_rev
current_rev=$(readlink -f "/snap/$snap_name/current" 2>/dev/null || readlink "/snap/$snap_name/current" 2>/dev/null)
[ -z "$current_rev" ] && return 1
[ ! -f "/snap/$snap_name/$current_rev/meta/snap.yaml" ] && return 1
fi
return 0
}
detect_chrome() {
# 优先使用 dpkg/原生安装的 chrome,其次 snap
local candidates=(
"/opt/google/chrome/google-chrome"
"/usr/bin/google-chrome"
"/usr/bin/google-chrome-stable"
"/usr/bin/chromium"
"/snap/bin/chromium"
)
for p in "${candidates[@]}"; do
if verify_chrome "$p"; then
echo "$p" && return
fi
done
# command -v 兜底
for cmd in google-chrome google-chrome-stable chromium chromium-browser; do
local p
p=$(command -v "$cmd" 2>/dev/null) || continue
if verify_chrome "$p"; then
echo "$p" && return
fi
done
echo ""
}
CHROME_PATH=$(detect_chrome)
if [ -z "$CHROME_PATH" ]; then
# 没有可用的 Chrome,尝试安装 dpkg 版
if command -v apt-get &>/dev/null; then
warn "未找到可用的 Chrome/Chromium,尝试安装 ..."
apt-get update -qq 2>/dev/null || true
if apt-get install -y -qq chromium 2>/dev/null; then
# 不要硬编码路径,重新检测实际安装位置
CHROME_PATH=$(detect_chrome)
if [ -n "$CHROME_PATH" ]; then
ok "Chromium 安装成功"
else
warn "apt 安装完成但未找到可用的 Chromium(可能是 snap 过渡包),尝试 snap 安装 ..."
fi
elif apt-get install -y -qq chromium-browser 2>/dev/null; then
CHROME_PATH=$(detect_chrome)
if [ -n "$CHROME_PATH" ]; then
ok "Chromium 安装成功"
else
warn "apt 安装完成但未找到可用的 Chromium,尝试 snap 安装 ..."
fi
fi
# 如果 apt 安装没拿到可用的 chromium,尝试 snap
if [ -z "$CHROME_PATH" ] && command -v snap &>/dev/null; then
info "通过 snap 安装 Chromium ..."
snap install chromium 2>/dev/null && sleep 2
CHROME_PATH=$(detect_chrome)
[ -n "$CHROME_PATH" ] && ok "Chromium (snap) 安装成功"
fi
# 最后兜底:下载 Google Chrome .deb
if [ -z "$CHROME_PATH" ] && command -v dpkg &>/dev/null; then
info "尝试安装 Google Chrome ..."
_arch=$(dpkg --print-architecture 2>/dev/null || echo "amd64")
if curl -fsSL --connect-timeout 10 -o /tmp/google-chrome.deb \
"https://dl.google.com/linux/direct/google-chrome-stable_current_${_arch}.deb" 2>/dev/null; then
apt-get install -y -qq /tmp/google-chrome.deb 2>/dev/null || true
rm -f /tmp/google-chrome.deb
CHROME_PATH=$(detect_chrome)
[ -n "$CHROME_PATH" ] && ok "Google Chrome 安装成功"
fi
fi
if [ -z "$CHROME_PATH" ]; then
warn "所有安装方式均失败,Web 类 Provider 不可用"
fi
fi
fi
if [ -n "$CHROME_PATH" ]; then
ok "Chrome: $CHROME_PATH"
else
warn "Web 类 Provider 需要手动安装 Chrome/Chromium"
fi
# ── 3. 克隆 & 安装 ──────────────────────────────────────────
if [ -d "$INSTALL_DIR" ]; then
info "更新已有安装 ..."
cd "$INSTALL_DIR"
if [ -d ".git" ]; then
# 暂存用户配置
[ -f config.yaml ] && cp config.yaml config.yaml.bak
if ! GIT_SSL_BACKEND=openssl git -c http.lowSpeedLimit=1000 -c http.lowSpeedTime=60 fetch origin main 2>&1; then
warn "fetch 失败,尝试 reset 本地 ..."
fi
if ! git reset --hard origin/main 2>&1; then
warn "reset 失败,保留当前版本"
fi
# 恢复用户配置
[ -f config.yaml.bak ] && mv config.yaml.bak config.yaml
fi
else
info "下载 zero-token ..."
GIT_SSL_BACKEND=openssl git -c http.lowSpeedLimit=1000 -c http.lowSpeedTime=60 \
clone --depth 1 "$REPO_URL" "$INSTALL_DIR" 2>/dev/null \
|| {
warn "git clone 失败,使用镜像下载 ..."
mkdir -p "$INSTALL_DIR"
curl -fsSL --connect-timeout 15 --max-time 120 \
"https://gh-proxy.com/${REPO_URL%.git}/archive/refs/heads/main.zip" \
-o /tmp/zero-token.zip
unzip -o /tmp/zero-token.zip -d /tmp/zt-extract
mv /tmp/zt-extract/zero-token-main/* /tmp/zt-extract/zero-token-main/.* "$INSTALL_DIR/" 2>/dev/null || true
rm -rf /tmp/zero-token.zip /tmp/zt-extract
}
cd "$INSTALL_DIR"
fi
info "安装依赖 ..."
npm ci --include=dev 2>/dev/null || npm install --include=dev
ok "依赖安装完成"
# 确保 tsdown 可用
if [ ! -f "node_modules/.bin/tsdown" ]; then
warn "tsdown 未找到,重新安装 ..."
npm install tsdown@0.21.2
fi
info "构建项目 ..."
npx tsdown
ok "构建完成"
npm prune --omit=dev 2>/dev/null || true
# ── 4. 默认配置 ──────────────────────────────────────────────
if [ ! -f "config.yaml" ]; then
cp config.yaml.example config.yaml 2>/dev/null || true
fi
# ── 5. 启动准备 ──────────────────────────────────────────────
# ── 完成 ─────────────────────────────────────────────────────
echo ""
echo -e "${GREEN}${BOLD}┌─────────────────────────────────────┐${NC}"
echo -e "${GREEN}${BOLD}│ 安装完成! │${NC}"
echo -e "${GREEN}${BOLD}└─────────────────────────────────────┘${NC}"
echo ""
echo -e " ${BOLD}安装目录${NC} ${INSTALL_DIR}"
echo -e " ${BOLD}配置文件${NC} ${INSTALL_DIR}/config.yaml"
echo ""
# ── 6. 启动 ──────────────────────────────────────────────────
# 先启动 Chrome(前台,非 headless,供用户交互登录)
CHROME_PID=""
CHROME_DATA_DIR="${HOME}/.zero-token/chrome-data"
if [ -n "$CHROME_PATH" ]; then
CHROME_ARGS=(
--remote-debugging-port="$CDP_PORT"
--user-data-dir="$CHROME_DATA_DIR"
--no-first-run
--no-default-browser-check
--disable-background-networking
--disable-sync
--disable-translate
--remote-allow-origins=*
--no-sandbox
--disable-dev-shm-usage
)
# 检查 Chrome 是否已经在运行(CDP 端口已监听)
if curl -sfm 3 "http://localhost:$CDP_PORT/json/version" > /dev/null 2>&1; then
ok "Chrome 已在运行 (CDP: http://localhost:$CDP_PORT),跳过启动"
else
# 杀掉残留 Chrome 进程(可能占着 CDP 端口)
if ss -tlnp 2>/dev/null | grep -q ":$CDP_PORT " || netstat -tlnp 2>/dev/null | grep -q ":$CDP_PORT "; then
warn "端口 $CDP_PORT 已被占用,清理残留 Chrome ..."
pkill -f "remote-debugging-port=$CDP_PORT" 2>/dev/null || true
sleep 2
fi
mkdir -p "$CHROME_DATA_DIR"
# 清理 singleton lock,避免"Opening in existing browser session"导致退出
rm -f "$CHROME_DATA_DIR/SingletonLock" "$CHROME_DATA_DIR/SingletonCookie" 2>/dev/null
"$CHROME_PATH" "${CHROME_ARGS[@]}" > /dev/null 2>&1 &
CHROME_PID=$!
ok "Chrome 启动中 (PID: $CHROME_PID) ..."
# 等待 Chrome CDP 就绪
info "等待 Chrome 就绪 ..."
for i in $(seq 1 15); do
if curl -sfm 4 "http://localhost:$CDP_PORT/json/version" > /dev/null 2>&1; then
break
fi
# 检查进程是否还活着
if ! kill -0 "$CHROME_PID" 2>/dev/null; then
warn "Chrome 进程已退出,尝试查看错误:"
# 重新启动一次并捕获错误输出
"$CHROME_PATH" "${CHROME_ARGS[@]}" 2>&1 | head -20 >&2 || true
CHROME_PID=""
break
fi
sleep 1
done
fi
if curl -sfm 4 "http://localhost:$CDP_PORT/json/version" > /dev/null 2>&1; then
ok "Chrome CDP 就绪 (http://localhost:$CDP_PORT)"
# 自动打开各 provider 登录页
PROVIDER_URLS=(
"https://chat.deepseek.com"
"https://claude.ai"
"https://kimi.com"
"https://doubao.com"
"https://xiaomimo.ai"
"https://chat.qwen.ai"
"https://chatglm.cn"
"https://chat.z.ai"
"https://perplexity.ai"
"https://chatgpt.com"
"https://gemini.google.com"
"https://grok.com"
)
info "自动打开 Provider 登录页 ..."
for url in "${PROVIDER_URLS[@]}"; do
curl -sfm 4 -X PUT "http://localhost:$CDP_PORT/json/new?$url" > /dev/null 2>&1 || true
done
ok "已打开 ${#PROVIDER_URLS[@]} 个 Provider 登录页"
else
warn "Chrome CDP 未就绪,跳过自动导航"
fi
echo ""
echo -e " ${YELLOW}请在各 Chrome 标签页中登录你需要的平台${NC}"
echo -e " ${YELLOW}登录完成后,按任意键继续...${NC}"
read -n 1 -s -r
echo ""
else
warn "未找到 Chrome/Chromium,Web 类 Provider 不可用"
warn "请手动安装 Chrome 后运行: chrome --remote-debugging-port=$CDP_PORT"
fi
# 抓取凭据
if [ -f "$INSTALL_DIR/scripts/onboard.mjs" ]; then
info "抓取登录凭据 ..."
cd "$INSTALL_DIR"
node scripts/onboard.mjs --all || warn "凭据抓取失败,可稍后手动运行: cd $INSTALL_DIR && node scripts/onboard.mjs"
fi
echo ""
info "启动 zero-token 服务 (端口: $SERVER_PORT) ..."
echo -e " ${YELLOW}按 Ctrl+C 停止服务${NC}"
echo ""
cd "$INSTALL_DIR"
export NODE_ENV=production
export SERVER_PORT="$SERVER_PORT"
# 前台运行 — 用户 Ctrl+C 退出时同时清理 Chrome
cleanup() {
echo ""
info "正在停止 ..."
if [ -n "$CHROME_PID" ] && kill -0 "$CHROME_PID" 2>/dev/null; then
kill "$CHROME_PID" 2>/dev/null || true
ok "Chrome 已停止"
else
info "Chrome 非本脚本启动,保持运行"
fi
ok "zero-token 已停止"
exit 0
}
trap cleanup INT TERM
# 通过 CDP 打开健康检查页
(
sleep 3
curl -sfm 4 -X PUT "http://localhost:$CDP_PORT/json/new?http://localhost:$SERVER_PORT/health" > /dev/null 2>&1 || true
) &
node dist/server.mjs
fi # ── 结束 首次安装 / 已安装分支 ──