-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.js
executable file
·211 lines (175 loc) · 7.21 KB
/
main.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
// SN4T14 2015-05-13
// License: WTFPL
// jshint node: true
'use strict';
var Promise = require('bluebird');
var yaml = require('js-yaml');
var fs = Promise.promisifyAll(require('fs'));
var bhttp = require('bhttp');
var cheerio = require('cheerio');
var moment = require('moment');
var childProcess = require('child_process');
var mkdirp = require('mkdirp');
var S = require('string');
var errors = require('errors');
var config = yaml.safeLoad(fs.readFileSync('config.yml', 'utf8'));
config.captureDirectory = S(config.captureDirectory).stripRight('/').s; // Because people will inevitably be idiots and sometimes use a trailing slash and sometimes not
var session = bhttp.session();
var modelsCurrentlyCapturing = [];
errors.create({
name: "ModelOfflineError",
explanation: "Model appears offline, it's normal for this to happen occasionally, if this happens to models that you know are online, you should file an issue on GitHub."
});
mkdirp(config.captureDirectory, function (err) {
if (err) {
console.log(err);
}
});
var debugPrint = function (printString) {
if (config.debug) {
console.log("[" + getCurrentDateTime() + "]", printString);
}
};
var getCurrentDateTime = function() {
return moment().format("YYYY-MM-DDThhmmss"); // The only true way of writing out dates and times, ISO 8601
};
var getFileSize = function (filename) {
return Promise.try(function() {
return fs.statsAsync(filename);
}).then(function (stats) {
return stats.size;
});
};
var getCommandArguments = function (modelName) {
return Promise.try(function() {
return session.get("https://chaturbate.com/" + modelName + "/");
}).then(function (response) {
var commandArguments = {
modelName: modelName,
username: config.username.toLowerCase(), // Username has to be in lowercase for authentication to work
captureDirectory: config.captureDirectory,
dateString: getCurrentDateTime()
};
var $ = cheerio.load(response.body);
var scripts = $("script")
.map(function(){
return $(this).text();
}).get().join("");
var streamData = scripts.match(/EmbedViewerSwf\(([\s\S]+?)\);/); // "EmbedViewerSWF" is ChaturBate's shitty name for the stream data, all their code has really cryptic names for everything
if (streamData !== null) {
commandArguments.streamServer = streamData
[1]
.split(",")
.map(function (line) {
return S(line.trim()).strip("'", '"').s;
})
[2];
} else {
throw new errors.ModelOfflineError();
}
commandArguments.passwordHash = scripts.match(/password: '([^']+)'/)[1].replace("\\u003D", "="); // As of 2015-05-15, this is a PBKDF2-SHA256 hash of the user's password, with the iteration count and salt generously provided. I could replace the empty line below with a line to make bhttp send this hash to my own server, where I'd be able to crack it at my leisure, but as you can see, that line is empty, you're welcome. :)
return commandArguments;
});
};
var capture = function (modelName) {
Promise.try(function() {
return getCommandArguments(modelName);
}).then(function (commandArguments) {
var filename = "./" + commandArguments.captureDirectory + "/Chaturbate_" + commandArguments.dateString + "_" + commandArguments.modelName + ".flv";
var spawnArguments = [
"--live",
config.debug ? "" : "--quiet",
"--rtmp",
"rtmp://" + commandArguments.streamServer + "/live-edge",
"--pageUrl",
"http://chaturbate.com/" + commandArguments.modelName,
"--conn",
"S:" + commandArguments.username,
"--conn",
"S:" + commandArguments.modelName,
"--conn",
"S:2.645", // Apparently this is the flash version, fucked if I know why this is needed, this seems to be extracted from a file listed two lines above where the streamServer is grabbed, with "p" in the filename changed to ".", and the path and extension removed
"--conn",
"S:" + commandArguments.passwordHash, // "Hey guys, know what'd be a great idea? Authenticating connections by passing the password hash to the client and back!"
"--token",
"m9z#$dO0qe34Rxe@sMYxx", // 0x5f3759df
"--playpath",
"playpath",
"--flv",
filename
];
var captureProcess = childProcess.spawn("rtmpdump", spawnArguments);
captureProcess.on("close", function (code) {
console.log("[" + getCurrentDateTime() + "]", commandArguments.modelName, "stopped streaming.");
var modelIndex = modelsCurrentlyCapturing.indexOf(modelName);
if(modelIndex !== -1) {
modelsCurrentlyCapturing.splice(modelIndex, 1);
}
Promise.try(function() {
return getFileSize(filename);
}).then(function (fileSize) {
if (fileSize === 0) {
return fs.unlinkAsync(filename);
}
});
});
captureProcess.stdout.on("data", function (data) {
console.log("[" + getCurrentDateTime() + "]", data.toString());
});
captureProcess.stderr.on("data", function (data) {
console.log("[" + getCurrentDateTime() + "]", data.toString());
});
}).catch(errors.ModelOfflineError, function (e) {
console.log("[" + getCurrentDateTime() + "]", e.explanation);
var modelIndex = modelsCurrentlyCapturing.indexOf(modelName);
if(modelIndex !== -1) {
modelsCurrentlyCapturing.splice(modelIndex, 1);
}
});
};
var getLiveModels = function() {
return Promise.try(function() {
return session.get("https://chaturbate.com/followed-cams/");
}).then(function (response) {
var $ = cheerio.load(response.body);
return $("#main div.content ul.list").children("li")
.filter(function(){
return $(this).find("div.details ul.sub-info li.cams").text() != "offline";
})
.map(function(){
return $(this).find("div.title a").text().trim();
})
.get();
});
};
var chaturbateLogin = function() {
return Promise.try(function() {
return session.get("https://chaturbate.com/auth/login/");
}).then(function (response) {
var $ = cheerio.load(response.body);
var csrfToken = $("#main form[action='/auth/login/'] input[name='csrfmiddlewaretoken']").val();
return session.post("https://chaturbate.com/auth/login/", {username: config.username, password: config.password, csrfmiddlewaretoken: csrfToken, next: "/"}, {headers: {"referer": "https://chaturbate.com/auth/login/"}});
});
};
var mainLoop = function() {
Promise.try(function() {
debugPrint("Logging in to Chaturbate (it's normal for this to happen all the time)");
return chaturbateLogin();
}).then(function (response) {
return getLiveModels();
}).then(function (liveModels) {
debugPrint("Found these live followed models: " + liveModels.toString());
liveModels.forEach(function (liveModel) {
if (modelsCurrentlyCapturing.indexOf(liveModel) === -1) {
console.log("[" + getCurrentDateTime() + "]", liveModel, "is now online, starting rtmpdump process");
modelsCurrentlyCapturing.push(liveModel);
capture(liveModel);
}
});
}).then(function() {
debugPrint("Started capturing all models found, will search for new models in " + config.modelScanInterval);
setTimeout(mainLoop, config.modelScanInterval);
});
};
console.log("[" + getCurrentDateTime() + "]", "capturebate-node started"); // Lol lies this is the first thing it does that isn't a variable definition
mainLoop();