-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
364 lines (319 loc) · 11.4 KB
/
index.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
var request = require("request"),
http = require("http"),
urllib = require("url"),
Stream = require("stream").Stream,
utillib = require("util"),
crypto = require("crypto");
// Expose to the world
/**
* Creates a PubSubHubbub subscriber service as a HTTP server.
* Usage:
* pubsub = createServer(options);
* pubsub.listen(1337);
*
* @param {Object} [options] Options object
* @param {String} [options.callbackUrl] Callback URL for the hub
* @param {String} [options.secret] Secret value for HMAC signatures
* @param {Number} [options.maxContentSize] Maximum allowed size of the POST messages
* @param {String} [options.username] Username for HTTP Authentication
* @param {String} [options.password] Password for HTTP Authentication
* @return {Object} A PubSubHubbub server object
*/
module.exports.createServer = function(options){
return new PubSubHubbub(options);
}
/**
* Create a PubSubHubbub client handler object. HTTP server is set up to listen
* the responses from the hubs.
*
* @constructor
* @param {Object} [options] Options object
* @param {String} [options.callbackUrl] Callback URL for the hub
* @param {String} [options.secret] Secret value for HMAC signatures
* @param {Number} [options.maxContentSize] Maximum allowed size of the POST messages
* @param {String} [options.username] Username for HTTP Authentication
* @param {String} [options.password] Password for HTTP Authentication
*/
function PubSubHubbub(options){
Stream.call(this);
options = options || {};
this.secret = options.secret || false;
this.callbackUrl = options.callbackUrl;
this.maxContentSize = options.maxContentSize || 3 * 1024 * 1024;
if (options.username) {
this.auth = {
'user': options.username,
'pass': options.password,
'sendImmediately': false
}
}
}
utillib.inherits(PubSubHubbub, Stream);
// PUBLIC API
/**
* Start listening on selected port
*
* @param {Number} port Port number for the HTTP server
*/
PubSubHubbub.prototype.listen = function(port){
this.port = port;
this.server = http.createServer(this._onRequest.bind(this));
this.server.on("error", this._onError.bind(this));
this.server.on("listening", this._onListening.bind(this));
this.server.listen(this.port);
}
/**
* Subsribe for a topic at selected hub
*
* @param {String} topic Atom or RSS feed URL
* @param {String} hub Hub URL
* @param {String} [callbackUrl] Define callback url for the hub, do not use the default
* @param {Function} [callback] Callback function, might not be very useful
*/
PubSubHubbub.prototype.subscribe = function(topic, hub, callbackUrl, callback){
this.setSubscription("subscribe", topic, hub, callbackUrl, callback);
}
/**
* Subsribe a topic at selected hub
*
* @param {String} topic Atom or RSS feed URL
* @param {String} hub Hub URL
* @param {String} [callbackUrl] Define callback url for the hub, do not use the default
* @param {Function} [callback] Callback function, might not be very useful
*/
PubSubHubbub.prototype.unsubscribe = function(topic, hub, callbackUrl, callback){
this.setSubscription("unsubscribe", topic, hub, callbackUrl, callback);
}
/**
* Subsribe or unsubscribe a topic at selected hub
*
* @param {String} mode Either "subscribe" or "unsubscribe"
* @param {String} topic Atom or RSS feed URL
* @param {String} hub Hub URL
* @param {String} [callbackUrl] Define callback url for the hub, do not use the default
* @param {Function} [callback] Callback function, might not be very useful
*/
PubSubHubbub.prototype.setSubscription = function(mode, topic, hub, callbackUrl, callback){
if(!callback && typeof callbackUrl == "function"){
callback = callbackUrl;
callbackUrl = undefined;
}
// by default the topic url is added as a GET parameter to the callback url
callbackUrl = callbackUrl || this.callbackUrl +
(this.callbackUrl.replace(/^https?:\/\//i, "").match(/\//)?"":"/") +
(this.callbackUrl.match(/\?/)?"&":"?") +
"topic="+encodeURIComponent(topic)+
"&hub="+encodeURIComponent(hub);
var form = {
"hub.callback": callbackUrl,
"hub.mode": mode,
"hub.topic": topic,
"hub.verify": "async"
},
postParams = {
url: hub,
form: form,
encoding: "utf-8"
};
if (this.auth) {
postParams.auth = this.auth;
}
if(this.secret){
// do not use the original secret but a generated one
form["hub.secret"] = crypto.createHmac("sha1", this.secret).update(topic).digest("hex");
}
request.post(postParams, function(error, response, responseBody){
if(error){
if(callback){
return callback(error);
}else{
return this.emit("denied", {topic: topic, error: error});
}
}
if(response.statusCode != 202 && response.statusCode != 204){
var err = new Error("Invalid response status " + response.statusCode);
err.responseBody = (responseBody || "").toString();
if(callback){
return callback(err);
}else{
return this.emit("denied", {topic: topic, error: err});
}
}
return callback && callback(null, topic);
});
}
// PRIVATE API
/**
* Request handler. Will be fired when a client (hub) opens a connection to the server
*
* @event
* @param {Object} req HTTP Request object
* @param {Object} res HTTP Response object
*/
PubSubHubbub.prototype._onRequest = function(req, res){
switch(req.method){
case "GET": return this._onGetRequest(req, res);
case "POST": return this._onPostRequest(req, res);
default:
return this._sendError(req, res, 405, "Method Not Allowed");
}
}
/**
* Error event handler for the HTTP server
*
* @event
* @param {Error} error Error object
*/
PubSubHubbub.prototype._onError = function(error){
if(error.syscall == "listen"){
error.message = "Failed to start listening on port " + this.port + " ("+error.code+")";
this.emit("error", error);
}else{
this.emit("error", error);
}
}
/**
* Will be fired when HTTP server has successfully started listening on the selected port
*
* @event
*/
PubSubHubbub.prototype._onListening = function(){
this.emit("listen");
}
/**
* GET request handler for the HTTP server. This should be called when the server
* tries to verify the intent of the subscriber.
*
* @param {Object} req HTTP Request object
* @param {Object} res HTTP Response object
*/
PubSubHubbub.prototype._onGetRequest = function(req, res){
var params = urllib.parse(req.url, true, true),
data;
// Does not seem to be a valid PubSubHubbub request
if(!params.query["hub.topic"] || !params.query['hub.mode']){
return this._sendError(req, res, 400, "Bad Request");
}
switch(params.query['hub.mode']){
case "denied":
res.writeHead(200, {'Content-Type': 'text/plain'});
data = {topic: params.query["hub.topic"], hub: params.query.hub};
res.end(params.query['hub.challenge'] || "ok");
break;
case "subscribe":
case "unsubscribe":
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(params.query['hub.challenge']);
data = {
lease: Number(params.query["hub.lease_seconds"] || 0) + Math.round(Date.now()/1000),
topic: params.query["hub.topic"],
hub: params.query.hub
};
break;
default:
// Not a valid mode
return this._sendError(req, res, 403, "Forbidden");
}
// Emit subscription information
this.emit(params.query["hub.mode"], data);
}
/**
* POST request handler. Should be called when the hub tries to notify the subscriber
* with new data
*
* @param {Object} req HTTP Request object
* @param {Object} res HTTP Response object
*/
PubSubHubbub.prototype._onPostRequest = function(req, res){
var bodyChunks = [],
params = urllib.parse(req.url, true, true),
topic = params && params.query && params.query.topic,
hub = params && params.query && params.query.hub,
bodyLen = 0,
tooLarge = false,
signatureParts, algo, signature, hmac;
// v0.4 hubs have a link header that includes both the topic url and hub url
(req.headers && req.headers.link || "").
replace(/<([^>]+)>\s*(?:;\s*rel=['"]([^'"]+)['"])?/gi, function(o, url, rel){
switch((rel || "").toLowerCase()){
case "self":
topic = url;
break;
case "hub":
hub = url;
break;
}
});
if(!topic){
return this._sendError(req, res, 400, "Bad Request");
}
// Hub must notify with signature header if secret specified.
if(this.secret && !req.headers['x-hub-signature']){
return this._sendError(req, res, 403, "Forbidden");
}
if(this.secret){
signatureParts = req.headers['x-hub-signature'].split("=");
algo = (signatureParts.shift() || "").toLowerCase();
signature = (signatureParts.pop() || "").toLowerCase();
try{
hmac = crypto.createHmac(algo, crypto.createHmac("sha1", this.secret).update(topic).digest("hex"));
}catch(E){
return this._sendError(req, res, 403, "Forbidden");
}
}
req.on("data", (function(chunk){
if(!chunk || !chunk.length || tooLarge){
return;
}
if(bodyLen + chunk.length <= this.maxContentSize){
bodyChunks.push(chunk);
bodyLen += chunk.length;
if(this.secret){
hmac.update(chunk);
}
}else{
tooLarge = true;
}
chunk = null;
}).bind(this));
req.on("end", (function(){
if(tooLarge){
return this._sendError(req, res, 413, "Request Entity Too Large");
}
// Must return 2xx code even if signature doesn't match.
if(this.secret && hmac.digest("hex").toLowerCase() != signature){
res.writeHead(202, {'Content-Type': 'text/plain; charset=utf-8'});
return res.end();
}
res.writeHead(204, {'Content-Type': 'text/plain; charset=utf-8'});
res.end();
this.emit("feed", {
topic: topic,
hub: hub,
callback: "http://" + req.headers.host + req.url,
feed: Buffer.concat(bodyChunks, bodyLen),
headers: req.headers
});
}).bind(this));
}
/**
* Generates and sends an error message as the response for a HTTP request
*
* @param {Object} req HTTP Request object
* @param {Object} res HTTP Response object
* @param {Number} code HTTP response status
* @param {String} message Error message to display
*/
PubSubHubbub.prototype._sendError = function(req, res, code, message){
res.writeHead(code, {"Content-Type": "text/html"});
res.end("<!DOCTYPE html>\n"+
"<html>\n"+
" <head>\n"+
" <meta charset=\"utf-8\"/>\n"+
" <title>" + code + " " + message + "</title>\n"+
" </head>\n"+
" <body>\n"+
" <h1>" + code + " " + message + "</h1>\n"+
" </body>\n"+
"</html>");
}