|
7 | 7 | 'use strict'; |
8 | 8 | const MailParser = require('mailparser').MailParser; |
9 | 9 | const simplesmtp = require('simplesmtp'); |
10 | | - |
| 10 | +const Redis = require('ioredis'); |
11 | 11 | const config = require('../config').default.getProperties(); |
12 | 12 |
|
13 | 13 | const TEMPLATES_WITH_NO_CODE = new Set(['passwordResetEmail']); |
14 | 14 |
|
| 15 | +// SMS polling |
| 16 | +const redis = new Redis(config.redis); |
| 17 | +const usersLastSms = {}; |
| 18 | +async function printMatchingKeys(startUp = false) { |
| 19 | + const redisKeyPattern = 'recovery-phone:sms-attempt:*:*'; |
| 20 | + try { |
| 21 | + const keys = await redis.keys(redisKeyPattern); |
| 22 | + |
| 23 | + if (keys.length > 0) { |
| 24 | + for (const key of keys) { |
| 25 | + const keyParts = key.split(':'); |
| 26 | + const uid = keyParts[2]; |
| 27 | + const code = keyParts[3]; |
| 28 | + |
| 29 | + if (!usersLastSms[uid]) { |
| 30 | + usersLastSms[uid] = {}; |
| 31 | + } |
| 32 | + |
| 33 | + // Check if this code was already printed for this user |
| 34 | + if (!usersLastSms[uid][code]) { |
| 35 | + if (!startUp) { |
| 36 | + console.log('\x1B[36mRecovery Phone Otp:', code, '\x1B[39m'); |
| 37 | + } |
| 38 | + usersLastSms[uid][code] = true; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } catch (error) { |
| 43 | + console.error('Failed to retrieve keys:', error); |
| 44 | + } finally { |
| 45 | + // 1s delay seems reasonable |
| 46 | + setTimeout(printMatchingKeys, 1000); |
| 47 | + } |
| 48 | +} |
| 49 | + |
15 | 50 | // SMTP half |
16 | 51 |
|
17 | 52 | const users = {}; |
@@ -147,14 +182,15 @@ module.exports = (printLogs) => { |
147 | 182 | }, |
148 | 183 | ]); |
149 | 184 |
|
150 | | - api.start().then(() => { |
| 185 | + api.start().then(async () => { |
151 | 186 | console.log('mail_helper started...'); |
152 | | - |
| 187 | + printMatchingKeys(true); |
153 | 188 | return resolve({ |
154 | 189 | close() { |
155 | 190 | return new Promise((resolve, reject) => { |
156 | 191 | let smtpClosed = false; |
157 | 192 | let apiClosed = false; |
| 193 | + redis.quit(); |
158 | 194 | smtp.server.end(() => { |
159 | 195 | smtpClosed = true; |
160 | 196 | if (apiClosed) { |
|
0 commit comments