1
- const config = require ( 'config' ) ;
2
1
const SteamUser = require ( 'steam-user' ) ;
3
2
const GlobalOffensive = require ( 'globaloffensive' ) ;
4
3
const SteamTotp = require ( 'steam-totp' ) ;
5
4
const async = require ( 'async' ) ;
6
5
const logger = require ( './Logger' ) ;
7
6
const Encryption = require ( './Encryption' ) ;
7
+ const SteamAccount = require ( '../database/models/SteamAccount' ) ;
8
8
9
- const accounts = config . get ( 'steam.accounts' ) . map ( ( account ) => ( {
10
- username : account . username ,
11
- password : Encryption . decrypt ( account . password ) ,
12
- sharedSecret : Encryption . decrypt ( account . sharedSecret ) ,
13
- } ) ) ;
14
-
15
- const clients = [ ] ;
16
-
17
- accounts . forEach ( ( account ) => {
18
- const client = new SteamUser ( {
19
- // This is necessary for `ownsApp` to work.
20
- enablePicsCache : true ,
21
- } ) ;
22
-
23
- const csgo = new GlobalOffensive ( client ) ;
24
-
25
- client . logOn ( {
26
- accountName : account . username ,
27
- password : account . password ,
28
- twoFactorCode : SteamTotp . getAuthCode ( account . sharedSecret ) ,
29
- } ) ;
30
-
31
- client . on ( 'loggedOn' , ( ) => {
32
- logger . info ( `Logged into Steam as ${ client . steamID . getSteam3RenderedID ( ) } ` ) ;
33
- client . setPersona ( SteamUser . EPersonaState . Online ) ;
34
- client . gamesPlayed ( 730 , true ) ;
35
- } ) ;
36
-
37
- client . on ( 'appOwnershipCached' , ( ) => {
38
- // If the account does not own CS:GO yet, claim the free license first.
39
- // This is necessary to connect to the game coordinator via `gamesPlayed`,
40
- // and `gamesPlayed` is necessary to inspect skins.
41
- if ( ! client . ownsApp ( 730 ) ) {
42
- client . requestFreeLicense ( 730 , ( err ) => {
43
- if ( err ) {
44
- logger . warn ( 'Failed to acquire lisence for CS:GO' ) ;
45
- logger . error ( err ) ;
46
- } else {
47
- logger . info ( 'Successfully acquired license for CS:GO' ) ;
48
- client . gamesPlayed ( 730 , true ) ;
49
- }
9
+ class Inspector {
10
+ constructor ( ) {
11
+ this . clients = [ ] ;
12
+
13
+ SteamAccount . query ( ) . then ( ( steamAccounts ) => {
14
+ steamAccounts . forEach ( ( steamAccount ) => {
15
+ this . addClient (
16
+ steamAccount . username ,
17
+ Encryption . decrypt ( steamAccount . password ) ,
18
+ Encryption . decrypt ( steamAccount . shared_secret ) ,
19
+ ) ;
50
20
} ) ;
51
- }
52
- } ) ;
53
-
54
- client . on ( 'error' , ( e ) => {
55
- // Some error occurred during logon
56
- logger . error ( 'Client error' ) ;
57
- logger . error ( e ) ;
58
- } ) ;
59
-
60
- client . on ( 'webSession' , ( ) => {
61
- logger . info ( 'Got web session' ) ;
62
- // Do something with these cookies if you wish
63
- } ) ;
64
-
65
- csgo . on ( 'connectedToGC' , ( ) => {
66
- logger . info ( 'Connected to CS:GO game coordinator' ) ;
67
- } ) ;
68
-
69
- csgo . on ( 'inspectItemTimedOut' , ( assetid ) => {
70
- logger . warn ( `Inspect timed out for assetid ${ assetid } ` ) ;
71
- } ) ;
72
-
73
- clients . push ( {
74
- client,
75
- csgo,
76
- busySince : null ,
77
- } ) ;
78
- } ) ;
79
-
80
- setInterval ( ( ) => {
81
- for ( let i = 0 ; i < clients . length ; i += 1 ) {
82
- // Mark clients as not busy if they have been busy for more than 10 seconds.
83
- if ( clients [ i ] . busySince && Date . now ( ) > ( clients [ i ] . busySince + 10000 ) ) {
84
- clients [ i ] . busySince = null ;
85
- logger . info ( `Marked client ${ i } as not busy after 10 second timeout` ) ;
86
- }
21
+ } ) ;
22
+
23
+ setInterval ( ( ) => {
24
+ for ( let i = 0 ; i < this . clients . length ; i += 1 ) {
25
+ // Mark clients as not busy if they have been busy for more than 10 seconds.
26
+ if ( this . clients [ i ] . busySince && Date . now ( ) > ( this . clients [ i ] . busySince + 10000 ) ) {
27
+ this . clients [ i ] . busySince = null ;
28
+ logger . info ( `Marked client ${ i } as not busy after 10 second timeout` ) ;
29
+ }
30
+ }
31
+ } , 1000 ) ;
87
32
}
88
- } , 1000 ) ;
89
33
90
- class Inspector {
91
- static getClients ( ) {
92
- return clients ;
34
+ addClient ( username , password , sharedSecret ) {
35
+ const client = new SteamUser ( {
36
+ // This is necessary for `ownsApp` to work.
37
+ enablePicsCache : true ,
38
+ } ) ;
39
+
40
+ const csgo = new GlobalOffensive ( client ) ;
41
+
42
+ client . logOn ( {
43
+ accountName : username ,
44
+ password,
45
+ twoFactorCode : SteamTotp . getAuthCode ( sharedSecret ) ,
46
+ } ) ;
47
+
48
+ client . on ( 'loggedOn' , ( ) => {
49
+ logger . info ( `Logged into Steam as ${ client . steamID . getSteam3RenderedID ( ) } ` ) ;
50
+ client . setPersona ( SteamUser . EPersonaState . Online ) ;
51
+ client . gamesPlayed ( 730 , true ) ;
52
+ } ) ;
53
+
54
+ client . on ( 'appOwnershipCached' , ( ) => {
55
+ // If the account does not own CS:GO yet, claim the free license first.
56
+ // This is necessary to connect to the game coordinator via `gamesPlayed`,
57
+ // and `gamesPlayed` is necessary to inspect skins.
58
+ if ( ! client . ownsApp ( 730 ) ) {
59
+ client . requestFreeLicense ( 730 , ( err ) => {
60
+ if ( err ) {
61
+ logger . warn ( 'Failed to acquire lisence for CS:GO' ) ;
62
+ logger . error ( err ) ;
63
+ } else {
64
+ logger . info ( 'Successfully acquired license for CS:GO' ) ;
65
+ client . gamesPlayed ( 730 , true ) ;
66
+ }
67
+ } ) ;
68
+ }
69
+ } ) ;
70
+
71
+ client . on ( 'error' , ( e ) => {
72
+ // Some error occurred during logon
73
+ logger . error ( 'Client error' ) ;
74
+ logger . error ( e ) ;
75
+ } ) ;
76
+
77
+ client . on ( 'webSession' , ( ) => {
78
+ logger . info ( 'Got web session' ) ;
79
+ // Do something with these cookies if you wish
80
+ } ) ;
81
+
82
+ csgo . on ( 'connectedToGC' , ( ) => {
83
+ logger . info ( 'Connected to CS:GO game coordinator' ) ;
84
+ } ) ;
85
+
86
+ csgo . on ( 'inspectItemTimedOut' , ( assetid ) => {
87
+ logger . warn ( `Inspect timed out for assetid ${ assetid } ` ) ;
88
+ } ) ;
89
+
90
+ this . clients . push ( {
91
+ client,
92
+ csgo,
93
+ busySince : null ,
94
+ } ) ;
93
95
}
94
96
95
- static inspect ( url , timeoutMs = 3000 ) {
97
+ inspect ( url , timeoutMs = 3000 ) {
96
98
return new Promise ( ( resolve , reject ) => {
97
99
// We'll want a timeout of at least 1500 ms.
98
100
if ( timeoutMs < 1500 ) {
@@ -113,7 +115,7 @@ class Inspector {
113
115
// Find an index of a client that is currently not busy and has a GC connection.
114
116
async
115
117
. retry ( { times : retryTimes , interval : retryInterval } , async ( ) => {
116
- const index = clients . findIndex (
118
+ const index = this . clients . findIndex (
117
119
( client ) => client . busySince === null && client . csgo . haveGCSession ,
118
120
) ;
119
121
@@ -130,15 +132,17 @@ class Inspector {
130
132
// If the inspection succeeds, `busySince` will be cleared immediately.
131
133
// If the inspection times out, `busySince` will be cleared after within
132
134
// 1 second after the timeout.
133
- clients [ index ] . busySince = Date . now ( ) ;
135
+ this . clients [ index ] . busySince = Date . now ( ) ;
134
136
135
- clients [ index ] . csgo . inspectItem ( url , ( item ) => {
136
- clients [ index ] . busySince = null ;
137
+ this . clients [ index ] . csgo . inspectItem ( url , ( item ) => {
138
+ this . clients [ index ] . busySince = null ;
137
139
resolve ( item ) ;
138
140
} ) ;
139
141
} ) ;
140
142
} ) ;
141
143
}
142
144
}
143
145
144
- module . exports = Inspector ;
146
+ const inspector = new Inspector ( ) ;
147
+
148
+ module . exports = inspector ;
0 commit comments