mirror of
https://github.com/kiddin9/op-packages.git
synced 2026-11-15 04:53:00 +08:00
100 lines
2.8 KiB
Bash
100 lines
2.8 KiB
Bash
#!/bin/sh /etc/rc.common
|
|
# Gately OpenWrt procd init script
|
|
|
|
START=99
|
|
STOP=10
|
|
USE_PROCD=1
|
|
|
|
PROG=/usr/bin/gately
|
|
|
|
sync_gately_config() {
|
|
local configdir="$1"
|
|
local admin_port="$2"
|
|
local admin_safe_url="$3"
|
|
local admin_internet="$4"
|
|
local initial_password="$5"
|
|
|
|
mkdir -p "$configdir"
|
|
chmod 700 "$configdir"
|
|
|
|
if [ -x /usr/bin/ucode ]; then
|
|
GATELY_CFG_DIR="$configdir" \
|
|
GATELY_PORT="$admin_port" \
|
|
GATELY_SAFE_URL="$admin_safe_url" \
|
|
GATELY_INTERNET="$admin_internet" \
|
|
/usr/bin/ucode -e '
|
|
import { access, readfile, writefile, chmod } from "fs";
|
|
let dir = getenv("GATELY_CFG_DIR") || "/etc/gately";
|
|
let port = trim(getenv("GATELY_PORT") || "16680");
|
|
let safe = trim(getenv("GATELY_SAFE_URL") || "");
|
|
let inet = (getenv("GATELY_INTERNET") === "1");
|
|
if (length(safe) > 0 && substr(safe, 0, 1) !== "/") {
|
|
safe = "/" + safe;
|
|
}
|
|
let path = dir + "/gately.json";
|
|
let cfg = {};
|
|
if (access(path, "r")) {
|
|
try { cfg = json(readfile(path) || "{}"); } catch (e) { cfg = {}; }
|
|
}
|
|
cfg.listen = ":" + port;
|
|
cfg.safePath = safe;
|
|
cfg.lanOnly = !inet;
|
|
writefile(path, sprintf("%.2J\n", cfg));
|
|
chmod(path, 0600);
|
|
' 2>/dev/null
|
|
fi
|
|
|
|
local pw_stamp="$configdir/.last_initial_password"
|
|
local old_pw=""
|
|
[ -f "$pw_stamp" ] && old_pw="$(cat "$pw_stamp" 2>/dev/null)"
|
|
if [ ! -f "$configdir/account.json" ] || [ "$initial_password" != "$old_pw" ]; then
|
|
rm -f "$configdir/account.json" "$configdir/auth.json"
|
|
printf '%s' "$initial_password" > "$pw_stamp"
|
|
chmod 600 "$pw_stamp"
|
|
fi
|
|
}
|
|
|
|
start_service() {
|
|
config_load gately
|
|
|
|
local enabled configdir applydir initial_password admin_port admin_safe_url admin_internet
|
|
config_get_bool enabled main enabled 0
|
|
config_get configdir main configdir '/etc/gately'
|
|
config_get applydir main applydir ''
|
|
config_get initial_password main initial_password 'admin123456'
|
|
config_get admin_port main admin_port '16680'
|
|
config_get admin_safe_url main admin_safe_url ''
|
|
config_get_bool admin_internet main admin_internet 0
|
|
|
|
sync_gately_config "$configdir" "$admin_port" "$admin_safe_url" "$admin_internet" "$initial_password"
|
|
|
|
[ "$enabled" -eq 1 ] || return 0
|
|
|
|
procd_open_instance gately
|
|
procd_set_param command "$PROG" -c "$configdir" -listen ":$admin_port"
|
|
[ -n "$applydir" ] && procd_append_param command -apply "$applydir"
|
|
[ -n "$initial_password" ] && procd_set_param env GATELY_INITIAL_PASSWORD="$initial_password"
|
|
|
|
# 进程异常退出后自动拉起:阈值 3600 秒内最多 5 次,间隔 5 秒
|
|
procd_set_param respawn 3600 5 5
|
|
procd_set_param limits nofile="65535 65535"
|
|
procd_set_param stdout 1
|
|
procd_set_param stderr 1
|
|
procd_set_param file /etc/config/gately "$configdir/gately.json"
|
|
procd_close_instance
|
|
}
|
|
|
|
stop_service() {
|
|
rm -f /tmp/gately_info_cache 2>/dev/null
|
|
}
|
|
|
|
reload_service() {
|
|
stop
|
|
sleep 1
|
|
start
|
|
}
|
|
|
|
service_triggers() {
|
|
procd_add_reload_trigger "gately"
|
|
}
|