blob: 406aa1f44b8d623e9dfb40740b509c24d490a1f7 (
plain)
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
|
#!/bin/bash
set -e
show_usage() {
cat <<'EOF'
网云穿服务管理工具
用法: wycctl <命令> [参数]
命令:
add <实例名> <token> 添加新实例
remove <实例名> 删除实例
start <实例名> 启动实例
stop <实例名> 停止实例
restart <实例名> 重启实例
status <实例名> 查看实例状态
list 列出所有实例
logpath <实例名> 查看实例日志路径
fix-perms 修复权限问题
logs <实例名> 查看实例日志
healthcheck <实例名> 健康检查,隧道异常时自动重启
示例:
wycctl add web token12345678
wycctl start web
wycctl logs web
wycctl healthcheck web
EOF
}
# 检查root权限
check_root() {
if [[ $EUID -ne 0 ]]; then
echo "错误: 此命令需要root权限" >&2
exit 1
fi
}
# 修复权限
fix_permissions() {
check_root
echo "正在修复权限..."
# 创建用户
if ! id wangyunchuan &>/dev/null; then
useradd -r -s /bin/false -d /nonexistent -M wangyunchuan
echo "创建用户: wangyunchuan"
fi
# 创建目录
mkdir -p /etc/wangyunchuan
mkdir -p /var/log/wangyunchuan
# 设置目录权限
chown wangyunchuan:wangyunchuan /etc/wangyunchuan
chmod 750 /etc/wangyunchuan
chown wangyunchuan:wangyunchuan /var/log/wangyunchuan
chmod 755 /var/log/wangyunchuan
echo "✅ 权限修复完成"
}
# 添加实例
add_instance() {
check_root
local instance="$1"
local token="$2"
if [[ -z "$instance" || -z "$token" ]]; then
echo "错误: 需要提供实例名和 token" >&2
exit 1
fi
# 验证token格式
if [[ ! "$token" =~ ^[a-zA-Z0-9]{16}$ ]]; then
echo "错误: Token 必须为 16 位字母数字组合" >&2
exit 1
fi
local log_prefix="${token:0:8}"
local token_file="/etc/wangyunchuan/${instance}_token"
# 确保基础环境
fix_permissions
# 创建token文件
cat >"$token_file" <<EOF
WYC_TOKEN=${token}
WYCLOG_PREFIX=${log_prefix}
INSTANCE=${instance}
EOF
chmod 640 "$token_file"
chown wangyunchuan:wangyunchuan "$token_file"
# 创建日志目录
local log_dir="/var/log/wangyunchuan/$log_prefix"
mkdir -p "$log_dir"
chown wangyunchuan:wangyunchuan "$log_dir"
chmod 755 "$log_dir"
echo "✅ 实例添加成功: $instance"
echo "📁 配置文件: $token_file"
echo "📁 日志目录: $log_dir"
systemctl daemon-reload
}
# 删除实例
remove_instance() {
check_root
local instance="$1"
if [[ -z "$instance" ]]; then
echo "错误: 需要提供实例名" >&2
exit 1
fi
local token_file="/etc/wangyunchuan/${instance}_token"
if [[ ! -f "$token_file" ]]; then
echo "错误: 实例不存在: $instance" >&2
exit 1
fi
# 停止服务
systemctl stop "wangyunchuan@$instance" 2>/dev/null || true
systemctl disable "wangyunchuan@$instance" 2>/dev/null || true
# 获取日志目录
source "$token_file" 2>/dev/null || true
local log_dir="/var/log/wangyunchuan/${WYCLOG_PREFIX}"
# 删除文件
rm -f "$token_file"
# 询问是否删除日志目录
if [[ -d "$log_dir" ]]; then
read -p "是否删除日志目录 $log_dir? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
rm -rf "$log_dir"
echo "已删除日志目录"
fi
fi
systemctl daemon-reload
echo "✅ 实例删除成功: $instance"
}
# 列出实例
list_instances() {
echo "已配置的实例:"
echo "=============="
for token_file in /etc/wangyunchuan/*_token; do
if [[ -f "$token_file" ]]; then
local instance=$(basename "$token_file" "_token")
source "$token_file" 2>/dev/null || continue
local status=$(systemctl is-active "wangyunchuan@$instance" 2>/dev/null || echo "未运行")
local enabled=$(systemctl is-enabled "wangyunchuan@$instance" 2>/dev/null || echo "-")
printf "%-12s %-8s... %-10s %s\n" \
"$instance" \
"${WYC_TOKEN:0:8}" \
"$status" \
"$([ "$enabled" = "enabled" ] && echo "✓" || echo "")"
fi
done
}
# 查看日志路径
show_log_path() {
local instance="$1"
local token_file="/etc/wangyunchuan/${instance}_token"
if [[ ! -f "$token_file" ]]; then
echo "错误: 实例不存在: $instance" >&2
exit 1
fi
source "$token_file"
echo "实例: $instance"
echo "日志前缀: $WYCLOG_PREFIX"
echo "日志目录: /var/log/wangyunchuan/$WYCLOG_PREFIX"
if [[ -d "/var/log/wangyunchuan/$WYCLOG_PREFIX" ]]; then
echo "日志文件:"
ls -la "/var/log/wangyunchuan/$WYCLOG_PREFIX/" 2>/dev/null || echo "目录为空"
fi
}
# 查看日志
show_logs() {
local instance="$1"
local token_file="/etc/wangyunchuan/${instance}_token"
if [[ ! -f "$token_file" ]]; then
echo "错误: 实例不存在: $instance" >&2
exit 1
fi
source "$token_file"
local log_dir="/var/log/wangyunchuan/$WYCLOG_PREFIX"
if [[ ! -d "$log_dir" ]]; then
echo "日志目录不存在: $log_dir" >&2
exit 1
fi
# 查找最新的日志文件
local latest_log=$(find "$log_dir" -type f -name "*.log" | sort -r | head -1)
if [[ -n "$latest_log" && -f "$latest_log" ]]; then
echo "正在查看日志: $latest_log"
echo "========================"
tail -f "$latest_log"
else
echo "没有找到日志文件"
# 查看systemd日志
journalctl -u "wangyunchuan@$instance" -f
fi
}
# 健康检查
healthcheck() {
local instance="$1"
local token_file="/etc/wangyunchuan/${instance}_token"
[[ ! -f "$token_file" ]] && exit 0
source "$token_file" 2>/dev/null || exit 0
local log_dir="/var/log/wangyunchuan/${WYCLOG_PREFIX}"
local stamp_file="${log_dir}/.watchdog_stamp"
# 服务未运行:尝试启动(处理网络恢复后 systemd 已放弃重启的情况)
if ! systemctl -q is-active "wangyunchuan@${instance}" 2>/dev/null; then
if systemctl -q is-enabled "wangyunchuan@${instance}" 2>/dev/null; then
echo "$(date '+%Y-%m-%d %H:%M:%S') [WATCHDOG] ${instance}: service inactive but enabled, starting" >> "${log_dir}/watchdog.log"
chown wangyunchuan:wangyunchuan "${log_dir}/watchdog.log" 2>/dev/null
systemctl start "wangyunchuan@${instance}"
fi
exit 0
fi
local log_file="${log_dir}/$(date +%Y-%m-%d).log"
[[ ! -f "$log_file" ]] && exit 0
# 检查隧道状态:最后一条 [OK ]/[FAIL ] 决定隧道是否正常
# 注意:日志格式为 [OK ] 或 [FAIL ],括号内有空格填充
local last_status
last_status=$(grep -E '\[OK +\]|\[FAIL +\]' "$log_file" | tail -1)
if [[ -z "$last_status" ]]; then
# 没有状态行,进程可能还在启动中
exit 0
fi
if [[ "$last_status" == *"[FAIL"* ]]; then
# 冷却期:避免重启后日志未更新而反复重启
if [[ -f "$stamp_file" ]]; then
local last_restart=$(stat -c %Y "$stamp_file" 2>/dev/null || echo 0)
local now=$(date +%s)
local cooldown=120 # 2 分钟冷却
if (( now - last_restart < cooldown )); then
exit 0
fi
fi
echo "$(date '+%Y-%m-%d %H:%M:%S') [WATCHDOG] ${instance}: tunnel failed, restarting" >> "${log_dir}/watchdog.log"
chown wangyunchuan:wangyunchuan "${log_dir}/watchdog.log" 2>/dev/null
touch "$stamp_file"
chown wangyunchuan:wangyunchuan "$stamp_file" 2>/dev/null
systemctl restart "wangyunchuan@${instance}"
else
# 隧道正常,清除冷却标记
rm -f "$stamp_file"
fi
}
# 主逻辑
case "${1:-}" in
add)
add_instance "$2" "$3"
;;
remove | del)
remove_instance "$2"
;;
start)
local instance="$2"
if [[ -z "$instance" ]]; then
echo "错误: 需要提供实例名" >&2
exit 1
fi
# 启动前检查和准备环境
local token_file="/etc/wangyunchuan/${instance}_token"
if [[ -f "$token_file" ]]; then
source "$token_file"
local log_dir="/var/log/wangyunchuan/$WYCLOG_PREFIX"
if [[ ! -d "$log_dir" ]]; then
echo "准备日志目录: $log_dir"
mkdir -p "$log_dir"
chown wangyunchuan:wangyunchuan "$log_dir"
chmod 755 "$log_dir"
fi
fi
systemctl start "wangyunchuan@$instance"
systemctl status "wangyunchuan@$instance" --no-pager
;;
stop)
systemctl stop "wangyunchuan@$2"
;;
restart)
systemctl restart "wangyunchuan@$2"
systemctl status "wangyunchuan@$2" --no-pager
;;
status)
systemctl status "wangyunchuan@$2" --no-pager
;;
list)
list_instances
;;
logpath)
show_log_path "$2"
;;
logs)
show_logs "$2"
;;
healthcheck)
set +e
healthcheck "$2"
;;
fix-perms)
fix_permissions
;;
"" | help | --help | -h)
show_usage
;;
*)
echo "错误: 未知命令 '$1'" >&2
echo "使用 'wycctl help' 查看帮助" >&2
exit 1
;;
esac
|