-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffline.sh
More file actions
335 lines (285 loc) · 8.65 KB
/
offline.sh
File metadata and controls
335 lines (285 loc) · 8.65 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
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
#!/usr/bin/env bash
set -euo pipefail
APP_NAME="bifrost-gate"
SERVICE_NAME="bifrost-gate.service"
INSTALL_DIR="/opt/bifrost"
BINARY_PATH="$INSTALL_DIR/bifrost-gate"
CONFIG_DIR="/etc/bifrost"
CONFIG_PATH="$CONFIG_DIR/config.json"
MANAGER_PATH="/usr/local/bin/bifrost"
DEFAULT_PUBLIC_IP="185.239.2.22"
SCRIPT_SOURCE="${BASH_SOURCE[0]-$0}"
SCRIPT_DIR="$(cd -- "$(dirname -- "$SCRIPT_SOURCE")" >/dev/null 2>&1 && pwd)"
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "$TMP_DIR"' EXIT
log() {
printf '[install] %s\n' "$*"
}
fail() {
printf '[install] ERROR: %s\n' "$*" >&2
exit 1
}
require_root() {
if [[ "$EUID" -ne 0 ]]; then
fail "run as root (example: sudo bash install.sh)"
fi
}
detect_pkg_manager() {
if command -v apt-get >/dev/null 2>&1; then echo apt; return; fi
if command -v dnf >/dev/null 2>&1; then echo dnf; return; fi
if command -v yum >/dev/null 2>&1; then echo yum; return; fi
if command -v pacman >/dev/null 2>&1; then echo pacman; return; fi
if command -v zypper >/dev/null 2>&1; then echo zypper; return; fi
if command -v apk >/dev/null 2>&1; then echo apk; return; fi
echo unknown
}
install_packages() {
local pm="$1"
shift
local pkgs=("$@")
case "$pm" in
apt)
DEBIAN_FRONTEND=noninteractive apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y "${pkgs[@]}"
;;
dnf)
dnf install -y "${pkgs[@]}"
;;
yum)
yum install -y "${pkgs[@]}"
;;
pacman)
pacman -Sy --noconfirm "${pkgs[@]}"
;;
zypper)
zypper --non-interactive install "${pkgs[@]}"
;;
apk)
apk add --no-cache "${pkgs[@]}"
;;
*)
return 1
;;
esac
}
ensure_cmd() {
local cmd="$1"
local pkg="$2"
if command -v "$cmd" >/dev/null 2>&1; then
return 0
fi
local pm
pm="$(detect_pkg_manager)"
if [[ "$pm" == "unknown" ]]; then
fail "missing '$cmd' and no supported package manager found"
fi
log "installing dependency: $pkg"
install_packages "$pm" "$pkg" || fail "failed installing $pkg"
command -v "$cmd" >/dev/null 2>&1 || fail "dependency '$cmd' still missing"
}
generate_hex() {
local len="$1"
if command -v openssl >/dev/null 2>&1; then
openssl rand -hex "$((len / 2))"
return
fi
tr -dc 'a-f0-9' </dev/urandom | head -c "$len"
}
normalize_arch_to_asset() {
local arch
arch="$(uname -m)"
case "$arch" in
x86_64|amd64)
echo "bifrost-gate-linux-amd64"
;;
aarch64|arm64)
echo "bifrost-gate-linux-arm64"
;;
armv7l|armv7|armhf)
echo "bifrost-gate-linux-armv7"
;;
*)
fail "unsupported architecture '$arch'. supported: amd64, arm64, armv7"
;;
esac
}
download_binary() {
local asset_name="$1"
local output_path="$2"
if [[ -f "$SCRIPT_DIR/$asset_name" ]]; then
log "using local asset: $SCRIPT_DIR/$asset_name"
cp "$SCRIPT_DIR/$asset_name" "$output_path"
return
fi
if [[ -f "$SCRIPT_DIR/${asset_name}.zip" ]]; then
log "using local asset archive: $SCRIPT_DIR/${asset_name}.zip"
extract_binary_from_zip "$SCRIPT_DIR/${asset_name}.zip" "$asset_name" "$output_path"
return
fi
local url="${BIFROST_DOWNLOAD_URL:-}"
if [[ -z "$url" ]]; then
local raw_base="${BIFROST_RAW_BASE_URL:-https://midasnet.work/tools/bifrost-gate}"
url="${raw_base%/}/${asset_name}.zip"
fi
local downloaded="$TMP_DIR/${asset_name}.download"
if [[ "$url" == *.zip ]]; then
downloaded="$TMP_DIR/${asset_name}.zip"
fi
log "downloading binary: $url"
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$url" -o "$downloaded" || fail "download failed from $url"
elif command -v wget >/dev/null 2>&1; then
wget -qO "$downloaded" "$url" || fail "download failed from $url"
else
fail "curl or wget is required"
fi
if [[ "$url" == *.zip ]]; then
extract_binary_from_zip "$downloaded" "$asset_name" "$output_path"
else
cp "$downloaded" "$output_path"
fi
}
extract_binary_from_zip() {
local zip_path="$1"
local asset_name="$2"
local output_path="$3"
ensure_cmd unzip unzip
if unzip -p "$zip_path" "$asset_name" >"$output_path" 2>/dev/null; then
return
fi
local zip_entry
zip_entry="$(unzip -Z1 "$zip_path" | awk 'NF {print; exit}')"
[[ -n "$zip_entry" ]] || fail "zip archive is empty: $zip_path"
unzip -p "$zip_path" "$zip_entry" >"$output_path" || fail "failed to extract binary from $zip_path"
}
download_manager_script() {
local manager_name="${BIFROST_MANAGER_NAME:-bifrost-manager.sh}"
local manager_url="${BIFROST_MANAGER_URL:-}"
local manager_tmp="$TMP_DIR/${manager_name##*/}"
if [[ -f "$SCRIPT_DIR/$manager_name" ]]; then
log "using local manager script: $SCRIPT_DIR/$manager_name"
install -m 755 "$SCRIPT_DIR/$manager_name" "$MANAGER_PATH"
return
fi
if [[ -z "$manager_url" ]]; then
local raw_base="${BIFROST_RAW_BASE_URL:-https://midasnet.work/tools/bifrost-gate}"
manager_url="${raw_base%/}/${manager_name}"
fi
log "downloading manager script: $manager_url"
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$manager_url" -o "$manager_tmp" || fail "manager download failed from $manager_url"
elif command -v wget >/dev/null 2>&1; then
wget -qO "$manager_tmp" "$manager_url" || fail "manager download failed from $manager_url"
else
fail "curl or wget is required"
fi
chmod 755 "$manager_tmp"
install -m 755 "$manager_tmp" "$MANAGER_PATH"
}
main() {
require_root
[[ "$(uname -s)" == "Linux" ]] || fail "Linux only"
command -v systemctl >/dev/null 2>&1 || fail "systemd is required"
ensure_cmd sed sed
ensure_cmd awk gawk
if ! command -v curl >/dev/null 2>&1 && ! command -v wget >/dev/null 2>&1; then
local pm
pm="$(detect_pkg_manager)"
[[ "$pm" != "unknown" ]] || fail "curl or wget required"
case "$pm" in
apt) install_packages "$pm" curl ca-certificates ;;
dnf|yum|zypper) install_packages "$pm" curl ca-certificates ;;
pacman) install_packages "$pm" curl ca-certificates ;;
apk) install_packages "$pm" curl ca-certificates ;;
esac
fi
local asset_name
asset_name="${BIFROST_ASSET_NAME:-$(normalize_arch_to_asset)}"
local panel_user panel_pass panel_pass2 admin_port license_timeout
read -r -p "Panel username [admin]: " panel_user
panel_user="${panel_user:-admin}"
while true; do
read -r -s -p "Panel password: " panel_pass
echo
read -r -s -p "Confirm panel password: " panel_pass2
echo
[[ -n "$panel_pass" ]] || { echo "Password cannot be empty"; continue; }
[[ "$panel_pass" == "$panel_pass2" ]] || { echo "Passwords do not match"; continue; }
break
done
read -r -p "Admin panel/API port [11001]: " admin_port
admin_port="${admin_port:-11001}"
[[ "$admin_port" =~ ^[0-9]+$ ]] || fail "admin port must be numeric"
(( admin_port >= 1 && admin_port <= 65535 )) || fail "admin port out of range"
read -r -p "License timeout seconds [10]: " license_timeout
license_timeout="${license_timeout:-10}"
[[ "$license_timeout" =~ ^[0-9]+$ ]] || fail "timeout must be numeric"
local api_key machine_id
api_key="$(generate_hex 48)"
machine_id="$(hostname)-$(uname -m)"
install -d -m 755 "$INSTALL_DIR"
install -d -m 755 "$CONFIG_DIR"
local downloaded="$TMP_DIR/$asset_name"
download_binary "$asset_name" "$downloaded"
install -m 755 "$downloaded" "$BINARY_PATH"
download_manager_script
if [[ -f "$CONFIG_PATH" ]]; then
cp "$CONFIG_PATH" "$CONFIG_PATH.bak.$(date +%s)"
fi
cat > "$CONFIG_PATH" <<JSON
{
"api_key": "$api_key",
"admin": {
"host": "0.0.0.0",
"port": $admin_port
},
"license": {
"license_key": "",
"machine_id": "$machine_id",
"timeout_seconds": $license_timeout
},
"panel": {
"username": "$panel_user",
"password": "$panel_pass"
},
"listeners": []
}
JSON
chmod 600 "$CONFIG_PATH"
cat > "/etc/systemd/system/$SERVICE_NAME" <<SERVICE
[Unit]
Description=Bifrost Gate Tunnel Service
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
Environment=PORT_FORWARDER_CONFIG=$CONFIG_PATH
ExecStart=$BINARY_PATH
WorkingDirectory=$INSTALL_DIR
Restart=always
RestartSec=2
LimitNOFILE=1048576
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=full
ProtectHome=read-only
ReadWritePaths=$CONFIG_DIR
[Install]
WantedBy=multi-user.target
SERVICE
systemctl daemon-reload
systemctl enable "$SERVICE_NAME"
systemctl restart "$SERVICE_NAME"
local ip
ip="$(hostname -I 2>/dev/null | awk '{print $1}')"
ip="${ip:-$DEFAULT_PUBLIC_IP}"
log "install complete"
log "service: $SERVICE_NAME"
log "binary: $BINARY_PATH"
log "config: $CONFIG_PATH"
log "manager: bifrost"
log "panel: http://${ip}:${admin_port}/login"
log "api key: $api_key"
systemctl --no-pager --full status "$SERVICE_NAME" || true
}
main "$@"