forked from kjsmita6/node-steam-parentbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparentbot-es6.js
283 lines (254 loc) · 10.3 KB
/
parentbot-es6.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
'use strict'
const Steam = require('steam');
const SteamWebLogon = require('steam-weblogon');
const GetSteamApiKey = require('steam-web-api-key');
const Winston = require('winston');
const SteamTrade = require('steam-trade');
const SteamTradeOffers = require('steam-tradeoffers');
const SteamTotp = require('steam-totp');
const SteamCommunity = require('steamcommunity');
const fs = require('fs');
const crypto = require('crypto');
const readline = require('readline');
class ParentBot {
constructor(username, password, options) {
this.username = username;
this.password = password;
this.options = options || {};
this.service = this.options.service || undefined;
this.apikey = this.options.apikey || undefined;
this.sentryfile = this.options.sentryfile || this.username + '.sentry';
this.logfile = this.options.logfile || this.username + '.log';
this.guardCode = this.options.guardCode || undefined;
this.twoFactorCode = this.options.twoFactorCode || undefined;
this.sharedSecret = this.options.sharedSecret || undefined;
this.identitySecret = this.options.identitySecret || undefined;
this.confirmationInterval = this.options.confirmationInterval || undefined;
this.gamePlayed = this.options.gamePlayed || undefined;
this.steamClient = new Steam.SteamClient();
this.steamUser = new Steam.SteamUser(this.steamClient);
this.steamFriends = new Steam.SteamFriends(this.steamClient);
this.steamTrading = new Steam.SteamTrading(this.steamClient);
this.steamGameCoordinator = (this.options.gamePlayed ? new Steam.SteamGameCoordinator(this.steamClient, parseInt(this.options.gamePlayed)) : undefined);
this.steamRichPresence = (this.options.richPresenceID ? new Steam.SteamRichPresence(this.steamClient, parseInt(this.options.richPresenceID)) : undefined);
this.steamUnifiedMessages = (this.options.service ? new Steam.SteamUnifiedMessages(this.steamClient, this.options.service) : undefined);
this.steamWebLogon = new SteamWebLogon(this.steamClient, this.steamUser);
this.community = new SteamCommunity();
this.steamTrade = new SteamTrade();
this.offers = new SteamTradeOffers();
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
this.logger = new (Winston.Logger)({
transports: [
new (Winston.transports.Console)({
colorize: true,
timestamp: true,
label: this.username,
level: 'silly',
json: false
}),
new (Winston.transports.File)({
level: 'debug',
timestamp: true,
json: false,
filename: this.logfile
})
]
});
if (this.sentryfile) {
fs.existsSync(this.sentryfile)
? this.logger.info('Using sentry file ' + this.sentryfile)
: this.logger.warn('Sentry defined in options doesn\'t exist and will be created on successful login');
}
//SteamClient events
this.steamClient.on('error', () => { this._onError() });
this.steamClient.on('connected', () => { this._onConnected() });
this.steamClient.on('logOnResponse', res => { this._onLogOnResponse(res) });
this.steamClient.on('loggedOff', eresult => { this._onLoggedOff(eresult) });
this.steamClient.on('debug', this.logger.silly);
//SteamUser events
this.steamUser.on('updateMachineAuth', (res, callback) => { this._onUpdateMachineAuth(res, callback) });
//SteamFriends events
this.steamFriends.on('friendMsg', (steamID, message, type) => { this._onFriendMsg(steamID, message, type) });
this.steamFriends.on('friend', (steamID, relationship) => { this._onFriend(steamID, relationship); });
}
connect() {
this.steamClient.connect();
this.logger.debug('Connecting to Steam...');
}
logOn() {
this.logger.debug('Logging in...');
try {
let sha = '';
if (fs.existsSync(this.sentryfile)) {
let file = fs.readFileSync(this.sentryfile);
sha = crypto.createHash('sha1').update(file).digest();
}
if (this.options.guardCode) {
this.steamUser.logOn({
account_name: this.username,
password: this.password,
auth_code: this.guardCode,
sha_sentryfile: sha
});
}
else if (this.options.twoFactorCode) {
this.steamUser.logOn({
account_name: this.username,
password: this.password,
two_factor_code: this.twoFactorCode,
sha_sentryfile: sha
});
}
else if (this.options.sharedSecret) {
this.steamUser.logOn({
account_name: this.username,
password: this.password,
two_factor_code: SteamTotp.generateAuthCode(this.options.sharedSecret),
sha_sentryfile: sha
});
}
else {
this.steamUser.logOn({
account_name: this.username,
password: this.password,
sha_sentryfile: sha
});
}
}
catch (err) {
this.logger.error('Error logging in: ' + err);
}
}
_onError() {
this.logger.error('Disconnected from Steam, reconnecting...');
this.connect();
}
_onConnected() {
this.logger.verbose('Connected to Steam, logging in...');
this.logOn();
}
_onLogOnResponse(response) {
if (response.eresult === Steam.EResult.OK) {
this.logger.info('Logged into Steam!');
this.steamFriends.setPersonaState(Steam.EPersonaState = 1);
this.steamUser.gamesPlayed({ "games_played": [{ "game_id": (this.gamePlayed ? parseInt(this.gamePlayed) : null) }] });
this.steamWebLogon.webLogOn((webSessionID, cookies) => {
this.community.setCookies(cookies);
cookies.forEach(cookie => {
this.steamTrade.setCookie(cookie.trim());
});
if (this.confirmationInterval && this.identitySecret) {
this.community.startConfirmationChecker(this.confirmationInterval, this.identitySecret);
}
this.community.enableTwoFactor((e, response) => {
if(e) {
if(parseInt(e.eresult) === 2) {
this.logger.error('Failed to enable two factor. Check if you have a phone number enabled for this account.');
this.logger.error(e.stack);
}
else if(parseInt(e.eresult) === 29) {
this.logger.warn('Already have 2FA enabled');
}
else {
this.logger.error(e.stack);
}
}
else {
this.logger.verbose('Writing information to ' + this.username + '.2fa_info. Please add shared_secret and identity_secret ' +
'to your config as options sharedSecret and identitySecret respectively.');
fs.writeFileSync(this.username + '.2fa_info', JSON.stringify(response, null, 2));
this.finalizeTwoFactor(response);
}
});
this.steamTrade.sessionID = webSessionID;
if (!this.apikey) {
GetSteamApiKey({
sessionID: webSessionID,
webCookie: cookies
}, (e, api) => {
if (e) this.logger.error('Error getting API key: ' + e);
else {
this.apikey = api;
this.offers.setup({
sessionID: webSessionID,
webCookie: cookies,
APIKey: this.apikey
});
}
});
}
else {
this.offers.setup({
sessionID: webSessionID,
webCookie: cookies,
APIKey: this.apikey
});
}
this.logger.info('Logged into Steam web');
});
}
else {
this.logger.warn('EResult for logon: ' + response.eresult);
if(response.eresult === 63) {
this.logger.warn('Please provide the steamguard code sent to your email at ' + response.email_domain);
process.exit(63);
}
}
}
finalizeTwoFactor(res) {
this.rl.question('Code received by SMS: ', code => {
this.community.finalizeTwoFactor(res.shared_secret, code, e => {
if(e) {
if(e.message === 'Invalid activation code') {
this.logger.error('Invalid activation code, please try again');
this.finalizeTwoFactor(res);
}
else {
this.logger.error(e.stack);
}
}
else {
this.logger.info('Two factor auth enabled.');
}
});
});
}
_onLoggedOff() {
this.logger.error('Logged off of Steam, logging in again...');
this.logOn();
}
_onUpdateMachineAuth(response, callback) {
this.logger.debug('New sentry: ' + response.filename);
fs.writeFileSync(this.sentryfile, response.bytes);
callback({
sha_file: crypto.createHash('sha1').update(response.bytes).digest()
});
}
_onFriendMsg(steamID, message, type) {
if (type === Steam.EChatEntryType.ChatMsg) {
this.logger.info('Message from ' + steamID + ': ' + message);
this.steamFriends.sendMessage(steamID, 'Hi, thanks for messaging me! If you are getting this message, it means that my ' +
'owner hasn\'t configured me properly. Annoy them with messages until they do!');
}
}
_onFriend(steamID, relationship) {
if (relationship === Steam.EFriendRelationship.RequestRecipient) {
this.steamFriends.addFriend(steamID);
this.steamFriends.sendMessage(steamID, 'Hi, thanks for adding me! If you are getting this message, it means that my ' +
'owner hasn\'t configured me properly. Annoy them with messages until they do!');
}
}
static get Steam() {
return Steam;
}
static get SteamCommunity() {
return SteamCommunity;
}
static get SteamWebApiKey() {
return GetSteamApiKey;
}
}
module.exports = ParentBot;