This repository has been archived by the owner on Sep 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
237 lines (208 loc) · 6.5 KB
/
app.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
'use strict';
var http = require('http');
var util = require('util');
var Stream = require('stream').Stream;
var crypto = require('crypto');
var request = require('request');
var url = require('url');
var querystring = require('querystring');
/**
* @param {Object} [options] Options object
* @param {String} [options.callback] Callback URL for the hub
* @param {String} [options.secret] Secret value for HMAC signatures
* @param {String} [options.client_id] Twitch Client ID
* @return {Object} A TwitchWebSub server object
*/
module.exports.server = function (options) {
return new TwitchWebSub(options);
};
/**
* @constructor
* @param {Object} [options] Options object
* @param {String} [options.callback] Callback URL for the hub
* @param {String} [options.secret] Secret value for HMAC signatures
* @param {String} [options.client_id] Twitch Client ID
*/
function TwitchWebSub (options) {
Stream.call(this);
options = options || {};
this.secret = options.secret || "I hate my life and don't care if people spoof requests from Twitch.";
this.client_id = options.client_id || '';
this.hub = "https://api.twitch.tv/helix/webhooks/hub";
this.twitch_callback = options.callback || '';
if (this.client_id === "") throw new Error("How rude! Introducing yourself with a *clientid* before requesting data from Twitch.");
if (this.twitch_callback === "") throw new Error("Twitch can't really call you back if you don't give them a *callback*... Duh.");
}
util.inherits(TwitchWebSub, Stream);
TwitchWebSub.prototype.listen = function () {
var args = Array.prototype.slice.call(arguments);
this.port = args[0];
this.server = http.createServer(this._onREQ.bind(this));
this.server.on('error', this._onError.bind(this));
this.server.on('listening', this._onListening.bind(this));
this.server.listen.apply(this.server, args);
};
/**
* @param {String} topic Helix endpoint
*/
TwitchWebSub.prototype.subscribe = function (topic) {
this._setSUB('subscribe', topic);
};
/**
* @param {String} topic Helix endpoint
*/
TwitchWebSub.prototype.unsubscribe = function (topic) {
this._setSUB('unsubscribe', topic);
};
/**
* @param {String} mode 'subscribe' || 'unsubscribe'
* @param {String} topic Helix endpoint
*/
TwitchWebSub.prototype._setSUB = function (mode, topic) {
var twitch_callback = this.twitch_callback + (this.twitch_callback.replace(/^https?:\/\//i, '').match(/\//) ? '' : '/') + (this.twitch_callback.match(/\?/) ? '&' : '?') + 'topic=' + encodeURIComponent(topic) + '&hub=' + encodeURIComponent(this.hub);
this._secret = crypto.createHmac('sha256', this.secret).update(topic).digest('hex');
request.post({
url: this.hub,
headers: {
"Client-ID": this.client_id
},
form: {
'hub.callback': twitch_callback,
'hub.mode': mode,
'hub.topic': topic,
'hub.lease_seconds': this.lease_seconds || 864000,
'hub.secret': this._secret
}
}, (err, res, body) => {
if (err) {
return this.emit('denied', {
topic: topic,
error: err
});
}
if (res.statusCode !== 202 && res.statusCode !== 204) {
err = new Error(`Invalid response status ${res.statusCode}`);
err.body = (body || '').toString();
return this.emit('denied', {
topic: topic,
error: err
});
}
});
};
/**
* @event
* @param {Object} req HTTP Request object
* @param {Object} res HTTP Response object
*/
TwitchWebSub.prototype._onREQ = function (req, res) {
switch (req.method) {
case 'GET':
return this._onGET(req, res);
case 'POST':
return this._onPOST(req, res);
default:
return this._ERR(req, res, 405, 'Method Not Allowed');
}
};
/**
* @event
* @param {Error} error Error object
*/
TwitchWebSub.prototype._onError = function (error) {
if (error.syscall === 'listen') error.message = `[${error.code}] Failed to start on port ${this.port}`;
this.emit('error', error);
};
/**
* @event
*/
TwitchWebSub.prototype._onListening = function () {
this.emit('listen');
};
/**
* @param {Object} req HTTP Request object
* @param {Object} res HTTP Response object
*/
TwitchWebSub.prototype._onPOST = function(req, res) {
var params = url.parse(req.url, true, true);
var topic = params && params.query && params.query.topic;
var hub = params && params.query && params.query.hub;
if (!topic) return this._sendError(req, res, 400, 'Bad Request');
if (this.secret && !req.headers['x-hub-signature']) return this._sendError(req, res, 403, 'Forbidden');
var signature = req.headers['x-hub-signature'].split('=')[1];
var body = '';
req.on('data', function (data) {
body += data;
// 50 * Math.pow(10, 6) - 50MB Max
if (body.length > 50000000) req.connection.destroy();
});
req.on('end', (function () {
if (crypto.createHmac('sha256', this._secret).update(body).digest('hex') !== 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: body,
headers: req.headers
});
}).bind(this));
};
/**
* @param {Object} req HTTP Request object
* @param {Object} res HTTP Response object
*/
TwitchWebSub.prototype._onGET = function (req, res) {
var data;
var params = url.parse(req.url, true, true);
if (!params.query['hub.topic'] || !params.query['hub.mode']) return this._ERR(req, res, 400, 'Bad Request');
switch (params.query['hub.mode']) {
case 'denied':
data = {
topic: params.query['hub.topic'],
hub: params.query.hub
};
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end(params.query['hub.challenge'] || 'ok');
break;
case 'subscribe':
case 'unsubscribe':
data = {
lease: Number(params.query['hub.lease_seconds'] || 0) + Math.round(Date.now() / 1000),
topic: params.query['hub.topic'],
hub: params.query.hub
};
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(params.query['hub.challenge']);
break;
default:
return this._ERR(req, res, 403, 'Forbidden');
}
this.emit(params.query['hub.mode'], data);
};
/**
* @param {Object} req HTTP Request object
* @param {Object} res HTTP Response object
* @param {Int32} code HTTP response status
* @param {String} message Error message to display
*/
TwitchWebSub.prototype._ERR = function (req, res, code, message) {
res.writeHead(code, { 'Content-Type': 'text/html' });
res.end(`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>TwitchWebSub - ${code} ${message}</title>
</head>
<body>
<h1>TwitchWebSub - ${code} ${message}</h1>
</body>
</html>`);
};