Repository navigation
Expand file tree
/
Copy pathgately.conf
More file actions
156 lines (146 loc) · 6.88 KB
/
Copy pathgately.conf
File metadata and controls
156 lines (146 loc) · 6.88 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
# Gately: https://github.com/wangwanjie/gately
# Follow the official host-network deployment; Docker management is opt-in.
local app_id="gately"
local app_name="Gately"
local app_text="类 Lucky 的开源网络工具集,支持 DDNS、STUN 穿透、端口转发、反向代理、证书与 WAF。"
local app_url="https://github.com/wangwanjie/gately"
local docker_name="gately"
local docker_port="16680"
local app_size="1"
gately_error() {
printf 'Gately: %s\n' "$*" >&2
return 1
}
gately_requirements() {
local tool
[ "$(uname -s)" = Linux ] || { gately_error "仅支持 Linux 服务器。"; return 1; }
case "$(uname -m)" in
x86_64|aarch64|arm64|armv7l) ;;
*) gately_error "官方镜像支持 amd64、arm64、arm/v7。"; return 1 ;;
esac
for tool in docker curl tar jq; do
command -v "$tool" >/dev/null 2>&1 || { gately_error "缺少依赖:$tool。"; return 1; }
done
docker info >/dev/null 2>&1 && docker compose version >/dev/null 2>&1
}
gately_compose() {
docker compose --project-name gately -f /home/docker/gately/docker-compose.yml "$@"
}
gately_owned() {
[ ! -L /home/docker/gately ] && [ ! -L /home/docker/gately/data ] &&
[ ! -L /home/docker/gately/docker-compose.yml ] &&
[ "$(cat /home/docker/gately/.market-owner 2>/dev/null)" = "kejilion-gately-v1" ] || {
gately_error "目录不属于此安装配置,拒绝覆盖或删除。"; return 1;
}
if docker container inspect gately >/dev/null 2>&1; then
[ "$(docker inspect -f '{{index .Config.Labels "cn.kejilion.app"}}' gately)" = gately ] &&
[ "$(docker inspect -f '{{index .Config.Labels "com.docker.compose.project"}}' gately)" = gately ] || {
gately_error "同名容器不属于此应用,拒绝接管。"; return 1;
}
fi
}
gately_wait_ready() {
local attempt
for attempt in {1..30}; do
if [ "$(docker inspect -f '{{.State.Health.Status}}' gately 2>/dev/null)" = healthy ]; then
return 0
fi
sleep 2
done
gately_error "启动检查失败,请运行 docker logs gately;配置及更新前备份已保留。"
}
gately_show_help() {
echo "账号:admin;首次随机密码可运行以下命令查看:"
echo "docker logs gately 2>&1 | grep -i 'initial password'"
echo "请登录后修改密码并启用两步验证;不要通过未加密的公网 HTTP 传输凭据。"
echo "数据目录:/home/docker/gately/data;管理端口由 Compose 的 -listen 指定。"
echo "host 网络直接使用宿主机端口,启用转发或 Web 服务前请检查与 Lucky、Nginx 的端口冲突。"
echo "Docker 管理默认关闭:需要时自行在 Compose 添加 /var/run/docker.sock:/var/run/docker.sock 后重建容器。"
echo "Tailscale、frp、cloudflared 需把对应 Linux 架构的官方程序放入 data/bin 并授予执行权限。"
echo "Lucky 迁移:设置 → 从 Lucky 导入;规则默认停用,请逐项切换,避免端口争用。"
echo "host 网络的访问控制请在 Gately 设置、主机防火墙或安全组配置。"
}
docker_app_install() {
gately_requirements || return 1
[[ "$docker_port" =~ ^[0-9]{1,5}$ ]] && (( 10#$docker_port >= 1 && 10#$docker_port <= 65535 )) || {
gately_error "管理端口必须是 1–65535。"; return 1;
}
docker_port=$((10#$docker_port))
if [ -e /home/docker/gately ] || [ -L /home/docker/gately ]; then
gately_owned || return 1
gately_error "安装目录已存在,请使用更新功能;不会覆盖已有配置。"
return 1
fi
if docker container inspect gately >/dev/null 2>&1; then
gately_error "已存在同名容器,请先确认其来源。"; return 1
fi
if command -v ss >/dev/null 2>&1 && ss -H -ltn "sport = :$docker_port" | grep -q .; then
gately_error "管理端口 $docker_port 已被占用,请选择其他端口。"; return 1
fi
# Pull before creating state so a registry failure is safe to retry.
docker pull ghcr.io/wangwanjie/gately:latest || return 1
(umask 077; mkdir -p /home/docker/gately/data &&
printf '%s\n' kejilion-gately-v1 > /home/docker/gately/.market-owner) || return 1
cat > /home/docker/gately/docker-compose.yml <<YAML
services:
gately:
image: ghcr.io/wangwanjie/gately:latest
container_name: gately
restart: unless-stopped
network_mode: host
command: ["-listen", ":${docker_port}"]
environment:
TZ: Asia/Shanghai
volumes:
- /home/docker/gately/data:/etc/gately
healthcheck:
# A configured secure entry may intentionally return 404 at /.
test: ["CMD", "curl", "-sS", "--max-time", "3", "-o", "/dev/null", "http://127.0.0.1:${docker_port}/"]
interval: 5s
timeout: 4s
retries: 6
start_period: 10s
labels:
cn.kejilion.app: gately
YAML
[ "$?" = 0 ] || return 1
gately_compose config --quiet && gately_compose up -d && gately_wait_ready || return 1
echo "Gately 安装完成。"
# Only first-install logs contain this randomly generated credential.
docker logs gately 2>&1 | grep -i 'initial password' || true
gately_show_help
check_docker_app_ip
}
docker_app_update() {
local backup_dir
gately_requirements && gately_owned || return 1
gately_compose config --quiet || return 1
# The generic noninteractive updater reloads the default metadata port.
# Read the persisted listener so custom-port access output stays accurate.
docker_port=$(gately_compose config --format json | jq -er '
.services.gately.command as $args |
($args | index("-listen")) as $i | select($i != null) |
$args[$i + 1] | capture(":(?<port>[0-9]{1,5})$").port
') || { gately_error "无法读取 Compose 中的管理端口,已停止更新。"; return 1; }
# A failed pull never stops the existing container or changes its files.
gately_compose pull || return 1
backup_dir="/home/docker/gately/backups/$(date -u +%Y%m%dT%H%M%SZ)-$$"
(umask 077; mkdir -p "$backup_dir") || return 1
# Stop writes while snapshotting JSON state (contains DNS keys and private keys).
gately_compose stop || return 1
if ! tar -C /home/docker/gately -czf "$backup_dir/config.tar.gz" data docker-compose.yml .market-owner; then
gately_compose start
gately_error "备份失败,已尝试恢复原容器。"; return 1
fi
echo "更新前备份:$backup_dir/config.tar.gz(包含敏感配置,请妥善保存)"
gately_compose up -d && gately_wait_ready || return 1
echo "Gately 更新完成,保留原端口、密码、配置和自定义挂载。"
check_docker_app_ip
}
docker_app_uninstall() {
gately_requirements && gately_owned || return 1
gately_compose down --rmi all || return 1
rm -rf -- /home/docker/gately || return 1
echo "Gately 已卸载,应用数据及更新备份已删除。"
}
docker_app_plus