@everneed/worker 2.0.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +1 -1
- package/package.json +1 -1
- package/worker/rabbitmq.js +230 -358
package/index.js
CHANGED
|
@@ -67,7 +67,7 @@ import pseudoEnv from "./pseudoenv.js"
|
|
|
67
67
|
url: `redis://${pseudoEnv.process.env.REDIS_USERNAME}:${pseudoEnv.process.env.REDIS_PASSWORD}@${pseudoEnv.process.env.REDIS_HOST}:${pseudoEnv.process.env.REDIS_PORT}`
|
|
68
68
|
} */)
|
|
69
69
|
.on("error", err => console.log("Redis Client Error", err))
|
|
70
|
-
|
|
70
|
+
.connect()
|
|
71
71
|
}
|
|
72
72
|
/* Socketio */
|
|
73
73
|
let socketioInstance = ()=>{}
|
package/package.json
CHANGED
package/worker/rabbitmq.js
CHANGED
|
@@ -3,12 +3,13 @@ import fs from "fs"
|
|
|
3
3
|
import crypto from "crypto"
|
|
4
4
|
import amqp from "amqplib/callback_api.js"
|
|
5
5
|
import moment from "moment"
|
|
6
|
+
import EventEmitter from "events"
|
|
6
7
|
|
|
7
8
|
import pseudoEnv from "@everneed/worker/pseudoenv.js"
|
|
8
9
|
import { createHmac } from "crypto"
|
|
9
10
|
import { redis } from "@everneed/worker"
|
|
10
11
|
import { ResponseCode } from "@everneed/responsecode"
|
|
11
|
-
import { pipe, switchPourStr, randomStr } from "@everneed/helper"
|
|
12
|
+
import { pipe, switchPourStr, randomStr, extractUserInfo } from "@everneed/helper"
|
|
12
13
|
|
|
13
14
|
class VersionManager{
|
|
14
15
|
#currentVer
|
|
@@ -181,9 +182,28 @@ class VersionManager{
|
|
|
181
182
|
}
|
|
182
183
|
const version = new VersionManager()
|
|
183
184
|
version.init()
|
|
185
|
+
const bus = new EventEmitter()
|
|
184
186
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
+
class RabbitMQ{
|
|
188
|
+
connection = null
|
|
189
|
+
channel = null
|
|
190
|
+
|
|
191
|
+
async init(){
|
|
192
|
+
await new Promise((resolve, reject)=>{
|
|
193
|
+
amqp.connect(pseudoEnv.process.env.RABBIT_HOST, (error0, connection)=>{
|
|
194
|
+
if(error0){ throw error0 }
|
|
195
|
+
this.connection = connection
|
|
196
|
+
|
|
197
|
+
this.connection.createChannel((error1, channel)=>{
|
|
198
|
+
if (error1){ throw error1 }
|
|
199
|
+
this.channel = channel
|
|
200
|
+
|
|
201
|
+
resolve("continue")
|
|
202
|
+
})
|
|
203
|
+
})
|
|
204
|
+
})
|
|
205
|
+
}
|
|
206
|
+
config(object){
|
|
187
207
|
// config({
|
|
188
208
|
// ENV: value,
|
|
189
209
|
// ...
|
|
@@ -205,143 +225,108 @@ export default {
|
|
|
205
225
|
}
|
|
206
226
|
/* Env Add Process */
|
|
207
227
|
pseudoEnv.config(object)
|
|
208
|
-
}
|
|
209
|
-
start
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
const
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
/* Generate consumer */
|
|
232
|
-
channel.assertQueue(`req/${pseudoEnv.process.env.SVC_NAME}/${moduleSpace}/${endpoint}`, {
|
|
233
|
-
durable: true
|
|
228
|
+
}
|
|
229
|
+
async start(){
|
|
230
|
+
const rabbitmqGateway = fs.readdirSync(path.join(process.cwd(), "/src/mq-gateway/"))
|
|
231
|
+
/* Generate consumer */
|
|
232
|
+
// iterating every files in /src/mq-gateway
|
|
233
|
+
// and its function to be built as rabbitmq
|
|
234
|
+
// subscriber (consumer)
|
|
235
|
+
for(const file of rabbitmqGateway){
|
|
236
|
+
const moduleSpace = file.slice(0, -3)
|
|
237
|
+
const main = await import(`file://${path.join(process.cwd(), `/src/mq-gateway/${moduleSpace}`)}.js`)
|
|
238
|
+
// const main = await import(`#@/mq-gateway/${moduleSpace}.js`)
|
|
239
|
+
|
|
240
|
+
for(const endpoint in main.default){
|
|
241
|
+
/* Generate consumer */
|
|
242
|
+
this.channel.assertQueue(`req/${pseudoEnv.process.env.SVC_NAME}/${moduleSpace}/${endpoint}`, {
|
|
243
|
+
durable: false
|
|
244
|
+
})
|
|
245
|
+
this.channel.consume(`req/${pseudoEnv.process.env.SVC_NAME}/${moduleSpace}/${endpoint}`, async (message)=>{
|
|
246
|
+
const mqHeader = message.properties.headers
|
|
247
|
+
const mqReq = await this.#decryptMessage({
|
|
248
|
+
message: message.content.toString(),
|
|
249
|
+
...(mqHeader.auth && { token: mqHeader.auth.token }),
|
|
250
|
+
...(mqHeader.auth && { userAgent: mqHeader.auth.userAgent })
|
|
234
251
|
})
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
message: message.content.toString(),
|
|
240
|
-
...(mqHeader.auth && { token: mqHeader.auth.token }),
|
|
241
|
-
...(mqHeader.auth && { userAgent: mqHeader.auth.userAgent })
|
|
252
|
+
const trueVersion = mqHeader.version ?
|
|
253
|
+
version.trueVersion({
|
|
254
|
+
endpoint: `/${pseudoEnv.process.env.SVC_NAME}/${moduleSpace}/${endpoint}`,
|
|
255
|
+
version: mqHeader.version
|
|
242
256
|
})
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
message: null
|
|
253
|
-
}
|
|
257
|
+
:
|
|
258
|
+
version.latestVersion()
|
|
259
|
+
const prep = {
|
|
260
|
+
message: null
|
|
261
|
+
}
|
|
262
|
+
let timeoutCounter = false
|
|
263
|
+
|
|
264
|
+
// Start Timeout Timer
|
|
265
|
+
const timeout = setTimeout(()=>{ timeoutCounter = true }, main.default[endpoint].timeout || 10000)
|
|
254
266
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
267
|
+
try{
|
|
268
|
+
/* Active Endpoint Filter */
|
|
269
|
+
if(!main.default[endpoint].active) throw "invalid_endpoint"
|
|
270
|
+
/* API enforce Auth */
|
|
271
|
+
if(main.default[endpoint].authentication){
|
|
272
|
+
if(!mqHeader.auth) throw "not_authenticated"
|
|
273
|
+
}
|
|
274
|
+
/* Main Process */
|
|
275
|
+
const result = await main.default[endpoint].function[`v${trueVersion}`](mqHeader.auth || false, mqReq.payload)
|
|
276
|
+
prep["message"] = await this.#encryptMessage({
|
|
277
|
+
message: result,
|
|
278
|
+
...(mqHeader.auth && { token: mqHeader.auth.token }),
|
|
279
|
+
...(mqHeader.auth && { userAgent: mqHeader.auth.userAgent })
|
|
280
|
+
})
|
|
281
|
+
/* Resolve */
|
|
282
|
+
if(timeoutCounter) throw "timeout"
|
|
283
|
+
else throw "finish"
|
|
284
|
+
}
|
|
285
|
+
catch(something){
|
|
286
|
+
/* Imbue response code according to throw */
|
|
287
|
+
if(something == "finish") clearTimeout(timeout)
|
|
288
|
+
if(something != "finish" && mqReq.trip == "returning"){
|
|
289
|
+
const response = new ResponseCode()
|
|
290
|
+
switch(something){
|
|
291
|
+
case "invalid_endpoint": response.pushCode("INVALID_ENDPOINT")
|
|
292
|
+
break;
|
|
293
|
+
case "not_authenticated": response.pushCode("NOT_AUTHENTICATED")
|
|
294
|
+
break;
|
|
295
|
+
case "timeout": response.pushCode("HTTP_TIMEOUT")
|
|
296
|
+
break;
|
|
297
|
+
default: response.pushCode("MQ_ERROR")
|
|
298
|
+
break;
|
|
277
299
|
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
})
|
|
292
|
-
}
|
|
293
|
-
else{ // AUTHENTICATED || REFRESH_SESSION
|
|
294
|
-
const result = await main.default[endpoint].function[`v${trueVersion}`](mqHeader.auth, mqReq.payload)
|
|
295
|
-
const response = new ResponseCode(result)
|
|
296
|
-
if(auth.hasCode("REFRESH_SESSION")) response.pushRefresh(auth.result.data)
|
|
297
|
-
prep["message"] = await this.encryptMessage({
|
|
298
|
-
mode: mqHeader.mode,
|
|
299
|
-
message: response.result,
|
|
300
|
-
token: token,
|
|
301
|
-
userAgent: userAgent
|
|
302
|
-
})
|
|
300
|
+
|
|
301
|
+
prep["message"] = await this.#encryptMessage({
|
|
302
|
+
message: response.result,
|
|
303
|
+
...(mqHeader.auth && { token: mqHeader.auth.token }),
|
|
304
|
+
...(mqHeader.auth && { userAgent: mqHeader.auth.userAgent })
|
|
305
|
+
})
|
|
306
|
+
}
|
|
307
|
+
/* Returning Trip */
|
|
308
|
+
if(mqReq.trip == "returning"){
|
|
309
|
+
this.channel.sendToQueue(message.properties.replyTo, Buffer.from(prep.message), {
|
|
310
|
+
headers:{
|
|
311
|
+
...(mqHeader.version && { version: mqHeader.version }),
|
|
312
|
+
...(mqHeader.auth && { auth: mqHeader.auth })
|
|
303
313
|
}
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
})
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
throw "finish"
|
|
317
|
-
}
|
|
318
|
-
catch(something){
|
|
319
|
-
if(mqReq.trip == "returning"){
|
|
320
|
-
channel.sendToQueue(message.properties.replyTo, Buffer.from(prep.message), {
|
|
321
|
-
correlationId: message.properties.correlationId,
|
|
322
|
-
headers:{
|
|
323
|
-
mode: mqHeader.mode,
|
|
324
|
-
...(mqHeader.version && { version: mqHeader.version }),
|
|
325
|
-
...(mqHeader.auth && { auth: mqHeader.auth })
|
|
326
|
-
},
|
|
327
|
-
})
|
|
328
|
-
channel.ack(message)
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
},{
|
|
332
|
-
noAck: false
|
|
333
|
-
})
|
|
334
|
-
}
|
|
314
|
+
})
|
|
315
|
+
}
|
|
316
|
+
/* Ack Compose */
|
|
317
|
+
if(something == "finish") this.channel.ack(message)
|
|
318
|
+
else this.channel.nack(message, false, false)
|
|
319
|
+
}
|
|
320
|
+
},{
|
|
321
|
+
noAck: false
|
|
322
|
+
},(error, ok)=>{ if(error) throw error })
|
|
335
323
|
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
},
|
|
340
|
-
publish: function({topic, version, mode="auth", auth=null, trip="passby", data}){
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
publish({topic, version, auth=null, trip="passby", data}){
|
|
341
327
|
/* Usage */
|
|
342
328
|
// publish({
|
|
343
329
|
// topic: <topic start at svc :String>,
|
|
344
|
-
// mode: <"auth"|"no auth" :String>
|
|
345
330
|
// auth:{
|
|
346
331
|
// token: <auth token :String>,
|
|
347
332
|
// userAgent: <ua string :String>,
|
|
@@ -354,266 +339,153 @@ export default {
|
|
|
354
339
|
// ex: req/cd6f01UjkB7E/rolling-tempat-duduk/class/getAll
|
|
355
340
|
// ex: res/cd6f01UjkB7E/rolling-tempat-duduk/class/getAll
|
|
356
341
|
|
|
342
|
+
const response = new ResponseCode()
|
|
357
343
|
return new Promise(async (resolve, reject)=>{
|
|
358
|
-
|
|
359
|
-
amqp.connect(pseudoEnv.process.env.RABBIT_HOST, async (error0, connection)=>{
|
|
360
|
-
if(error0){ throw error0 }
|
|
361
|
-
|
|
362
|
-
/* Creates connection & channel */
|
|
363
|
-
const channel = connection.createChannel((error1, channel)=>{
|
|
364
|
-
if (error1){ throw error1 }
|
|
365
|
-
return channel
|
|
366
|
-
})
|
|
367
|
-
|
|
344
|
+
try{
|
|
368
345
|
/* Build payload structure */
|
|
369
346
|
// like topic, address, etc.
|
|
370
|
-
const userName = auth ?
|
|
347
|
+
const userName = auth ? extractUserInfo({token: auth.token}).userName : `guest${randomStr({length: 12})}`
|
|
371
348
|
const prep = {
|
|
372
349
|
topic: `req/${topic}`,
|
|
373
350
|
message:{
|
|
374
351
|
trip: trip,
|
|
375
|
-
senderAddress: `${createHmac("sha256", userName).digest("hex").slice(0, 12)}${moment().utc().format("HHmmssSSS")}`,
|
|
376
352
|
payload: data
|
|
377
353
|
}
|
|
378
354
|
}
|
|
355
|
+
let senderAddress
|
|
356
|
+
/* Validate Auth */
|
|
357
|
+
if(auth){
|
|
358
|
+
const validate = await this.publish({
|
|
359
|
+
topic: "authenthor/session/svVerifyToken",
|
|
360
|
+
trip: "returning",
|
|
361
|
+
data:{
|
|
362
|
+
token: auth.token,
|
|
363
|
+
userAgent: auth.userAgent,
|
|
364
|
+
noRefresh: true
|
|
365
|
+
}
|
|
366
|
+
})
|
|
367
|
+
|
|
368
|
+
if(validate.checkError()){
|
|
369
|
+
response.createNew(validate.result)
|
|
370
|
+
throw "abort"
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
/* Prepare Listener */
|
|
374
|
+
if(trip == "returning"){
|
|
375
|
+
/* Creates consumer for returning messages */
|
|
376
|
+
senderAddress = await new Promise((resolva, rejecta)=>{
|
|
377
|
+
this.channel.assertQueue("", { exclusive: true, autoDelete: true }, (error, ok)=>{
|
|
378
|
+
resolva(ok.queue)
|
|
379
|
+
})
|
|
380
|
+
})
|
|
381
|
+
/* Returning Message's Sub-Consumer */
|
|
382
|
+
this.channel.consume(senderAddress, async (message)=>{
|
|
383
|
+
const mqRes = await this.#decryptMessage({
|
|
384
|
+
message: message.content.toString(),
|
|
385
|
+
...(auth && { token: auth.token }),
|
|
386
|
+
...(auth && { userAgent: auth.userAgent })
|
|
387
|
+
})
|
|
388
|
+
|
|
389
|
+
this.channel.ack(message)
|
|
390
|
+
response.createNew(mqRes)
|
|
391
|
+
this.channel.cancel(senderAddress)
|
|
392
|
+
resolve(response)
|
|
393
|
+
}, {
|
|
394
|
+
noAck: false,
|
|
395
|
+
consumerTag: senderAddress
|
|
396
|
+
},(error, ok)=>{ if(error) throw error })
|
|
397
|
+
}
|
|
379
398
|
/* Main publisher */
|
|
380
|
-
const mqReq = await this
|
|
381
|
-
mode: mode,
|
|
399
|
+
const mqReq = await this.#encryptMessage({
|
|
382
400
|
message: prep.message,
|
|
383
401
|
...(auth && { token: auth.token }),
|
|
384
402
|
...(auth && { userAgent: auth.userAgent })
|
|
385
403
|
})
|
|
386
404
|
|
|
387
|
-
channel.assertQueue(prep.topic, {
|
|
388
|
-
|
|
405
|
+
this.channel.assertQueue(prep.topic, {
|
|
406
|
+
durable: false
|
|
407
|
+
})
|
|
408
|
+
this.channel.sendToQueue(prep.topic, Buffer.from(mqReq), {
|
|
389
409
|
headers:{
|
|
390
410
|
...(version && { version: version }),
|
|
391
|
-
mode: mode,
|
|
392
411
|
...(auth && { auth: auth })
|
|
393
412
|
},
|
|
394
|
-
...(trip == "returning" && {
|
|
395
|
-
...(trip == "returning" && { replyTo: `res/${topic}` }),
|
|
413
|
+
...(trip == "returning" && { replyTo: senderAddress })
|
|
396
414
|
})
|
|
415
|
+
/* Non-returning Trip Resolvation */
|
|
416
|
+
if(trip == "passby"){
|
|
417
|
+
response.pushCode("GENERAL_OK")
|
|
397
418
|
|
|
398
|
-
|
|
399
|
-
if(trip == "returning"){
|
|
400
|
-
/* Creates consumer for returning messages */
|
|
401
|
-
channel.assertQueue(`res/${topic}`, { durable: true })
|
|
402
|
-
channel.consume(`res/${topic}`, async (message)=>{
|
|
403
|
-
/* Differentiate normal flow with refresh session flow */
|
|
404
|
-
const sessionRefresh = ()=>{
|
|
405
|
-
const response = new ResponseCode(message.content.toString())
|
|
406
|
-
if(response.hasCode("REFRESH_SESSION")) return true
|
|
407
|
-
else return false
|
|
408
|
-
}
|
|
409
|
-
const mqRes = await this.decryptMessage({
|
|
410
|
-
mode: mode,
|
|
411
|
-
message: message.content.toString(),
|
|
412
|
-
...((auth && !sessionRefresh()) && { token: auth.token }),
|
|
413
|
-
...((auth && sessionRefresh()) && { token: message.content.toString().data.token }),
|
|
414
|
-
...(auth && { userAgent: auth.userAgent })
|
|
415
|
-
})
|
|
416
|
-
|
|
417
|
-
if(message.properties.correlationId == prep.message.senderAddress){
|
|
418
|
-
resolve(mqRes)
|
|
419
|
-
channel.ack(message)
|
|
420
|
-
setTimeout(()=>{
|
|
421
|
-
connection.close()
|
|
422
|
-
}, 500)
|
|
423
|
-
}
|
|
424
|
-
}, {
|
|
425
|
-
noAck: false
|
|
426
|
-
})
|
|
427
|
-
}
|
|
428
|
-
else if(trip == "passby"){
|
|
429
|
-
resolve("sent")
|
|
430
|
-
|
|
431
|
-
setTimeout(()=>{
|
|
432
|
-
connection.close()
|
|
433
|
-
}, 500)
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
// connection.close()
|
|
437
|
-
})
|
|
438
|
-
})
|
|
439
|
-
},
|
|
440
|
-
encryptMessage: function({mode="auth", message, token=null, userAgent=null}){
|
|
441
|
-
return new Promise(async (resolve, reject)=>{
|
|
442
|
-
if(mode == "auth"){
|
|
443
|
-
/* Active Auth Session */
|
|
444
|
-
if(token && userAgent){
|
|
445
|
-
const { userId } = this.getUserInfo({ tokenBody: token.split(".")[0] })
|
|
446
|
-
const uaHash = crypto.hash("sha256", switchPourStr(userAgent, pseudoEnv.process.env.TOKEN_UA_SALT))
|
|
447
|
-
await redis.connect().catch(error => {})
|
|
448
|
-
const authKey = await redis.get(`auth:session:long:${userId}:${uaHash}`)
|
|
449
|
-
const hash = crypto.hash("sha256", switchPourStr(authKey, pseudoEnv.process.env.RABBIT_MESSAGE_SALT))
|
|
450
|
-
const key = hash.substring(0, 16)
|
|
451
|
-
const iv = hash.substring(48)
|
|
452
|
-
|
|
453
|
-
const cipher = crypto.createCipheriv("aes-128-cbc", Buffer.from(key), Buffer.from(iv))
|
|
454
|
-
let encrypted = cipher.update(JSON.stringify(message), "utf-8", "base64")
|
|
455
|
-
encrypted += cipher.final("base64")
|
|
456
|
-
return resolve(encrypted)
|
|
457
|
-
}
|
|
458
|
-
/* No Auth */
|
|
459
|
-
else{
|
|
460
|
-
const cipher = crypto.createCipheriv("aes-128-cbc", Buffer.from(pseudoEnv.process.env.RABBIT_MESSAGE_KEY), Buffer.from(pseudoEnv.process.env.RABBIT_MESSAGE_IV))
|
|
461
|
-
let encrypted = cipher.update(JSON.stringify(message), "utf-8", "base64")
|
|
462
|
-
encrypted += cipher.final("base64")
|
|
463
|
-
return resolve(encrypted)
|
|
419
|
+
resolve(response)
|
|
464
420
|
}
|
|
465
421
|
}
|
|
466
|
-
else if(mode == "no auth"){
|
|
467
|
-
/* Unchecked Token (Illegal/Refresh) */
|
|
468
|
-
if(token){
|
|
469
|
-
const {key, iv} = pipe(token)
|
|
470
|
-
.then(token=> token.split(".")[0])
|
|
471
|
-
.then(tokenBody=> this.getUserInfo({tokenBody: tokenBody}))
|
|
472
|
-
.then(({userName, userId, displayName})=>{
|
|
473
|
-
return switchPourStr(
|
|
474
|
-
userName,
|
|
475
|
-
pseudoEnv.process.env.RABBIT_MESSAGE_KEY,
|
|
476
|
-
userId,
|
|
477
|
-
pseudoEnv.process.env.RABBIT_MESSAGE_IV,
|
|
478
|
-
displayName,
|
|
479
|
-
)
|
|
480
|
-
})
|
|
481
|
-
.then(mixWord=> crypto.hash("sha256", mixWord))
|
|
482
|
-
.then(hash=>{
|
|
483
|
-
return {
|
|
484
|
-
key: hash.substring(0, 16),
|
|
485
|
-
iv: hash.substring(48)
|
|
486
|
-
}
|
|
487
|
-
})
|
|
488
|
-
.result
|
|
489
|
-
|
|
490
|
-
const cipher = crypto.createCipheriv("aes-128-cbc", Buffer.from(key), Buffer.from(iv))
|
|
491
|
-
let encrypted = cipher.update(JSON.stringify(message), "utf-8", "base64")
|
|
492
|
-
encrypted += cipher.final("base64")
|
|
493
|
-
return resolve(encrypted)
|
|
494
|
-
}
|
|
495
|
-
/* No Auth */
|
|
496
|
-
else{
|
|
497
|
-
const cipher = crypto.createCipheriv("aes-128-cbc", Buffer.from(pseudoEnv.process.env.RABBIT_MESSAGE_KEY), Buffer.from(pseudoEnv.process.env.RABBIT_MESSAGE_IV))
|
|
498
|
-
let encrypted = cipher.update(JSON.stringify(message), "utf-8", "base64")
|
|
499
|
-
encrypted += cipher.final("base64")
|
|
500
|
-
return resolve(encrypted)
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
})
|
|
504
|
-
},
|
|
505
|
-
decryptMessage: function({mode="auth", message, token=null, userAgent=null}){
|
|
506
|
-
return new Promise(async (resolve, reject)=>{
|
|
507
|
-
if(mode == "auth"){
|
|
508
|
-
/* Active Auth Session */
|
|
509
|
-
if(token && userAgent){
|
|
510
|
-
const { userId } = this.getUserInfo({ tokenBody: token.split(".")[0] })
|
|
511
|
-
const uaHash = crypto.hash("sha256", switchPourStr(userAgent, pseudoEnv.process.env.TOKEN_UA_SALT))
|
|
512
|
-
await redis.connect().catch(error => {})
|
|
513
|
-
const authKey = await redis.get(`auth:session:long:${userId}:${uaHash}`)
|
|
514
|
-
const hash = crypto.hash("sha256", switchPourStr(authKey, pseudoEnv.process.env.RABBIT_MESSAGE_SALT))
|
|
515
|
-
const key = hash.substring(0, 16)
|
|
516
|
-
const iv = hash.substring(48)
|
|
517
|
-
|
|
518
|
-
const decipher = crypto.createDecipheriv("aes-128-cbc", Buffer.from(key), Buffer.from(iv))
|
|
519
|
-
let decrypted = decipher.update(message, "base64", "utf-8")
|
|
520
|
-
decrypted += decipher.final("utf-8")
|
|
521
|
-
return resolve(JSON.parse(decrypted))
|
|
522
|
-
}
|
|
523
|
-
/* No Auth */
|
|
524
|
-
else{
|
|
525
|
-
const decipher = crypto.createDecipheriv("aes-128-cbc", Buffer.from(pseudoEnv.process.env.RABBIT_MESSAGE_KEY), Buffer.from(pseudoEnv.process.env.RABBIT_MESSAGE_IV))
|
|
526
|
-
let decrypted = decipher.update(message, "base64", "utf-8")
|
|
527
|
-
decrypted += decipher.final("utf-8")
|
|
528
|
-
return resolve(JSON.parse(decrypted))
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
else if(mode == "no auth"){
|
|
532
|
-
/* Unchecked Token (Illegal/Refresh) */
|
|
533
|
-
if(token){
|
|
534
|
-
const {key, iv} = pipe(token)
|
|
535
|
-
.then(token=> token.split(".")[0])
|
|
536
|
-
.then(tokenBody=> this.getUserInfo({tokenBody: tokenBody}))
|
|
537
|
-
.then(({userName, userId, displayName})=>{
|
|
538
|
-
return switchPourStr(
|
|
539
|
-
userName,
|
|
540
|
-
pseudoEnv.process.env.RABBIT_MESSAGE_KEY,
|
|
541
|
-
userId,
|
|
542
|
-
pseudoEnv.process.env.RABBIT_MESSAGE_IV,
|
|
543
|
-
displayName,
|
|
544
|
-
)
|
|
545
|
-
})
|
|
546
|
-
.then(mixWord=> crypto.hash("sha256", mixWord))
|
|
547
|
-
.then(hash=>{
|
|
548
|
-
return {
|
|
549
|
-
key: hash.substring(0, 16),
|
|
550
|
-
iv: hash.substring(48)
|
|
551
|
-
}
|
|
552
|
-
})
|
|
553
|
-
.result
|
|
554
|
-
|
|
555
|
-
const decipher = crypto.createDecipheriv("aes-128-cbc", Buffer.from(key), Buffer.from(iv))
|
|
556
|
-
let decrypted = decipher.update(message, "base64", "utf-8")
|
|
557
|
-
decrypted += decipher.final("utf-8")
|
|
558
|
-
return resolve(JSON.parse(decrypted))
|
|
559
|
-
}
|
|
560
|
-
/* No Auth */
|
|
561
|
-
else{
|
|
562
|
-
const decipher = crypto.createDecipheriv("aes-128-cbc", Buffer.from(pseudoEnv.process.env.RABBIT_MESSAGE_KEY), Buffer.from(pseudoEnv.process.env.RABBIT_MESSAGE_IV))
|
|
563
|
-
let decrypted = decipher.update(message, "base64", "utf-8")
|
|
564
|
-
decrypted += decipher.final("utf-8")
|
|
565
|
-
return resolve(JSON.parse(decrypted))
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
})
|
|
569
|
-
},
|
|
570
|
-
verifyToken: function({token, userAgent}){
|
|
571
|
-
const response = new ResponseCode()
|
|
572
|
-
return new Promise(async (resolve, reject)=>{
|
|
573
|
-
try{
|
|
574
|
-
await this.publish({
|
|
575
|
-
topic: "authenthor/session/svVerifyToken",
|
|
576
|
-
mode: "no auth",
|
|
577
|
-
trip: "returning",
|
|
578
|
-
data:{
|
|
579
|
-
token: token,
|
|
580
|
-
userAgent: userAgent
|
|
581
|
-
}
|
|
582
|
-
})
|
|
583
|
-
.then(apiRes=>{
|
|
584
|
-
response.createNew(apiRes)
|
|
585
|
-
throw "abort"
|
|
586
|
-
})
|
|
587
|
-
}
|
|
588
422
|
catch(something){
|
|
423
|
+
response.createNew(response.result)
|
|
589
424
|
if(something != "abort"){
|
|
590
425
|
// err: other error
|
|
591
426
|
response.pushCode("LOGIC_ERROR")
|
|
592
|
-
response.pushTrace({code: "LOGIC_ERROR", trace: `Unexpected error on
|
|
427
|
+
response.pushTrace({code: "LOGIC_ERROR", trace: `Unexpected error on queue publisher`})
|
|
593
428
|
}
|
|
594
429
|
|
|
595
430
|
resolve(response)
|
|
596
431
|
}
|
|
597
432
|
})
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
return
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
433
|
+
}
|
|
434
|
+
#encryptMessage({message, token=null, userAgent=null}){
|
|
435
|
+
return new Promise(async (resolve, reject)=>{
|
|
436
|
+
/* Active Auth Session */
|
|
437
|
+
if(token && userAgent){
|
|
438
|
+
const { userId } = extractUserInfo({ token: token })
|
|
439
|
+
const uaHash = crypto.hash("sha256", switchPourStr(userAgent, pseudoEnv.process.env.TOKEN_UA_SALT))
|
|
440
|
+
const authKey = await redis.get(`auth:session:long:${userId}:${uaHash}`)
|
|
441
|
+
const hash = crypto.hash("sha256", switchPourStr(authKey || "kontol", pseudoEnv.process.env.RABBIT_MESSAGE_SALT))
|
|
442
|
+
const key = hash.substring(0, 16)
|
|
443
|
+
const iv = hash.substring(48)
|
|
444
|
+
|
|
445
|
+
const cipher = crypto.createCipheriv("aes-128-cbc", Buffer.from(key), Buffer.from(iv))
|
|
446
|
+
let encrypted = cipher.update(JSON.stringify(message), "utf-8", "base64")
|
|
447
|
+
encrypted += cipher.final("base64")
|
|
448
|
+
return resolve(encrypted)
|
|
449
|
+
}
|
|
450
|
+
/* No Auth */
|
|
451
|
+
else{
|
|
452
|
+
const cipher = crypto.createCipheriv("aes-128-cbc", Buffer.from(pseudoEnv.process.env.RABBIT_MESSAGE_KEY), Buffer.from(pseudoEnv.process.env.RABBIT_MESSAGE_IV))
|
|
453
|
+
let encrypted = cipher.update(JSON.stringify(message), "utf-8", "base64")
|
|
454
|
+
encrypted += cipher.final("base64")
|
|
455
|
+
return resolve(encrypted)
|
|
456
|
+
}
|
|
605
457
|
})
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
458
|
+
}
|
|
459
|
+
#decryptMessage({message, token=null, userAgent=null}){
|
|
460
|
+
return new Promise(async (resolve, reject)=>{
|
|
461
|
+
/* Active Auth Session */
|
|
462
|
+
if(token && userAgent){
|
|
463
|
+
const { userId } = extractUserInfo({ token: token })
|
|
464
|
+
const uaHash = crypto.hash("sha256", switchPourStr(userAgent, pseudoEnv.process.env.TOKEN_UA_SALT))
|
|
465
|
+
const authKey = await redis.get(`auth:session:long:${userId}:${uaHash}`)
|
|
466
|
+
const hash = crypto.hash("sha256", switchPourStr(authKey || "kontol", pseudoEnv.process.env.RABBIT_MESSAGE_SALT))
|
|
467
|
+
const key = hash.substring(0, 16)
|
|
468
|
+
const iv = hash.substring(48)
|
|
469
|
+
|
|
470
|
+
const decipher = crypto.createDecipheriv("aes-128-cbc", Buffer.from(key), Buffer.from(iv))
|
|
471
|
+
let decrypted = decipher.update(message, "base64", "utf-8")
|
|
472
|
+
decrypted += decipher.final("utf-8")
|
|
473
|
+
return resolve(JSON.parse(decrypted))
|
|
474
|
+
}
|
|
475
|
+
/* No Auth */
|
|
476
|
+
else{
|
|
477
|
+
const decipher = crypto.createDecipheriv("aes-128-cbc", Buffer.from(pseudoEnv.process.env.RABBIT_MESSAGE_KEY), Buffer.from(pseudoEnv.process.env.RABBIT_MESSAGE_IV))
|
|
478
|
+
let decrypted = decipher.update(message, "base64", "utf-8")
|
|
479
|
+
decrypted += decipher.final("utf-8")
|
|
480
|
+
return resolve(JSON.parse(decrypted))
|
|
481
|
+
}
|
|
482
|
+
})
|
|
483
|
+
}
|
|
484
|
+
update(){
|
|
611
485
|
version.update()
|
|
612
|
-
},
|
|
613
|
-
apiVer: function(){
|
|
614
|
-
return version.deprecatedVersion()+1
|
|
615
|
-
},
|
|
616
|
-
latestVer: function(){
|
|
617
|
-
return version.latestVersion()
|
|
618
486
|
}
|
|
619
|
-
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
const instance = new RabbitMQ()
|
|
490
|
+
await instance.init()
|
|
491
|
+
export default instance
|