forked from wsh307/chatgpt-share-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
327 lines (268 loc) · 9.99 KB
/
install.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
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
#!/bin/bash
if [ "$(id -u)" != "0" ]; then
echo "该脚本必须以 root 权限运行" 1>&2
exit 1
fi
# 向 /etc/sysctl.conf 添加禁用 IPv6 的设置
echo "net.ipv6.conf.all.disable_ipv6 = 1" >> /etc/sysctl.conf
echo "net.ipv6.conf.default.disable_ipv6 = 1" >> /etc/sysctl.conf
if [ -d "/root/chatgpt-proxy-node" ]; then
echo "目录存在,执行命令..."
# 进入目录
cd /root/chatgpt-proxy-node
# 关闭 Docker Compose 服务
docker compose down
# 删除所有悬空的镜像,即未被任何容器使用的镜像
docker image prune -f --filter "dangling=true"
# 退出目录
cd ..
# 删除 chatgpt-proxy-node 文件夹
rm -rf /root/chatgpt-proxy-node
else
echo "目录不存在,跳过命令。"
fi
# 删除 chatgpt-proxy-node 文件夹
rm -rf /root/chatgpt-proxy-node
CURRENT_DIR=$(
cd "$(dirname "$0")"
pwd
)
function log() {
message="[Proxy Log]: $1 "
echo -e "${message}" 2>&1 | tee -a ${CURRENT_DIR}/glider-install.log
}
log "======================= 开始安装 ======================="
function Check_Root() {
if [[ $EUID -ne 0 ]]; then
echo "请使用 root 或 sudo 权限运行此脚本"
exit 1
fi
}
function Install_Docker(){
if which docker >/dev/null 2>&1; then
log "检测到 Docker 已安装,跳过安装步骤"
log "启动 Docker "
systemctl start docker 2>&1 | tee -a ${CURRENT_DIR}/install.log
else
log "... 在线安装 docker"
if [[ $(curl -s ipinfo.io/country) == "CN" ]]; then
sources=(
"https://mirrors.aliyun.com/docker-ce"
"https://mirrors.tencent.com/docker-ce"
"https://mirrors.163.com/docker-ce"
"https://mirrors.cernet.edu.cn/docker-ce"
)
get_average_delay() {
local source=$1
local total_delay=0
local iterations=3
for ((i = 0; i < iterations; i++)); do
delay=$(curl -o /dev/null -s -w "%{time_total}\n" "$source")
total_delay=$(awk "BEGIN {print $total_delay + $delay}")
done
average_delay=$(awk "BEGIN {print $total_delay / $iterations}")
echo "$average_delay"
}
min_delay=${#sources[@]}
selected_source=""
for source in "${sources[@]}"; do
average_delay=$(get_average_delay "$source")
if (( $(awk 'BEGIN { print '"$average_delay"' < '"$min_delay"' }') )); then
min_delay=$average_delay
selected_source=$source
fi
done
if [ -n "$selected_source" ]; then
echo "选择延迟最低的源 $selected_source,延迟为 $min_delay 秒"
export DOWNLOAD_URL="$selected_source"
curl -fsSL "https://get.docker.com" -o get-docker.sh
sh get-docker.sh 2>&1 | tee -a ${CURRENT_DIR}/install.log
log "... 启动 docker"
systemctl enable docker; systemctl daemon-reload; systemctl start docker 2>&1 | tee -a ${CURRENT_DIR}/install.log
docker_config_folder="/etc/docker"
if [[ ! -d "$docker_config_folder" ]];then
mkdir -p "$docker_config_folder"
fi
docker version >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
log "docker 安装失败"
exit 1
else
log "docker 安装成功"
fi
else
log "无法选择源进行安装"
exit 1
fi
else
log "非中国大陆地区,无需更改源"
export DOWNLOAD_URL="https://download.docker.com"
curl -fsSL "https://get.docker.com" -o get-docker.sh
sh get-docker.sh 2>&1 | tee -a ${CURRENT_DIR}/install.log
log "... 启动 docker"
systemctl enable docker; systemctl daemon-reload; systemctl start docker 2>&1 | tee -a ${CURRENT_DIR}/install.log
docker_config_folder="/etc/docker"
if [[ ! -d "$docker_config_folder" ]];then
mkdir -p "$docker_config_folder"
fi
docker version >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
log "docker 安装失败"
exit 1
else
log "docker 安装成功"
fi
fi
fi
}
function Install_Compose(){
docker-compose version >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
log "... 在线安装 docker-compose"
arch=$(uname -m)
if [ "$arch" == 'armv7l' ]; then
arch='armv7'
fi
curl -L https://resource.fit2cloud.com/docker/compose/releases/download/v2.22.0/docker-compose-$(uname -s | tr A-Z a-z)-$arch -o /usr/local/bin/docker-compose 2>&1 | tee -a ${CURRENT_DIR}/install.log
if [[ ! -f /usr/local/bin/docker-compose ]];then
log "docker-compose 下载失败,请稍候重试"
exit 1
fi
chmod +x /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose version >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
log "docker-compose 安装失败"
exit 1
else
log "docker-compose 安装成功"
fi
else
compose_v=`docker-compose -v`
if [[ $compose_v =~ 'docker-compose' ]];then
read -p "检测到已安装 Docker Compose 版本较低(需大于等于 v2.0.0 版本),是否升级 [y/n] : " UPGRADE_DOCKER_COMPOSE
if [[ "$UPGRADE_DOCKER_COMPOSE" == "Y" ]] || [[ "$UPGRADE_DOCKER_COMPOSE" == "y" ]]; then
rm -rf /usr/local/bin/docker-compose /usr/bin/docker-compose
Install_Compose
else
log "Docker Compose 版本为 $compose_v,可能会影响应用商店的正常使用"
fi
else
log "检测到 Docker Compose 已安装,跳过安装步骤"
fi
fi
}
function Set_Port(){
DEFAULT_PORT=`expr $RANDOM % 55535 + 10000`
while true; do
read -p "设置 glider 端口(默认为$DEFAULT_PORT):" PANEL_PORT
if [[ "$PANEL_PORT" == "" ]];then
PANEL_PORT=$DEFAULT_PORT
fi
if ! [[ "$PANEL_PORT" =~ ^[1-9][0-9]{0,4}$ && "$PANEL_PORT" -le 65535 ]]; then
echo "错误:输入的端口号必须在 1 到 65535 之间"
continue
fi
if command -v lsof >/dev/null 2>&1; then
if lsof -i:$PANEL_PORT >/dev/null 2>&1; then
echo "端口$PANEL_PORT被占用,请重新输入..."
continue
fi
elif command -v ss >/dev/null 2>&1; then
if ss -tlun | grep -q ":$PANEL_PORT " >/dev/null 2>&1; then
echo "端口$PANEL_PORT被占用,请重新输入..."
continue
fi
elif command -v netstat >/dev/null 2>&1; then
if netstat -tlun | grep -q ":$PANEL_PORT " >/dev/null 2>&1; then
echo "端口$PANEL_PORT被占用,请重新输入..."
continue
fi
fi
log "您设置的端口为:$PANEL_PORT"
break
done
}
function Set_Username(){
DEFAULT_USERNAME=`cat /dev/urandom | head -n 16 | md5sum | head -c 10`
while true; do
read -p "设置 glider 代理用户(默认为$DEFAULT_USERNAME):" PANEL_USERNAME
if [[ "$PANEL_USERNAME" == "" ]];then
PANEL_USERNAME=$DEFAULT_USERNAME
fi
if [[ ! "$PANEL_USERNAME" =~ ^[a-zA-Z0-9_]{3,30}$ ]]; then
echo "错误:用户仅支持字母、数字、下划线,长度 3-30 位"
continue
fi
log "您设置的用户为:$PANEL_USERNAME"
break
done
}
function Set_Password(){
DEFAULT_PASSWORD=`cat /dev/urandom | head -n 16 | md5sum | head -c 10`
while true; do
read -p "设置 glider 代理密码(默认为$DEFAULT_PASSWORD):" PANEL_PASSWORD
if [[ "$PANEL_PASSWORD" == "" ]];then
PANEL_PASSWORD=$DEFAULT_PASSWORD
fi
if [[ ! "$PANEL_PASSWORD" =~ ^[a-zA-Z0-9_!@#$%*,.?]{3,30}$ ]]; then
echo "错误:密码仅支持字母、数字、特殊字符(!@#$%*_,.?),长度 3-30 位"
continue
fi
log "您设置的密码为:$PANEL_PASSWORD"
break
done
}
function InitNode() {
log "配置 Proxy glider Service"
git clone -b main --depth=1 https://github.com/wm-chatgpt/chatgpt-proxy-node-deploy.git chatgpt-proxy-node
cd chatgpt-proxy-node
RUN_BASE_DIR=/opt/chatgpt-proxy-node
mkdir -p $RUN_BASE_DIR
rm -rf $RUN_BASE_DIR/*
cp ./docker-compose.yml $RUN_BASE_DIR
sed -i "s/8443:8443/$PANEL_PORT:8443/g" $RUN_BASE_DIR/docker-compose.yml
echo listen=$PANEL_USERNAME:$PANEL_PASSWORD@:8443 > $RUN_BASE_DIR/glider.conf
cd $RUN_BASE_DIR
docker compose pull
docker compose up -d --remove-orphans
## 提示信息
}
function Get_Ip(){
active_interface=$(ip route get 8.8.8.8 | awk 'NR==1 {print $5}')
if [[ -z $active_interface ]]; then
LOCAL_IP="127.0.0.1"
else
LOCAL_IP=`ip -4 addr show dev "$active_interface" | grep -oP '(?<=inet\s)\d+(\.\d+){3}'`
fi
PUBLIC_IP=`curl -s https://api64.ipify.org`
if [[ -z "$PUBLIC_IP" ]]; then
PUBLIC_IP="N/A"
fi
if echo "$PUBLIC_IP" | grep -q ":"; then
PUBLIC_IP=[${PUBLIC_IP}]
1pctl listen-ip ipv6
fi
}
function Show_Result(){
log ""
log "=================感谢您的耐心等待,安装已经完成=================="
log ""
log "代理地址:socks5://$PANEL_USERNAME:$PANEL_PASSWORD@$PUBLIC_IP:$PANEL_PORT"
log ""
log "如果使用的是云服务器,请至安全组开放 $PANEL_PORT 端口"
log ""
log "================================================================"
}
function main(){
Check_Root
Install_Docker
Install_Compose
Set_Port
Set_Username
Set_Password
InitNode
Get_Ip
Show_Result
}
main