Skip to content

Commit e710bdb

Browse files
CrazyBoyMclaude
andcommitted
Rewrite HTTP server: remove websocat, fix nc with FIFO for all Unix
- Remove websocat support (nearly nobody has it installed) - Rename gateway_run_http -> gateway_run_socat (clearer) - Replace broken nc -c/-e approach with universal FIFO pipe method: nc listens on port, pipes through FIFO to bash handler Works on OpenBSD nc (macOS/Ubuntu), GNU nc, BusyBox nc, ncat - Auto-detect nc flavor (-l vs -l -p) via help output - Clean up FIFO on gateway shutdown - Update doctor output to reflect socat -> nc fallback chain - Update gateway usage text with reverse proxy recommendation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent efc3c52 commit e710bdb

2 files changed

Lines changed: 43 additions & 47 deletions

File tree

bashclaw

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,17 +263,14 @@ cmd_doctor() {
263263

264264
# Check gateway HTTP server capability
265265
local http_server=""
266-
if is_command_available websocat; then
267-
printf '[OK] websocat found (gateway: WebSocket + HTTP)\n'
268-
http_server="websocat"
269-
elif is_command_available socat; then
270-
printf '[OK] socat found (gateway: HTTP)\n'
266+
if is_command_available socat; then
267+
printf '[OK] socat found (gateway: concurrent HTTP)\n'
271268
http_server="socat"
272269
elif is_command_available nc; then
273-
printf '[OK] nc found (gateway: basic HTTP fallback)\n'
270+
printf '[OK] nc found (gateway: serial HTTP via FIFO)\n'
274271
http_server="nc"
275272
else
276-
printf '[WARN] No HTTP server tool found (websocat/socat/nc). Gateway will not serve HTTP.\n'
273+
printf '[WARN] No HTTP server tool found (socat or nc). Gateway will not serve HTTP.\n'
277274
issues=$((issues + 1))
278275
fi
279276

lib/cmd_gateway.sh

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,25 @@ gateway_start() {
6969
local health_pid=$!
7070

7171
# Start the HTTP server (foreground)
72-
if is_command_available websocat; then
73-
gateway_run_websocat
74-
elif is_command_available socat; then
75-
gateway_run_http
72+
if is_command_available socat; then
73+
gateway_run_socat
74+
elif is_command_available nc; then
75+
gateway_run_nc
7676
else
77-
gateway_run_bash_http
77+
log_warn "No HTTP server available (need socat or nc)"
78+
log_warn "Gateway is running but cannot serve HTTP."
79+
log_warn "CLI and channel listeners will still work."
80+
while [[ "$GATEWAY_RUNNING" == "true" ]]; do
81+
sleep 10
82+
done
7883
fi
7984

8085
# Cleanup on exit
8186
GATEWAY_RUNNING=false
8287
kill "$channels_pid" "$cron_pid" "$health_pid" 2>/dev/null
8388
wait "$channels_pid" "$cron_pid" "$health_pid" 2>/dev/null
8489
rm -f "$GATEWAY_PID_FILE"
90+
rm -f "${BASHCLAW_STATE_DIR:?}/gateway.fifo"
8591
log_info "Gateway stopped"
8692
}
8793

@@ -172,48 +178,38 @@ gateway_handle_sigterm() {
172178

173179
# ---- HTTP Servers ----
174180

175-
gateway_run_websocat() {
176-
log_info "Starting WebSocket server via websocat on port $GATEWAY_PORT"
177-
websocat -s "$GATEWAY_PORT" --text -e \
178-
sh -c 'source '"'"'${BASH_SOURCE[0]%/*}/../gateway/http_handler.sh'"'"' && handle_request' \
179-
2>&1 || log_error "websocat exited"
180-
}
181-
182-
gateway_run_http() {
181+
gateway_run_socat() {
183182
log_info "Starting HTTP server via socat on port $GATEWAY_PORT"
183+
local handler_script
184+
handler_script="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/gateway/http_handler.sh"
184185
socat TCP-LISTEN:"$GATEWAY_PORT",reuseaddr,fork \
185-
EXEC:"bash ${BASH_SOURCE[0]%/*}/../gateway/http_handler.sh" \
186+
EXEC:"bash '$handler_script'" \
186187
2>&1 || log_error "socat exited"
187188
}
188189

189-
gateway_run_bash_http() {
190+
gateway_run_nc() {
190191
local handler_script
191192
handler_script="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/gateway/http_handler.sh"
192193

193-
if is_command_available nc; then
194-
log_info "Starting HTTP server via nc on port $GATEWAY_PORT"
195-
while [[ "$GATEWAY_RUNNING" == "true" ]]; do
196-
# nc -l listens for one connection, pipes it through the handler script
197-
# macOS nc: -l -p PORT; GNU nc: -l -p PORT or -l PORT
198-
# Use a subshell to handle one request at a time
199-
if [[ "$(uname -s)" == "Darwin" ]]; then
200-
nc -l "$GATEWAY_PORT" -c "bash '$handler_script'" 2>/dev/null || \
201-
bash -c "exec 3<>/dev/tcp/127.0.0.1/$GATEWAY_PORT 2>/dev/null; exec 3>&-" 2>/dev/null || \
202-
true
203-
else
204-
nc -l -p "$GATEWAY_PORT" -e "bash '$handler_script'" 2>/dev/null || \
205-
nc -l "$GATEWAY_PORT" -c "bash '$handler_script'" 2>/dev/null || \
206-
true
207-
fi
208-
done
209-
else
210-
log_warn "No HTTP server available (need websocat, socat, or nc)"
211-
log_warn "Gateway is running but cannot serve HTTP. Install socat: brew install socat"
212-
log_warn "CLI and channel listeners will still work."
213-
while [[ "$GATEWAY_RUNNING" == "true" ]]; do
214-
sleep 10
215-
done
194+
local fifo_path="${BASHCLAW_STATE_DIR:?}/gateway.fifo"
195+
rm -f "$fifo_path"
196+
mkfifo "$fifo_path"
197+
198+
# Detect nc flavor: OpenBSD nc (macOS/Ubuntu) uses "nc -l PORT",
199+
# GNU/BusyBox nc uses "nc -l -p PORT"
200+
local nc_args="-l"
201+
if nc -h 2>&1 | grep -q '\-p'; then
202+
nc_args="-l -p"
216203
fi
204+
205+
log_info "Starting HTTP server via nc + FIFO on port $GATEWAY_PORT (nc_args='$nc_args')"
206+
207+
while [[ "$GATEWAY_RUNNING" == "true" ]]; do
208+
nc $nc_args "$GATEWAY_PORT" < "$fifo_path" | \
209+
bash "$handler_script" > "$fifo_path" 2>/dev/null || true
210+
done
211+
212+
rm -f "$fifo_path"
217213
}
218214

219215
# ---- Channel Listeners ----
@@ -443,7 +439,10 @@ Options:
443439
--stop Stop running gateway daemon
444440
-h, --help Show this help
445441
446-
The gateway runs channel listeners, a cron runner, and an HTTP/WebSocket
447-
server for receiving messages.
442+
The gateway runs channel listeners, a cron runner, and an HTTP server
443+
for the web dashboard and API.
444+
445+
HTTP server: socat (concurrent) or nc + FIFO (serial fallback).
446+
For production, put nginx/caddy in front as reverse proxy.
448447
EOF
449448
}

0 commit comments

Comments
 (0)