-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·469 lines (403 loc) · 11.9 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
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
#! /usr/bin/node --use_strict
// requires
var fs = require('fs');
var logger = require('tru-logger');
var mkdirp = require('mkdirp');
var SteamID = require('steamid');
var SteamUser = require('steam-user');
var SteamTotp = require('steam-totp');
var SteamCommunity = require('steamcommunity');
var TradeOfferManager = require('steam-tradeoffer-manager');
// cli paramters
var args = process.argv.splice(2);
var username;
if(args[0]) {
username = args[0];
} else if(process.env.STEAMBOT_USERNAME) {
username = process.env.STEAMBOT_USERNAME;
} else {
throw Error("MISTAKE: no username provided");
}
var client = new SteamUser();
var community = new SteamCommunity();
// config files
var configsDir = __dirname + "/configs";
var configFile = configsDir + "/config.json";
var accountConfigFile = configsDir + "/" + username + ".json";
// variables
var logDir = __dirname + "/logs/";
mkdirp(logDir, function(err) {
// path was created unless there was error
});
var dataDirectory = __dirname + "/data";
var admin_usernames = [];
var admin_ids = [];
var admins = {};
var my_steamid;
var lastTwoFactorCode = null;
var config = [];
var account_config = [];
// number of seconds to wait between login attempts
var loginSleepTime = 10;
// main config
if(fs.existsSync(configFile)) {
var data = fs.readFileSync(configFile, 'utf8');
if(data != undefined) {
config = JSON.parse(data);
} else {
throw Error("MISTAKE: there was a problem reading the config file: " + configFile);
}
} else {
throw Error("MISTAKE: configFile does not exist: " + configFile);
}
// account config
if(fs.existsSync(accountConfigFile)) {
var data = fs.readFileSync(accountConfigFile, 'utf8');
if(data != undefined) {
account_config = JSON.parse(data);
} else {
throw Error("MISTAKE: there was a problem reading the config file: " + accountConfigFile);
}
} else {
throw Error("MISTAKE: accountConfigFile does not exist: " + accountConfigFile);
}
// make sure there is a username defined
if(account_config.username == undefined) {
throw new Error("Please specify username");
}
// make sure there is a password defined
if(account_config.password == undefined) {
throw new Error("Please specify password");
}
// initialize log
var logOptions = {
file: logDir + account_config.username + ".txt",
date: true,
print: true,
write_color: true,
//log_level: ["success", "error"],
log_level: ["all"],
prefix: account_config.username
};
var myLog = new logger(logOptions);
if(config.admin_accounts) {
var sid;
for(var account in config.admin_accounts) {
admin_usernames.push(account);
if(config.admin_accounts[account].id != undefined) {
sid = new SteamID(config.admin_accounts[account].id);
admins[config.admin_accounts[account].id] = account;
admin_ids.push(sid);
}
}
}
function isAdminSteamID(sid) {
sid = JSON.stringify(sid);
for(obj in admin_ids) {
if(sid == JSON.stringify(admin_ids[obj])) {
return true;
}
}
return false;
}
var manager = new TradeOfferManager({
"steam": client,
"domain": "trucraft.net",
"language": "en"
});
if(fs.existsSync('polldata.json')) {
manager.pollData = JSON.parse(fs.readFileSync('polldata.json'));
}
var logOnOptions = getLogonOptions();
/**
* steam-user client methods BEGIN
*/
client.setOptions({"dataDirectory": dataDirectory});
client.logOn(logOnOptions);
client.on('loggedOn', function(details) {
my_steamid = client.steamID;
myLog.success("Logged into Steam as " + client.steamID.getSteam3RenderedID());
client.setPersona(SteamUser.Steam.EPersonaState.Online);
//client.gamesPlayed(440);
client.getSteamGuardDetails(function(enabled, enabledTime, machineTime, canTrade) {
/*myLog.info(enabled);
myLog.info(enabledTime);
myLog.info(machineTime);
myLog.info(canTrade);*/
});
});
client.on('error', function(e) {
if(e = "Error: InvalidPassword") {
account_config.login_key = undefined;
// write new login key to config file
client.logOff();
var logOnOptions = getLogonOptions();
client.logOn(logOnOptions);
}
// Some error occurred during logon
myLog.error("Undefined error: " + e);
});
client.on('webSession', function(sessionID, cookies) {
manager.setCookies(cookies, function(err) {
if(err) {
myLog.error(err);
process.exit(1); // Fatal error since we couldn't get API key
}
myLog.success("Got API key: " + manager.apiKey);
});
});
client.on('newItems', function(count) {
myLog.success(count + " new items in this account's inventory");
});
client.on('emailInfo', function(address, validated) {
myLog.info("The email address of this account is " + address + " and it's " + (validated ? "validated" : "not validated"));
});
client.on('wallet', function(hasWallet, currency, balance) {
myLog.info("This account's wallet balance is " + SteamUser.formatCurrency(balance, currency));
});
client.on('accountLimitations', function(limited, communityBanned, locked, canInviteFriends) {
var limitations = [];
if(limited) {
limitations.push('LIMITED');
}
if(communityBanned) {
limitations.push('COMMUNITY BANNED');
}
if(locked) {
limitations.push('LOCKED');
}
if(limitations.length === 0) {
myLog.info("This account has no limitations.");
} else {
myLog.warning("This account is " + limitations.join(', ') + ".");
}
if(canInviteFriends) {
myLog.info("This account can invite friends.");
}
});
client.on('vacBans', function(numBans, appids) {
myLog.warning("This account has " + numBans + " VAC ban" + (numBans == 1 ? '' : 's') + ".");
if(appids.length > 0) {
myLog.error("This account is VAC banned from apps: " + appids.join(', '));
}
});
client.on('licenses', function(licenses) {
myLog.chat("This account owns " + licenses.length + " license" + (licenses.length == 1 ? '' : 's') + ".");
});
client.on('loginKey', function(loginKey) {
myLog.info("New Login Key: " + loginKey);
account_config.login_key = loginKey;
// write new login key to config file
fs.writeFile(accountConfigFile, JSON.stringify(account_config, null, "\t"), function(err) {
if(err) {
myLog.error(err);
}
myLog.info("The config file \"" + accountConfigFile + "\" was saved");
});
});
client.on("sentry", function(sentry) {
myLog.attention(sentry);
});
/**
* steam-user client methods END
*/
/**
* steam-tradeoffer-manager methods BEGIN
*/
manager.on('newOffer', function(offer) {
myLog.warning("New offer #" + offer.id + " from " + offer.partner.getSteam3RenderedID());
processOffer(offer, function(passed) {
if(passed) {
community.checkConfirmations();
}
});
});
manager.on('receivedOfferChanged', function(offer, oldState) {
myLog.info("Offer #" + offer.id + " changed: " + TradeOfferManager.ETradeOfferState[oldState] + " -> " + TradeOfferManager.ETradeOfferState[offer.state]);
if(offer.state == TradeOfferManager.ETradeOfferState.Accepted) {
offer.getReceivedItems(function(err, items) {
if(err) {
myLog.error("Couldn't get received items: " + err);
} else {
if(items.length > 0) {
var names = items.map(function(item) {
return item.name;
});
myLog.success("Received: " + names.join(', '));
}
}
});
}
});
/**
* steam-tradeoffer-manager methods END
*/
var login_details;
getLoginDetails();
function getLoginDetails() {
login_details = {
"accountName": account_config.username,
"password": account_config.password,
"twoFactorCode": SteamTotp.generateAuthCode(account_config.steam_guard.shared_secret)
};
}
community.loggedIn(function(err, loggedIn) {
if(err != null) {
myLog.error(err);
process.exit(1);
}
if(loggedIn) {
myLog.success("Already logged into community");
} else {
community_login(function() {
myLog.info("Starting comfirmation polling");
community.startConfirmationChecker(10000, account_config.steam_guard.identity_secret);
myLog.info("Checking confirmations");
community.checkConfirmations();
});
}
});
function community_login(callback) {
if(lastTwoFactorCode != login_details.twoFactorCode) {
lastTwoFactorCode = login_details.twoFactorCode;
community.login(login_details, function(err) {
myLog.info("Logging into community");
if(err != null) {
myLog.error("Failed to login to community: " + err);
community_login(callback);
} else {
myLog.success("Successfully logged into community");
if(typeof(callback) == "function") {
callback();
}
}
});
} else {
myLog.warning("SteamGuard code has not changed yet: " + lastTwoFactorCode + " == " + login_details.twoFactorCode);
myLog.attention("Sleeping " + loginSleepTime + " seconds and trying again...");
// wait for loginSleepTime seconds and try again
getLoginDetails();
setTimeout(function() {
community_login(callback);
}, 1000 * loginSleepTime);
}
}
community.on("newConfirmation", function(confirmation) {
myLog.attention("New confirmation");
processConfirmation(confirmation, function(passed) {
myLog.success(passed);
});
});
community.on("confKeyNeeded", function(tag, callback) {
myLog.warning("Confirmation key needed");
var time = Math.floor(Date.now() / 1000);
myLog.info(callback.toString());
callback(null, time, SteamTotp.getConfirmationKey(account_config.steam_guard.identity_secret, time, tag));
});
function processConfirmation(confirmation, callback) {
var time = Math.floor(Date.now() / 1000);
var key = SteamTotp.getConfirmationKey(account_config.steam_guard.identity_secret, time, "conf");
community.respondToConfirmation(confirmation.id, confirmation.key, time, key, true, function(err) {
myLog.info("Responding to confirmation " + confirmation.id);
var ret = true;
if(err != null) {
myLog.error(err);
ret = false;
myLog.error("Responding to confirmation " + confirmation.id + " failed");
} else {
myLog.success("Responding to confirmation " + confirmation.id + " succeeded");
}
if(typeof(callback) == "function") {
callback(ret);
}
});
}
function processOfferByID(id, callback) {
myLog.attention("Process offer by id " + id);
getOffer(id, function(err, offer) {
if(err != null) {
myLog.error(err);
if(typeof(callback) == "function") {
callback(false);
}
} else {
processOffer(offer, callback);
}
});
}
function processOffer(offer, callback) {
myLog.attention("Process offer");
if(isAdminSteamID(offer.partner)) {
var partner_name;
var partner_id = offer.partner.getSteamID64();
if(admins[partner_id] !== undefined) {
partner_name = admins[partner_id];
} else {
partner_name = "unknown";
}
myLog.attention("## Admin offer from " + partner_name);
offer.accept(function(err) {
var ret = true;
if(err) {
myLog.error("Unable to accept offer from " + partner_name + ": " + err.message);
if(err.message == "HTTP error 403") {
// logoff and log back on
setTimeout(function() {
client.logOff();
}, 1000 * loginSleepTime);
var logOnOptions = getLogonOptions();
setTimeout(function() {
client.logOn(logOnOptions);
}, 1000 * loginSleepTime);
}
//client.webLogOn();
//processOffer(offer, callback);
ret = false;
} else {
myLog.success("Offer from " + partner_name + " accepted");
}
if(typeof(callback) == "function") {
callback(ret);
}
});
} else {
myLog.warning("## Not an admin");
if(typeof(callback) == "function") {
callback(false);
}
}
}
function getLogonOptions() {
var options = {
"accountName": account_config.username
};
if(account_config.login_key != undefined) {
options.loginKey = account_config.login_key;
} else {
if(account_config.password != undefined) {
options.password = account_config.password;
options.rememberPassword = true;
}
}
if(account_config.steam_guard.shared_secret != undefined) {
options.twoFactorCode = SteamTotp.generateAuthCode(account_config.steam_guard.shared_secret);
}
//myLog.info(options);
return options;
}
function showMethods(obj) {
myLog.attention("\nMETHODS\n");
for(var m in obj) {
if(typeof obj[m] == "function") {
myLog.chat(m);
}
}
}
function showMembers(obj) {
myLog.attention("\nMEMBERS\n");
for(var m in obj) {
if(typeof obj[m] != "function") {
myLog.chat(m);
}
}
}