-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
216 lines (191 loc) · 6.77 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
const fs = require('fs');
const config = JSON.parse(fs.readFileSync('./config.json'));
var currentReq = null, currentRes = null;
const Steam = require('steam');
const SteamUserPlus = require('steam-user-plus');
const Client = new Steam.SteamClient();
const User = new SteamUserPlus(Client);
var logOnDetails = {account_name:'', password:''}
const Idler = require('./lib/idler');
registerClientHandlers(Client);
registerUserHandlers(User);
// Web
const express=require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const port = config.server_port || 8080;
app.use(express.static('static'))
app.get('/:page?', (req, res) => {
if (!req.params.page) {
if (Client._connection && Client.loggedOn){
stopIdle(idlingTimeouts);
Client.disconnect();
Client.emit('disconnected');
}
req.params.page = 'login'
}
res.sendFile(`${req.params.page}.html`,{ root: __dirname+'/views' });
});
app.post('/', (req, res) => {
if (req.body.user && req.body.password){
logOnDetails.account_name = req.body.user;
logOnDetails.password = req.body.password;
delete logOnDetails.two_factor_code
if (req.body.token){
if (User.loginType){
if (User.loginType == 'two_factor')
logOnDetails.two_factor_code = req.body.token;
else if (User.loginType == 'email')
logOnDetails.auth_code = req.body.token;
}
}
if (Client._connection){
stopIdle();
Client.disconnect();
Client.emit('disconnected');
}
// Connects to steam servers
currentReq = req;
currentRes = res;
Client.connect();
//res.sendFile('./http/success.html', { root: __dirname });
}else {
res.redirect('/fail', { root: __dirname });
}
});
app.listen(port, (err) => {
if (err)
return console.log('something bad happened', err)
console.log(`server is listening on ${port}, please, go to the home page and submit your credentials information.`)
});
var idlingProcess = null;
var recacheProcess = null;
function startIdle(gamesList, args){
var idler = new Idler(User, gamesList);
debug('Idler', idler);
User.emit('debug','Starting new idle session');
User.emit('debug','Session info:');
User.emit('debug',' Games to idle: '+idler.remainingGames.length);
User.emit('debug',' Idling restart time: '+config.idle.session_time_ms/1000/60+' minutes');
if (args && args.recache_after) User.emit('debug',' Game list recache time: '+args.recache_after+' minutes');
console.log(''); //Space on console.
if (args && args.recache_after){
recacheProcess = recacheAfter(idler, args.recache_after);
}
idlingProcess = startIdleHelper(idler);
if (currentRes){
currentRes.redirect('/success');
currentRes = currentReq = null;
}
}
function startIdleHelper(idler){
if (idler.idle()){
console.log(''); //Space on console.
return setTimeout(function(){
idler.shuffleRemaining();
idlingProcess = startIdleHelper(idler);
}, config.idle.session_time_ms);
}
return null;
}
function stopIdle(idler){
if (idler) idler.stop();
if (idlingProcess) clearTimeout(idlingProcess);
if (recacheProcess) clearTimeout(recacheProcess);
}
function recacheAfter(idler, minutes){
User.emit('debug','Setting up recache strategy.');
console.log('');
var milliseconds = minutes*60*1000;
return setTimeout(function(){
User.emit('debug','It has been a long time since we last checked the badge list, recaching...');
getBadgeList({no_cache:true},function(list){
User.emit('debug','The list of badges was successfully retrieved, '+list.length+' games in the list.');
User.emit('debug','Updating idler');
console.log('');
idler.updateGamesLists(list);
recacheProcess = recacheAfter(idler,minutes);
});
},milliseconds);
}
function registerClientHandlers(client){
debug('Client',client);
client.on('connected',function(){
User.logOn(logOnDetails);
});
client.on('disconnected',function(){
client.emit('debug','Client disconnected.');
});
client.on('error',function(){
console.log('There was an error with the client, disconnected. Waiting some time and retrying');
stopIdle(idlingTimeouts);
setTimeout(function(){
client.connect();
}, 60000);
});
}
function registerUserHandlers(user){
debug('User',user);
user.on('error',function(reason){
console.log('Error');
console.log(reason);
if (currentRes){
currentRes.redirect('/fail?reason='+reason);
currentReq = null;
currentRes = null;
}
});
user.on('webLogOnResponse',function(){
user.emit('debug',"Web Logged, calling badge list");
getBadgeList({},function(list){
user.emit('debug','The list of badges was successfully retrieved, '+list.length+' games in the list.');
user.emit('debug','Starting Idle process');
startIdle(list,{
recache_after: config.idle.recache_time_mnts // minutes
});
});
});
}
function debug(name,obj){
obj.on('debug',function(msg){
console.log('[INFO]['+name+']\t'+formatDate(new Date())+' - '+msg);
});
}
function formatDate(date){
var day = date.getDate(),
month = date.getMonth()+1,
year = date.getFullYear();
var hour = date.getHours(),
minutes = date.getMinutes(),
seconds = date.getSeconds(),
milliseconds = date.getMilliseconds();
return `${month}/${day}/${year} ${hour}:${minutes}:${seconds}.${milliseconds}`;
}
function getBadgeList(args,cb){
var no_cache = args.no_cache || false;
// First, let's check if we have a valid cache file
if (!no_cache && fs.existsSync('./cache.json')){
var fileContent = JSON.parse(fs.readFileSync('./cache.json'));
var cacheDate = new Date(fileContent.timestamp);
var maxDate = new Date(new Date() - 5*60000);
if (cacheDate >= maxDate){
return cb(fileContent.list);
}else
return User.badgeList(function(list){
updateCache(list);
cb(list);
});
}else
return User.badgeList(function(list){
updateCache(list);
cb(list);
});
}
function updateCache(list){
var cache = {};
cache.timestamp = new Date().toISOString();
cache.list = list;
fs.writeFileSync('./cache.json',JSON.stringify(cache));
}