forked from pusspounder/CapMyFreeCamsChaturbateNodeJS
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain-rtmp.js
623 lines (515 loc) · 17.8 KB
/
main-rtmp.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
'use strict';
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
var mvAsync = Promise.promisify(require('mv'));
var mkdirp = require('mkdirp');
var moment = require('moment');
var colors = require('colors');
var yaml = require('js-yaml');
var path = require('path');
var childProcess = require('child_process');
var HttpDispatcher = require('httpdispatcher');
var dispatcher = new HttpDispatcher();
var http = require('http');
var mfc = require('MFCAuto');
var EOL = require('os').EOL;
var compression = require('compression');
var bhttp = require('bhttp');
var session = bhttp.session();
var useDefaultOptions = {};
var compress = compression(useDefaultOptions);
var noop = () => {};
var onlineModels = []; // the list of online models from myfreecams.com
var cachedModels = []; // "cached" copy of onlineModels (primarily for index.html)
var captureModels = []; // the list of currently capturing models
var config = yaml.safeLoad(fs.readFileSync('config-rtmp.yml', 'utf8'));
config.captureDirectory = config.captureDirectory || './capture';
config.completeDirectory = config.completeDirectory || './complete';
config.modelScanInterval = config.modelScanInterval || 30;
config.port = config.port || 9081;
config.minFileSizeMb = config.minFileSizeMb || 0;
config.debug = !!config.debug;
config.models = Array.isArray(config.models) ? config.models : [];
config.queue = Array.isArray(config.queue) ? config.queue : [];
config.dateFormat = config.dateFormat || 'YYYYMMDD-HHmmss';
config.createModelDirectory = !!config.createModelDirectory;
var captureDirectory = path.resolve(config.captureDirectory);
var completeDirectory = path.resolve(config.completeDirectory);
var isDirty = false;
var minFileSize = config.minFileSizeMb * 1048576;
var patchedRtmpdump = false;
var mfcClient = new mfc.Client();
function getCurrentDateTime() {
return colors.gray('[' + moment().format('MM/DD/YYYY - HH:mm:ss') + ']');
}
function printMsg(msg) {
console.log(getCurrentDateTime(), msg);
}
function printErrorMsg(msg) {
console.log(getCurrentDateTime(), colors.red('[ERROR]'), msg);
}
function printDebugMsg(msg) {
if (config.debug && msg) {
console.log(getCurrentDateTime(), colors.yellow('[DEBUG]'), msg);
}
}
function mkdir(dir) {
mkdirp(dir, err => {
if (err) {
printErrorMsg(err);
process.exit(1);
}
});
}
function remove(value, array) {
let idx = array.indexOf(value);
if (idx !== -1) {
array.splice(idx, 1);
}
}
function getProxyModels() {
if (!config.proxyServer) {
return [];
}
return new Promise((resolve, reject) => {
return Promise
.try(() => session.get(`http://${config.proxyServer}/models?nc=${Date.now()}`))
.timeout(10000) // 10 seconds
.then(response => {
resolve(response.body || []);
})
.catch(err => {
printDebugMsg(err.toString());
resolve([]);
});
});
}
function getOnlineModels(proxyModels) {
let models = [];
mfc.Model.knownModels.forEach(m => {
if (m.bestSession.vs !== mfc.STATE.Offline && m.bestSession.camserv > 0 && !!m.bestSession.nm) {
// if rtmpdump is "patched" then add all models
// otherwise add only HD models
if (patchedRtmpdump || m.bestSession.phase === 'a') {
models.push({
uid: m.bestSession.uid,
vs: m.bestSession.vs,
nm: m.bestSession.nm,
camserv: m.bestSession.camserv,
camscore: m.bestSession.camscore,
continent: m.bestSession.continent,
new_model: m.bestSession.new_model,
rc: m.bestSession.rc,
age: m.bestSession.age,
missmfc: m.bestSession.missmfc,
city: m.bestSession.city,
country: m.bestSession.country,
ethnic: m.bestSession.ethnic,
phase: m.bestSession.phase
});
}
}
});
if (proxyModels.length > 0) {
// remove models from proxyModels (foreign region) if they are available in the current region
let newModels = (patchedRtmpdump)
? proxyModels.filter(pm => (!models.find(m => (m.uid === pm.uid))))
: proxyModels.filter(pm => (pm.phase !== 'a' && !models.find(m => (m.uid === pm.uid))));
printDebugMsg(`${newModels.length} new model(s) from proxy`);
// merge newModels with "local" models
onlineModels = newModels.concat(models);
} else {
onlineModels = models;
}
printMsg(`${onlineModels.length} model(s) online`);
}
function updateConfigModels() {
printDebugMsg(`${config.queue.length} model(s) in the queue`);
config.queue = config.queue.filter(queueModel => {
// if uid is not set then search uid of the model in the list of online models
if (!queueModel.uid) {
let onlineModel = onlineModels.find(m => (m.nm === queueModel.nm));
// if we could not find the uid of the model we leave her in the queue and jump to the next queue model
if (!onlineModel) {
return true;
}
queueModel.uid = onlineModel.uid;
}
// looking for the model in our config
let configModel = config.models.find(m => (m.uid === queueModel.uid));
if (!configModel) {
// if we don't have the model in our config we add here in
config.models.push({
uid: queueModel.uid,
mode: queueModel.mode
});
} else {
configModel.mode = queueModel.mode;
}
isDirty = true;
// probably here we should remove duplicates from config
return false;
});
}
function selectModelsToCapture() {
printDebugMsg(`${config.models.length} model(s) in config`);
let modelsToCapture = [];
let now = moment().unix();
config.models.forEach(configModel => {
let onlineModel = onlineModels.find(m => (m.uid === configModel.uid));
if (!onlineModel) { // skip the model if she is not online
return;
}
// if the model has "expired" me mark her as "excluded"
if (configModel.mode > 1 && configModel.mode < now) {
printMsg(colors.green(onlineModel.nm) + ' expired');
configModel.mode = 0;
isDirty = true;
}
onlineModel.mode = configModel.mode;
if (configModel.mode < 1) { // skip the mode if she is "deleted" or "excluded"
return;
}
// save the name of the model in config if it has not been set before
if (!configModel.nm) {
configModel.nm = onlineModel.nm;
isDirty = true;
}
onlineModel.dir_nm = configModel.nm;
if (onlineModel.vs === 0) {
modelsToCapture.push(onlineModel);
} else {
printMsg(`${colors.green(onlineModel.nm)} is away or in a private (vs = ${onlineModel.vs})`);
}
});
printDebugMsg(`${modelsToCapture.length} model(s) to capture`);
return modelsToCapture;
}
function createRtmpCaptureProcess(model) {
return Promise
.try(() => mfcClient.joinRoom(model.uid))
.then(packet => {
let filename = model.nm + '-' + moment().format(config.dateFormat) + '-flv.flv';
let sid = mfcClient.sessionId;
let roomId = 100000000 + model.uid;
let ctx = decodeURIComponent(mfcClient.stream_vidctx);
let server;
let captureProcess;
if (model.phase === 'a') {
server = model.camserv - 1000;
captureProcess = childProcess.spawn('rtmpdump', [
'-q',
'-a',
'NxServer',
'-f',
'WIN 25,0,0,127', // MAC 22,0,0,209
'-W',
'https://www.myfreecams.com/flash/MfcVideoNg-1080p-20180316.swf?nc=130',
'-s',
'https://www.myfreecams.com/flash/MfcVideoNg-1080p-20180316.swf?nc=130',
'-t',
`rtmp://video${server}.myfreecams.com:1935/NxServer`,
'-r',
`rtmp://video${server}.myfreecams.com:1935/NxServer`,
'-p',
'https://www.myfreecams.com/_html/player.html?broadcaster_id=0&vcc=1522120575&target=main',
'-C',
`N:${sid}`,
'-C',
`S:${mfcClient.stream_password}`,
'-C',
`S:${roomId}`,
'-C',
'S:a',
'-C',
'S:DOWNLOAD',
'-C',
`N:${model.uid}`,
'-C',
'N:0',
'-C',
`S:${ctx}`,
'-y',
`mfc_${model.phase}_${roomId}?ctx=${ctx}&tkx=${mfcClient.stream_password}`,
config.rtmpDebug ? '-V' : '',
'-o',
path.join(captureDirectory, filename),
'-m', 30
]);
} else {
server = model.camserv - 500;
captureProcess = childProcess.spawn('rtmpdump', [
'-q',
'-a',
'NxServer',
'-f',
'WIN 25,0,0,127', // MAC 22,0,0,209
'-W',
'https://www.myfreecams.com/flash/Video170823.swf',
'-s',
'https://www.myfreecams.com/flash/Video170823.swf',
'-t',
`rtmp://video${server}.myfreecams.com:1935/NxServer`,
'-r',
`rtmp://video${server}.myfreecams.com:1935/NxServer`,
'-p',
'https://www.myfreecams.com/_html/player.html?broadcaster_id=0&cache_id=1485777695&target=main',
'-C',
`N:${sid}`,
'-C',
'S:""',
'-C',
`N:${100000000 + model.uid}`,
'-C',
'S:DOWNLOAD',
'-C',
`N:${model.uid}`,
'-y',
`mp4:mfc_${100000000 + model.uid}.f4v`,
config.rtmpDebug ? '-V' : '',
'-o',
path.join(captureDirectory, filename)
]);
}
if (!captureProcess.pid) {
return;
}
captureProcess.stdout.on('data', data => {
printMsg(data.toString());
});
captureProcess.stderr.on('data', data => {
printMsg(data.toString());
});
captureProcess.on('close', code => {
printMsg(`${colors.green(model.nm)} stopped streaming`);
let stoppedModel = captureModels.find(m => m.captureProcess === captureProcess);
remove(stoppedModel, captureModels);
let src = path.join(captureDirectory, filename);
let dst = config.createModelDirectory
? path.join(completeDirectory, model.dir_nm, filename)
: path.join(completeDirectory, filename);
fs.statAsync(src)
// if the file is big enough we keep it otherwise we delete it
.then(stats => (stats.size <= minFileSize) ? fs.unlinkAsync(src) : mvAsync(src, dst, { mkdirp: true }))
.catch(err => {
if (err.code !== 'ENOENT') {
printErrorMsg('[' + colors.green(model.nm) + '] ' + err.toString());
}
});
});
captureModels.push({
nm: model.nm,
uid: model.uid,
filename: filename,
captureProcess: captureProcess,
checkAfter: moment().unix() + 60, // we are gonna check this process after 1 min
size: 0
});
})
.catch(err => {
printErrorMsg('[' + colors.green(model.nm) + '] ' + err.toString());
});
}
function createCaptureProcess(model) {
if (model.camserv < 840) { // skip models without "mobile feed"
return;
}
// just in case
if (model.phase !== 'a') {
// return;
}
let captureModel = captureModels.find(m => (m.uid === model.uid));
if (captureModel) {
printDebugMsg(colors.green(model.nm) + ' is already capturing');
return;
}
printMsg(colors.green(model.nm) + ' is now online, starting capturing process');
return createRtmpCaptureProcess(model);
}
function checkCaptureProcess(model) {
var onlineModel = onlineModels.find(m => (m.uid === model.uid));
if (onlineModel) {
if (onlineModel.mode >= 1) {
onlineModel.capturing = true;
} else if (model.captureProcess) {
// if the model was excluded or deleted we stop her "captureProcess"
printDebugMsg(colors.green(model.nm) + ' has to be stopped');
model.captureProcess.kill();
return;
}
}
// if this is not the time to check the process and resolve immediately
if (model.checkAfter > moment().unix()) {
return;
}
return fs
.statAsync(path.join(captureDirectory, model.filename))
.then(stats => {
// we check model's process every 10 minutes,
// if the size of the file has not changed for the last 10 min, we kill this process
if (stats.size > model.size) {
printDebugMsg(colors.green(model.nm) + ' is alive');
model.checkAfter = moment().unix() + 300; // 5 minutes
model.size = stats.size;
} else if (model.captureProcess) {
// we assume that onClose will do all the cleaning for us
printErrorMsg('[' + colors.green(model.nm) + '] Process is dead');
model.captureProcess.kill();
} else {
// probably we should forcefully remove the model from captureModels
// because her captureProcess is unset, but let's leave it as is for now
// remove(model, captureModels);
}
})
.catch(err => {
if (err.code === 'ENOENT') {
// do nothing, file does not exists,
} else {
printErrorMsg('[' + colors.green(model.nm) + '] ' + err.toString());
}
});
}
function saveConfig() {
if (!isDirty) {
return;
}
// remove duplicates,
// we should not have them, but just in case...
config.models = config.models.filter((m, index, self) => (index === self.indexOf(m)));
printDebugMsg('Save changes in config-rtmp.yml');
return fs
.writeFileAsync('config-rtmp.yml', yaml.safeDump(config).replace(/\n/g, EOL), 'utf8')
.then(() => {
isDirty = false;
});
}
function cacheModels() {
cachedModels = onlineModels.filter(m => (m.mode !== -1));
}
function mainLoop() {
printDebugMsg('Start new cycle');
Promise
.try(getProxyModels)
.then(getOnlineModels)
.then(updateConfigModels)
.then(selectModelsToCapture)
.then(modelsToCapture => Promise.all(modelsToCapture.map(createCaptureProcess)))
.then(() => Promise.all(captureModels.map(checkCaptureProcess)))
.then(saveConfig)
.then(cacheModels)
.catch(printErrorMsg)
.finally(() => {
printMsg(`Done, will search for new models in ${config.modelScanInterval} second(s).`);
setTimeout(mainLoop, config.modelScanInterval * 1000);
});
}
mkdir(captureDirectory);
mkdir(completeDirectory);
Promise
.try(() => detectRtmpdump())
.then(() => mfcClient.connectAndWaitForModels())
.timeout(120000) // if we could not get a list of online models in 2 minutes then exit
.then(() => mainLoop())
.catch(err => {
printErrorMsg(err.toString());
process.exit(1);
});
function addInQueue(req, res) {
let model;
let mode = 0;
if (req.url.startsWith('/models/include')) {
mode = 1;
if (req.params && req.params.expire_after) {
let expireAfter = parseFloat(req.params.expire_after);
if (!isNaN(expireAfter) && expireAfter > 0) {
mode = moment().unix() + (expireAfter * 3600);
}
}
} else if (req.url.startsWith('/models/delete')) {
mode = -1;
}
if (req.params && req.params.uid) {
let uid = parseInt(req.params.uid, 10);
if (!isNaN(uid)) {
model = { uid: uid, mode: mode };
}
} else if (req.params && req.params.nm) {
model = { nm: req.params.nm, mode: mode };
}
if (!model) {
res.writeHead(422, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Invalid request' }));
} else {
printDebugMsg(colors.green(model.uid || model.nm) + ' to ' + (mode >= 1 ? 'include' : (mode === 0 ? 'exclude' : 'delete')));
config.queue.push(model);
let cachedModel = !model.uid
? cachedModels.find(m => (m.nm === model.nm))
: cachedModels.find(m => (m.uid === model.uid));
if (cachedModel) {
cachedModel.nextMode = mode;
}
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(model)); // this will be sent back to the browser
}
}
dispatcher.onGet('/', (req, res) => {
fs.readFile(path.join(__dirname, 'index.html'), (err, data) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('Not Found');
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data, 'utf-8');
}
});
});
dispatcher.onGet('/favicon.ico', (req, res) => {
fs.readFile(path.join(__dirname, 'favicon.ico'), (err, data) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('Not Found');
} else {
res.writeHead(200, { 'Content-Type': 'image/x-icon' });
res.end(data);
}
});
});
dispatcher.onGet('/models', (req, res) => {
compress(req, res, noop);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(cachedModels));
});
// when we include the model we only "express our intention" to do so,
// in fact the model will be included in the config only with the next iteration of mainLoop
dispatcher.onGet('/models/include', addInQueue);
// whenever we exclude the model we only "express our intention" to do so,
// in fact the model will be exclude from config only with the next iteration of mainLoop
dispatcher.onGet('/models/exclude', addInQueue);
// whenever we delete the model we only "express our intention" to do so,
// in fact the model will be marked as "deleted" in config only with the next iteration of mainLoop
dispatcher.onGet('/models/delete', addInQueue);
http.createServer((req, res) => {
dispatcher.dispatch(req, res);
}).listen(config.port, () => {
printMsg('Server listening on: ' + colors.green('0.0.0.0:' + config.port));
});
function detectRtmpdump() {
return new Promise((resolve, reject) => {
let rtmpdump = childProcess.spawn('rtmpdump', [
'--help'
]);
let output = '';
rtmpdump.stderr.on('data', data => {
output += data.toString();
});
rtmpdump.on('close', code => {
patchedRtmpdump = !!(output.match(/mfc-node/));
if (patchedRtmpdump) {
printDebugMsg('Patched rtmpdump detected');
} else {
printMsg('Your ' + colors.green('rtmpdump') + ' supports only new HD models');
}
resolve();
});
});
}