Updates PacketBuilder to accept key from user settings and general code cleanup

This commit is contained in:
Sandy Milne 2017-10-23 20:25:59 +11:00
parent 2092ead277
commit 7f9b58216c
4 changed files with 23 additions and 34 deletions

View File

@ -6,7 +6,8 @@ const httpPort = 3000;
// Create a settings object to pass PK key and map sockets to names
const settings = {
ORVIBO_KEY: '', // put your PK key here as plain text
LOG_PACKET: false, //Show incoming packet data from the socket
ORVIBO_KEY: '', // put your PK key here as plain text (See Readme)
plugInfo : [
// Add uid and a name so you can easily identify the connected sockets
{
@ -29,7 +30,7 @@ orvbio.on('plugStateUpdated', ({uid, state , name}) => {
});
// The plug sends a hearbeat to let the server know it's still alive
orvbio.on('gotHeartbeat', ({uid, state , name}) => {
orvbio.on('gotHeartbeat', ({uid, name}) => {
console.log(`Plug ${name} ${uid} sent heartbeat`);
});
@ -66,7 +67,7 @@ const requestHandler = (request, response) => {
response.end(JSON.stringify(sockets));
};
const httpServer = http.createServer(requestHandler)
const httpServer = http.createServer(requestHandler);
httpServer.listen(httpPort, (err) => {
if (err) {

View File

@ -1,24 +1,20 @@
const net = require('net');
const util = require('util');
const Packet = require('./OrviboPacket');
const PacketBuilder = require('./PacketBuilder');
const Utils = require('./Utils');
const Settings = require('./OrviboSettings');
const EventEmitter = require('events').EventEmitter;
let socketSettings = Settings;
let ORVIBO_KEY = socketSettings.ORVIBO_KEY;
let LOG_PACKET = socketSettings.LOG_PACKET;
let ORVIBO_KEY = Settings.ORVIBO_KEY;
let LOG_PACKET = Settings.LOG_PACKET;
let PLUG_INFO = Settings.plugInfo;
const Orvibo = function(userSettings) {
// Allow user to pass in settings
if (userSettings != null) {
socketSettings = userSettings;
ORVIBO_KEY = socketSettings.ORVIBO_KEY;
LOG_PACKET = socketSettings.LOG_PACKET;
ORVIBO_KEY = userSettings.ORVIBO_KEY;
LOG_PACKET = userSettings.LOG_PACKET;
PLUG_INFO = userSettings.plugInfo;
}
if (ORVIBO_KEY === '') {
@ -66,6 +62,7 @@ let helloHandler = (plugPacket, socket) => {
encryptionKey: Utils.generateRandomTextValue(16),
id: Utils.generateRandomHexValue(32),
modelId: plugPacket.getModelId(),
orviboKey: plugPacket.getOrviboKey()
};
respondAndSetData(pkData, socket, PacketBuilder.helloPacket);
};

View File

@ -11,10 +11,6 @@ let pkt = function(packetBuffer) {
};
pkt.prototype.logPacket = function(type) {
let output = {
PacketId : this.packetIdText(),
Payload : this.payloadJSON
};
console.log(type, JSON.stringify(this.payloadJSON));
};
@ -40,10 +36,11 @@ pkt.prototype.getModelId = function() {
pkt.prototype.processPacket = function(key) {
this.payloadJSON = this.decodeJSON(key);
this.orviboKey = key;
};
pkt.prototype.getKeyValue = function() {
return this.payloadJSON.key;
pkt.prototype.getOrviboKey = function() {
return this.orviboKey;
};
pkt.prototype.validCRC = function() {
@ -54,10 +51,6 @@ pkt.prototype.packetTypeText = function() {
return this.packetType.toString('ascii');
};
pkt.prototype.packetIdText = function() {
return this.packetId.toString('ascii');
};
pkt.prototype.decodeJSON = function(key) {
let decipher = crypto.createDecipheriv('aes-128-ecb', key, '');
decipher.setAutoPadding(true);

View File

@ -1,11 +1,8 @@
const crc32 = require('buffer-crc32');
const crypto = require('crypto');
const Settings = require('./OrviboSettings');
const ORVIBO_KEY = Settings.ORVIBO_KEY;
let Packet = {
encryptionKey : ORVIBO_KEY,
encryptionKey : '',
magic: new Buffer("6864", 'hex'),
build: function () {
let packetId = new Buffer(this.id, 'ascii');
@ -24,7 +21,7 @@ let DKPacket = Object.assign({}, Packet, {
packetType: new Buffer('dk', 'ascii'),
});
let helloPacket = function({ serial, encryptionKey, id }) {
let helloPacket = function({ serial, encryptionKey, id, orviboKey }) {
let json = {
cmd: 0,
status: 0,
@ -34,7 +31,8 @@ let helloPacket = function({ serial, encryptionKey, id }) {
let pkt = Object.assign(Object.create(PKPacket), {
json: json,
id: id
id: id,
encryptionKey: orviboKey,
});
return pkt.build();
@ -51,7 +49,7 @@ let handshakePacket = function({ serial, encryptionKey, id }) {
let pkt = Object.assign(Object.create(DKPacket), {
json: json,
id: id,
encryptionKey, encryptionKey
encryptionKey: encryptionKey
});
return pkt.build();
@ -69,7 +67,7 @@ let heartbeatPacket = function({serial, uid, encryptionKey, id}) {
let pkt = Object.assign(Object.create(DKPacket), {
json: json,
id: id,
encryptionKey, encryptionKey
encryptionKey,
});
return pkt.build();
@ -77,7 +75,7 @@ let heartbeatPacket = function({serial, uid, encryptionKey, id}) {
let comfirmStatePacket = function({serial, uid, state, encryptionKey, id}) {
var json = {
let json = {
uid: uid,
cmd: 42,
statusType: 0,
@ -95,7 +93,7 @@ let comfirmStatePacket = function({serial, uid, state, encryptionKey, id}) {
let pkt = Object.assign(Object.create(DKPacket), {
json: json,
id: id,
encryptionKey, encryptionKey
encryptionKey,
});
return pkt.build();
@ -113,7 +111,7 @@ let defaultPacket = function({serial, uid, cmd, id, encryptionKey}) {
let pkt = Object.assign(Object.create(DKPacket), {
json: json,
id: id,
encryptionKey, encryptionKey
encryptionKey,
});
return pkt.build();
@ -140,7 +138,7 @@ let updatePacket = function({ uid, state, serial, id, clientSessionId , deviceId
let pkt = Object.assign(Object.create(DKPacket), {
json: json,
id: id,
encryptionKey, encryptionKey
encryptionKey,
});
return pkt.build();