11"use strict" ;
2+
23const { "Launcher" : EpicGames } = require ( "epicgames-client" ) ;
4+ const { freeGamesPromotions } = require ( "./src/gamePromotions" ) ;
5+
6+ const Auths = require ( `${ __dirname } /device_auths.json` ) ;
37const CheckUpdate = require ( "check-update-github" ) ;
4- const ClientLoginAdapter = require ( "epicgames-client-login-adapter" ) ;
58const Config = require ( `${ __dirname } /config.json` ) ;
69const Logger = require ( "tracer" ) . console ( `${ __dirname } /logger.js` ) ;
710const Package = require ( "./package.json" ) ;
8- const TwoFactor = require ( "node-2fa" ) ;
9- const Cookie = require ( 'tough-cookie' ) . Cookie ;
10-
11- const { freeGamesPromotions } = require ( './src/gamePromotions' ) ;
1211
1312function isUpToDate ( ) {
1413 return new Promise ( ( res , rej ) => {
1514 CheckUpdate ( {
1615 "name" : Package . name ,
1716 "currentVersion" : Package . version ,
1817 "user" : "revadike" ,
19- "branch" : "master"
18+ "branch" : "master" ,
2019 } , ( err , latestVersion ) => {
2120 if ( err ) {
2221 rej ( err ) ;
@@ -27,116 +26,36 @@ function isUpToDate() {
2726 } ) ;
2827}
2928
30- function getChromeCookie ( cookie ) {
31- cookie = Object . assign ( { } , cookie ) ;
32- cookie . name = cookie . key ;
33- if ( cookie . expires instanceof Date ) {
34- cookie . expires = cookie . expires . getTime ( ) / 1000.0 ;
35- } else {
36- delete cookie . expires ;
37- }
38- return cookie ;
39- }
40-
41- function getToughCookie ( cookie ) {
42- cookie = Object . assign ( { } , cookie ) ;
43- cookie . key = cookie . name ;
44- cookie . expires = new Date ( cookie . expires * 1000 ) ;
45- return new Cookie ( cookie ) ;
29+ function sleep ( delay ) {
30+ return new Promise ( ( res ) => setTimeout ( res , delay * 60000 ) ) ;
4631}
4732
4833( async ( ) => {
4934 if ( ! await isUpToDate ( ) ) {
5035 Logger . warn ( `There is a new version available: ${ Package . url } ` ) ;
5136 }
5237
53- let { accounts, options, delay, loop } = Config ;
54- if ( ! options ) {
55- options = { } ;
56- }
57- let sleep = delay => new Promise ( res => setTimeout ( res , delay * 60000 ) ) ;
38+ let { options, delay, loop } = Config ;
5839 do {
59- if ( process . argv . length > 2 ) {
60- loop = false ;
61- accounts = [ {
62- "email" : process . argv [ 2 ] ,
63- "password" : process . argv [ 3 ] ,
64- "rememberLastSession" : Boolean ( Number ( process . argv [ 4 ] ) ) ,
65- "secret" : process . argv [ 5 ] ,
66- } ] ;
67- }
68-
69- for ( let account of accounts ) {
70- let noSecret = ! account . secret || account . secret . length === 0 ;
71- if ( ! noSecret ) {
72- let { token } = TwoFactor . generateToken ( account . secret ) ;
73- account . twoFactorCode = token ;
74- }
75-
76- let epicOptions = Object . assign ( { } , options ) ;
77- Object . assign ( epicOptions , account ) ;
78-
79- let client = new EpicGames ( epicOptions ) ;
80-
40+ for ( let email in Auths ) {
41+ let useDeviceAuth = true ;
42+ let clientOptions = { email, ...options } ;
43+ let client = new EpicGames ( clientOptions ) ;
8144 if ( ! await client . init ( ) ) {
8245 throw new Error ( "Error while initialize process." ) ;
8346 }
8447
85- let success = false ;
86- try {
87- success = await client . login ( account ) ;
88- } catch ( error ) {
89- Logger . warn ( error . message ) ;
90- }
91-
48+ let success = await client . login ( { useDeviceAuth } ) ;
9249 if ( ! success ) {
93- Logger . warn ( `Failed to login as ${ client . config . email } , please attempt manually.` ) ;
94-
95-
96- if ( account . rememberLastSession ) {
97- if ( ! options . cookies ) {
98- options . cookies = [ ] ;
99- }
100- if ( account . cookies && account . cookies . length ) {
101- options . cookies = options . cookies . concat ( account . cookies ) ;
102- }
103- client . http . jar . _jar . store . getAllCookies ( ( err , cookies ) => {
104- for ( const cookie of cookies ) {
105- options . cookies . push ( getChromeCookie ( cookie ) ) ;
106- }
107- } ) ;
108- }
109-
110- let auth = await ClientLoginAdapter . init ( account , options ) ;
111- let exchangeCode = await auth . getExchangeCode ( ) ;
112-
113- if ( account . rememberLastSession ) {
114- let cookies = await auth . getPage ( ) . then ( p => p . cookies ( ) ) ;
115- for ( let cookie of cookies ) {
116- cookie = getToughCookie ( cookie ) ;
117- client . http . jar . setCookie ( cookie , "https://" + cookie . domain ) ;
118- }
119- }
120-
121- await auth . close ( ) ;
122-
123- if ( ! await client . login ( null , exchangeCode ) ) {
124- throw new Error ( "Error while logging in." ) ;
125- }
50+ throw new Error ( `Failed to login as ${ client . config . email } ` ) ;
12651 }
12752
12853 Logger . info ( `Logged in as ${ client . account . name } (${ client . account . id } )` ) ;
12954
130- let { country } = client . account . country ;
55+ let { country } = client . account ;
13156 let freePromos = await freeGamesPromotions ( client , country , country ) ;
13257
13358 for ( let offer of freePromos ) {
134- let launcherQuery = await client . launcherQuery ( offer . namespace , offer . id ) ;
135- if ( launcherQuery . data . Launcher . entitledOfferItems . entitledToAllItemsInOffer ) {
136- Logger . info ( `${ offer . title } is already claimed for this account` ) ;
137- continue ;
138- }
139-
14059 try {
14160 let purchased = await client . purchase ( offer , 1 ) ;
14261 if ( purchased ) {
@@ -167,7 +86,7 @@ function getToughCookie(cookie) {
16786 process . exit ( 0 ) ;
16887 }
16988 } while ( loop ) ;
170- } ) ( ) . catch ( err => {
89+ } ) ( ) . catch ( ( err ) => {
17190 Logger . error ( err ) ;
17291 process . exit ( 1 ) ;
17392} ) ;
0 commit comments