-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
110 lines (90 loc) · 3.11 KB
/
main.js
File metadata and controls
110 lines (90 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* fairyMQ Node.JS Consumer
* ******************************************************************
* Copyright (C) fairyMQ
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import EventEmitter from 'node:events'
import * as fs from 'node:fs'
import * as dgram from 'node:dgram'
import crypto from 'crypto'
function splitBuf(b, sep) {
const ret = [];
let s = 0;
let i = b.indexOf(sep, s);
while (i >= 0) {
if (i >= 0) {
ret.push(b.slice(s, i));
}
s = i + 1;
i = b.indexOf(sep, s);
}
ret.push(b.slice(s));
return ret;
}
class Consumer {
constructor(privateKey) {
this.privateKey = fs.readFileSync(privateKey).toString()
this.server = dgram.createSocket('udp4')
this.events = new EventEmitter()
this.port = 5992
this.key = ""
}
ConfigureKey(key) {
this.key = key
}
SetPort(port) {
this.port = port
}
Listen() {
let obj = this
let lastTimestamp = 0;
this.server.on('error', (err) => {
console.error(`server error:\n${err.stack}`)
server.close()
})
this.server.on('message', (msg, rinfo) => {
const decrypted = crypto.privateDecrypt(
{
key: this.privateKey,
padding: crypto.constants.RSA_PKCS1_PADDING,
},
msg
)
if (decrypted.toString().split("\r\n")[0].split("ENQUEUE").length > 1) {
// If a key was provided the listener will only accept messages with the same set key
if (this.key !== "") {
if (this.key !== decrypted.toString().split("\r\n")[0].split("ENQUEUE")[1].trim()) {
return
}
}
}
if (lastTimestamp === decrypted.toString().split("\r\n")[1]) {
return
}
lastTimestamp = decrypted.toString().split("\r\n")[1]
const bufferArray = splitBuf(decrypted, "\r\n")
obj.events.emit('event', bufferArray[bufferArray.length-1].slice(1))
let ack = Buffer.from("ACK\r\n", 'utf-8');
this.server.send(ack, 0, ack.length, rinfo.port, rinfo.address )
})
this.server.on('listening', () => {
const address = this.server.address()
console.log(`Consumer listening ${address.address}:${address.port}`)
})
this.server.bind(this.port)
}
}
export default Consumer