fix(streamlit): Domain editing UCI update before async HAProxy reload

- Move UCI domain update BEFORE slow haproxyctl reload (prevents RPC timeout)
- Run HAProxy generate/reload/cert in background subshell
- Fix vhost name encoding: use tr '.-' '_' (matches streamlitctl)
- Use sed instead of jq for mitmproxy routes (jq may not be installed)
- Tested: domain edit returns immediately, UCI updated correctly

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
CyberMind-FR 2026-02-25 11:51:31 +01:00
parent 41d5fadf3f
commit adc83c3d8e

View File

@ -910,8 +910,9 @@ rename_instance() {
local port=$(uci -q get "${CONFIG}.${id}.port") local port=$(uci -q get "${CONFIG}.${id}.port")
if [ "$emancipated" = "1" ] && [ -n "$new_domain" ] && [ "$new_domain" != "$old_domain" ]; then if [ "$emancipated" = "1" ] && [ -n "$new_domain" ] && [ "$new_domain" != "$old_domain" ]; then
local old_vhost=$(echo "$old_domain" | sed 's/\./_/g') # Use tr '.-' '_' to match streamlitctl vhost naming
local new_vhost=$(echo "$new_domain" | sed 's/\./_/g') local old_vhost=$(echo "$old_domain" | tr '.-' '_')
local new_vhost=$(echo "$new_domain" | tr '.-' '_')
local backend_name="streamlit_${id}" local backend_name="streamlit_${id}"
# Remove old vhost and cert entries # Remove old vhost and cert entries
@ -935,33 +936,35 @@ rename_instance() {
uci commit haproxy uci commit haproxy
# Update mitmproxy routes # Update instance domain FIRST (before slow operations)
uci set "${CONFIG}.${id}.domain=${new_domain}"
uci commit "$CONFIG"
# Update mitmproxy routes (use sed, jq may not be available)
local routes_file="/srv/mitmproxy/haproxy-routes.json" local routes_file="/srv/mitmproxy/haproxy-routes.json"
if [ -f "$routes_file" ]; then if [ -f "$routes_file" ]; then
# Remove old route and add new one # Remove old route
if command -v jq >/dev/null 2>&1; then sed -i "s/,\"${old_domain}\":\[\"[^\"]*\",[0-9]*\]//g" "$routes_file" 2>/dev/null || true
jq --arg old "$old_domain" --arg new "$new_domain" --argjson port "$port" \ sed -i "s/\"${old_domain}\":\[\"[^\"]*\",[0-9]*\],//g" "$routes_file" 2>/dev/null || true
'del(.[$old]) | . + {($new): ["192.168.255.1", $port]}' "$routes_file" > "/tmp/routes_$$.json" && \ # Add new route
mv "/tmp/routes_$$.json" "$routes_file" sed -i "s/}$/,\"${new_domain}\":[\"192.168.255.1\",${port}]}/" "$routes_file" 2>/dev/null || true
fi
fi fi
# Reload HAProxy and mitmproxy # Reload HAProxy and mitmproxy in background (don't block RPC)
haproxyctl generate >/dev/null 2>&1 (
haproxyctl reload >/dev/null 2>&1 haproxyctl generate >/dev/null 2>&1
/etc/init.d/mitmproxy restart >/dev/null 2>&1 & haproxyctl reload >/dev/null 2>&1
/etc/init.d/mitmproxy restart >/dev/null 2>&1
# Update instance domain # Request new certificate if not on gk2 wildcard
uci set "${CONFIG}.${id}.domain=${new_domain}" case "$new_domain" in
*.gk2.secubox.in) ;;
# Request new certificate *) haproxyctl cert add "$new_domain" >/dev/null 2>&1 ;;
case "$new_domain" in esac
*.gk2.secubox.in) ;; ) &
*) haproxyctl cert add "$new_domain" >/dev/null 2>&1 & ;;
esac
fi fi
uci commit "$CONFIG" # Commit any remaining changes (display name if domain wasn't changed)
uci commit "$CONFIG" 2>/dev/null || true
json_init_obj json_init_obj
json_add_boolean "success" 1 json_add_boolean "success" 1