-
Notifications
You must be signed in to change notification settings - Fork 61
/
coap.js
266 lines (225 loc) · 7.28 KB
/
coap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
'use strict'
/*******************************************************************************
* Copyright (c) 2013-2017 Matteo Collina
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http:https://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http:https://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Matteo Collina - initial API and implementation and/or initial documentation
* Jovan Kostovski - added standard js for source code style checking
*******************************************************************************/
/**
* CoAP Module
* @description This module handles the CoAP requests
* @module
*/
var coap = require('coap')
var rRegexp = /^\/r\/(.+)$/
var callback = require('callback-stream')
/**
* [CoAP description]
* @param {Object} opts
* @param {Function} done [description]
*/
function CoAP (opts, done) {
if (!(this instanceof CoAP)) {
return new CoAP(opts, done)
}
if (typeof opts === 'function') {
done = opts
opts = {}
}
var that = this
this._persistence = opts.ponte.persistence
this._broker = opts.ponte.broker
this._ponte = opts.ponte
if (typeof opts.authenticate === 'function') {
this.authenticate = opts.authenticate
}
if (typeof opts.authorizeGet === 'function') {
this.authorizeGet = opts.authorizeGet
}
if (typeof opts.authorizePut === 'function') {
this.authorizePut = opts.authorizePut
}
var logger = this._logger = opts.ponte.logger.child({ service: 'CoAP' })
this.server = coap.createServer(function handler (req, res) {
var match = req.url.match(rRegexp)
var topic
req.on('error', function (err) {
logger.info(err)
})
res.on('error', function (err) {
logger.info(err)
})
logger.info({ url: req.url, code: req.code, sender: req.rsinfo, headers: req.headers }, 'request received')
if (match) {
topic = match[1]
that.authenticate(req, function (err, authenticated, subject) {
if (err) {
that._handleAuthError(err, res)
return
}
if (!authenticated) {
that._handleNotAuthenticated(res)
return
}
if (req.method === 'GET') {
that.authorizeGet(subject, topic, function (err, authorized) {
if (err) {
that._handleAuthError(err, res)
return
}
if (!authorized) {
that._handleNotAuthorized(res)
return
}
that._handleGET(topic, req, res)
})
} else if (req.method === 'PUT') {
req.pipe(callback(function (err, payload) {
if (err) {
logger.error(err)
return
}
payload = Buffer.concat(payload)
that.authorizePut(subject, topic, payload, function (err, authorized) {
if (err) {
that._handleAuthError(err, res)
return
}
if (!authorized) {
that._handleNotAuthorized(res)
return
}
that._handlePUT(topic, payload, res)
})
}))
}
})
} else {
res.statusCode = '4.04'
res.end()
}
})
this.server.listen(opts.port, opts.host, function (err) {
done(err, that)
logger.info({ port: opts.port }, 'server started')
})
}
/**
* Use this function to close the the CoAP server
* @param {Function} done callback to be called
* @return {[type]} [description]
*/
CoAP.prototype.close = function (done) {
this.server.close(done)
}
/**
* Handles the GET requests
* @param {string} topic The topic of the message
* @param {object} req the request object
* @param {object} res the response object
* @return {[type]} [description]
*/
CoAP.prototype._handleGET = function (topic, req, res) {
var that = this
var deliver = 'end'
var logger = this._logger
var cb = function (topic, payload) {
logger.debug({ url: req.url, code: req.code, sender: req.rsinfo }, 'sending update')
res.write(payload)
}
that._persistence.lookupRetained(topic, function (err, packets) {
if (err) {
logger.error(err)
return
}
if (packets.length === 0) {
logger.info({ url: req.url, code: req.code, sender: req.rsinfo }, 'not found')
res.statusCode = '4.04'
return res.end()
}
if (req.headers.Observe === 0) {
logger.debug({ url: req.url, code: req.code, sender: req.rsinfo }, 'registering for topic')
deliver = 'write'
that._broker.subscribe(topic, cb)
req.on('error', function () {
that._broker.unsubscribe(topic, cb)
})
res.on('finish', function () {
that._broker.unsubscribe(topic, cb)
})
}
logger.debug({ url: req.url, code: req.code, sender: req.rsinfo }, 'delivering retained')
res[deliver](packets[0].payload)
})
}
CoAP.prototype._handlePUT = function (topic, payload, res) {
var that = this
var packet = { topic: topic, payload: payload, retain: true }
that._persistence.storeRetained(packet, function () {
that._broker.publish(topic, payload, {}, function () {
res.setOption('Location-Path', '/r/' + topic)
res.statusCode = '2.04'
res.end()
that._ponte.emit('updated', topic, payload)
})
})
}
/**
* The function that will be used to authenticate requests.
* This default implementation authenticates everybody.
* The returned subject is just a new Object.
*
* @param {Object} req The incoming message @link https://github.com/mcollina/node-coap#incoming
* @param {Function} cb The callback function. Has the following structure: cb(err, authenticated, subject)
*/
CoAP.prototype.authenticate = function (req, cb) {
cb(null, true, {})
}
/**
* The function that will be used to authorize subjects to GET messages from topics.
* This default implementation authorizes everybody.
*
* @param {Object} subject The subject returned by the authenticate function
* @param {string} topic The topic
* @param {Function} cb The callback function. Has the following structure: cb(err, authorized)
*/
CoAP.prototype.authorizeGet = function (subject, topic, cb) {
cb(null, true)
}
/**
* The function that will be used to authorize subjects to PUT messages to topics.
* This default implementation authorizes everybody.
*
* @param {Object} subject The subject returned by the authenticate function
* @param {string} topic The topic
* @param {Buffer} payload The payload
* @param {Function} cb The callback function. Has the following structure: cb(err, authorized)
*/
CoAP.prototype.authorizePut = function (subject, topic, payload, cb) {
cb(null, true)
}
CoAP.prototype._handleAuthError = function (err, res) {
this._logger.info(err)
res.statusCode = '5.00'
res.end()
}
CoAP.prototype._handleNotAuthenticated = function (res) {
this._logger.info('authentication denied')
res.statusCode = '4.01'
res.end()
}
CoAP.prototype._handleNotAuthorized = function (res) {
this._logger.info('not authorized')
res.statusCode = '4.03'
res.end()
}
module.exports = CoAP