-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
129 lines (112 loc) · 3.01 KB
/
Copy pathentrypoint.sh
File metadata and controls
129 lines (112 loc) · 3.01 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
#!/bin/sh
set -eu
NETWORK_KERNEL="${NETWORK_KERNEL:-singbox}"
SINGBOX_CONFIG="${SINGBOX_CONFIG:-/etc/sing-box/config.jsonc}"
MIHOMO_CONFIG="${MIHOMO_CONFIG:-/etc/mihomo/config.yaml}"
KERNEL_PID=""
CONTAINERBOOT_PID=""
EXPORTER_PID=""
EXPORTER_KIND=""
log() {
printf '[tailscale-exitnode] %s\n' "$*"
}
stop() {
trap - INT TERM
log "stopping"
if [ -n "$EXPORTER_PID" ]; then
kill "$EXPORTER_PID" 2>/dev/null || true
fi
if [ -n "$CONTAINERBOOT_PID" ]; then
kill "$CONTAINERBOOT_PID" 2>/dev/null || true
fi
if [ -n "$KERNEL_PID" ]; then
kill "$KERNEL_PID" 2>/dev/null || true
fi
wait 2>/dev/null || true
}
start_exporter() {
case "$NETWORK_KERNEL" in
singbox | sing-box)
if [ -n "${CLASH_EXPORTER_HOST:-}" ]; then
EXPORTER_KIND="clash-exporter"
log "starting clash exporter for $CLASH_EXPORTER_HOST"
CLASH_HOST="$CLASH_EXPORTER_HOST" CLASH_TOKEN="${CLASH_EXPORTER_TOKEN:-}" CLASH_EXPORTER_LISTEN="${CLASH_EXPORTER_LISTEN:-0.0.0.0:2112}" \
clash-exporter &
EXPORTER_PID="$!"
else
log "warning: CLASH_EXPORTER_HOST is not set; clash exporter disabled"
fi
;;
mihomo)
if [ -n "${NGINX_EXPORTER_SCRAPE_URI:-}" ]; then
EXPORTER_KIND="nginx-exporter"
log "starting nginx exporter for $NGINX_EXPORTER_SCRAPE_URI"
NGINX_EXPORTER_SCRAPE_URI="$NGINX_EXPORTER_SCRAPE_URI" NGINX_EXPORTER_LISTEN="${NGINX_EXPORTER_LISTEN:-0.0.0.0:9113}" \
nginx-exporter &
EXPORTER_PID="$!"
else
log "warning: NGINX_EXPORTER_SCRAPE_URI is not set; nginx exporter disabled"
fi
;;
esac
}
restart_exporter() {
if [ -z "$EXPORTER_KIND" ]; then
return 0
fi
log "warning: $EXPORTER_KIND exited; restarting"
EXPORTER_PID=""
start_exporter
}
is_alive() {
pid="$1"
[ -n "$pid" ] && kill -0 "$pid" 2>/dev/null
}
trap 'stop; exit 143' INT TERM
case "$NETWORK_KERNEL" in
singbox | sing-box)
log "starting sing-box with $SINGBOX_CONFIG"
sing-box run -c "$SINGBOX_CONFIG" &
KERNEL_PID="$!"
;;
mihomo)
log "starting mihomo with $MIHOMO_CONFIG"
mihomo -f "$MIHOMO_CONFIG" &
KERNEL_PID="$!"
;;
*)
log "unsupported NETWORK_KERNEL: $NETWORK_KERNEL"
exit 1
;;
esac
sleep 2
if ! kill -0 "$KERNEL_PID" 2>/dev/null; then
status=0
wait "$KERNEL_PID" || status="$?"
log "network kernel exited during startup with status $status"
exit "$status"
fi
log "starting tailscale containerboot"
containerboot &
CONTAINERBOOT_PID="$!"
start_exporter
log "ready"
while :; do
if ! is_alive "$KERNEL_PID"; then
status=0
wait "$KERNEL_PID" || status="$?"
log "network kernel exited with status $status"
exit "$status"
fi
if ! is_alive "$CONTAINERBOOT_PID"; then
status=0
wait "$CONTAINERBOOT_PID" || status="$?"
log "containerboot exited with status $status"
exit "$status"
fi
if [ -n "$EXPORTER_PID" ] && ! is_alive "$EXPORTER_PID"; then
wait "$EXPORTER_PID" 2>/dev/null || true
restart_exporter
fi
sleep 2
done