CoolFace
Apppublic

ckriti/HuggingClaw

sourceHugging Facemitupdated 7mo agoView on Hugging Face
0likes
token-redirect.cjs38 linesDownload Raw Back to scripts
1/**2 * token-redirect.cjs — Node.js preload script3 *4 * Intercepts HTTP requests to the root URL "/" and redirects to5 * "/?token=GATEWAY_TOKEN" so the Control UI auto-fills the gateway token.6 *7 * Loaded via NODE_OPTIONS --require before OpenClaw starts.8 */9'use strict';10 11const http = require('http');12 13const GATEWAY_TOKEN = process.env.GATEWAY_TOKEN || 'huggingclaw';14const origEmit = http.Server.prototype.emit;15 16http.Server.prototype.emit = function (event, ...args) {17  if (event === 'request') {18    const [req, res] = args;19    // Only redirect normal GET to "/" without token — skip WebSocket upgrades20    if (req.method === 'GET' && !req.headers.upgrade) {21      try {22        const url = new URL(req.url, `http://${req.headers.host || 'localhost'}`);23        if (url.pathname === '/' && !url.searchParams.has('token')) {24          url.searchParams.set('token', GATEWAY_TOKEN);25          res.writeHead(302, { Location: url.pathname + url.search });26          res.end();27          return true;28        }29      } catch (_) {30        // URL parse error — pass through31      }32    }33  }34  return origEmit.apply(this, [event, ...args]);35};36 37console.log('[token-redirect] Gateway token redirect active');38