Repository navigation
Expand file tree
/
Copy pathgrok2api.conf
More file actions
184 lines (157 loc) · 6.8 KB
/
Copy pathgrok2api.conf
File metadata and controls
184 lines (157 loc) · 6.8 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
# Official: https://github.com/chenyme/grok2api
# Grok2API is a Go gateway with an integrated React administration console.
# --- 基础信息 / Basic Information ---
local app_id="grok2api"
local app_name="Grok2API"
local app_text="多账号 Grok API 网关,支持 OpenAI/Anthropic 兼容接口与管理控制台"
local app_url="https://github.com/chenyme/grok2api"
local docker_name="grok2api"
# 8100 不与当前 apps/ 目录中声明的默认端口重复;用户仍可在安装时自定义。
local docker_port="${docker_port:-8100}"
local app_size="2"
# 下载上游原始文件。先使用宿主提供的 GitHub 代理;代理为空、格式不完整或下载失败时自动回退直连。
grok2api_download_file() {
local destination="$1"
local source_path="$2"
local upstream_url="https://raw.githubusercontent.com/chenyme/grok2api/main/${source_path}"
local proxy_url
# 仅接受带主机名的完整 HTTP(S) 代理前缀;例如 https、https:// 等残缺值会直接回退直连。
case "${gh_proxy:-}" in
http://?*|https://?*)
proxy_url="${gh_proxy}${upstream_url}"
if curl -fsSL --retry 3 --connect-timeout 15 --max-time 120 \
-o "${destination}" "${proxy_url}"; then
return 0
fi
echo "GitHub 代理下载失败,正在尝试直连上游。"
;;
esac
curl -fsSL --retry 3 --connect-timeout 15 --max-time 120 \
-o "${destination}" "${upstream_url}"
}
# 下载当前官方 Compose 文件及其可选 quality-guard profile 所需的构建文件。
# 下载完成后再替换,避免网络中断留下半更新的 Compose 文件。
grok2api_download_release_files() {
local app_dir="$1"
local temp_dir="${app_dir}/.release-download"
rm -rf "${temp_dir}"
mkdir -p "${temp_dir}/tools/egress-quality-guard" || return 1
if ! grok2api_download_file "${temp_dir}/docker-compose.yml" "docker-compose.yml" || \
! grok2api_download_file "${temp_dir}/tools/egress-quality-guard/Dockerfile" \
"tools/egress-quality-guard/Dockerfile" || \
! grok2api_download_file "${temp_dir}/tools/egress-quality-guard/quality_guard.py" \
"tools/egress-quality-guard/quality_guard.py"; then
rm -rf "${temp_dir}"
echo "下载 Grok2API 发布文件失败,请检查网络后重试。"
return 1
fi
mkdir -p "${app_dir}/tools/egress-quality-guard" || return 1
mv "${temp_dir}/docker-compose.yml" "${app_dir}/docker-compose.yml" || return 1
mv "${temp_dir}/tools/egress-quality-guard/Dockerfile" \
"${app_dir}/tools/egress-quality-guard/Dockerfile" || return 1
mv "${temp_dir}/tools/egress-quality-guard/quality_guard.py" \
"${app_dir}/tools/egress-quality-guard/quality_guard.py" || return 1
rm -rf "${temp_dir}"
}
grok2api_wait_for_health() {
local app_dir="$1"
local attempt
for attempt in $(seq 1 30); do
if curl -fsS --max-time 3 "http://127.0.0.1:${docker_port}/healthz" >/dev/null; then
return 0
fi
sleep 2
done
echo "Grok2API 未在 60 秒内通过 /healthz 健康检查,最近日志如下:"
(cd "${app_dir}" && docker compose logs --tail=100 grok2api)
return 1
}
docker_app_install() {
local app_dir="/home/docker/${app_name}"
local jwt_secret
local credential_encryption_key
local admin_password
if ! [[ "${docker_port}" =~ ^[1-9][0-9]{0,4}$ ]] || [ "${docker_port}" -gt 65535 ]; then
echo "端口必须是 1-65535 之间的整数。"
return 1
fi
mkdir -p "${app_dir}" || return 1
cd "${app_dir}" || return 1
if [ -e "config.yaml" ]; then
echo "检测到已有配置文件:${app_dir}/config.yaml"
echo "请使用更新功能,而不是重复安装。"
return 1
fi
jwt_secret=$(openssl rand -hex 32 2>/dev/null) || {
echo "无法生成 JWT 密钥,安装已停止。"
return 1
}
credential_encryption_key=$(openssl rand -base64 32 2>/dev/null) || {
echo "无法生成凭据加密密钥,安装已停止。"
return 1
}
admin_password=$(openssl rand -base64 24 2>/dev/null) || {
echo "无法生成管理员密码,安装已停止。"
return 1
}
if ! grok2api_download_release_files "${app_dir}" || \
! grok2api_download_file "${app_dir}/config.yaml" "config.example.yaml"; then
echo "下载 Grok2API 配置模板失败,安装已停止。"
return 1
fi
sed -i \
-e "s|replace-with-at-least-32-characters|${jwt_secret}|" \
-e "s|replace-with-base64-key|${credential_encryption_key}|" \
-e "s|replace-with-a-strong-password|${admin_password}|" \
"${app_dir}/config.yaml" || return 1
chmod 600 "${app_dir}/config.yaml" || return 1
cat > "${app_dir}/.env" <<EOF
# 由 kejilion.sh 生成;可在服务停止后按需调整。
GROK2API_PORT=${docker_port}
TZ=Asia/Shanghai
EOF
chmod 600 "${app_dir}/.env" || return 1
if ! docker compose config -q; then
echo "Grok2API Compose 配置校验失败,安装已停止。"
return 1
fi
docker compose pull || return 1
docker compose up -d || return 1
grok2api_wait_for_health "${app_dir}" || return 1
echo ""
echo "Grok2API 安装完成。"
echo "访问地址:http://\${server_ip}:${docker_port}/"
echo "管理员账号:admin"
echo "管理员密码:${admin_password}"
echo "配置文件:${app_dir}/config.yaml"
echo "请妥善保存密码;首次登录后修改管理员密码,并不要更换 credentialEncryptionKey。"
echo "如通过 HTTPS 反向代理公开服务,请将 config.yaml 中 auth.secureCookies 设为 true 后重启。"
check_docker_app_ip
}
docker_app_update() {
local app_dir="/home/docker/${app_name}"
cd "${app_dir}" || return 1
if [ ! -f "config.yaml" ]; then
echo "检测到旧版 FastAPI 部署,不能安全地自动迁移到当前 Go 版本。"
echo "请先按上游文档导出旧账号数据,再卸载并重新安装最新版 Grok2API。"
return 1
fi
grok2api_download_release_files "${app_dir}" || return 1
if ! docker compose config -q; then
echo "Grok2API Compose 配置校验失败,更新已停止。"
return 1
fi
docker compose pull || return 1
docker compose up -d || return 1
grok2api_wait_for_health "${app_dir}" || return 1
echo "Grok2API 已更新至最新版,并通过 /healthz 健康检查。"
}
docker_app_uninstall() {
local app_dir="/home/docker/${app_name}"
cd "${app_dir}" || return 1
docker compose down --volumes --rmi all
rm -rf "${app_dir}"
echo "Grok2API 已卸载,容器、镜像、卷和应用目录均已清理。"
}
# --- 注册(必须包含)/ Registration (Mandatory) ---
docker_app_plus