-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuser.js
96 lines (86 loc) · 2.72 KB
/
user.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
const prompt = require('prompt-sync')();
const SteamTotp = require('steam-totp');
const SteamCommunity = require('steamcommunity');
const NodeSteamUser = require('steam-user');
const _ = require('lodash');
const Plugins = require('./plugins.json');
const Storage = require('./storage');
const storage = new Storage();
module.exports = class SteamUser {
constructor(data) {
this.end = false;
this.mods = {};
this.data = data ? data : {};
this.community = new SteamCommunity();
this.user = new NodeSteamUser({ renewRefreshTokens: true });
this.data.account = this.data.account
? this.data.account
: prompt('Enter your account: ');
this.refreshToken = storage.getRefreshToken(this.data.account);
if (_.isNil(this.refreshToken)) {
this.data.password = this.data.password
? this.data.password
: prompt('Enter your password: ', { echo: '*' });
} else {
console.log(`[${this.data.account}] login with refresh token`);
}
this.initSteamUser();
}
async initSteamUser() {
const loginData = {};
if (_.isNil(this.refreshToken)) {
loginData.accountName = this.data.account;
loginData.password = this.data.password;
if (this.data.shared_secret) {
loginData.twoFactorCode = SteamTotp.generateAuthCode(
this.data.shared_secret
);
}
} else {
loginData.refreshToken = this.refreshToken;
}
this.user.logOn(loginData);
this.user.on('loggedOn', () => {
console.log(`[${this.data.account}] logged on steam`);
});
this.user.on('refreshToken', (token) => {
console.log(`[${this.data.account}] save refresh token`);
storage.saveRefreshToken(this.data.account, token);
});
this.user.on('error', (err) => {
console.error(`[${this.data.account}]`, err);
});
this.user.on('webSession', async (sessionID, cookies) => {
console.log(`[${this.data.account}] login web session`);
this.community.setCookies(cookies);
try {
this.loadMods();
} catch (err) {
console.error(`[${this.data.account}]`, 'load mods with error: ', err);
} finally {
this.end = true;
this.user.logOff();
}
});
}
async loadMods() {
for (let plugin of Plugins) {
if (plugin.enable) {
console.log(`[${this.data.account}] Loading plugin: ${plugin.name}`);
this.mods[plugin.name] = require(`./plugins/${plugin.script}`).bind(
this
);
try {
await this.mods[plugin.name](plugin);
} catch (err) {
console.error(
`[${this.data.account}]`,
'run plugin with error: ',
err
);
}
}
}
this.end = true;
}
};