Skip to content

Commit

Permalink
Issue 26 (#27)
Browse files Browse the repository at this point in the history
TCP TLS Support, closes #26
  • Loading branch information
kkamkou authored Feb 26, 2017
1 parent ab3a83f commit 8a64efe
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ node_js:
- "3.3"
- "4.7"
- "5.12"
- "6.9"
- "7.2"
- "6.10"
- "7.6"
notifications:
slack:
on_success: never
Expand Down
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ node-gelf - Graylog2 client library for Node.js. Pro - because of code-quality.
## Installation
```
"dependencies": {
"gelf-pro": "~1.0" // see the "releases" section
"gelf-pro": "~1.1" // see the "releases" section
}
```
```npm install gelf-pro``` (**ALL** node.js versions are supported :)
Expand All @@ -32,13 +32,24 @@ log.setConfig({
transform: [], // optional; transformers for a message
broadcast: [], // optional; listeners of a message
levels: {}, // optional; default: see the levels section below
adapterName: 'udp', // optional; currently supported "udp" and "tcp"; default: udp
adapterOptions: {
protocol: 'udp4', // udp only; optional; udp adapter: udp4, udp6; default: udp4
adapterName: 'udp', // optional; currently supported "udp", "tcp" and "tcp-tls"; default: udp
adapterOptions: { // this object is passed to the adapter.connect() method
// common
host: '127.0.0.1', // optional; default: 127.0.0.1
port: 12201, // optional; default: 12201
// ... and so on, for :
// tcp adapter example
family: 4, // tcp only; optional; version of IP stack; default: 4
timeout: 1000, // tcp only; optional; default: 10000 (10 sec)
host: '127.0.0.1', // optional; default: 127.0.0.1
port: 12201 // optional; default: 12201
// udp adapter example
protocol: 'udp4', // udp only; optional; udp adapter: udp4, udp6; default: udp4
// tcp-tls adapter example
key: fs.readFileSync('client-key.pem'), // tcp-tls only; optional; only if using the client certificate authentication
cert: fs.readFileSync('client-cert.pem'), // tcp-tls only; optional; only if using the client certificate authentication
ca: [fs.readFileSync('server-cert.pem')] // tcp-tls only; optional; only for the self-signed certificate
}
});
```
Expand Down Expand Up @@ -119,6 +130,7 @@ log.setConfig({

- UDP (with deflation and chunking)
- TCP
- TCP via TLS(SSL)

### Tests
#### Cli
Expand Down
29 changes: 29 additions & 0 deletions lib/adapter/tcp-tls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Licensed under the MIT License
*
* @author Kanstantsin A Kamkou (2ka.by)
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @link https://github.com/kkamkou/node-gelf-pro
*/

'use strict';

// required stuff
var path = require('path'),
tls = require('tls'),
tcp = require(path.join(__dirname, 'tcp'));

// the class itself
var adapter = Object.create(tcp);

/**
* @param {Object} options
* @returns {tls.TLSSocket}
* @access private
*/
adapter._instance = function (options) {
return tls.connect(options);
};

// exporting outside
module.exports = adapter;
10 changes: 5 additions & 5 deletions lib/adapter/tcp.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ var _ = require('lodash'),
abstract = require(path.join(__dirname, 'abstract'));

// the class itself
var tcp = Object.create(abstract);
var adapter = Object.create(abstract);

/**
* Sends a message to the server
* @param {String} message
* @param {Function} callback
* @returns {tcp}
* @returns {adapter}
*/
tcp.send = function (message, callback) {
adapter.send = function (message, callback) {
var cb = _.once(callback),
timeout = this.options.timeout || 10000,
client = this._instance(this.options);
Expand Down Expand Up @@ -54,9 +54,9 @@ tcp.send = function (message, callback) {
* @returns {net.Socket}
* @access private
*/
tcp._instance = function (options) {
adapter._instance = function (options) {
return net.connect(options);
};

// exporting outside
module.exports = tcp;
module.exports = adapter;
12 changes: 6 additions & 6 deletions lib/adapter/udp.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var path = require('path'),
* at https://github.com/Graylog2/graylog2-server/blob/master/graylog2-server
* [2] https://www.graylog.org/resources/gelf/
*/
var udp = Object.create(abstract, {
var adapter = Object.create(abstract, {
specification: {
value: {
version: '1.1',
Expand All @@ -38,7 +38,7 @@ var udp = Object.create(abstract, {
* @returns {dgram.Socket}
* @private
*/
udp._createSocket = function () {
adapter._createSocket = function () {
return dgram.createSocket(this.options.protocol);
};

Expand All @@ -48,7 +48,7 @@ udp._createSocket = function () {
* @param {Number} maxSize
* @return {Array}
*/
udp.getArrayFromBuffer = function (buffer, maxSize) {
adapter.getArrayFromBuffer = function (buffer, maxSize) {
var chunks = [], i;
for (i = 0; i <= buffer.length; i += maxSize) {
chunks.push(Array.prototype.slice.call(buffer, i, i + maxSize));
Expand All @@ -60,9 +60,9 @@ udp.getArrayFromBuffer = function (buffer, maxSize) {
* Sends a chunk to the server
* @param {Object} packet
* @param {Function} cb
* @returns {udp}
* @returns {adapter}
*/
udp.send = function (message, callback) {
adapter.send = function (message, callback) {
var client = this._createSocket(),
bytesSentTotal = 0,
self = this;
Expand Down Expand Up @@ -116,4 +116,4 @@ udp.send = function (message, callback) {
};

// exporting outside
module.exports = udp;
module.exports = adapter;
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "gelf-pro",
"version": "1.0.0",
"version": "1.1.0",
"main": "./lib/gelf-pro.js",
"types": "./lib/definition.d.ts",
"author": "Kanstantsin Kamkou <[email protected]>",
"description": "The Graylog Extended Log Format for the Node.js",
"keywords": ["graylog", "gelf", "logging", "udp", "tcp"],
"keywords": ["graylog", "gelf", "logging", "udp", "tcp", "tls", "ssl"],
"repository" : {
"type" : "git",
"url" : "https://github.com/kkamkou/node-gelf-pro.git"
Expand All @@ -25,7 +25,7 @@
"istanbul": "~0.4.3",
"should": "~11.1",
"mocha": "~3.2.0",
"mocha-lcov-reporter": "~1.2.0",
"mocha-lcov-reporter": "~1.3.0",
"coveralls": "~2.11",
"sinon": "2.0.0-pre.4"
}
Expand Down
8 changes: 8 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,5 +407,13 @@ module.exports = {

this.eventEmitter.emit('error', new Error('err1'));
}
},

'Adapter TCP(TLS)': {
'Abstract functionality': function () {
var tls = require('tls'),
adapter = getAdapter('tcp-tls');
adapter._instance({host: 'unknown', port: 5555}).should.be.instanceOf(tls.TLSSocket);
}
}
};

0 comments on commit 8a64efe

Please sign in to comment.