Comparing version 1.3.5 to 1.3.6
154
index.js
'use strict'; // always strict mode | ||
// Import dependencies | ||
const EventEmitter = require ('events').EventEmitter; | ||
const uuid = require ('uuid'); | ||
const SocketIo = require ('socket.io'); | ||
const SocketIoClient = require ('socket.io-client'); | ||
const nacl = require ('tweetnacl'); | ||
const EventEmitter = require ('events').EventEmitter; | ||
const uuid = require ('uuid'); | ||
const { TrannyServer, TrannyClient, genKeyPair: genSocketKeyPair } = require ('cryptotranny'); | ||
@@ -20,2 +18,3 @@ // Create RPC events class | ||
// Send new event to remote | ||
send (id, ...params) { | ||
@@ -59,5 +58,2 @@ // Emit event to remote | ||
this._child = child; | ||
// Listen for events if have child class | ||
if (child != null) this._listen (); | ||
} | ||
@@ -94,3 +90,7 @@ | ||
_listen () { | ||
// Listen for RPC calls | ||
listen () { | ||
// Don't bother if no child | ||
if (this._child == null) return; | ||
// Listen for function calls | ||
@@ -191,6 +191,11 @@ this._stream.on ('fnCall', async (call) => { | ||
// Destroy underlying stream | ||
destroy () { | ||
this._stream.destroy (); | ||
} | ||
// Induced by implementations of `Rpc` | ||
_disconnect () { | ||
_onDisconnect () { | ||
// Emit disconnect event so users of RPC will be aware | ||
this.emit ('disconnect'); | ||
this.emit ('disconnected'); | ||
} | ||
@@ -200,9 +205,8 @@ } | ||
class ServerSocketInterface extends EventEmitter { | ||
constructor (socket, cryptKey, onDisconnect) { | ||
constructor (client, onDisconnect) { | ||
super (); | ||
// Set private class variables | ||
this._socket = socket; | ||
this._client = client; | ||
this._onDisconnect = onDisconnect; | ||
this._cryptKey = cryptKey; | ||
@@ -214,14 +218,5 @@ // Listen for IPC data | ||
_listen () { | ||
this._socket.on ('message', (data) => { | ||
// silently ignore bad data | ||
if (data.nonce == null || data.encrypted == null) return; | ||
// open nacl secret box | ||
let decrypted = nacl.secretbox.open (Buffer.from (data.encrypted, 'base64'), Buffer.from (data.nonce, 'base64'), this._cryptKey); | ||
// silently ignore bad crypt, TODO: handle | ||
if (decrypted == false) return; | ||
this._client.on ('message', (data) => { | ||
// decode JSON message | ||
let decoded = JSON.parse (Buffer.from (decrypted).toString ()); | ||
let decoded = JSON.parse (data.toString ()); | ||
@@ -232,4 +227,3 @@ // emit event | ||
this._socket.on ('disconnect', () => { | ||
// Run self ondisconnect to handle server disconnection | ||
this._client.on ('disconnected', () => { | ||
this._onDisconnect (); | ||
@@ -241,18 +235,14 @@ }); | ||
// Encode object to JSON | ||
let encoded = JSON.stringify ({ | ||
let encoded = Buffer.from (JSON.stringify ({ | ||
'id' : id, | ||
'data' : data | ||
}); | ||
})); | ||
// Generate a random nonce for cryptography | ||
let nonce = nacl.randomBytes (nacl.box.nonceLength); | ||
// Send nonce, from secret key, and data over IPC | ||
this._client.send (encoded); | ||
} | ||
// Encrypt encoded object using nonce, hardcoded server public key and our secret key | ||
let encrypted = nacl.secretbox (Buffer.from (encoded), nonce, this._cryptKey); | ||
// Send nonce, from secret key, and encrypted data over IPC | ||
this._socket.emit ('message', { | ||
'nonce' : Buffer.from (nonce).toString ('base64'), | ||
'encrypted' : Buffer.from (encrypted).toString ('base64') | ||
}); | ||
// Destroy underlying connection | ||
destroy () { | ||
this._client.destroy (); | ||
} | ||
@@ -262,7 +252,6 @@ } | ||
class ServerSocketRpc extends Rpc { | ||
constructor (socket, cryptKey, child) { | ||
// Create event interface from provided ipc socket | ||
let ipcInterface = new ServerSocketInterface (socket, cryptKey, () => { | ||
// Induce self disconnect when socket interface disconnected | ||
this._disconnect (); | ||
constructor (client, child) { | ||
// Create event interface from provided client | ||
let ipcInterface = new ServerSocketInterface (client, () => { | ||
this._onDisconnect (); | ||
}); | ||
@@ -272,2 +261,4 @@ | ||
super (ipcInterface, child); | ||
this.pk = client.pk; | ||
} | ||
@@ -277,14 +268,15 @@ } | ||
class ServerSocketRpcMaster extends EventEmitter { | ||
constructor(port, cryptKey, child) { | ||
constructor(port, keyPair, child) { | ||
super (); | ||
let socketIo = new SocketIo (); | ||
// Create new server using cryptotranny | ||
let server = new TrannyServer(keyPair, port); | ||
socketIo.on ('connection', (socket) => { | ||
let clientRpc = new ServerSocketRpc (socket, cryptKey, child); | ||
// Listen for new clients | ||
server.on ('client', (client) => { | ||
// Wrap client in RPC | ||
let clientRpc = new ServerSocketRpc (client, child); | ||
// Emit new client | ||
this.emit ('client', clientRpc); | ||
}); | ||
socketIo.listen (port); | ||
} | ||
@@ -294,52 +286,44 @@ } | ||
class ClientSocketInterface extends EventEmitter { | ||
constructor (host, port, cryptKey) { | ||
constructor (host, port, keyPair, remotePk, onDisconnect) { | ||
// Create parent event emitter | ||
super (); | ||
let socket = SocketIoClient ('http://' + host + ':' + port); | ||
// Create new connection to specified host using cryptotranny | ||
let server = new TrannyClient(host, port, keyPair, remotePk) | ||
this._cryptKey = cryptKey; | ||
this._socket = socket; | ||
// Set private properties | ||
this._server = server; | ||
this._onDisconnect = onDisconnect; | ||
// Listen | ||
this._listen (); | ||
} | ||
// Listen for all events | ||
_listen () { | ||
this._socket.on ('message', (data) => { | ||
// silently ignore bad data | ||
if (data.nonce == null || data.encrypted == null) return; | ||
this._server.on ('message', (data) => { | ||
let decoded = JSON.parse (data.toString ()); | ||
// open nacl secret box | ||
let decrypted = nacl.secretbox.open (Buffer.from (data.encrypted, 'base64'), Buffer.from (data.nonce, 'base64'), this._cryptKey); | ||
// silently ignore bad crypt, TODO: handle | ||
if (decrypted == null) return; | ||
// decode JSON message | ||
let decoded = JSON.parse (Buffer.from (decrypted).toString ()); | ||
// emit event | ||
this.emit (decoded.id, decoded.data); | ||
}); | ||
this._server.on ('disconnected', () => { | ||
this._onDisconnect (); | ||
}); | ||
} | ||
// Send event to remote | ||
send (id, data) { | ||
// Encode object to JSON | ||
let encoded = JSON.stringify ({ | ||
let encoded = Buffer.from (JSON.stringify ({ | ||
'id' : id, | ||
'data' : data, | ||
}); | ||
})); | ||
// Generate a random nonce for cryptography | ||
let nonce = nacl.randomBytes (nacl.box.nonceLength) | ||
this._server.send (encoded); | ||
} | ||
// Encrypt encoded object using nonce, hardcoded server public key and our secret key | ||
let encrypted = nacl.secretbox (Buffer.from (encoded), nonce, this._cryptKey) | ||
// Send nonce, from secret key, and encrypted data over IPC | ||
this._socket.emit ('message', { | ||
'nonce' : Buffer.from (nonce).toString ('base64'), | ||
'encrypted' : Buffer.from (encrypted).toString ('base64') | ||
}); | ||
// Destroy underlying connection | ||
destroy () { | ||
this._server.destroy (); | ||
} | ||
@@ -349,5 +333,7 @@ } | ||
class ClientSocketRpc extends Rpc { | ||
constructor (host, port, cryptKey, client) { | ||
constructor (host, port, keyPair, remotePk, client) { | ||
// Create client IPC interface for provided namespace | ||
let socketInterface = new ClientSocketInterface (host, port, cryptKey); | ||
let socketInterface = new ClientSocketInterface (host, port, keyPair, remotePk, () => { | ||
this._onDisconnect (); | ||
}); | ||
@@ -360,2 +346,2 @@ // Create parent RPC module using interface as stream | ||
// Export classes | ||
exports = module.exports = { Rpc, ClientSocketRpc, ServerSocketRpcMaster }; | ||
exports = module.exports = { Rpc, ClientSocketRpc, ServerSocketRpcMaster, genSocketKeyPair }; |
{ | ||
"name": "arc-rpc", | ||
"version": "1.3.5", | ||
"version": "1.3.6", | ||
"description": "Asynchronous Remote Classes make RPC simple", | ||
"main": "index.js", | ||
"dependencies": { | ||
"socket.io": "^2.0.3", | ||
"socket.io-client": "^2.0.3", | ||
"cryptotranny": "^1.0.1", | ||
"tweetnacl": "^1.0.0", | ||
@@ -10,0 +9,0 @@ "uuid": "^3.0.1" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3
11083
271
+ Addedcryptotranny@^1.0.1
+ Addedbuffer-alloc-unsafe@1.1.0(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addedcryptotranny@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedlength-prefixed-stream@1.6.0(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedvarint@5.0.2(transitive)
- Removedsocket.io@^2.0.3
- Removedsocket.io-client@^2.0.3
- Removedaccepts@1.3.8(transitive)
- Removedafter@0.8.2(transitive)
- Removedarraybuffer.slice@0.0.7(transitive)
- Removedbacko2@1.0.2(transitive)
- Removedbase64-arraybuffer@0.1.4(transitive)
- Removedbase64id@2.0.0(transitive)
- Removedblob@0.0.5(transitive)
- Removedcomponent-bind@1.0.0(transitive)
- Removedcomponent-emitter@1.2.11.3.1(transitive)
- Removedcomponent-inherit@0.0.3(transitive)
- Removedcookie@0.4.2(transitive)
- Removeddebug@3.1.04.1.1(transitive)
- Removedengine.io@3.6.2(transitive)
- Removedengine.io-client@3.5.4(transitive)
- Removedengine.io-parser@2.2.1(transitive)
- Removedhas-binary2@1.0.3(transitive)
- Removedhas-cors@1.1.0(transitive)
- Removedindexof@0.0.1(transitive)
- Removedisarray@2.0.1(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedms@2.0.02.1.3(transitive)
- Removednegotiator@0.6.3(transitive)
- Removedparseqs@0.0.6(transitive)
- Removedparseuri@0.0.6(transitive)
- Removedsocket.io@2.5.1(transitive)
- Removedsocket.io-adapter@1.1.2(transitive)
- Removedsocket.io-client@2.5.0(transitive)
- Removedsocket.io-parser@3.3.43.4.3(transitive)
- Removedto-array@0.1.4(transitive)
- Removedws@7.5.10(transitive)
- Removedxmlhttprequest-ssl@1.6.3(transitive)
- Removedyeast@0.1.2(transitive)