forked from Revadike/epicgames-freebies-claimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
claimer.js
189 lines (164 loc) · 6.6 KB
/
claimer.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
"use strict";
const { "Launcher": EpicGames } = require("epicgames-client");
const CheckUpdate = require("check-update-github");
const ClientLoginAdapter = require("epicgames-client-login-adapter");
const Config = require(`${__dirname}/config.json`);
const Logger = require("tracer").console(`${__dirname}/logger.js`);
const Package = require("./package.json");
const TwoFactor = require("node-2fa");
const Cookie = require('tough-cookie').Cookie;
const https = require('https');
const { freeGamesPromotions } = require('./src/gamePromotions');
function isUpToDate() {
return new Promise((res, rej) => {
CheckUpdate({
"name": Package.name,
"currentVersion": Package.version,
"user": "revadike",
"branch": "master"
}, (err, latestVersion) => {
if (err) {
rej(err);
} else {
res(latestVersion === Package.version);
}
});
});
}
function getChromeCookie(cookie) {
cookie = Object.assign({}, cookie);
cookie.name = cookie.key;
if (cookie.expires instanceof Date) {
cookie.expires = cookie.expires.getTime() / 1000.0;
} else {
delete cookie.expires;
}
return cookie;
}
function getToughCookie(cookie) {
cookie = Object.assign({}, cookie);
cookie.key = cookie.name;
cookie.expires = new Date(cookie.expires * 1000);
return new Cookie(cookie);
}
(async() => {
if (!await isUpToDate()) {
Logger.warn(`There is a new version available: ${Package.url}`);
}
let { accounts, options, delay, loop, useHealthchecks, healthcheckUrl } = Config;
if (!options) {
options = {};
}
let sleep = delay => new Promise(res => setTimeout(res, delay * 60000));
do {
if (process.argv.length > 2) {
loop = false;
accounts = [{
"email": process.argv[2],
"password": process.argv[3],
"rememberLastSession": Boolean(Number(process.argv[4])),
"secret": process.argv[5],
}];
}
for (let account of accounts) {
let noSecret = !account.secret || account.secret.length === 0;
if (!noSecret) {
let { token } = TwoFactor.generateToken(account.secret);
account.twoFactorCode = token;
}
let epicOptions = Object.assign({}, options);
Object.assign(epicOptions, account);
let client = new EpicGames(epicOptions);
if (!await client.init()) {
throw new Error("Error while initialize process.");
}
let success = false;
try {
success = await client.login(account);
} catch (error) {
Logger.warn(error.message);
}
if (!success) {
Logger.warn(`Failed to login as ${client.config.email}, please attempt manually.`);
if (account.rememberLastSession) {
if (!options.cookies) {
options.cookies = [];
}
if (account.cookies && account.cookies.length) {
options.cookies = options.cookies.concat(account.cookies);
}
client.http.jar._jar.store.getAllCookies((err, cookies) => {
for (const cookie of cookies) {
options.cookies.push(getChromeCookie(cookie));
}
});
}
let auth = await ClientLoginAdapter.init(account, options);
let exchangeCode = await auth.getExchangeCode();
if (account.rememberLastSession) {
let cookies = await auth.getPage().then(p => p.cookies());
for (let cookie of cookies) {
cookie = getToughCookie(cookie);
client.http.jar.setCookie(cookie, "https://" + cookie.domain);
}
}
await auth.close();
if (!await client.login(null, exchangeCode)) {
throw new Error("Error while logging in.");
}
}
Logger.info(`Logged in as ${client.account.name} (${client.account.id})`);
let { country } = client.account.country;
let freePromos = await freeGamesPromotions(client, country, country);
for (let offer of freePromos) {
let launcherQuery = await client.launcherQuery(offer.namespace, offer.id);
if (launcherQuery.data.Launcher.entitledOfferItems.entitledToAllItemsInOffer) {
Logger.info(`${offer.title} is already claimed for this account`);
continue;
}
try {
let purchased = await client.purchase(offer, 1);
if (purchased) {
Logger.info(`Successfully claimed ${offer.title} (${purchased})`);
} else {
Logger.info(`${offer.title} was already claimed for this account`);
}
} catch (err) {
Logger.warn(`Failed to claim ${offer.title} (${err})`);
if (err.response
&& err.response.body
&& err.response.body.errorCode === "errors.com.epicgames.purchase.purchase.captcha.challenge") {
// It's pointless to try next one as we'll be asked for captcha again.
Logger.error("Aborting!");
break;
}
}
}
await client.logout();
Logger.info(`Logged ${client.account.name} out of Epic Games`);
}
if(useHealthchecks) {
if(healthcheckUrl != null) {
try {
https.get(healthcheckUrl);
Logger.info('Made call to healthcheck URL');
} catch (error) {
Logger.error(error);
}
} else {
Logger.error('Health check URL hasn\'t been defined');
}
}
if (loop) {
Logger.info(`Waiting ${delay} minutes`);
await sleep(delay);
} else {
Logger.info(`Waiting 1 minute before closing`);
await sleep(1);
process.exit(0);
}
} while (loop);
})().catch(err => {
Logger.error(err);
process.exit(1);
});