_set_captcha.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Helper: inject a known captcha into Redis (FastJSON-serialized) then login.
  2. // Usage: node _set_captcha.js -> prints the token to stdout (last line: TOKEN=<token>)
  3. const net = require('net');
  4. const http = require('http');
  5. const uuid = 'valcap' + Date.now() + Math.floor(Math.random() * 1000);
  6. const key = 'tenantid:system:captcha_codes:' + uuid;
  7. // FastJSON toJSONString("1234", WriteClassName) => "1234" (with quotes)
  8. const value = '"1234"';
  9. function setRedis(cb) {
  10. const c = net.createConnection({ host: 'localhost', port: 6379 }, () => {
  11. const klen = Buffer.byteLength(key);
  12. const vlen = Buffer.byteLength(value);
  13. const cmd =
  14. '*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';
  15. c.write(cmd);
  16. });
  17. let buf = '';
  18. c.on('data', d => { buf += d.toString(); if (buf.includes('\r\n')) { c.end(); } });
  19. c.on('end', () => cb(null, buf.trim()));
  20. c.on('error', e => cb(e));
  21. }
  22. function login(cb) {
  23. const body = JSON.stringify({
  24. username: 'admin',
  25. password: 'Admin@123456',
  26. tenantCode: 'cs1',
  27. code: '1234',
  28. uuid: uuid
  29. });
  30. const req = http.request({
  31. hostname: 'localhost',
  32. port: 8006,
  33. path: '/login',
  34. method: 'POST',
  35. headers: { 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': Buffer.byteLength(body) }
  36. }, res => {
  37. let d = '';
  38. res.on('data', c => { d += c; });
  39. res.on('end', () => cb(null, res.statusCode, d));
  40. });
  41. req.on('error', e => cb(e));
  42. req.write(body);
  43. req.end();
  44. }
  45. setRedis((err, resp) => {
  46. if (err) { console.error('REDIS ERR', err.message); process.exit(1); }
  47. console.log('SET resp:', resp);
  48. login((err, status, d) => {
  49. if (err) { console.error('LOGIN ERR', err.message); process.exit(1); }
  50. console.log('LOGIN status:', status);
  51. console.log('LOGIN body:', d.substring(0, 200));
  52. try {
  53. const j = JSON.parse(d);
  54. if (j.token) console.log('TOKEN=' + j.token);
  55. else console.log('TOKEN=NONE');
  56. } catch (e) { console.log('TOKEN=NONE'); }
  57. });
  58. });