# 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 : the block (no leading blank line). frpc_block() { local church="$1" product="$2" web="$3" api="$4" name name="$church-$product" cat </dev/null; } # frpc_ports : 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 : 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 : 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 : 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" # 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 : 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_reload() { "$FRPC_BIN" reload -c "$FRPC_INI" || warn "frpc reload failed: restart frpc (systemctl restart frpc) to pick up the change." }