forked from duncanthrax/iptv-m3u-restream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restream.js
326 lines (267 loc) · 10.6 KB
/
restream.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
process.chdir(__dirname);
const cluster = require('cluster');
const Https = require('https');
const Http = require('http');
const Url = require('url');
const { spawn } = require('child_process');
var Channels = [];
const Cfg = require('./restream-cfg.json');
if (!Cfg.extUrl.match(/\/$/)) Cfg.extUrl = Cfg.extUrl + '/';
const logger = function(fac, msg, obj) {
if (cluster.isMaster) {
if (obj) console.log(`<master> [${fac}] ${msg}`, obj);
else console.log(`<master> [${fac}] ${msg}`);
}
else {
if (obj) console.log(`<${cluster.worker.id}> [${fac}] ${msg}`, obj);
else console.log(`<${cluster.worker.id}> [${fac}] ${msg}`);
}
}
// Simple GET wrapper that follows redirects and does http and https
const GetRequest = function(url, cb) {
var req = (url.match(/^https/i) ? Https : Http).request(url);
req.on('response', res => {
logger('request', `${url}: Code ${res.statusCode} with headers`, res.headers);
if (res.statusCode > 300 && res.statusCode < 400 && res.headers.location)
return process.nextTick(function() { GetRequest(res.headers.location, cb) });
cb(null, req, res);
});
req.on('abort', () => {
logger('request', `Aborted ${url}`);
cb("aborted", null, null);
});
req.on('error', err => {
logger('request', `Error on ${url}`, err);
cb(err ? err : "error", null, null);
});
req.end();
};
// Wrap above for simple GET body retriever
const GetRequestBody = function(url, cb) {
GetRequest(url, (err, req, res) =>{
if (err) return cb(err, null);
res.setEncoding('utf8');
var body = '';
res.on('data', chunk => {
body += chunk;
});
res.on('end', () => {
cb(null, body);
});
});
};
// --------------------------------------------------------------------------------------------
// Master
if (cluster.isMaster) {
logger('master', `restream master running with PID ${process.pid}`);
const eachWorker = function(callback) {
for (const id in cluster.workers) {
callback(cluster.workers[id]);
}
};
const replenishWorkers = function() {
while (Object.keys(cluster.workers).length < Cfg.numWorkers) {
cluster.fork().on('online', () => {
eachWorker(worker => {
worker.send({ type: 'channels', channels: Channels });
});
});
};
};
cluster.on('message', (worker, message, handle) => {
if (message.type == 'streaming')
eachWorker(wrk => {
if (wrk.id != worker.id) wrk.send({ type: 'quitStreaming' });
});
});
const loadChannels = function() {
GetRequestBody(Cfg.m3uSrc, (err, body) => {
if (err) {
logger('master', "loadChannels request error, retrying in 60 seconds", err);
return setTimeout(loadChannels, (60 * 1000));
};
var lines = body.split(/[\r\n]+/);
var numChannels = 0;
Channels = [];
while (lines.length) {
var line = lines.shift();
if (line.match(/#EXTINF:/) && line.length < 200) {
// Blacklists
if (Cfg.blacklist.find(item => {
var rx = new RegExp(item, 'i');
return line.match(rx) ? true : false;
})) continue;
Channels.push({
id : ++numChannels,
extinf: line,
url : lines.shift()
});
}
}
logger('master', `loaded channel list with ${Channels.length} channels`);
replenishWorkers();
setTimeout(loadChannels, (86400 * 1000));
});
};
cluster.on('exit', (worker, code, signal) => {
logger('master',`worker ${worker.process.pid} died`);
process.nextTick(replenishWorkers);
});
loadChannels();
}
// --------------------------------------------------------------------------------------------
// Worker
else {
// True if we're streaming
var streaming = false;
cluster.worker.on('message', msg => {
if (msg.type == 'channels') Channels = msg.channels;
if (msg.type == 'quitStreaming' && streaming) doShutdown();
});
var transcoder = false;
const doShutdown = function() {
if (transcoder) transcoder.kill();
process.exit(0);
};
const StreamURL = function(url, clientRes, profile) {
transcoder = spawn(profile.transcoder, profile.transcoderOpts, { env: profile.transcoderEnv || {} });
transcoder.stderr.on('data', data => {
if (Cfg.debug['transcoder']) logger('transcoder', data.toString());
});
transcoder.stdout.on('error', function (err) {
logger('transcoder', "stdout error", err);
doShutdown();
});
transcoder.stderr.on('error', function (err) {
logger('transcoder', "stderr error", err);
doShutdown();
});
transcoder.stdin.on('error', function (err) {
logger('transcoder', "stdin error", err);
doShutdown();
});
transcoder.on('error', (err) => {
logger('transcoder', "error", err);
doShutdown();
});
transcoder.on('close', () => {
logger('transcoder', "closed IO");
doShutdown();
});
transcoder.on('exit', code => {
logger('transcoder', "exited with code", code);
doShutdown();
});
logger('streaming', "Streaming URL", url);
// If client acts up, bomb out.
clientRes.on('close', err => {
logger('client', "close");
doShutdown();
});
clientRes.on('error', err => {
logger('client', "error", err);
doShutdown();
});
// Tell the client we'll be sending entertainment.
clientRes.writeHead(200, { 'Content-Type': profile.contentType, 'Connection': 'close' });
// Connect transcoder out to client in
transcoder.stdout.on('data', transcoderChunk => {
clientRes.write(transcoderChunk);
});
// Retryable server connection
const connectServer = function() {
var retryTimeout = false;
const scheduleRetry = function() {
if (!retryTimeout) {
logger('streaming', "Connection to server lost, reconnecting");
retryTimeout = setTimeout(connectServer, 10);
}
}
GetRequest(url, (err, serverReq, serverRes) => {
// Ignore callbacks about aborted request.
if (err == "aborted") return;
// If the server does not like us on sight, bomb out.
if (err || serverRes.statusCode != 200) doShutdown();
// Connect server out to transcoder in
var idleTimeout = false;
serverRes.on('data', serverChunk => {
// If we don't get a follow-up chunk within ten seconds, declare server lame.
if (idleTimeout) clearTimeout(idleTimeout);
idleTimeout = setTimeout(() => { serverReq.abort() }, 10000);
if (!serverReq.aborted) transcoder.stdin.write(serverChunk);
});
// On inline server-side errors, schedule a reconnect.
serverRes.on('end', () => {
logger('server', "end");
scheduleRetry();
});
serverRes.on('aborted', () => {
logger('server', "aborted");
scheduleRetry();
});
serverRes.on('close', () => {
logger('server', "close");
scheduleRetry();
});
serverRes.on('error', (err) => {
logger('server', "error", err);
scheduleRetry();
});
serverReq.on('error', err => {
logger('server', "error", err);
scheduleRetry();
});
serverReq.on('close', () => {
logger('server', "error", err);
scheduleRetry();
});
});
}
connectServer();
};
// Server instance with URL parsing
const server = Http.createServer();
server.on('request', (clientReq, clientRes) => {
var url = Url.parse(clientReq.url, true);
// Send channels list as M3U
if (url.pathname.match(/channels/)) {
var profile = (url.query && url.query.profile && Cfg.profiles[url.query.profile]) ?
url.query.profile : "default";
clientRes.writeHead(200, { 'Content-Type': 'audio/x-mpegurl' });
return clientRes.end(
"#EXTM3U\r\n" + Channels.map(item => {
return `${item.extinf}\r\n${Cfg.extUrl}watch?channelId=${item.id}&profile=${profile}`;
}).join("\r\n"),
'utf8',
function() { logger('client', `Sent M3U channel list with ${Channels.length} entries`) }
);
}
// Watch a channel
WATCH: if (url.query && url.query.channelId && url.pathname.match(/watch/)) {
var channelId = parseInt(url.query.channelId) - 1;
var profile = url.query.profile && Cfg.profiles[url.query.profile] ? url.query.profile : "default";
if (!Channels[channelId]) break WATCH;
logger('client', `Requested channel ${channelId} for playback, profile ${profile}`);
// Close server, we don't want to handle any more requests.
process.nextTick(function() { server.close() });
// Tell master we're streaming
streaming = true;
process.send({ type: 'streaming' });
// Delay a bit so that other streaming workers get a chance to quit.
return setTimeout(function() { StreamURL(Channels[channelId].url, clientRes, Cfg.profiles[profile]) }, 100);
}
// No handler for URL
logger('client', "Unknown URL requested");
clientRes.writeHead(404);
clientRes.end("Not found");
});
server.on('error', err => {
logger('self', "error", err);
doShutdown();
});
server.on('clientError', (err, socket) => {
logger('client', "error", err);
doShutdown();
});
server.listen(Cfg.port);
};