// Helper: inject a known captcha into Redis (FastJSON-serialized) then login. // Usage: node _set_captcha.js -> prints the token to stdout (last line: TOKEN=) const net = require('net'); const http = require('http'); const uuid = 'valcap' + Date.now() + Math.floor(Math.random() * 1000); const key = 'tenantid:system:captcha_codes:' + uuid; // FastJSON toJSONString("1234", WriteClassName) => "1234" (with quotes) const value = '"1234"'; function setRedis(cb) { const c = net.createConnection({ host: 'localhost', port: 6379 }, () => { const klen = Buffer.byteLength(key); const vlen = Buffer.byteLength(value); const cmd = '*5\r\n$3\r\nSET\r\n$' + klen + '\r\n' + key + '\r\n$' + vlen + '\r\n' + value + '\r\n$2\r\nEX\r\n$3\r\n600\r\n'; c.write(cmd); }); let buf = ''; c.on('data', d => { buf += d.toString(); if (buf.includes('\r\n')) { c.end(); } }); c.on('end', () => cb(null, buf.trim())); c.on('error', e => cb(e)); } function login(cb) { const body = JSON.stringify({ username: 'admin', password: 'Admin@123456', tenantCode: 'cs1', code: '1234', uuid: uuid }); const req = http.request({ hostname: 'localhost', port: 8006, path: '/login', method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': Buffer.byteLength(body) } }, res => { let d = ''; res.on('data', c => { d += c; }); res.on('end', () => cb(null, res.statusCode, d)); }); req.on('error', e => cb(e)); req.write(body); req.end(); } setRedis((err, resp) => { if (err) { console.error('REDIS ERR', err.message); process.exit(1); } console.log('SET resp:', resp); login((err, status, d) => { if (err) { console.error('LOGIN ERR', err.message); process.exit(1); } console.log('LOGIN status:', status); console.log('LOGIN body:', d.substring(0, 200)); try { const j = JSON.parse(d); if (j.token) console.log('TOKEN=' + j.token); else console.log('TOKEN=NONE'); } catch (e) { console.log('TOKEN=NONE'); } }); });