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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337 | 1×
1×
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
1×
1×
1×
1×
1×
1×
1×
2×
2×
1×
1×
1×
1×
1×
1×
1×
| var util = require('util'),
EventEmitter = require('events').EventEmitter,
net = require('net'),
Parser = require('./Parser'),
Message = require('./Message'),
packageInfo = require('../package.json'),
proxyParser = require('./proxyParser')
/**
* Handles relp client communication
* Will emit new messages and connection closes for the consumer to act on
* If a connection closes, non ack'd messages should be dropped since they will be retransmitted
*
* @param {Object} options Configuration options for the server
* @param {Number} options.port The port to listen for new connections
* @param {String} [options.host] The host to listen for new connections on
* @param {Server} [options.server] Configure your own server and provide it here
* You must call listen on the provided server object
* @param {String} [options.connectionEvent='connection'] Change the event name for new connections.
* This is especially useful for TLS support since a TLS server emits 'secureConnection` events when
* a socket is ready
* @param {Boolean} [options.proxyProtocol=false] True to expect PROXY protocol, false if not
* @param {Object} [options.parser=Parser] A custom parser, if you must
* @param {Object} [options.proxyParser=proxyParser] A custom PROXY protocol parser, if you must
* @param {Object} [options.logger] A logger to use in place of console
*
* @constructor
*/
var Server = function (options) {
Server.super_.call(this)
var self = this
self.options = options || {}
self.logger = self.options.logger || console
self.sockets = {}
self._nextSocketId = 1
self.paused = false
self.server = self.options.server || net.createServer()
self.server.on(self.options.connectionEvent || 'connection', function (socket) {
self._setupConnection(socket)
})
self.server.on('error', function (error) {
self.logger.error('RELP Server Error', error.stack)
})
Eif (!self.options.server) {
self.server.listen(self.options.port, self.options.host)
}
}
Server.RELP_VERSION = '0'
Server.RELP_SOFTWARE = 'node-' + packageInfo.name + ',' + packageInfo.version + ',' + packageInfo.repository.url
Server.RELP_COMMANDS = ['open', 'close', 'syslog']
util.inherits(Server, EventEmitter)
module.exports = Server
/**
* Acknowledges the receipt of a relp message to the relp client
* Messages with a transaction id below 1 will not be ack'd
* Messages that have already been ack'd will not be re-ack'd
*
* @param {Message} message A relp message to ack
* @param {String} [data='200 OK'] Data to send
* @param {boolean} [close=false] Whether or not to close the socket after sending
*
* @returns {boolean} True if the message was ack'd, false if not
*/
Server.prototype.ack = function (message, data, close) {
var self = this,
socket = self.sockets[message.socketId],
actualData = data || '200 OK'
if (!socket || message.transactionId < 1 || message.acked) {
return false
}
var ackMessage = new Message(message.transactionId, 'rsp', actualData),
buffer = socket.parser.serialize(ackMessage)
if (close) {
socket.end(buffer)
} else {
socket.write(buffer)
}
message.acked = true
return true
}
/**
* Negatively acknowledges the receipt of a relp message to the relp client
* This should result in receiving the message again from the client
*
* @param {Message} message A relp message to ack
* @param {String} [error='Error'] Error message to send
* @param {boolean} [close=false] Whether or not to close the socket after sending
*
* @returns {boolean} True if the message was nack'd, false if not
*/
Server.prototype.nack = function (message, error, close) {
var useError = error || 'Error'
return this.ack(message, '500 ' + useError, close)
}
/**
* Pauses all existing and future connections
* You must resume before seeing anymore messages
*
* @return {boolean} True on success, false if already paused
*/
Server.prototype.pause = function () {
if (this.paused) {
return false
}
for (var socketId in this.sockets) {
this.sockets[socketId].pause()
}
this.paused = true
return true
}
/**
* Resumes a previously paused server
* Expect a flood of messages!
*
* @returns {boolean} True on success, false if not already paused
*/
Server.prototype.resume = function () {
if (!this.paused) {
return false
}
for (var socketId in this.sockets) {
this.sockets[socketId].resume()
}
this.paused = false
return true
}
/**
* Closes each open connection and finally the server
*/
Server.prototype.close = function () {
this.server.close()
for (var socketId in this.sockets) {
this.sockets[socketId].end()
}
}
/**
* Dispatches a message to the proper handler
*
* @param {Message} message The message to handle
*
* @private
*/
Server.prototype._handleMessage = function (message) {
var self = this,
socket = this.sockets[message.socketId]
switch (message.command) {
case 'syslog':
//Are we getting messages before the handshake?
if (socket.relpHandshakeComplete === false) {
//TODO: emit?
self.logger.error(
'Connection id', message.socketId, 'sent a syslog message before the handshake, disconnecting'
)
return socket.destroy()
}
this.emit('message', message)
break
case 'open':
this._handleOpen(message)
break
case 'close':
this._handleClose(message)
break
default:
//TODO: emit?
self.logger.error('Connection id', message.socketId, 'sent an unknown command:', message.command)
return socket.destroy()
}
}
/**
* Handles the open message
* This is what starts a relp session
*
* @param {Message} message The open message to work from
*
* @private
*/
Server.prototype._handleOpen = function (message) {
var self = this
, rawOffers = message.body.split('\n')
, offers = {}
, body
rawOffers.forEach(function (rawOffer) {
var parts = rawOffer.split('=')
offers[parts[0]] = parts[1]
})
if (offers['relp_version'] !== Server.RELP_VERSION) {
body = '500 Insufficient version\n'
+ Server.RELP_VERSION + ' required, ' + offers['relp_version'] + ' provided'
return self.ack(message, body, true)
}
if (!offers['commands']) {
body = '500 No commands provided'
return self.ack(message, body, true)
}
offers['commands'].split(',').some(function (command) {
if (Server.RELP_COMMANDS.indexOf(command.trim().toLowerCase()) === -1) {
body = '500 Invalid command\n'
+ command + ' is not supported'
return self.ack(message, body, true)
}
})
body = '200 OK '
+ 'relp_version=' + Server.RELP_VERSION + '\n'
+ 'relp_software=' + Server.RELP_SOFTWARE + '\n'
+ 'commands=' + offers['commands']
self.ack(message, body)
self.sockets[message.socketId].relpHandshakeComplete = true
}
/**
* Handles a close message
* Acks the message and closes the socket
*
* @param {Message} message The close message to work from
*
* @private
*/
Server.prototype._handleClose = function (message) {
this.ack(message, '', true)
}
/**
* Sets up a connection to be managed
* Handles new messages, socket closes/opens, etc
*
* @param {Socket} socket The socket to handle events for
*
* @private
*/
Server.prototype._setupConnection = function (socket) {
if (this.paused) {
socket.pause()
}
var self = this,
useParser = self.options.parser || Parser,
useProxyParser = self.options.proxyParser || proxyParser
socket.socketId = self._nextSocketId++
self.sockets[socket.socketId] = socket
socket.parser = new useParser()
socket.proxyHandshakeComplete = false
socket.relpHandshakeComplete = false
socket.parser.on('error', function (error) {
self.logger.error('Connection #', socket.socketId, 'had a parse error, disconnecting.', error)
socket.destroy()
})
socket.parser.on('message', function (message) {
message.socketId = socket.socketId
message.remoteAddress = socket.remoteAddress
message.proxyDetails = socket.proxyDetails
self._handleMessage(message)
})
socket.on('data', function (data) {
if (self.options.proxyProtocol && socket.proxyHandshakeComplete === false) {
var result = useProxyParser.consume(data)
socket.proxyHandshakeComplete = true
socket.proxyDetails = result.proxy
data = result.buffer
}
socket.parser.consume(data)
})
socket.on('error', function (error) {
self.logger.error('Connection #', socket.socketId, 'had an error:', error)
socket.destroy()
})
socket.on('close', function () {
/**
* Notifies consumers that a client connection has closed and is about to be unreferenced
*
* @event Server#connectionClosed
* @type {Socket} The socket that closed
* @property {Number} socketId The id of the socket that was closed, any non ack'd message with this
* socketId should stop processing to avoid duplicates
*/
self.emit('connectionClosed', socket)
delete self.sockets[socket.socketId]
})
/**
* Notifies consumers that a new client connection has opened
*
* @event Server#connectionOpened
* @type {Socket} The socket that opened
* @property {Number} socketId the id of the socket that just opened, all messages received on this connection
* will contain this socketId
*/
self.emit('connectionOpened', socket)
}
|