node-simple-socket
Wrapper for Node.js sockets that makes it easy to send data compressed and crypted (RSA / AES).
How does it work?
Sockets are wrapped by a class that exchanges data between two endpoints by encrypting and compressing it.
You do not need to setup anything for the encryption and compression ... you only need to start a server and connect with a client by using the new class(es)!
The new "sockets" make a handshake and share a (strong) random password, which is used for the "live" communication, with the help of RSA in the background. This is done at the time, you start to send and receive data.
Install
npm install node-simple-socket --save
Usage
Import
var simpleSocketModule = ;
The TypeScript way:
;
Create a server
// listening for new connections// on port 5979simpleSocketModule;
Connect to a server
// connect to a server (socket)// that listens on port 5979simpleSocketModule;
Send and receive data
Raw data
Send:
var dataToSend = 'Hello, TM!' 'utf8'; senderSocket;
Receive:
recipientSocket;
Strings
Send:
senderSocket;
Receive:
recipientSocket;
JSON objects
Send:
var myObject = TM: 5979 MK: '23979' PZSUX: true; senderSocket;
Receive:
recipientSocket;
Files
Send:
senderSocket;
Receive:
recipientSocket;
Streams
Send:
var fs = ; fs;
Receive:
var fs = ; fs;
Mixed (files <=> stream)
File => Stream
Send:
senderSocket;
Receive:
var fs = ; fs;
Stream => File
Send:
var fs = ; fs;
recipientSocket;
Events
socket;socket;socket;socket;socket;socket;socket;socket;socket;socket;socket;socket;
Settings
RSA
A RSA key pair is generated by node-rsa module on the client side.
The default key size for a handshake is 512
.
You can change this, by setting the rsaKeySize
property:
clientSocketrsaKeySize = 2048; // better, but takes more time
Custom password generator
While a handshake, (server) sockets generate a random password via randomBytes function of Crypto module, that is exchanged with RSA encryption.
Those passwords have a size of 48
bytes by default.
Passwords are used for the aes-256-ctr
algorithm of the crypto module, which encrypts and decrypts all data of the "live" communication.
You can define a custom password generator by setting the passwordGenerator
property:
serverSocket { // generate a password as // buffer or string // that should be encrypted with the // received RSA public key // of the connected client // // you can return it directly // or as promise, if you work async};
Maximum data (package) size
By default, you cannot send and receive data with more than 16777211
bytes.
You can change this, by setting the maxPackageSize
property:
socketmaxPackageSize = 597923979;
Default values
The module provides the following (public) variables that store default settings for properties of SimpleSocket class.
// initial value for 'compress' property// Default: (undefined) / autosimpleSocketModuleCompress = true; // initial value for 'cwd' property// Default: process.cwd()simpleSocketModuleDefaultCWD = 'E:/test'; // initial value for 'dataTransformer' property// Default: (undefined)simpleSocketModule { // ctx.data => the data to transform // ctx.direction => the direction, s. https://mkloubert.github.io/node-simple-socket/enums/_index_.datatransformerdirection.html // return transformed data as buffer // directly or as Promise}; // initial value for 'encoding' property// Default: utf8simpleSocketModuleDefaultEncoding = 'ascii'; // initial value for 'handshakeTransformer' property// Default: (undefined)simpleSocketModule { // ctx.data => the data to transform // ctx.direction => the direction, s. https://mkloubert.github.io/node-simple-socket/enums/_index_.datatransformerdirection.html // return transformed data as buffer // directly or as Promise}; // initial value for 'maxPackageSize' property// Default: 16777211simpleSocketModuleDefaultMaxPackageSize = 239795979; // initial value for 'passwordGenerator' property// Default: (undefined)simpleSocketModule { // return transformed password as buffer // directly or as Promise}; // initial value for 'readBufferSize' property// Default: 8192simpleSocketModuleDefaultReadBufferSize = 10240; // initial value for 'rsaKeySize' property// Default: 512simpleSocketModuleDefaultRSAKeySize = 4096;
Documentation
The full API documentation can be found here.
Migration
1.x.x => 2.x.x and higher
- since version 2.0 there is an important protocol update to improve encryption ... communication with older versions (less than 2.0) might not work anymore, when using compression