From 737d856a3235cc416091c0f938ee6216d62799b1 Mon Sep 17 00:00:00 2001 From: Brandon Nozaki Miller Date: Sun, 27 Sep 2015 21:52:16 -0700 Subject: [PATCH] added tls ssl support, examples, and local only certs --- README.md | 134 ++++++- .../goodbye-client.js | 47 +++ .../hello-client.js | 53 +++ .../world-server.js | 55 +++ .../basic-more-secure/hello-client.js | 47 +++ .../basic-more-secure/world-server.js | 48 +++ .../basic-most-secure/hello-client.js | 48 +++ .../basic-most-secure/world-server.js | 49 +++ example/TLSSocket/basic/hello-client.js | 42 +++ example/TLSSocket/basic/world-server.js | 42 +++ .../hello-client.js | 45 +++ .../world.server.js | 53 +++ lib/client.js | 46 ++- lib/socketServer.js | 37 +- local-node-ipc-certs/client.pub | 23 ++ local-node-ipc-certs/openssl.cnf | 352 ++++++++++++++++++ local-node-ipc-certs/private/client.key | 27 ++ local-node-ipc-certs/private/dhparam.pem | 8 + local-node-ipc-certs/private/server.key | 27 ++ local-node-ipc-certs/server.pub | 24 ++ node-ipc.js | 99 ++--- 21 files changed, 1245 insertions(+), 61 deletions(-) create mode 100644 example/TLSSocket/Multi-Client-Broadcast-basic/goodbye-client.js create mode 100644 example/TLSSocket/Multi-Client-Broadcast-basic/hello-client.js create mode 100644 example/TLSSocket/Multi-Client-Broadcast-basic/world-server.js create mode 100644 example/TLSSocket/basic-more-secure/hello-client.js create mode 100644 example/TLSSocket/basic-more-secure/world-server.js create mode 100644 example/TLSSocket/basic-most-secure/hello-client.js create mode 100644 example/TLSSocket/basic-most-secure/world-server.js create mode 100644 example/TLSSocket/basic/hello-client.js create mode 100644 example/TLSSocket/basic/world-server.js create mode 100644 example/TLSSocket/rawBuffer-only-works-with-most-secure/hello-client.js create mode 100644 example/TLSSocket/rawBuffer-only-works-with-most-secure/world.server.js create mode 100644 local-node-ipc-certs/client.pub create mode 100644 local-node-ipc-certs/openssl.cnf create mode 100644 local-node-ipc-certs/private/client.key create mode 100644 local-node-ipc-certs/private/dhparam.pem create mode 100644 local-node-ipc-certs/private/server.key create mode 100644 local-node-ipc-certs/server.pub diff --git a/README.md b/README.md index ea3fd95..7a76c77 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ This work is licenced via the [DBAD Public Licence](http://www.dbad-license.org/ 1. [Server for Unix||Windows Sockets & TCP Sockets](#server-for-unix-sockets--tcp-sockets) 2. [Client for Unix||Windows Sockets & TCP Sockets](#client-for-unix-sockets--tcp-sockets) 3. [Server & Client for UDP Sockets](#server--client-for-udp-sockets) + 4. [Raw Buffers or Binary Sockets](#raw-buffer-or-binary-sockets) 5. [Advanced Examples](https://github.com/RIAEvangelist/node-ipc/tree/master/example) @@ -46,7 +47,7 @@ This work is licenced via the [DBAD Public Licence](http://www.dbad-license.org/ |-----------|-----------|-----------| |Unix Socket or Windows Socket| Stable | Gives Linux, Mac, and Windows lightning fast communication and avoids the network card to reduce overhead and latency. [Local Unix and Windows Socket examples ](https://github.com/RIAEvangelist/node-ipc/tree/master/example/unixWindowsSocket/ "Unix and Windows Socket Node IPC examples") | |TCP Socket | Stable | Gives the most reliable communication across the network. Can be used for local IPC as well, but is slower than #1's Unix Socket Implementation because TCP sockets go through the network card while Unix Sockets and Windows Sockets do not. [Local or remote network TCP Socket examples ](https://github.com/RIAEvangelist/node-ipc/tree/master/example/TCPSocket/ "TCP Socket Node IPC examples") | -|TLS Socket | Alpha | ***coming soon...*** | +|TLS Socket | Stable | Configureable and secure network socket over SSL. Equivalent to https. | |UDP Sockets| Stable | Gives the **fastest network communication**. UDP is less reliable but much faster than TCP. It is best used for streaming non critical data like sound, video, or multiplayer game data as it can drop packets depending on network connectivity and other factors. UDP can be used for local IPC as well, but is slower than #1's Unix Socket or Windows Socket Implementation because UDP sockets go through the network card while Unix and Windows Sockets do not. [Local or remote network UDP Socket examples ](https://github.com/RIAEvangelist/node-ipc/tree/master/example/UDPSocket/ "UDP Socket Node IPC examples") | | OS | Supported Sockets | @@ -61,6 +62,8 @@ This work is licenced via the [DBAD Public Licence](http://www.dbad-license.org/ Set these variables in the `ipc.config` scope to overwrite or set default values. +```javascript + { appspace : 'app.', socketRoot : '/tmp/', @@ -76,6 +79,7 @@ Set these variables in the `ipc.config` scope to overwrite or set default values stopRetrying : false } +``` | variable | documentation | |----------|---------------| @@ -106,6 +110,8 @@ ipc.log will accept any number of arguments and if `ipc.config.silent` is not se The log also supports [colors](https://github.com/Marak/colors.js) implementation. All of the available styles are supported and the theme styles are as follows : +```javascript + { good : 'green', notice : 'yellow', @@ -116,8 +122,12 @@ The log also supports [colors](https://github.com/Marak/colors.js) implementatio data : 'blue' } +``` + You can override any of these settings by requireing colors and setting the theme as follows : +```javascript + var colors=require('colors'); colors.setTheme( @@ -127,6 +137,9 @@ You can override any of these settings by requireing colors and setting the them ... } ); + +``` + ---- ##### connectTo @@ -142,10 +155,16 @@ Used for connecting as a client to local Unix Sockets and Windows Sockets. ***Th **examples** arguments can be ommitted so long as they are still in order. +```javascript + ipc.connectTo('world'); +``` + or using just an id and a callback +```javascript + ipc.connectTo( 'world', function(){ @@ -159,15 +178,23 @@ or using just an id and a callback } ); +``` + or explicitly setting the path +```javascript + ipc.connectTo( 'world', 'myapp.world' ); +``` + or explicitly setting the path with callback +```javascript + ipc.connectTo( 'world', 'myapp.world', @@ -175,6 +202,9 @@ or explicitly setting the path with callback ... } ); + +``` + ---- ##### connectToNet @@ -192,10 +222,16 @@ Used to connect as a client to a TCP or TLS socket via the network card. This ca **examples** arguments can be ommitted so long as they are still in order. So while the default is : (id,host,port,callback), the following examples will still work because they are still in order (id,port,callback) or (id,host,callback) or (id,port) etc. +```javascript + ipc.connectToNet('world'); +``` + or using just an id and a callback +```javascript + ipc.connectToNet( 'world', function(){ @@ -203,16 +239,24 @@ or using just an id and a callback } ); +``` + or explicitly setting the host and path +```javascript + ipc.connectToNet( 'world', 'myapp.com',serve(path,callback) 3435 ); +``` + or only explicitly setting port and callback +```javascript + ipc.connectToNet( 'world', 3435, @@ -221,6 +265,8 @@ or only explicitly setting port and callback } ); +``` + ---- ##### disconnect @@ -234,8 +280,12 @@ Used to disconnect a client from a Unix, Windows, TCP or TLS socket. The socket **examples** +```javascript + ipc.disconnect('world'); +``` + ---- ##### serve `ipc.serve(path,callback);` @@ -249,27 +299,43 @@ Used to create local Unix Socket Server or Windows Socket Server to which Client ***examples*** arguments can be omitted so long as they are still in order. +```javascript + ipc.serve(); +``` + or specifying callback +```javascript + ipc.serve( function(){...} ); +``` + or specify path +```javascript + ipc.serve( '/tmp/myapp.myservice' ); +``` + or specifying everything +```javascript + ipc.serve( '/tmp/myapp.myservice', function(){...} ); +``` + ---- ##### serveNet @@ -289,41 +355,67 @@ Used to create TCP, TLS or UDP Socket Server to which Clients can bind or other default tcp server +```javascript + ipc.serveNet(); +``` + default udp server +```javascript + ipc.serveNet('udp4'); +``` + or specifying TCP server with callback +```javascript + ipc.serveNet( function(){...} ); +``` + or specifying UDP server with callback +```javascript + ipc.serveNet( 'udp4', function(){...} ); +``` + or specify port +```javascript + ipc.serveNet( 3435 ); +``` + or specifying everything TCP +```javascript + ipc.serveNet( 'MyMostAwesomeApp.com', 3435, function(){...} ); +``` + or specifying everything UDP +```javascript + ipc.serveNet( 'MyMostAwesomeApp.com', 3435, @@ -331,6 +423,8 @@ or specifying everything UDP function(){...} ); +``` + ---- ### IPC Stores and Default Variables @@ -346,6 +440,8 @@ You can find [Advanced Examples](https://github.com/RIAEvangelist/node-ipc/tree/ #### Server for Unix Sockets, Windows Sockets & TCP Sockets The server is the process keeping a socket for IPC open. Multiple sockets can connect to this server and talk to it. It can also broadcast to all clients or emit to a specific client. This is the most basic example which will work for local Unix and Windows Sockets as well as local or remote network TCP Sockets. +```javascript + var ipc=require('node-ipc'); ipc.config.id = 'world'; @@ -369,9 +465,13 @@ The server is the process keeping a socket for IPC open. Multiple sockets can co ipc.server.start(); +``` + #### Client for Unix Sockets & TCP Sockets The client connects to the servers socket for Inter Process Communication. The socket will receive events emitted to it specifically as well as events which are broadcast out on the socket by the server. This is the most basic example which will work for both local Unix Sockets and local or remote network TCP Sockets. +```javascript + var ipc=require('node-ipc'); ipc.config.id = 'hello'; @@ -405,6 +505,8 @@ The client connects to the servers socket for Inter Process Communication. The s } ); +``` + #### Server & Client for UDP Sockets UDP Sockets are different than Unix, Windows & TCP Sockets because they must be bound to a unique port on their machine to receive messages. For example, A TCP, Unix, or Windows Socket client could just connect to a separate TCP, Unix, or Windows Socket sever. That client could then exchange, both send and receive, data on the servers port or location. UDP Sockets can not do this. They must bind to a port to receive or send data. @@ -414,6 +516,8 @@ This is the most basic example which will work for both local and remote UDP Soc ##### UDP Server 1 - "World" +```javascript + var ipc=require('../../../node-ipc'); ipc.config.id = 'world'; @@ -446,9 +550,13 @@ This is the most basic example which will work for both local and remote UDP Soc ipc.server.start(); +``` + ##### UDP Server 2 - "Hello" *note* we set the port here to 8001 because the world server is already using the default ipc.config.networkPort of 8000. So we can not bind to 8000 while world is using it. +```javascript + ipc.config.id = 'hello'; ipc.config.retry= 1500; @@ -481,20 +589,31 @@ This is the most basic example which will work for both local and remote UDP Soc ipc.server.start(); +``` + #### Raw Buffer or Binary Sockets Binary or Buffer sockets can be used with any of the above socket types, however the way data events are emit is ***slightly*** different. When setting up a rawBuffer socket you must specify it as such : +```javascript + ipc.config.rawBuffer=true; +``` + You can also specify its encoding type. The default is ` utf8 ` +```javascript + ipc.config.encoding='utf8'; +``` emit string buffer : +```javascript + //server ipc.server.emit( socket, @@ -506,8 +625,12 @@ emit string buffer : 'hello' ) +``` + emit byte array buffer : +```javascript + //server ipc.server.emit( socket, @@ -519,8 +642,12 @@ emit byte array buffer : [10,20,30] ); +``` + emit hex array buffer : +```javascript + //server ipc.server.emit( socket, @@ -531,3 +658,8 @@ emit hex array buffer : ipc.server.emit( [0x05,0x6d,0x5c] ); + +``` + +#### Licensed under DBAD license +See the [DBAD license](https://github.com/philsturgeon/dbad) in your language or our [licence.md](https://github.com/RIAEvangelist/node-phidget-API/blob/master/license.md) file. diff --git a/example/TLSSocket/Multi-Client-Broadcast-basic/goodbye-client.js b/example/TLSSocket/Multi-Client-Broadcast-basic/goodbye-client.js new file mode 100644 index 0000000..6c8f8f3 --- /dev/null +++ b/example/TLSSocket/Multi-Client-Broadcast-basic/goodbye-client.js @@ -0,0 +1,47 @@ +var ipc=require('../../../node-ipc'); + +/***************************************\ + * + * You should start both hello and world + * then you will see them communicating. + * + * *************************************/ + +ipc.config.id = 'goodbye'; +ipc.config.retry= 1500; +ipc.config.maxRetries= 10; +ipc.config.tls={ + rejectUnauthorized:false +}; + +ipc.connectToNet( + 'world', + function(){ + ipc.of.world.on( + 'connect', + function(){ + ipc.log('## connected to world ##'.rainbow, ipc.config.delay); + ipc.of.world.emit( + 'app.message', + { + id : ipc.config.id, + message : 'goodbye' + } + ) + } + ); + ipc.of.world.on( + 'disconnect', + function(){ + ipc.log('disconnected from world'.notice); + } + ); + ipc.of.world.on( + 'kill.connection', + function(data){ + ipc.log('world requested kill.connection'.notice); + ipc.disconnect('world'); + } + ); + } +); diff --git a/example/TLSSocket/Multi-Client-Broadcast-basic/hello-client.js b/example/TLSSocket/Multi-Client-Broadcast-basic/hello-client.js new file mode 100644 index 0000000..7ae92f0 --- /dev/null +++ b/example/TLSSocket/Multi-Client-Broadcast-basic/hello-client.js @@ -0,0 +1,53 @@ +var ipc=require('../../../node-ipc'); + +/***************************************\ + * + * You should start both hello and world + * then you will see them communicating. + * + * *************************************/ + +ipc.config.id = 'hello'; +ipc.config.retry= 1500; +ipc.config.maxRetries=10; +ipc.config.tls={ + rejectUnauthorized:false +}; + +ipc.connectToNet( + 'world', + function(){ + ipc.of.world.on( + 'connect', + function(){ + ipc.log('## connected to world ##'.rainbow, ipc.config.delay); + ipc.of.world.emit( + 'app.message', + { + id : ipc.config.id, + message : 'hello' + } + ) + } + ); + ipc.of.world.on( + 'disconnect', + function(){ + ipc.log('disconnected from world'.notice); + } + ); + ipc.of.world.on( + 'app.message', + function(data){ + ipc.log('got a message from world : '.debug, data.message); + } + ); + ipc.of.world.on( + 'kill.connection', + function(data){ + ipc.log('world requested kill.connection'.notice); + ipc.disconnect('world'); + } + ); + } +); diff --git a/example/TLSSocket/Multi-Client-Broadcast-basic/world-server.js b/example/TLSSocket/Multi-Client-Broadcast-basic/world-server.js new file mode 100644 index 0000000..7c8a9b6 --- /dev/null +++ b/example/TLSSocket/Multi-Client-Broadcast-basic/world-server.js @@ -0,0 +1,55 @@ +var ipc=require('../../../node-ipc'); + +/***************************************\ + * + * You should start both hello and world + * then you will see them communicating. + * + * *************************************/ + +ipc.config.id = 'world'; +ipc.config.retry= 1500; +ipc.config.tls={ + public: '../../../local-node-ipc-certs/server.pub', + private: '../../../local-node-ipc-certs/private/server.key' +} + +var messages={ + goodbye:false, + hello:false +} + +ipc.serveNet( + function(){ + ipc.server.on( + 'app.message', + function(data,socket){ + ipc.log('got a message from'.debug, (data.id).variable, (data.message).data); + messages[data.id]=true; + ipc.server.emit( + socket, + 'app.message', + { + id : ipc.config.id, + message : data.message+' world!' + } + ); + + if(messages.hello && messages.goodbye){ + ipc.log('got all required events, telling clients to kill connection'.good); + ipc.server.broadcast( + 'kill.connection', + { + id:ipc.config.id + } + ); + } + } + ); + } +); + +ipc.server.define.listen['app.message']='This event type listens for message strings as value of data key.'; +ipc.server.define.broadcast['kill.connection']='This event is a command to kill connection to this server, the data object will contain the id of this server incase the client needs it'; + +ipc.server.start(); diff --git a/example/TLSSocket/basic-more-secure/hello-client.js b/example/TLSSocket/basic-more-secure/hello-client.js new file mode 100644 index 0000000..4104d26 --- /dev/null +++ b/example/TLSSocket/basic-more-secure/hello-client.js @@ -0,0 +1,47 @@ +var ipc=require('../../../node-ipc'); + +/***************************************\ + * + * You should start both hello and world + * then you will see them communicating. + * + * *************************************/ + +ipc.config.id = 'hello'; +ipc.config.retry= 1500; +ipc.config.tls={ + private: '../../../local-node-ipc-certs/private/client.key', + public: '../../../local-node-ipc-certs/client.pub', + rejectUnauthorized:false, + trustedConnections: [ + '../../../local-node-ipc-certs/server.pub' + ] +}; + +ipc.connectToNet( + 'world', + function(){ + ipc.of.world.on( + 'connect', + function(){ + ipc.log('## connected to world ##'.rainbow, ipc.config.delay); + ipc.of.world.emit( + 'message', + 'hello' + ) + } + ); + ipc.of.world.on( + 'disconnect', + function(){ + ipc.log('disconnected from world'.notice); + } + ); + ipc.of.world.on( + 'message', + function(data){ + ipc.log('got a message from world : '.debug, data); + } + ); + } +); diff --git a/example/TLSSocket/basic-more-secure/world-server.js b/example/TLSSocket/basic-more-secure/world-server.js new file mode 100644 index 0000000..1120bca --- /dev/null +++ b/example/TLSSocket/basic-more-secure/world-server.js @@ -0,0 +1,48 @@ +var ipc=require('../../../node-ipc'); + +/***************************************\ + * + * You should start both hello and world + * then you will see them communicating. + * + * *************************************/ + +ipc.config.id = 'world'; +ipc.config.retry= 1500; +ipc.config.tls={ + public: '../../../local-node-ipc-certs/server.pub', + private: '../../../local-node-ipc-certs/private/server.key', + dhparam: '../../../local-node-ipc-certs/private/dhparam.pem', + requestCert: true, + rejectUnauthorized:false, + trustedConnections: [ + '../../../local-node-ipc-certs/client.pub' + ] +} + +ipc.serveNet( + function(){ + ipc.server.on( + 'message', + function(data,socket){ + ipc.log('got a message : '.debug, data); + ipc.server.emit( + socket, + 'message', + data+' world!' + ); + } + ); + + ipc.server.on( + 'socket.disconnected', + function(data,socket){ + console.log(arguments) + } + ); + } +); + +ipc.server.define.listen.message='This event type listens for message strings as value of data key.'; + +ipc.server.start(); diff --git a/example/TLSSocket/basic-most-secure/hello-client.js b/example/TLSSocket/basic-most-secure/hello-client.js new file mode 100644 index 0000000..2bfced2 --- /dev/null +++ b/example/TLSSocket/basic-most-secure/hello-client.js @@ -0,0 +1,48 @@ +var ipc=require('../../../node-ipc'); + +/***************************************\ + * + * You should start both hello and world + * then you will see them communicating. + * + * *************************************/ + +ipc.config.id = 'hello'; +ipc.config.retry= 1500; +ipc.config.networkHost='localhost'; +ipc.config.tls={ + private: '../../../local-node-ipc-certs/private/client.key', + public: '../../../local-node-ipc-certs/client.pub', + rejectUnauthorized:true, + trustedConnections: [ + '../../../local-node-ipc-certs/server.pub' + ] +}; + +ipc.connectToNet( + 'world', + function(){ + ipc.of.world.on( + 'connect', + function(){ + ipc.log('## connected to world ##'.rainbow, ipc.config.delay); + ipc.of.world.emit( + 'message', + 'hello' + ) + } + ); + ipc.of.world.on( + 'disconnect', + function(){ + ipc.log('disconnected from world'.notice); + } + ); + ipc.of.world.on( + 'message', + function(data){ + ipc.log('got a message from world : '.debug, data); + } + ); + } +); diff --git a/example/TLSSocket/basic-most-secure/world-server.js b/example/TLSSocket/basic-most-secure/world-server.js new file mode 100644 index 0000000..49f6b09 --- /dev/null +++ b/example/TLSSocket/basic-most-secure/world-server.js @@ -0,0 +1,49 @@ +var ipc=require('../../../node-ipc'); + +/***************************************\ + * + * You should start both hello and world + * then you will see them communicating. + * + * *************************************/ + +ipc.config.id = 'world'; +ipc.config.retry= 1500; +ipc.config.networkHost='localhost'; +ipc.config.tls={ + public: '../../../local-node-ipc-certs/server.pub', + private: '../../../local-node-ipc-certs/private/server.key', + dhparam: '../../../local-node-ipc-certs/private/dhparam.pem', + requestCert: true, + rejectUnauthorized:true, + trustedConnections: [ + '../../../local-node-ipc-certs/client.pub' + ] +} + +ipc.serveNet( + function(){ + ipc.server.on( + 'message', + function(data,socket){ + ipc.log('got a message : '.debug, data); + ipc.server.emit( + socket, + 'message', + data+' world!' + ); + } + ); + + ipc.server.on( + 'socket.disconnected', + function(data,socket){ + console.log(arguments) + } + ); + } +); + +ipc.server.define.listen.message='This event type listens for message strings as value of data key.'; + +ipc.server.start(); diff --git a/example/TLSSocket/basic/hello-client.js b/example/TLSSocket/basic/hello-client.js new file mode 100644 index 0000000..2712a51 --- /dev/null +++ b/example/TLSSocket/basic/hello-client.js @@ -0,0 +1,42 @@ +var ipc=require('../../../node-ipc'); + +/***************************************\ + * + * You should start both hello and world + * then you will see them communicating. + * + * *************************************/ + +ipc.config.id = 'hello'; +ipc.config.retry= 1500; +ipc.config.tls={ + rejectUnauthorized:false +}; + +ipc.connectToNet( + 'world', + function(){ + ipc.of.world.on( + 'connect', + function(){ + ipc.log('## connected to world ##'.rainbow, ipc.config.delay); + ipc.of.world.emit( + 'message', + 'hello' + ) + } + ); + ipc.of.world.on( + 'disconnect', + function(){ + ipc.log('disconnected from world'.notice); + } + ); + ipc.of.world.on( + 'message', + function(data){ + ipc.log('got a message from world : '.debug, data); + } + ); + } +); diff --git a/example/TLSSocket/basic/world-server.js b/example/TLSSocket/basic/world-server.js new file mode 100644 index 0000000..7af34c3 --- /dev/null +++ b/example/TLSSocket/basic/world-server.js @@ -0,0 +1,42 @@ +var ipc=require('../../../node-ipc'); + +/***************************************\ + * + * You should start both hello and world + * then you will see them communicating. + * + * *************************************/ + +ipc.config.id = 'world'; +ipc.config.retry= 1500; +ipc.config.tls={ + public: '../../../local-node-ipc-certs/server.pub', + private: '../../../local-node-ipc-certs/private/server.key' +} + +ipc.serveNet( + function(){ + ipc.server.on( + 'message', + function(data,socket){ + ipc.log('got a message : '.debug, data); + ipc.server.emit( + socket, + 'message', + data+' world!' + ); + } + ); + + ipc.server.on( + 'socket.disconnected', + function(data,socket){ + console.log(arguments) + } + ); + } +); + +ipc.server.define.listen.message='This event type listens for message strings as value of data key.'; + +ipc.server.start(); diff --git a/example/TLSSocket/rawBuffer-only-works-with-most-secure/hello-client.js b/example/TLSSocket/rawBuffer-only-works-with-most-secure/hello-client.js new file mode 100644 index 0000000..b893444 --- /dev/null +++ b/example/TLSSocket/rawBuffer-only-works-with-most-secure/hello-client.js @@ -0,0 +1,45 @@ +var ipc=require('../../../node-ipc'); + +/***************************************\ + * + * You should start both hello and world + * then you will see them communicating. + * + * *************************************/ + +ipc.config.id = 'hello'; +ipc.config.retry= 1500; +ipc.config.rawBuffer=true; +ipc.config.encoding='ascii'; +ipc.config.networkHost='localhost'; + +ipc.config.tls={ + private: '../../../local-node-ipc-certs/private/client.key', + public: '../../../local-node-ipc-certs/client.pub', + rejectUnauthorized:true, + trustedConnections: [ + '../../../local-node-ipc-certs/server.pub' + ] +}; + +ipc.connectToNet( + 'world', + function(){ + ipc.of.world.on( + 'connect', + function(){ + ipc.log('## connected to world ##'.rainbow, ipc.config.delay); + ipc.of.world.emit( + 'hello' + ) + } + ); + + ipc.of.world.on( + 'data', + function(data){ + ipc.log('got a message from world : '.debug, data,data.toString()); + } + ); + } +); diff --git a/example/TLSSocket/rawBuffer-only-works-with-most-secure/world.server.js b/example/TLSSocket/rawBuffer-only-works-with-most-secure/world.server.js new file mode 100644 index 0000000..d4053ea --- /dev/null +++ b/example/TLSSocket/rawBuffer-only-works-with-most-secure/world.server.js @@ -0,0 +1,53 @@ +var ipc=require('../../../node-ipc'); + +/***************************************\ + * + * You should start both hello and world + * then you will see them communicating. + * + * *************************************/ + +ipc.config.id = 'world'; +ipc.config.retry= 1500; +ipc.config.rawBuffer=true; +ipc.config.encoding='ascii'; +ipc.config.networkHost='localhost'; + +ipc.config.tls={ + public: '../../../local-node-ipc-certs/server.pub', + private: '../../../local-node-ipc-certs/private/server.key', + dhparam: '../../../local-node-ipc-certs/private/dhparam.pem', + requestCert: true, + rejectUnauthorized:true, + trustedConnections: [ + '../../../local-node-ipc-certs/client.pub' + ] +} + +ipc.serveNet( + function(){ + ipc.server.on( + 'connect', + function(socket){ + console.log('connection detected'); + ipc.server.emit( + socket, + 'hello' + ); + } + ); + + ipc.server.on( + 'data', + function(data,socket){ + ipc.log('got a message'.debug, data,data.toString()); + ipc.server.emit( + socket, + 'goodbye' + ); + } + ); + } +); + +ipc.server.start(); diff --git a/lib/client.js b/lib/client.js index c525820..5a161a2 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1,7 +1,9 @@ var net = require('net'), + tls = require('tls'), eventParser = require('../lib/eventParser.js'), pubsub = require('event-pubsub'), - Message = require('js-message'); + Message = require('js-message'), + fs = require('fs'); function init(config,log){ var client={ @@ -56,13 +58,43 @@ function connect(){ } ); }else{ - client.log('Connecting client via TCP to'.debug, client.path.variable ,client.port); - client.socket = net.connect( - { - port:client.port, - host:client.path + if(!client.config.tls){ + client.log('Connecting client via TCP to'.debug, client.path.variable ,client.port); + client.socket = net.connect( + { + port:client.port, + host:client.path + } + ); + }else{ + client.log('Connecting client via TLS to'.debug, client.path.variable ,client.port,client.config.tls); + if(client.config.tls.private){ + client.config.tls.key=fs.readFileSync(client.config.tls.private); } - ); + if(client.config.tls.public){ + client.config.tls.cert=fs.readFileSync(client.config.tls.public); + } + if(client.config.tls.trustedConnections){ + if(typeof client.config.tls.trustedConnections === 'string'){ + client.config.tls.trustedConnections=[client.config.tls.trustedConnections]; + } + client.config.tls.ca=[]; + for(var i=0; i