-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoundbitten.js
254 lines (222 loc) · 10.5 KB
/
soundbitten.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
#!/usr/bin/env node
/*
* Soundbitten v0.1 - Soundbite NodeJS Server Demo Application
*
* 3 July, 2022 - John Chidgey
*
* Companion Application for Soundbite Demo: https://johnchidgey.github.io/managesoundbites.html
* Manage Soundbite functionality requires this server to be running
*/
'use strict';
const folderLocalPath = '/Users/johnchidgey/Documents/sites/engineered/data/soundbites/';
const folderServerPath = '/home/soundbittennode/soundbites'; // TD Server Working
//const folderServerPath = '/app/soundbites'; // Heroku Attempt
// Install jsdom
var path = require('path');
var fs = require('fs');
var webSocket = require('ws');
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
var fetch = require("node-fetch-commonjs");
var https = require('https'); // Only required for TD Server WSS (Not Local)
//var WebSocketServer = require('ws').Server; // Only required for TD Server WSS (Not Local)
var podcastFileItems = [];
var podcasts = [];
var soundbitesLocal = [];
var soundbites = [];
var soundbite = [{episode:"", enabled:"", startTime:"", duration:"", title:"", url:"", name:"", type:"", address:"", customKey:"", customValue:""}];
var podcastFeedItems = [];
var folderPath = '';
// Extract all exiting SoundBite JSON Files from local storage
try {
if(process.env.NODE_ENV === 'production') folderPath = folderServerPath;
else folderPath = folderLocalPath;
var directoryList = fs.readdirSync(folderPath, { withFileTypes: true });
directoryList.forEach(function (directory) {
if (directory.isDirectory())
podcastFileItems.push({folder:directory.name});
}); // End ForEach for Directories
console.log(directoryList);
podcastFileItems.forEach((podcast, index, fullArray) => {
var podcastFolder = path.join(folderPath, podcast.folder);
soundbitesLocal = [];
try {
var fileList = fs.readdirSync(podcastFolder, { withFileTypes: true });
fileList.forEach(function (file) {
if ((file.isFile()) && (path.extname(file.name) == ".json")) {
var filePath = path.join(podcastFolder, file.name);
try {
var data = fs.readFileSync(filePath, 'utf8'); // parse JSON string to JSON object
var rawJSON = JSON.parse(data);
if (file.name == "index.json") {
podcastFileItems[index].title = rawJSON.title;
podcastFileItems[index].url = rawJSON.url;
}
else {
var episode = path.basename(file.name, path.extname(file.name));
soundbitesLocal.push({episode:episode, soundbites:rawJSON});
}
} catch(err) { console.log(`Error reading file from disk: ${err}`); }
} // End IF JSON
}); // End ForEach Files Loop
} catch(err) { console.log('Unable to scan directory: ' + err); }
if (soundbitesLocal.length > 0) { podcastFileItems[index].soundbites = soundbitesLocal; }
}); // End ForEach Podcast Directories
} catch(err) { console.log('Unable to scan directory: ' + err); }
// Extract all episodes for each podcast from RSS feed
try {
podcastFileItems.forEach((podcast, index, fullArray) => {
var RSS_URL = podcast.url;
var episode, title, soundbitesOpen, enclosureSoundbites, enclosureFeed, enclosure;
var podcastItems = [];
fetch(RSS_URL)
.then(response => response.text())
.then(str => new JSDOM(str, 'text/xml'))//, { features: { QuerySelector: true } }))
.then(data => {
const allitems = data.window.document.querySelectorAll("item", data);
var feedURL = data.window.document.querySelector('atom\\:link') ? data.window.document.querySelector('atom\\:link').getAttribute("href") : "";
podcastItems = [];
allitems.forEach(item => {
episode = item.querySelector('itunes\\:episode') ? item.querySelector('itunes\\:episode').textContent : "";
title = item.querySelector('title') ? item.querySelector('title').textContent : "";
soundbitesOpen = item.querySelector('podcast\\:soundbites') ? item.querySelector('podcast\\:soundbites').getAttribute("open") : "";
enclosureSoundbites = item.querySelector('podcast\\:soundbites') ? item.querySelector('podcast\\:soundbites').getAttribute("enclosure") : "";
enclosureFeed = item.querySelector("enclosure") ? item.querySelector("enclosure").getAttribute("url") : "";
enclosure = enclosureSoundbites ? enclosureSoundbites : enclosureFeed; // Use the URL in the Soundbite tag, if present
if (episode != "") podcastItems.push({'episode':episode, 'title':title, 'enclosure':enclosure, 'soundbites':soundbitesOpen});
});
podcastFeedItems.push({url: feedURL, episodes: podcastItems}); // TBD Need to add the Podcast name / title to link the two datasets, currently just an Array not a Key:Value store
});
});
} catch(err) { console.log('Unable to read RSS: ' + err); }
/***************************************************
* WEB SOCKETS *
***************************************************/
if(process.env.NODE_ENV === 'production') { // TD Server Only
var privateKey = fs.readFileSync('/etc/letsencrypt/live/ws.techdistortion.com/privkey.pem', 'utf8'); // TD Server Only
var certificate = fs.readFileSync('/etc/letsencrypt/live/ws.techdistortion.com/fullchain.pem', 'utf8'); // TD Server Only
var credentials = { key: privateKey, cert: certificate }; // TD Server Only
var httpsServer = https.createServer(credentials); // TD Server Only
}
var port = process.env.PORT || 5001;
var proxied = process.env.PROXIED === 'true';
if(process.env.NODE_ENV === 'production') { // TD Server Only
httpsServer.listen(port); // TD Server Only
var socketServer = new webSocket.Server({ server: httpsServer }); // TD Server Only
}
else var socketServer = new webSocket.Server({port: port}); // Local / Heroku
var titles = [];
var connections = [];
// DOS protection - we disconnect any address which sends more than windowLimit
// messages in a window of windowSize milliseconds.
var windowLimit = 50;
var windowSize = 5000;
var currentWindow = 0;
var recentMessages = {};
// console.log(socketServer);
function floodedBy(socket) {
// To be called each time we get a message or connection attempt. If that address has been flooding us, we disconnect all open connections
// from that address and return `true` to indicate that it should be ignored. (They will not be prevented from re-connecting after waiting
// for the next window.)
if (socket.readyState == socket.CLOSED) {
return true;
}
var address = getRequestAddress(socket.upgradeReq);
var updatedWindow = 0 | ((new Date) / windowSize);
if (currentWindow !== updatedWindow) {
currentWindow = updatedWindow;
recentMessages = {};
}
if (address in recentMessages) {
recentMessages[address]++;
} else {
recentMessages[address] = 1;
}
if (recentMessages[address] > windowLimit) {
console.warn("Disconnecting flooding address: " + address);
socket.terminate();
for (var i = 0, l = connections.length; i < l; i++) {
if (getRequestAddress(connections[i].upgradeReq) === address &&
connections[i] != socket) {
console.log("Disconnecting additional connection.");
connections[i].terminate();
}
}
return true;
} else {
return false;
}
}
function getRequestAddress(request) {
if (proxied && 'x-forwarded-for' in request.headers) {
// This assumes that the X-Forwarded-For header is generated by a trusted proxy such as Heroku. If not, a malicious user could take
// advantage of this logic and use it to to spoof their IP.
var forwardedForAddresses = request.headers['x-forwarded-for'].split(',');
return forwardedForAddresses[forwardedForAddresses.length - 1].trim();
} else {
// This is valid for direct deployments, without routing/load balancing.
return request.connection.remoteAddress;
}
}
function mergeFeedAndFile() {
podcasts = [];
var episodeEntries = [];
var soundbiteFile = [];
podcastFileItems.forEach((fileItem, fileIndex) => {
episodeEntries = [];
podcastFeedItems.forEach((feedItem, feedIndex) => {
if(feedItem.url == fileItem.url) {
feedItem.episodes.forEach((episodeItem, episodeIndex) => {
soundbiteFile = [];
if(fileItem.soundbites) {
fileItem.soundbites.forEach((soundbiteItem, soundbiteIndex) => {
if(soundbiteItem.episode == episodeItem.episode) {
soundbiteFile = soundbiteItem.soundbites;
}
});
}
if(soundbiteFile.length > 0) episodeEntries.push({'episode':episodeItem.episode, 'title':episodeItem.title, 'enclosure':episodeItem.enclosure, 'soundbitesOpen':episodeItem.soundbites, 'soundbites':soundbiteFile});
else episodeEntries.push({'episode':episodeItem.episode, 'title':episodeItem.title, 'enclosure':episodeItem.enclosure, 'soundbitesOpen':episodeItem.soundbites});
});
}
});
podcasts.push({title:fileItem.title, folder:fileItem.folder, url:fileItem.url, episodes:episodeEntries});
});
}
socketServer.on('connection', function(socket, req) {
socket.upgradeReq = req;
if (floodedBy(socket)) return;
connections.push(socket);
var address = getRequestAddress(socket.upgradeReq);
console.log('Client connected: ' + address);
// Okay this is a hack - should be forcing a Sync Fetch but that's annoying
if(podcastFileItems.length == podcastFeedItems.length) {
mergeFeedAndFile();
}
socket.send(JSON.stringify({operation: 'REFRESH', soundbites: podcasts}));
socket.on('close', function () {
console.log('Client disconnected: ' + address);
connections.splice(connections.indexOf(socket), 1);
});
socket.on('error', function (reason, code) {
console.log('socket error: reason ' + reason + ', code ' + code);
});
socket.on('message', function (data) {
if (floodedBy(socket)) return;
var packet;
try {
packet = JSON.parse(data);
} catch (e) {
console.log('error: malformed JSON message (' + e + '): '+ data);
return;
}
if (packet.operation === 'PING') {
socket.send(JSON.stringify({operation: 'PONG'}));
} else if (packet.operation === 'COMMAND') {
handleAdminCommand(packet['id']);
socket.send(JSON.stringify({operation: 'CMDREPLY'}));
} else {
console.log("Don't know what to do with " + packet['operation']);
}
});
});