Skip to content

Small directory structure change + external configurability #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Changelog
=========

- 0.1.6 - Move npm package to repository root level to make it referencable via github URL, Make all server settings configurable
- 0.1.5 - cleaning up connection key logging, adding early data patch
- 0.1.4 - adding verbose logging option
- 0.1.3 - adding OTA size workaround
- 0.0.2 - Working alpha version, needs refactor for API wrapper
- 0.0.1 - Initial imports and cleanup of base classes
File renamed without changes.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@ What code modules should I start with?
There's lots of fun stuff here, but in particular you should know about the "SparkCore" ( https://github.com/spark/spark-protocol/blob/master/js/clients/SparkCore.js ) , and "DeviceServer" ( https://github.com/spark/spark-protocol/blob/master/js/server/DeviceServer.js ) modules. The "DeviceServer" module runs a server that creates "SparkCore" objects, which represent your connected devices.



Keys!
====================

Create your keys

Keep your private key secret

Wire up your server public key into your settings / module

Create a special version of your key with the server DNS name / IP address
Copy your server public key to your core

Copy your core public key into the server


Running the server
====================

Startup a tcp server on the port of your choosing, and startup a SparkCore object with each new socket that's opened


How do I start a server in code?
---------------------------

Expand Down
File renamed without changes.
File renamed without changes.
12 changes: 6 additions & 6 deletions js/clients/SparkCore.js → clients/SparkCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ SparkCore.prototype = extend(ISparkCore.prototype, EventEmitter.prototype, {
msg._type = this.getResponseType(msg.getTokenString());
}

//console.log("core got message of type " + msg._type + " with token " + msg.getTokenString() + " " + messages.getRequestType(msg));
console.log("core got message of type " + msg._type + " with token " + msg.getTokenString() + " " + messages.getRequestType(msg));

if (msg.isAcknowledgement()) {
if (!msg._type) {
Expand All @@ -378,7 +378,7 @@ SparkCore.prototype = extend(ISparkCore.prototype, EventEmitter.prototype, {
if (msg.isEmpty() && msg.isConfirmable()) {
this._lastCorePing = new Date();
//var delta = (this._lastCorePing - this._connStartTime) / 1000.0;
//logger.log("core ping @ ", delta, " seconds ", { coreID: this.getHexCoreID() });
logger.log("core ping @ ", delta, " seconds ", { coreID: this.getHexCoreID() });
this.sendReply("PingAck", msg.getId());
return;
}
Expand All @@ -392,7 +392,7 @@ SparkCore.prototype = extend(ISparkCore.prototype, EventEmitter.prototype, {
return;
}

//this.sendMessage("Ignored", null, {}, null, null);
// this.sendMessage("Ignored", null, {}, null, null);
this.disconnect("Bad Counter");
return;
}
Expand Down Expand Up @@ -420,7 +420,7 @@ SparkCore.prototype = extend(ISparkCore.prototype, EventEmitter.prototype, {
return;
}
this.secureOut.write(msg, null, null);
//logger.log("Replied with message of type: ", name, " containing ", data);
logger.log("Replied with message of type: ", name, " containing ", data);
},


Expand All @@ -442,8 +442,8 @@ SparkCore.prototype = extend(ISparkCore.prototype, EventEmitter.prototype, {
return;
}
this.secureOut.write(msg, null, null);
// logger.log("Sent message of type: ", name, " containing ", data,
// "BYTES: " + msg.toString('hex'));
logger.log("Sent message of type: ", name, " containing ", data,
"BYTES: " + msg.toString('hex'));

return token;
},
Expand Down
File renamed without changes.
2 changes: 0 additions & 2 deletions js/.gitignore

This file was deleted.

165 changes: 0 additions & 165 deletions js/LICENSE.txt

This file was deleted.

10 changes: 0 additions & 10 deletions js/README.md

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
32 changes: 16 additions & 16 deletions js/lib/Handshake.js → lib/Handshake.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,16 +259,16 @@ Handshake.prototype = extend(IHandshake.prototype, {
//logger.log("onSocketData: ", data.toString('hex'));

if (!this.secureIn) {
//logger.log('!secureIn');
logger.log('!secureIn');
this.nextStep(data);
}
else {
if (this.useChunkingStream && this.chunkingIn) {
//logger.log('chunkingIn');
logger.log('chunkingIn');
this.chunkingIn.write(data);
}
else {
//logger.log('secureIn');
logger.log('secureIn');
this.secureIn.write(data);
}
}
Expand Down Expand Up @@ -299,9 +299,9 @@ Handshake.prototype = extend(IHandshake.prototype, {
that.nonce = buf;
that.socket.write(buf);

// if (buf) {
// logger.log("sent nonce ", buf.toString('hex'));
// }
if (buf) {
logger.log("sent nonce ", buf.toString('hex'));
}

//a good start
that.nextStep();
Expand Down Expand Up @@ -354,7 +354,7 @@ Handshake.prototype = extend(IHandshake.prototype, {
return;
}

//logger.log("read_coreid decrypted to ", plaintext.toString('hex'));
logger.log("read_coreid decrypted to ", plaintext.toString('hex'));

//success
//plaintext should be 52 bytes, else fail
Expand All @@ -372,7 +372,7 @@ Handshake.prototype = extend(IHandshake.prototype, {
if (plaintext.length > (Handshake.NONCE_BYTES + Handshake.ID_BYTES)) {
var coreKey = new Buffer(plaintext.length - 52);
plaintext.copy(coreKey, 0, 52, plaintext.length);
//console.log("got key ", coreKey.toString('hex'));
console.log("got key ", coreKey.toString('hex'));
this.coreProvidedPem = utilities.convertDERtoPEM(coreKey);
}

Expand All @@ -384,7 +384,7 @@ Handshake.prototype = extend(IHandshake.prototype, {

//sweet!
that.coreID = vCoreID.toString('hex');
//logger.log("core reported coreID: " + that.coreID);
logger.log("core reported coreID: " + that.coreID);

that.stage++;
that.nextStep();
Expand Down Expand Up @@ -447,12 +447,12 @@ Handshake.prototype = extend(IHandshake.prototype, {
var signedhmac = CryptoLib.sign(null, hash);

//Server sends ~384 bytes to Core: the ciphertext then the signature.
//logger.log("server: ciphertext was :", ciphertext.toString('hex'));
//console.log("signature block was: " + signedhmac.toString('hex'));
logger.log("server: ciphertext was :", ciphertext.toString('hex'));
console.log("signature block was: " + signedhmac.toString('hex'));

var msg = Buffer.concat([ciphertext, signedhmac], ciphertext.length + signedhmac.length);
that.socket.write(msg);
//logger.log('Handshake: sent encrypted sessionKey');
logger.log('Handshake: sent encrypted sessionKey');

that.secureIn = CryptoLib.CreateAESDecipherStream(that.sessionKey);
that.secureOut = CryptoLib.CreateAESCipherStream(that.sessionKey);
Expand Down Expand Up @@ -506,7 +506,7 @@ Handshake.prototype = extend(IHandshake.prototype, {
}

//waiting on data.
//logger.log("server: waiting on hello");
logger.log("server: waiting on hello");
this._socketTimeout = setTimeout(function () {
that.handshakeFail("get_hello timed out");
}, 30 * 1000);
Expand All @@ -522,7 +522,7 @@ Handshake.prototype = extend(IHandshake.prototype, {
return;
}
this.client.recvCounter = msg.getId();
//logger.log("server: got a good hello! Counter was: " + msg.getId());
logger.log("server: got a good hello! Counter was: " + msg.getId());

try {
if (msg.getPayload) {
Expand All @@ -531,7 +531,7 @@ Handshake.prototype = extend(IHandshake.prototype, {
var r = new buffers.BufferReader(payload);
this.client.spark_product_id = r.shiftUInt16();
this.client.product_firmware_version = r.shiftUInt16();
//logger.log('version of core firmware is ', this.client.spark_product_id, this.client.product_firmware_version);
logger.log('version of core firmware is ', this.client.spark_product_id, this.client.product_firmware_version);
}
}
else {
Expand Down Expand Up @@ -565,7 +565,7 @@ Handshake.prototype = extend(IHandshake.prototype, {
*/
send_hello: function () {
//client will set the counter property on the message
//logger.log("server: send hello");
logger.log("server: send hello");
this.client.secureOut = this.secureOut;
this.client.sendCounter = CryptoLib.getRandomUINT16();
this.client.sendMessage("Hello", {}, null, null);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 4 additions & 2 deletions js/server/DeviceServer.js → server/DeviceServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ var fs = require('fs');
var DeviceServer = function (options) {
this.options = options;
this.options = options || {};
settings.coreKeysDir = this.options.coreKeysDir = this.options.coreKeysDir || settings.coreKeysDir;
for (property in settings) {
settings[property] = this.options[property] = this.options[property] || settings[property];
}

this._allCoresByID = {};
this._attribsByID = {};
Expand Down Expand Up @@ -277,4 +279,4 @@ DeviceServer.prototype = {
}

};
module.exports = DeviceServer;
module.exports = DeviceServer;
File renamed without changes.
File renamed without changes.