Files
crosscode-platform/scripts/lib/frpc.sh
T
joshuaandClaude Opus 5.5 19709c2c75 frpc: restart it the first time the admin lines are added (a reload can't work yet)
Found in the VM test: the new install's tunnels never opened, because
frpc only reads admin_addr when it starts, so the first reload failed.
That first time (and whenever a reload fails), frpc is restarted under
systemd and the report says so; later changes reload as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 15:48:28 +02:00

148 lines
6.2 KiB
Bash

# shellcheck shell=bash
# frpc tunnels for church installs (docs/AGENT-1.0-PLAN.md §6). The script only ever appends
# marked blocks to frpc.ini and removes its own blocks again; nothing outside a block is changed,
# except two admin lines added once to [common] so `frpc reload` can pick up new tunnels without
# dropping the others. Every change: a backup, a temporary file, `frpc verify`, then the move.
FRPC_INI="${FRPC_INI:-/etc/frp/frpc.ini}"
FRPC_BACKUPS="${FRPC_BACKUPS:-/etc/frp/backups}"
FRPC_BIN="${FRPC_BIN:-frpc}"
frpc_begin() { printf '# >>> crosscode:%s:%s' "$1" "$2"; }
frpc_end() { printf '# <<< crosscode:%s:%s' "$1" "$2"; }
# frpc_block <church> <product> <web port> <api port>: the block (no leading blank line).
frpc_block() {
local church="$1" product="$2" web="$3" api="$4" name
name="$church-$product"
cat <<EOF
$(frpc_begin "$church" "$product") (added by crosscode $(date +%F), do not edit by hand)
[$name-web]
type = tcp
local_ip = 127.0.0.1
local_port = $web
remote_port = $web
[$name-api]
type = tcp
local_ip = 127.0.0.1
local_port = $api
remote_port = $api
$(frpc_end "$church" "$product")
EOF
}
frpc_check_format() {
[ -f "$FRPC_INI" ] || die "No $FRPC_INI: is frpc installed and set up on this server?"
if [ -f "$(dirname "$FRPC_INI")/frpc.toml" ] && ! grep -q '^\[common\]' "$FRPC_INI"; then
die "This server's frpc uses frpc.toml; crosscode only edits frpc.ini so far."
fi
}
frpc_has_block() { grep -qF "$(frpc_begin "$1" "$2")" "$FRPC_INI" 2>/dev/null; }
# frpc_ports <church> <product>: the web and api ports in its block ("web api"), "" if none.
frpc_ports() {
awk -v b="$(frpc_begin "$1" "$2")" -v e="$(frpc_end "$1" "$2")" '
index($0, b) == 1 { inb = 1; next }
index($0, e) == 1 { inb = 0 }
inb && /^\[.*-web\]/ { w = 1; a = 0 }
inb && /^\[.*-api\]/ { a = 1; w = 0 }
inb && /^remote_port/ { split($0, p, "="); gsub(/ /, "", p[2]); if (w) web = p[2]; if (a) api = p[2] }
END { if (web != "" || api != "") print web " " api }
' "$FRPC_INI" 2>/dev/null
}
# with_admin_lines <frpc.ini>: the file, with admin_addr / admin_port added to [common] if it has
# no admin_addr there (an existing one is left as it is).
with_admin_lines() {
if awk '/^\[common\]/ { c = 1; next } /^\[/ { c = 0 } c && /^[[:space:]]*admin_addr[[:space:]]*=/ { f = 1 } END { exit !f }' "$1"; then
cat "$1"
else
awk '{ print } /^\[common\][[:space:]]*$/ && !done { print "admin_addr = 127.0.0.1"; print "admin_port = 7400"; done = 1 }' "$1"
fi
}
# frpc_replace <new file>: checks and moves a new frpc.ini into place (backup first).
frpc_replace() {
local new="$1" stamp
if ! "$FRPC_BIN" verify -c "$new" >/dev/null 2>&1; then
rm -f "$new"
die "frpc verify refused the new frpc.ini, so it wasn't changed."
fi
stamp="$(date +%Y%m%d%H%M%S)"
mkdir -p "$FRPC_BACKUPS"
cp -p "$FRPC_INI" "$FRPC_BACKUPS/frpc.ini.$stamp"
chmod --reference="$FRPC_INI" "$new" 2>/dev/null || chmod 600 "$new"
mv -f "$new" "$FRPC_INI"
}
# frpc_add <church> <product> <web> <api>: appends the block (no change if it's there already).
frpc_add() {
local church="$1" product="$2" web="$3" api="$4" new name
frpc_check_format
if frpc_has_block "$church" "$product"; then
info "frpc: $church:$product is already in $FRPC_INI."
return 0
fi
name="$church-$product"
if grep -qE "^\[($name-web|$name-api)\]" "$FRPC_INI"; then
die "frpc: a tunnel named $name-web or $name-api is already in $FRPC_INI (outside crosscode's blocks)."
fi
if grep -qE "^[[:space:]]*(local_port|remote_port)[[:space:]]*=[[:space:]]*($web|$api)[[:space:]]*$" "$FRPC_INI"; then
die "frpc: port $web or $api is already used in $FRPC_INI."
fi
new="$(mktemp "$(dirname "$FRPC_INI")/.frpc.ini.XXXXXX")"
with_admin_lines "$FRPC_INI" > "$new"
# The admin lines were added just now: the running frpc only reads them when it starts.
grep -q "^admin_addr" "$FRPC_INI" || FRPC_NEEDS_RESTART=1
# Keep the file's last line intact: only add a newline if it had none.
[ -z "$(tail -c 1 "$new")" ] || echo >> "$new"
{ echo; frpc_block "$church" "$product" "$web" "$api"; } >> "$new"
frpc_replace "$new"
info "frpc: added $name-web ($web) and $name-api ($api)."
}
# frpc_remove <church> <product>: removes its block (and the blank line added before it).
frpc_remove() {
local church="$1" product="$2" new
frpc_check_format
frpc_has_block "$church" "$product" || { info "frpc: no block for $church:$product."; return 0; }
new="$(mktemp "$(dirname "$FRPC_INI")/.frpc.ini.XXXXXX")"
# Drops the block's lines and the one blank line straight before its begin marker.
awk -v b="$(frpc_begin "$church" "$product")" -v e="$(frpc_end "$church" "$product")" '
{ lines[NR] = $0 }
END {
start = 0; stop = 0
for (i = 1; i <= NR; i++) if (index(lines[i], b) == 1) { start = i; break }
for (i = start; i <= NR; i++) if (index(lines[i], e) == 1) { stop = i; break }
if (start > 1 && lines[start - 1] == "") start--
for (i = 1; i <= NR; i++) if (i < start || i > stop) print lines[i]
}
' "$FRPC_INI" > "$new"
frpc_replace "$new"
info "frpc: removed $church-$product."
}
FRPC_NEEDS_RESTART=0
# frpc_reload: picks up the changed frpc.ini. Normally a reload (no other tunnel drops). The first
# time the admin lines were added, or if the reload fails, frpc is restarted instead: its other
# tunnels drop for a few seconds (found in the VM test: a reload can't work before the restart).
frpc_reload() {
if [ "$FRPC_NEEDS_RESTART" != 1 ] && "$FRPC_BIN" reload -c "$FRPC_INI" >/dev/null 2>&1; then
return 0
fi
if command -v systemctl >/dev/null && systemctl list-unit-files frpc.service >/dev/null 2>&1 && systemctl cat frpc >/dev/null 2>&1; then
if [ "$FRPC_NEEDS_RESTART" = 1 ]; then
warn "frpc: restarting it once so it reads the admin lines (its other tunnels drop for a few seconds; later changes reload without this)."
else
warn "frpc: reload didn't work, so restarting it (its other tunnels drop for a few seconds)."
fi
report_result frpcRestarted true
systemctl restart frpc && FRPC_NEEDS_RESTART=0 && return 0
fi
warn "frpc couldn't be reloaded or restarted: restart it by hand (systemctl restart frpc) to pick up the change."
return 1
}