| 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 |
1×
1×
1×
1×
1×
1×
3×
3×
23×
3×
23×
1×
1×
1×
1×
1×
1×
1×
22×
22×
22×
22×
22×
22×
20×
19×
20×
18×
20×
4×
20×
16×
15×
22×
22×
1×
1×
19×
19×
19×
19×
19×
19×
19×
19×
19×
19×
1×
6×
3×
2×
2×
1×
3×
3×
2×
2×
1×
6×
1×
24×
24×
19×
5×
19×
4×
15×
1×
19×
1×
19×
14×
19×
19×
19×
19×
19×
19×
19×
19×
2×
17×
9×
9×
9×
9×
8×
8×
8×
7×
7×
1×
1×
19×
1×
19×
4×
15×
15×
15×
15×
15×
1×
15×
15×
15×
15×
15×
9×
9×
9×
9×
7×
7×
1×
1×
9×
1×
8×
1×
1×
1×
8×
8×
8×
8×
6×
6×
6×
6×
6×
6×
1×
6×
6×
6×
1×
2×
1×
2×
2×
1×
1×
22×
1×
1×
1×
|
// Copyright 2015-2016 Stephen Vickers <stephen.vickers.sv@gmail.com>
var dgram = require("dgram");
var events = require("events");
var net = require("net");
var os = require("os");
var util = require("util");
function _expandConstantObject(object) {
var keys = [];
for (var key in object)
keys.push(key);
for (var i = 0; i < keys.length; i++)
object[object[keys[i]]] = parseInt(keys[i]);
}
var Transport = {
Tcp: 1,
Udp: 2
};
_expandConstantObject(Transport);
var Facility = {
Kernel: 0,
User: 1,
System: 3,
Audit: 13,
Alert: 14,
Local0: 16,
Local1: 17,
Local2: 18,
Local3: 19,
Local4: 20,
Local5: 21,
Local6: 22,
Local7: 23
};
_expandConstantObject(Facility);
var Severity = {
Emergency: 0,
Alert: 1,
Critical: 2,
Error: 3,
Warning: 4,
Notice: 5,
Informational: 6,
Debug: 7
};
_expandConstantObject(Severity);
var Client = function(target, options) {
this.target = target || "127.0.0.1";
this.syslogHostname = os.hostname();
this.port = 514;
this.tcpTimeout = 10000;
this.transport = Transport.Udp;
if (options) {
if (options.syslogHostname)
this.syslogHostname = options.syslogHostname;
if (options.port)
this.port = options.port;
if (options.tcpTimeout)
this.tcpTimeout = options.tcpTimeout;
if (options.transport) {
if (options.transport == Transport.Udp || options.transport == Transport.Tcp)
this.transport = options.transport;
}
}
this.getTransportRequests = [];
return this;
};
util.inherits(Client, events.EventEmitter);
Client.prototype.buildFormattedMessage = function(message, options) {
var elems = new Date().toString().split(/\s+/);
var month = elems[1];
var day = elems[2];
var time = elems[4];
/**
** BSD syslog requires leading 0's to be a space.
**/
Iif (day[0] == "0")
day = " " + day.substr(1, 1);
var timestamp = month + " " + day + " " + time;
var pri = (options.facility * 8) + options.severity;
var newline = message[message.length - 1] == "\n" ? "" : "\n";
var formattedMessage = "<"
+ pri
+ "> "
+ timestamp
+ " "
+ this.syslogHostname
+ " "
+ message
+ newline;
return new Buffer(formattedMessage);
};
Client.prototype.close = function () {
if (this.transport == Transport.Tcp) {
if (this.transport_) {
this.transport_.destroy();
delete this.transport_;
} else {
this.onClose();
}
} else Eif (this.transport == Transport.Udp) {
if (this.transport_) {
this.transport_.close();
delete this.transport_;
} else {
this.onClose();
}
}
return this;
};
Client.prototype.log = function() {
var message, options, cb;
if (typeof arguments[0] === "string")
message = arguments[0];
else
throw new Error("first argument must be string");
if (typeof arguments[1] === "function")
cb = arguments[1];
else if (typeof arguments[1] === "object")
options = arguments[1];
if (typeof arguments[2] === "function")
cb = arguments[2];
if (!cb)
cb = function () {};
var facility = options ? options.facility : Facility.Local0;
Iif (facility === undefined)
facility = Facility.Local0;
var severity = options ? options.severity : Severity.Informational;
Iif (severity === undefined)
severity = Severity.Informational;
var fm = this.buildFormattedMessage(message, {
facility: facility,
severity: severity
});
var me = this;
this.getTransport(function(error, transport) {
if (error) {
cb(error);
} else {
if (me.transport == Transport.Tcp) {
try {
transport.write(fm, function(error) {
Iif (error) {
cb(new Error("net.write() failed: " + error.message));
} else {
cb();
}
});
} catch (err) {
me.onError(err);
cb(err);
}
} else Eif (me.transport == Transport.Udp) {
try {
transport.send(fm, 0, fm.length, me.port, me.target, function(error, bytes) {
Iif (error) {
cb(new Error("dgram.send() failed: " + error.message));
} else {
cb();
}
});
} catch (err) {
me.onError(err);
cb(err);
}
} else {
cb(new Error("unknown transport '%s' specified to Client", me.transport));
}
}
});
return this;
};
Client.prototype.getTransport = function(cb) {
if (this.transport_ !== undefined)
return cb(null, this.transport_);
this.getTransportRequests.push(cb);
Iif (this.connecting)
return this;
else
this.connecting = true;
var af = net.isIPv4(this.target) ? 4 : 6;
var me = this;
function doCb(error, transport) {
while (me.getTransportRequests.length > 0) {
var nextCb = me.getTransportRequests.shift();
nextCb(error, transport);
}
me.connecting = false;
};
if (this.transport == Transport.Tcp) {
var options = {
host: this.target,
port: this.port,
family: af
};
var transport;
try {
transport = net.createConnection(options, function() {
me.transport_ = transport;
doCb(null, me.transport_);
});
} catch (err) {
doCb(err);
me.onError(err);
};
if (!transport)
return;
transport.setTimeout(this.tcpTimeout, function() {
var err = new Error("connection timed out");
me.emit("error", err);
doCb(err);
});
transport.on("end", function() {
var err = new Error("connection closed");
me.emit("error", err);
doCb(err);
});
transport.on("close", me.onClose.bind(me));
transport.on("error", function (err) {
doCb(err);
me.onError(err);
});
transport.unref();
} else Eif (this.transport == Transport.Udp) {
this.transport_ = dgram.createSocket("udp" + af);
this.transport_.on("close", this.onClose.bind(this));
this.transport_.on("error", function (err) {
me.onError(err);
doCb(err);
});
this.transport_.unref();
doCb(null, this.transport_);
} else {
doCb(new Error("unknown transport '%s' specified to Client", this.transport));
}
};
Client.prototype.onClose = function() {
Iif (this.transport_)
delete this.transport_;
this.emit("close");
return this;
};
Client.prototype.onError = function(error) {
if (this.transport_)
delete this.transport_;
this.emit("error", error);
return this;
};
exports.Client = Client;
exports.createClient = function(target, options) {
return new Client(target, options);
};
exports.Transport = Transport;
exports.Facility = Facility;
exports.Severity = Severity;
|