From 8a64efef5924cdbaa8244ad616c2e2c3f148944d Mon Sep 17 00:00:00 2001 From: Kanstantsin Kamkou Date: Sun, 26 Feb 2017 14:09:01 +0100 Subject: [PATCH] Issue 26 (#27) TCP TLS Support, closes #26 --- .travis.yml | 4 ++-- README.md | 24 ++++++++++++++++++------ lib/adapter/tcp-tls.js | 29 +++++++++++++++++++++++++++++ lib/adapter/tcp.js | 10 +++++----- lib/adapter/udp.js | 12 ++++++------ package.json | 6 +++--- test/tests.js | 8 ++++++++ 7 files changed, 71 insertions(+), 22 deletions(-) create mode 100644 lib/adapter/tcp-tls.js diff --git a/.travis.yml b/.travis.yml index 91c47be..e2be5e1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/README.md b/README.md index 01ea772..f82a8b8 100644 --- a/README.md +++ b/README.md @@ -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 :) @@ -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 } }); ``` @@ -119,6 +130,7 @@ log.setConfig({ - UDP (with deflation and chunking) - TCP +- TCP via TLS(SSL) ### Tests #### Cli diff --git a/lib/adapter/tcp-tls.js b/lib/adapter/tcp-tls.js new file mode 100644 index 0000000..05a814f --- /dev/null +++ b/lib/adapter/tcp-tls.js @@ -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; diff --git a/lib/adapter/tcp.js b/lib/adapter/tcp.js index 47b50d4..b28b26c 100644 --- a/lib/adapter/tcp.js +++ b/lib/adapter/tcp.js @@ -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); @@ -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; diff --git a/lib/adapter/udp.js b/lib/adapter/udp.js index 40407b7..5844846 100644 --- a/lib/adapter/udp.js +++ b/lib/adapter/udp.js @@ -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', @@ -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); }; @@ -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)); @@ -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; @@ -116,4 +116,4 @@ udp.send = function (message, callback) { }; // exporting outside -module.exports = udp; +module.exports = adapter; diff --git a/package.json b/package.json index 9a04f56..cec887c 100644 --- a/package.json +++ b/package.json @@ -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 ", "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" @@ -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" } diff --git a/test/tests.js b/test/tests.js index 3c4b06a..23a17d3 100644 --- a/test/tests.js +++ b/test/tests.js @@ -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); + } } };