From 66472b0efe5d3daa5cff989f6739e8cf25d37b6f Mon Sep 17 00:00:00 2001 From: Tautvydas Belgeras Date: Fri, 18 Jan 2019 11:58:58 +0200 Subject: [PATCH 1/8] Changed coap agent, API and coap package --- 8dev_emulate/nodes/clientNodeInstance.js | 6 +++--- 8dev_emulate/nodes/sensorInstances.js | 16 ++++++++-------- package.json | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/8dev_emulate/nodes/clientNodeInstance.js b/8dev_emulate/nodes/clientNodeInstance.js index 0497cbc..37aa76e 100644 --- a/8dev_emulate/nodes/clientNodeInstance.js +++ b/8dev_emulate/nodes/clientNodeInstance.js @@ -41,7 +41,7 @@ function Interval(callback, delay) { } class ClientNodeInstance extends EventEmitter { - constructor(lifetime, manufacturer, model, queueMode, endpointClientName, serverURI, clientPort) { + constructor(lifetime, manufacturer, model, queueMode, endpointClientName, serverURI, clientPort, serverPort) { super(); this._state = 'stopped'; @@ -57,10 +57,10 @@ class ClientNodeInstance extends EventEmitter { this.requestListener(req, res); }); this.coapServer.listen(clientPort); - this.coapAgent = new coap.Agent({ type: 'udp6', socket: this.coapServer._sock }); + this.coapAgent = clientPort; this.requestOptions = { host: serverURI, - port: 5555, + port: serverPort, method: 'POST', confirmable: 'true', agent: this.coapAgent, diff --git a/8dev_emulate/nodes/sensorInstances.js b/8dev_emulate/nodes/sensorInstances.js index 1a63aaf..cf880aa 100644 --- a/8dev_emulate/nodes/sensorInstances.js +++ b/8dev_emulate/nodes/sensorInstances.js @@ -35,8 +35,8 @@ function humiditySensorHandler(averageHumidity = 70, fluctuation = 0.05) { class Sensor3700 extends ClientNode { - constructor(lifetime, UUID, serverIP, clientPort) { - super(lifetime, '8devices', '8dev_3700', true, UUID, serverIP, clientPort); + constructor(lifetime, UUID, serverIP, clientPort, serverPort) { + super(lifetime, '8devices', '8dev_3700', true, UUID, serverIP, clientPort, serverPort); this.timeInitialisation = new Date().getTime(); @@ -86,8 +86,8 @@ class Sensor3700 extends ClientNode { } class Sensor3800 extends ClientNode { - constructor(lifetime, UUID, serverIP, clientPort) { - super(lifetime, '8devices', '8dev_3800', true, UUID, serverIP, clientPort); + constructor(lifetime, UUID, serverIP, clientPort, serverPort) { + super(lifetime, '8devices', '8dev_3800', true, UUID, serverIP, clientPort, serverPort); this.createObject(3303, 0); this.createObject(3304, 0); @@ -99,8 +99,8 @@ class Sensor3800 extends ClientNode { } class Sensor4400 extends ClientNode { - constructor(lifetime, UUID, serverIP, clientPort) { - super(lifetime, '8devices', '8dev_4400', true, UUID, serverIP, clientPort); + constructor(lifetime, UUID, serverIP, clientPort, serverPort) { + super(lifetime, '8devices', '8dev_4400', true, UUID, serverIP, clientPort, serverPort); this.createObject(3200, 0); this.createObject(3303, 0); @@ -129,8 +129,8 @@ class Sensor4400 extends ClientNode { } class Sensor4500 extends ClientNode { - constructor(lifetime, UUID, serverIP, clientPort) { - super(lifetime, '8devices', '8dev_4500', true, UUID, serverIP, clientPort); + constructor(lifetime, UUID, serverIP, clientPort, serverPort) { + super(lifetime, '8devices', '8dev_4500', true, UUID, serverIP, clientPort, serverPort); this.createObject(3202, 0); diff --git a/package.json b/package.json index 329d603..67386fd 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ }, "homepage": "https://github.com/8devices/restserver-api#readme", "dependencies": { - "coap": "^0.21.0", + "coap": "git+https://github.com/GiedriusM/node-coap.git#pr-server-custom-socket", "node-rest-client": "^3.1.0" }, "devDependencies": { From b19d9718b60bdc75ec1009bce074ce7a3a621667 Mon Sep 17 00:00:00 2001 From: Tautvydas Belgeras Date: Fri, 18 Jan 2019 16:47:31 +0200 Subject: [PATCH 2/8] Add support for secure client node creation --- 8dev_emulate/nodes/clientNodeInstance.js | 36 +++++++++++++++++------- 8dev_emulate/nodes/sensorInstances.js | 16 +++++------ 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/8dev_emulate/nodes/clientNodeInstance.js b/8dev_emulate/nodes/clientNodeInstance.js index 37aa76e..cfb45bb 100644 --- a/8dev_emulate/nodes/clientNodeInstance.js +++ b/8dev_emulate/nodes/clientNodeInstance.js @@ -4,6 +4,7 @@ const coap = require('coap'); const EventEmitter = require('events'); const ObjectInstance = require('./objectInstance.js'); const { RESOURCE_TYPE } = require('./resourceInstance.js'); +const mbedtls = require('node-mbedtls'); const DATE = new Date(); const LWM2M_VERSION = '1.0'; @@ -41,7 +42,7 @@ function Interval(callback, delay) { } class ClientNodeInstance extends EventEmitter { - constructor(lifetime, manufacturer, model, queueMode, endpointClientName, serverURI, clientPort, serverPort) { + constructor(options) { super(); this._state = 'stopped'; @@ -49,18 +50,33 @@ class ClientNodeInstance extends EventEmitter { this.updatesIterator = {}; this.observedResources = {}; this.registrationPath = '/rd'; - this.listeningPort = clientPort; + this.listeningPort = options.clientPort; this.updatesInterval = 10; // updates interval in seconds - this.endpointClientName = endpointClientName; + this.endpointClientName = options.endpointClientName; this.coapServer = coap.createServer({ type: 'udp6' }, (req, res) => { this.requestListener(req, res); }); - this.coapServer.listen(clientPort); - this.coapAgent = clientPort; + if (options.cacert) { + this.client = new mbedtls.Socket(); + this.client.ssl_config.ca_chain(options.cacert, null); + this.client.ssl_config.own_cert(options.cacert, options.pk_key); + this.client.ssl_config.authmode(options.authmode); + this.client.ssl_config.ciphersuites(options.ciphersuites); + this.coapAgent = new coap.Agent({ + socket: this.client, + }); + this.coapServer.listen(this.coapAgent); + } else { + this.coapServer.listen(options.clientPort); + this.coapAgent = new coap.Agent({ + type: 'udp6', + socket: this.coapServer._sock, + }); + } this.requestOptions = { - host: serverURI, - port: serverPort, + host: options.serverURI, + port: options.serverPort, method: 'POST', confirmable: 'true', agent: this.coapAgent, @@ -68,10 +84,10 @@ class ClientNodeInstance extends EventEmitter { this.stateListener(); - this.initiateSecurityObject(serverURI); - this.initiateServerObject(lifetime, queueMode); + this.initiateSecurityObject(options.serverURI); + this.initiateServerObject(options.lifetime, options.queueMode); this.initiateAccessControlObject(); - this.initiateDeviceObject(manufacturer, model, queueMode); + this.initiateDeviceObject(options.manufacturer, options.model, options.queueMode); this.initiateConnectivityMonitoringObject(); this.initiateFirmwareObject(); this.initiateLocationObject(); diff --git a/8dev_emulate/nodes/sensorInstances.js b/8dev_emulate/nodes/sensorInstances.js index cf880aa..1ef7b81 100644 --- a/8dev_emulate/nodes/sensorInstances.js +++ b/8dev_emulate/nodes/sensorInstances.js @@ -35,8 +35,8 @@ function humiditySensorHandler(averageHumidity = 70, fluctuation = 0.05) { class Sensor3700 extends ClientNode { - constructor(lifetime, UUID, serverIP, clientPort, serverPort) { - super(lifetime, '8devices', '8dev_3700', true, UUID, serverIP, clientPort, serverPort); + constructor(options) { + super(options); this.timeInitialisation = new Date().getTime(); @@ -86,8 +86,8 @@ class Sensor3700 extends ClientNode { } class Sensor3800 extends ClientNode { - constructor(lifetime, UUID, serverIP, clientPort, serverPort) { - super(lifetime, '8devices', '8dev_3800', true, UUID, serverIP, clientPort, serverPort); + constructor(options) { + super(options); this.createObject(3303, 0); this.createObject(3304, 0); @@ -99,8 +99,8 @@ class Sensor3800 extends ClientNode { } class Sensor4400 extends ClientNode { - constructor(lifetime, UUID, serverIP, clientPort, serverPort) { - super(lifetime, '8devices', '8dev_4400', true, UUID, serverIP, clientPort, serverPort); + constructor(options) { + super(options); this.createObject(3200, 0); this.createObject(3303, 0); @@ -129,8 +129,8 @@ class Sensor4400 extends ClientNode { } class Sensor4500 extends ClientNode { - constructor(lifetime, UUID, serverIP, clientPort, serverPort) { - super(lifetime, '8devices', '8dev_4500', true, UUID, serverIP, clientPort, serverPort); + constructor(options) { + super(options); this.createObject(3202, 0); From d814829db99247d934439956ca4a8ead5906a233 Mon Sep 17 00:00:00 2001 From: Tautvydas Belgeras Date: Fri, 18 Jan 2019 17:02:04 +0200 Subject: [PATCH 3/8] Emit asynchronous event on dtls handshake finish --- 8dev_emulate/nodes/clientNodeInstance.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/8dev_emulate/nodes/clientNodeInstance.js b/8dev_emulate/nodes/clientNodeInstance.js index cfb45bb..3102884 100644 --- a/8dev_emulate/nodes/clientNodeInstance.js +++ b/8dev_emulate/nodes/clientNodeInstance.js @@ -67,6 +67,9 @@ class ClientNodeInstance extends EventEmitter { socket: this.client, }); this.coapServer.listen(this.coapAgent); + this.client.on('handshake', () => { + this.emit('handshake'); + }); } else { this.coapServer.listen(options.clientPort); this.coapAgent = new coap.Agent({ From aed8516f0368c6489e171b2b4ec1262eb821c18e Mon Sep 17 00:00:00 2001 From: Tautvydas Belgeras Date: Wed, 20 Feb 2019 11:47:41 +0200 Subject: [PATCH 4/8] Use new version for coap package --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 67386fd..485f62a 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ }, "homepage": "https://github.com/8devices/restserver-api#readme", "dependencies": { - "coap": "git+https://github.com/GiedriusM/node-coap.git#pr-server-custom-socket", + "coap": "git+https://github.com/mcollina/node-coap.git#v0.23.0", "node-rest-client": "^3.1.0" }, "devDependencies": { From 42448d2140c1514d6dcd116cd63740a6d71b2184 Mon Sep 17 00:00:00 2001 From: Tautvydas Belgeras Date: Wed, 20 Feb 2019 17:02:37 +0200 Subject: [PATCH 5/8] Fix after API changes --- 8dev_emulate/nodes/clientNodeInstance.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/8dev_emulate/nodes/clientNodeInstance.js b/8dev_emulate/nodes/clientNodeInstance.js index 3102884..27f5c67 100644 --- a/8dev_emulate/nodes/clientNodeInstance.js +++ b/8dev_emulate/nodes/clientNodeInstance.js @@ -58,7 +58,7 @@ class ClientNodeInstance extends EventEmitter { this.requestListener(req, res); }); if (options.cacert) { - this.client = new mbedtls.Socket(); + this.client = new mbedtls.Connection(options); this.client.ssl_config.ca_chain(options.cacert, null); this.client.ssl_config.own_cert(options.cacert, options.pk_key); this.client.ssl_config.authmode(options.authmode); From 4da0d5c892ad0d8989e59ca7fc9a8553c5527a99 Mon Sep 17 00:00:00 2001 From: Tautvydas Belgeras Date: Mon, 25 Feb 2019 12:09:07 +0200 Subject: [PATCH 6/8] Add PSK based DTLS support --- 8dev_emulate/nodes/clientNodeInstance.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/8dev_emulate/nodes/clientNodeInstance.js b/8dev_emulate/nodes/clientNodeInstance.js index 27f5c67..9f49767 100644 --- a/8dev_emulate/nodes/clientNodeInstance.js +++ b/8dev_emulate/nodes/clientNodeInstance.js @@ -57,7 +57,14 @@ class ClientNodeInstance extends EventEmitter { this.coapServer = coap.createServer({ type: 'udp6' }, (req, res) => { this.requestListener(req, res); }); - if (options.cacert) { + + if (!options.ciphersuites) { + this.coapServer.listen(options.clientPort); + this.coapAgent = new coap.Agent({ + type: 'udp6', + socket: this.coapServer._sock, + }); + } else if (options.ciphersuites[0] == 0xC0AE || options.ciphersuites[0] == 0xC023) { this.client = new mbedtls.Connection(options); this.client.ssl_config.ca_chain(options.cacert, null); this.client.ssl_config.own_cert(options.cacert, options.pk_key); @@ -70,13 +77,20 @@ class ClientNodeInstance extends EventEmitter { this.client.on('handshake', () => { this.emit('handshake'); }); - } else { - this.coapServer.listen(options.clientPort); + } else if (options.ciphersuites[0] == 0xC0A8 || options.ciphersuites[0] == 0x00AF) { + this.client = new mbedtls.Connection(options); + this.client.ssl_config.psk(options.pskIdentity, options.psk); + this.client.ssl_config.psk(options.psk, options.pskIdentity); + this.client.ssl_config.ciphersuites(options.ciphersuites); this.coapAgent = new coap.Agent({ - type: 'udp6', - socket: this.coapServer._sock, + socket: this.client, + }); + this.coapServer.listen(this.coapAgent); + this.client.on('handshake', () => { + this.emit('handshake'); }); } + this.requestOptions = { host: options.serverURI, port: options.serverPort, From 7e5f256d3e6be6e0ccf674771aa3a9009e09cc6e Mon Sep 17 00:00:00 2001 From: Tautvydas Belgeras Date: Mon, 25 Feb 2019 17:10:10 +0200 Subject: [PATCH 7/8] Remove leftover merge conflict output --- 8dev_emulate/nodes/clientNodeInstance.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/8dev_emulate/nodes/clientNodeInstance.js b/8dev_emulate/nodes/clientNodeInstance.js index 3ccc99b..a033fa8 100644 --- a/8dev_emulate/nodes/clientNodeInstance.js +++ b/8dev_emulate/nodes/clientNodeInstance.js @@ -120,7 +120,6 @@ class ClientNodeInstance extends EventEmitter { this.coapServer = coap.createServer({ type: 'udp6' }, (req, res) => { this.requestListener(req, res); }); -<<<<<<< HEAD if (!options.ciphersuites) { this.coapServer.listen(options.clientPort); @@ -155,13 +154,6 @@ class ClientNodeInstance extends EventEmitter { }); } -======= - this.coapServer.listen(clientPort); - this.coapAgent = new coap.Agent({ - type: 'udp6', - socket: this.coapServer._sock, // eslint-disable-line no-underscore-dangle - }); ->>>>>>> master this.requestOptions = { host: options.serverURI, port: options.serverPort, From 2fcafdefcc1463cf4955319bec14a004fda46b61 Mon Sep 17 00:00:00 2001 From: Tautvydas Belgeras Date: Wed, 27 Feb 2019 11:44:19 +0200 Subject: [PATCH 8/8] Move DTLS initialization to user scripts --- 8dev_emulate/nodes/clientNodeInstance.js | 45 +++++++----------------- 1 file changed, 12 insertions(+), 33 deletions(-) diff --git a/8dev_emulate/nodes/clientNodeInstance.js b/8dev_emulate/nodes/clientNodeInstance.js index a033fa8..a565e23 100644 --- a/8dev_emulate/nodes/clientNodeInstance.js +++ b/8dev_emulate/nodes/clientNodeInstance.js @@ -3,7 +3,6 @@ const EventEmitter = require('events'); const { ObjectInstance } = require('./objectInstance.js'); const { Resource } = require('./resourceInstance.js'); const { TLV } = require('../../lwm2m/index.js'); -const mbedtls = require('node-mbedtls'); const { getDictionaryByValue } = TLV; const LWM2M_VERSION = '1.0'; @@ -120,39 +119,18 @@ class ClientNodeInstance extends EventEmitter { this.coapServer = coap.createServer({ type: 'udp6' }, (req, res) => { this.requestListener(req, res); }); + this.coapServer.listen(options.clientPort); + this.coapAgent = new coap.Agent({ + type: 'udp6', + socket: this.coapServer._sock, // eslint-disable-line no-underscore-dangle + }); - if (!options.ciphersuites) { - this.coapServer.listen(options.clientPort); - this.coapAgent = new coap.Agent({ - type: 'udp6', - socket: this.coapServer._sock, - }); - } else if (options.ciphersuites[0] == 0xC0AE || options.ciphersuites[0] == 0xC023) { - this.client = new mbedtls.Connection(options); - this.client.ssl_config.ca_chain(options.cacert, null); - this.client.ssl_config.own_cert(options.cacert, options.pk_key); - this.client.ssl_config.authmode(options.authmode); - this.client.ssl_config.ciphersuites(options.ciphersuites); - this.coapAgent = new coap.Agent({ - socket: this.client, - }); - this.coapServer.listen(this.coapAgent); - this.client.on('handshake', () => { - this.emit('handshake'); - }); - } else if (options.ciphersuites[0] == 0xC0A8 || options.ciphersuites[0] == 0x00AF) { - this.client = new mbedtls.Connection(options); - this.client.ssl_config.psk(options.pskIdentity, options.psk); - this.client.ssl_config.psk(options.psk, options.pskIdentity); - this.client.ssl_config.ciphersuites(options.ciphersuites); - this.coapAgent = new coap.Agent({ - socket: this.client, - }); - this.coapServer.listen(this.coapAgent); - this.client.on('handshake', () => { - this.emit('handshake'); - }); - } + this.coapAgent.on('error', (error) => { + this.emit('error', error); + }); + this.coapServer.on('error', (error) => { + this.emit('error', error); + }); this.requestOptions = { host: options.serverURI, @@ -817,6 +795,7 @@ class ClientNodeInstance extends EventEmitter { break; } case 'registered': { + this.emit('registered'); break; } default: {