Skip to content
14 changes: 11 additions & 3 deletions backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ const logger = {
},
_write(streams, obj) {
const line = JSON.stringify(obj) + '\n';
for (const s of streams) {
if (s) s.write(line); else console.log(line.trimEnd());
const writable = streams.filter(Boolean);
if (writable.length > 0) {
for (const s of writable) s.write(line);
} else {
console.log(line.trimEnd());
}
},
info(eventType, msg, extra = {}) {
Expand Down Expand Up @@ -204,7 +207,12 @@ app.post('/autofix/webhook', async (req, res) => {
const requestId = req.id;
try {
const sig = req.headers['x-autofix-signature'] ?? req.headers['x-hub-signature-256'] ?? '';
const rawBody = req.rawBody ?? Buffer.alloc(0);
const rawBody = req.rawBody;

if (!rawBody || !Buffer.isBuffer(rawBody)) {
logger.error('autofix.webhook', 'Missing rawBody — middleware misconfiguration', { requestId });
return res.status(500).json({ ok: false, error: 'Internal server error', requestId });
}

if (!AUTOFIX_SECRET || !verifySignature(rawBody, sig)) {
const reason = !AUTOFIX_SECRET ? 'AUTOFIX_SECRET not configured' : 'Signature mismatch';
Expand Down
10 changes: 8 additions & 2 deletions prod-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -424,16 +424,22 @@ if command -v systemctl >/dev/null 2>&1; then
say "runewager-endpoint restarted via systemd"
else
warn "systemctl restart runewager-endpoint failed — falling back to nohup"
pkill -f "node .*backend\.js" 2>/dev/null || true
pkill -f "node .*${PROJECT_DIR}/backend\.js" 2>/dev/null || true
sleep 1
# Re-check port: another process may have bound it during the failed systemd restart window
if is_port_listening "$ENDPOINT_PORT"; then
say "Port $ENDPOINT_PORT still in use — freeing before nohup fallback..."
free_port_if_conflicted "$ENDPOINT_PORT" "" || true
sleep 1
fi
nohup node "$PROJECT_DIR/backend.js" \
>> "$ENDPOINT_LOG" 2>> "$ENDPOINT_ERR" < /dev/null &
disown || true
say "runewager-endpoint started via nohup fallback"
fi
else
# No systemd — direct nohup
pkill -f "node .*backend\.js" 2>/dev/null || true
pkill -f "node .*${PROJECT_DIR}/backend\.js" 2>/dev/null || true
sleep 1
if is_port_listening "$ENDPOINT_PORT"; then
free_port_if_conflicted "$ENDPOINT_PORT" "" || true
Expand Down
3 changes: 2 additions & 1 deletion runewager-endpoint.service
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Wants=network-online.target
Type=simple
WorkingDirectory=/var/www/html/Runewager
# Clear any stale process holding port 3001 before each start to prevent EADDRINUSE crash-loops.
ExecStartPre=/bin/sh -c 'port=${ENDPOINT_PORT:-3001}; pid=$(lsof -t -i:"$port" 2>/dev/null || fuser "$port"/tcp 2>/dev/null || true); [ -n "$pid" ] && echo "[endpoint-prestart] killing stale PID $pid on port $port" && kill -9 $pid && sleep 1 || true'
# The '+' prefix runs this step as root so it can kill processes owned by any user.
ExecStartPre=+/bin/sh -c 'port=${ENDPOINT_PORT:-3001}; pid=$(lsof -t -i:"$port" 2>/dev/null || fuser "$port"/tcp 2>/dev/null || true); [ -n "$pid" ] && echo "[endpoint-prestart] killing stale PID $pid on port $port" && kill -9 $pid && sleep 1 || true'
ExecStart=/usr/bin/node /var/www/html/Runewager/backend.js
User=root
Group=root
Expand Down
3 changes: 2 additions & 1 deletion scripts/rw_cpu_guard.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ DRY_RUN="${DRY_RUN:-0}"
# Any process whose *parent* matches a pattern is also protected.
#
WHITELIST_PATTERNS=(
# Guard itself
# Guard itself + sibling monitoring scripts — never touch these
"rw_cpu_guard"
"gcz_cpu_guard"

# Runewager bot — all known launch forms
"runewager"
Expand Down