-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
355 lines (296 loc) · 10.2 KB
/
index.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
'use strict';
const fs = require('fs');
const request = require('request');
const Cheerio = require('cheerio');
const SteamTotp = require('steam-totp');
const _ = require('underscore');
const events = require('events');
const Confirmation = require('./classes/Confirmation.js');
class SteamCommunityMobileConfirmations {
constructor(data) {
// Initialize values from the data object
this.steamId = data.steamId;
this.identitySecret = data.identitySecret;
this.deviceId = data.deviceId || SteamTotp.getDeviceID(this.steamId);
this.offset = data.steamOffset || 0;
this.timeBetweenCalls = data.waitTime || 10000;
// Initialize all others
this.STEAM_BASE = 'https://steamcommunity.com'
this.has429Error = false;
this.needsNewSession = false;
this.errorEvent = new events.EventEmitter;
this._requestJar = request.jar();
this._request = request.defaults({ jar: this._requestJar });
// Set each cookie NOM NOM
for (let cookie of data.webCookie) {
this._requestJar.setCookie(request.cookie(cookie), this.STEAM_BASE);
}
}
/**
* Updates the cookies if the session expired
* @param {Object} webCookie The new set of cookies
* @return {void}
*/
updateCookies(webCookie) {
for (let cookie of webCookie) {
this._requestJar.setCookie(request.cookie(cookie), this.STEAM_BASE);
}
this.needsNewSession = false;
}
/**
* Grabs all confirmations
* @param {Function} callback The function callback'd.
* @return {void}
*/
fetchConfirmations(callback) {
let url = `${this.STEAM_BASE}/mobileconf/conf?${this._generateQueryString('conf')}`;
this.request(url, 'GET', (err, response, body) => {
let error = checkResponse(err, response, body);
if (error) {
callback(error, null);
return;
}
// Parse our confirmations
let confirmations = [];
let finalConfirmations = [];
let $ = Cheerio.load(body);
fs.writeFileSync(`output.html`, body);
// Go through each confirmation, and generate a new Confirmation object
$('[data-confid]').each((index, element) => {
let $confirmation = $(element);
confirmations.push(new Confirmation({
id: $confirmation.data('confid'),
type: $confirmation.data('type'),
key: $confirmation.data('key'),
tradeId: $confirmation.data('creator')
}));
});
callback(null, confirmations);
});
}
/**
* Sends the response to accept the confirmation
* @param {Confirmation} confirmation The Confirmation we are responding too (or Confirmation[])
* @param {Function} callback
* @param {boolean} secondTry Whether or not we try to accept again. undefined for false
* @return {void}
*/
acceptConfirmation(confirmation, callback, secondTry) {
var handleConfirmationResponse = (error, result) => {
if (error || !result.success) {
console.log('Failed to accept first time: ' + error || result);
if (secondTry == undefined) {
setTimeout(() => {
this.acceptConfirmation(confirmation, callback, true);
}, this.timeBetweenCalls);
return;
}
callback(error, false);
return;
}
callback(null, true);
};
if (Array.isArray(confirmation)) {
this._sendMultiConfirmationResponse(confirmation, 'allow', handleConfirmationResponse);
} else {
this._sendConfirmationResponse(confirmation, 'allow', handleConfirmationResponse);
}
}
/**
* This retrieves the tradeId from a confirmation
* @param {Confirmation} confirmation The specific confirmation
* @param {Function} callback
* @return {void}
*/
getConfirmationTradeId(confirmation, callback) {
this._getConfirmationDetails(confirmation, (error, body) => {
if (error) {
callback(error, confirmation);
return;
}
let $ = Cheerio.load(body);
fs.writeFileSync(`output-details.html`, body);
let tradeId = $('div.tradeoffer').attr('id').match(/\d+/i);
callback(null, tradeId);
});
}
/**
* Our custom request call, applies headers where needed / etc
* @param {String} url The URL to be requested
* @param {String} method The HTTP method (GET, POST, PUT, DELETE)
* @param {Object} form (Optional) A form to be sent, only works if method is POST
* @param {Function} callback
* @return {void}
*/
request(url, method, form, callback) {
if (this.needsNewSession) {
console.error('Waiting for new session before calling request!');
setTimeout(() => {
this.request(url, method, form, callback);
}, this.timeBetweenCalls);
return;
}
if (typeof form === 'function') {
callback = form;
form = {};
}
// Generate some headers to simulate a real application
let headers = {
'accept': 'text/javascript, text/html, application/xml, text/xml, */*',
'user-agent': 'Mozilla/5.0 (Linux; U; Android 4.1.1; en-us; Google Nexus 4 - 4.1.1 - API 16 - 768x1280 Build/JRO03S) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'
};
this._request({
method: method,
url: url,
headers: headers,
form: form
}, (error, response, body) => {
if (error) {
console.error(error);
console.error(response.statusCode);
console.error(body);
this.needsNewSession = true;
this.errorEvent.emit('needsNewSession');
setTimeout(() => {
this.request(url, method, form, callback);
}, this.timeBetweenCalls);
return;
} else if (response.statusCode == 429) {
// If we have a 429 error, because volvo hates us, wait for 3x the normal wait, and then try again.
this.has429Error = true;
setTimeout(() => {
this.request(url, method, form, callback);
}, this.timeBetweenCalls * 3);
return;
}
callback(error, response, body);
});
}
/**
* Sends a confirmation response
* @param {Confirmation} confirmation The Confirmation we are responding too
* @param {String} operation allow / cancel
* @param {Function} callback The function callback'd
* @return {void}
*/
_sendConfirmationResponse(confirmation, operation, callback) {
let url = `${this.STEAM_BASE}/mobileconf/ajaxop?op=${operation}&${this._generateQueryString(operation)}&cid=${confirmation.id}&ck=${confirmation.key}`;
this.request(url, 'GET', (err, response, body) => {
let error = checkResponse(err, response, body);
if (error) {
callback(error, null);
return;
}
try {
let result = JSON.parse(body);
callback(null, result);
} catch (e) {
callback(new Error('Failed to parse body'), null);
}
});
}
/**
* Allows multiple confirmations to be sent at once (either all accept, or all deny)
* @param {Array} confirmations The confirmation array
* @param {String} operation accept / deny
* @param {Function} callback
* @return {void}
*/
_sendMultiConfirmationResponse(confirmations, operation, callback) {
let url = `${this.STEAM_BASE}/mobileconf/multiajaxop`;
let queryVariables = this._generateQueryVariables(operation);
// Initialize our form
let form = {
op: operation,
cid: [],
ck: []
};
// Move our query variables to the new form
_.extend(form, queryVariables);
// Loop over each confirmation, adding each id / key to their respective array
for (let confirmation of confirmations) {
form.cid.push(confirmation.id);
form.ck.push(confirmation.key);
}
this.request(url, 'POST', form, (err, response, body) => {
let error = checkResponse(err, response, body);
if (error) {
callback(error, null);
return;
}
try {
let result = JSON.parse(body);
callback(null, result);
} catch (e) {
callback(new Error('Failed to parse body'), null);
}
});
}
/**
* Gets the confirmation details
* @param {Confirmation} confirmation The Confirmation we care about
* @param {Function} callback
* @return {void}
*/
_getConfirmationDetails(confirmation, callback) {
let url = `${this.STEAM_BASE}/mobileconf/details/${confirmation.id}?${this._generateQueryString('details')}`;
this.request(url, 'GET', (err, response, body) => {
let error = checkResponse(err, response, body);
if (error) {
callback(error, null);
return;
}
callback(null, body);
});
}
/**
* Generates the HTTP query string, to be prepended to all GET requests
* @param {String} tag The action. "conf" to load the confirmations page, "details" to load details about a trade, "allow" to confirm a trade, "cancel" to cancel it.
* @return {String} The generated string
*/
_generateQueryString(tag) {
let queryVariables = this._generateQueryVariables(tag);
let queryString = '';
// Generate our string
for (let name in queryVariables) {
queryString += `${name}=${queryVariables[name]}&`;
}
// Remove trailing &
queryString = queryString.slice(0, -1);
return queryString;
}
/**
* Actually generate the needed variables
* @param {String} tag Action tag
* @return {Object} The variables, zipped together a nice & neat object!
*/
_generateQueryVariables(tag) {
let time = SteamTotp.time(this.steamOffset);
let result = {
p: this.deviceId,
a: this.steamId,
k: SteamTotp.generateConfirmationKey(this.identitySecret, time, tag),
t: time,
m: 'android',
tag: tag
};
return result;
}
}
/**
* Checks the HTTP response
* @param {Error} error An HTTP error
* @param {Object} response The HTTP response object
* @param {Object} body The HTTP body
* @return {Error} An error, if one exists (null otherwise)
*/
function checkResponse(error, response, body) {
if (error || response.statusCode != 200) {
return error || new Error(response.statusCode);
}
if (!body) {
return new Error('Invalid response body');
}
return null;
}
module.exports = SteamCommunityMobileConfirmations;