#!/usr/bin/env node const http = require('http'); const fs = require('fs'); const PORT = 9876; const LOG_FILE = '/tmp/clawdnode-debug.log'; // Open log file const logStream = fs.createWriteStream(LOG_FILE, { flags: 'a' }); function log(msg) { const ts = new Date().toISOString(); const line = `[${ts}] ${msg}`; console.log(line); logStream.write(line + '\n'); } function parseBody(req) { return new Promise((resolve) => { let body = ''; req.on('data', chunk => body += chunk); req.on('end', () => { try { resolve(JSON.parse(body || '{}')); } catch { resolve({ raw: body }); } }); }); } const server = http.createServer(async (req, res) => { const body = await parseBody(req); res.setHeader('Content-Type', 'application/json'); res.setHeader('Access-Control-Allow-Origin', '*'); if (req.method === 'OPTIONS') { res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); res.end(); return; } const ip = req.socket.remoteAddress; switch (req.url) { case '/health': res.end(JSON.stringify({ status: 'ok', time: new Date().toISOString() })); break; case '/log': log(`📋 [LOG] from ${ip}: ${JSON.stringify(body)}`); res.end(JSON.stringify({ status: 'logged' })); break; case '/notification': log(`🔔 [NOTIFICATION] from ${ip}:`); log(` App: ${body.app || body.packageName}`); log(` Title: ${body.title}`); log(` Text: ${body.text}`); log(` Package: ${body.package || body.packageName}`); log(` Actions: ${JSON.stringify(body.actions || [])}`); log(` Full: ${JSON.stringify(body)}`); res.end(JSON.stringify({ status: 'received', id: body.id })); break; case '/call': log(`📞 [CALL] from ${ip}:`); log(` Number: ${body.number}`); log(` Contact: ${body.contact}`); log(` State: ${body.state}`); log(` Full: ${JSON.stringify(body)}`); res.end(JSON.stringify({ status: 'received' })); break; case '/event': log(`⚡ [EVENT:${body.type}] from ${ip}: ${JSON.stringify(body)}`); res.end(JSON.stringify({ status: 'received' })); break; case '/error': log(`❌ [ERROR] from ${ip}: ${JSON.stringify(body)}`); res.end(JSON.stringify({ status: 'logged' })); break; case '/lifecycle': log(`🔄 [LIFECYCLE:${body.event}] from ${ip}: ${body.message || ''}`); res.end(JSON.stringify({ status: 'logged' })); break; default: log(`[${req.method}] ${req.url} from ${ip}`); res.end(JSON.stringify({ status: 'ok', endpoints: ['/health', '/log', '/notification', '/call', '/event', '/error', '/lifecycle'] })); } }); server.listen(PORT, '0.0.0.0', () => { log(`🚀 ClawdNode Debug Server starting on :${PORT}`); log(`📝 Logging to ${LOG_FILE}`); log(`🌐 Accessible at http://100.123.216.65:${PORT} (Tailscale)`); });