-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
256 lines (217 loc) · 6.79 KB
/
app.js
File metadata and controls
256 lines (217 loc) · 6.79 KB
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
const SteamUser = require('steam-user');
const GlobalOffensive = require('globaloffensive');
const http = require('http');
const Config = require('./config.json');
const readline = require('readline');
const EventEmitter = require('events').EventEmitter;
const SteamTotp = require('steam-totp');
const ws = require('nodejs-websocket');
class Bot extends EventEmitter {
constructor({username, password, twoFactorCode} = {}) {
super();
this.username = username;
const client = new SteamUser({enablePicsCache: true});
this.log("Attempting to log in...");
client.logOn({
accountName: username,
password: password,
twoFactorCode: twoFactorCode,
promptSteamGuardCode: false
});
let loggedOnOnce = false;
client.on('loggedOn', _ => {
if(!loggedOnOnce) {
this.emit("loggedOn");
loggedOnOnce = true;
}
this.log(`Connected to Steam.`);
client.setPersona(1);
})
client.on('friendMessage', (steamid, message) => {
client.chatMessage(steamid, "This account is currently being used as an inspect float bot.")
})
this.csgoAlreadyConnected = false;
this.isPlayingCSGO = false;
client.on('appOwnershipCached', _ => {
// if already connected to CSGO, then just return, also make sure this bot OWNS csgo
if(this.csgoAlreadyConnected) return;
if(!client.ownsApp(730)) return this.log("I don't own CSGO!");
this.log("I own CSGO, connecting to GC");
// connect to csgo
this.csgoAlreadyConnected = true;
let csgo = new GlobalOffensive(client);
this.csgo = csgo;
this.playCSGO();
// emit that this bot is ready
csgo.on('connectedToGC', _ => {
this.log("Connected to CSGO")
this.emit("ready");
})
})
this.client = client;
this.errorCount = 0;
}
playCSGO() {
this.errorCount = 0;
if(this.isPlayingCSGO) {
this.client.gamesPlayed([]);
setTimeout(_ => {
this.client.gamesPlayed([730]);
}, 3000);
} else {
this.client.gamesPlayed([730]);
}
this.isPlayingCSGO = true;
}
getFloat(args, retryCount) {
let {sm, a, d} = args;
// 2 second timeout... (we can assume error if it takes longer than this) It will auto-retry 3 times.
let inspectTimeout = setTimeout(_ => {
if(this.errorCount >= 30) { // 30 errors, maybe re-connect to GC
this.log("Had 30 failures. I'm reconnecting to the game coordinator.");
return this.playCSGO();
}
if(retryCount > 2) {
console.log(`Couldn't get float for ${a}`);
floatEvents.emit(`itemfloat-${a}`, null);
this.emit("ready");
} else {
console.log(`Retrying ${a}`);
this.getFloat(args, ++retryCount || 1);
this.errorCount++;
}
}, 2000);
// time that this was initated, so I can offset the "ready" callback
let timeInitiated = Date.now();
this.csgo.inspectItem(sm, a, d, item => {
clearTimeout(inspectTimeout); // clear the error timeout
let wear = (+item.paintwear).toFixed(8);
// emit to all listeners that this item's float is ready
cache[a] = wear
floatEvents.emit(`itemfloat-${a}`, wear);
setTimeout(_ => { // wait 1 second minus how long it took to get this item's float
this.emit("ready");
}, timeInitiated - Date.now() + 1000);
});
}
log(str) {
console.log(`[${this.username}] ${str}`)
}
}
const floatEvents = new EventEmitter();
const cache = {};
const freeBots = [];
/**
* @type {{sm: string, a: string, d: string}[]}
*/
const queue = [];
let rl;
//Start all the bots that have shared secrets and can generate their own 2fa codes
let bots = Config.bots.filter(data => data.shared_secret).map(data => {
data.twoFactorCode = SteamTotp.generateAuthCode(data.shared_secret);
let bot = new Bot(data);
bot.on("ready", _ => {
if(queue.length) { // there is stuff in the queue, this bot should be assigned to something.
let nextItem = queue.pop();
bot.getFloat(decodeInspectUrl(nextItem));
} else {
freeBots.unshift(bot); // theres nothing to be done, so put this bot into the free bots poool
}
})
return bot;
})
let manualBots = Config.bots.filter(data => !data.shared_secret);
if(manualBots.length) { // need some info from the user
console.log("One or more bots may need 2FA codes entered!")
function startManualBot(i) {
console.log(`If prompted, please enter the 2FA code for ${manualBots[i].username}`)
let bot = new Bot(manualBots[i]);
bot.on("loggedOn", _ => {
bot.on("ready", _ => {
if(queue.length) { // there is stuff in the queue, this bot should be assigned to something.
let nextItem = queue.pop();
bot.getFloat(decodeInspectUrl(nextItem));
} else {
freeBots.unshift(bot); // theres nothing to be done, so put this bot into the free bots poool
}
})
bots.push(bot);
if(i == manualBots.length-1) {
console.log("You may now type inspect links!");
rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', onStdIn);
} else {
setTimeout(_ => startManualBot(i + 1), 1500) // wait 1.5s for the other bot to print its shit.
}
});
}
startManualBot(0)
} else {
rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', onStdIn); // start the normal STDIN handler
}
bots.forEach(bot => {
})
function decodeInspectUrl(url) {
let sm = (url.match(/S(\d*)/) || [])[1] ||
(url.match(/M(\d*)/) || [])[1] ||
(url.match(/SM(\d*)/)|| [])[1];
let a = (url.match(/A(\d*)/) || [])[1];
let d = (url.match(/D(\d*)/) || [])[1];
return {sm:sm, a:a, d:d};
}
function getFloat(url) {
return new Promise((resolve, reject) => {
// get inspect link stuff
let smad = decodeInspectUrl(url);
let {sm, a, d} = smad;
if(!sm || !a || !d) return reject("Invalid Inspect Link");
if(cache[a]) return resolve(`${a}:${cache[a]}`);
if(freeBots.length) { // there's a bot free, this makes it easy.
let bot = freeBots.pop();
bot.getFloat(smad);
} else {
let forQueue = `SM${sm}A${a}D${d}`;
// if there's no free bots and this item is not already in the queue add it to the queue and hope.
if(!~queue.indexOf(forQueue))
queue.unshift(forQueue)
}
floatEvents.on(`itemfloat-${a}`, float => { // wait for this float to be gotten.
if(float == null) return reject(`An error occurred getting asset id ${a}'s float.`);
resolve(`${a}:${float}`);
})
})
}
/**
* Called when the server gets an new input from STDIN
* @param {string} input The inputted string
*/
function onStdIn(input) {
let a = (input.match(/A(\d*)/) || [])[1];
if(!a) return console.log("Invalid inspect link");
console.log(`Item's asset ID: ${a}`)
getFloat(input)
.then(float => {
console.log(`Float for asset id ${a}: ${float.split(':')[1]}`);
})
.catch(e => {
console.log(e);
})
}
const server = ws.createServer(conn => {
conn.on("text", str => {
getFloat(str)
.then(float => {
conn.sendText(`F${float}`);
})
.catch(e => {
conn.send(e);
})
})
}).listen(8000);