@@ -9,24 +9,11 @@ const settings = require("./config/config");
9
9
const { sleep, loadData, getRandomNumber, saveToken, isTokenExpired, saveJson, updateEnv } = require ( "./utils" ) ;
10
10
const { Worker, isMainThread, parentPort, workerData } = require ( "worker_threads" ) ;
11
11
const { checkBaseUrl } = require ( "./checkAPI" ) ;
12
+ const headers = require ( "./core/header" ) ;
12
13
13
14
class ClientAPI {
14
15
constructor ( queryId , accountIndex , proxy , baseURL ) {
15
- this . headers = {
16
- Accept : "*/*" ,
17
- "Accept-Encoding" : "gzip, deflate, br" ,
18
- "Accept-Language" : "vi-VN,vi;q=0.9,fr-FR;q=0.8,fr;q=0.7,en-US;q=0.6,en;q=0.5" ,
19
- "Content-Type" : "application/json" ,
20
- Origin : "https://tg-sleepagotchi-tmp-cdn.sfo3.digitaloceanspaces.com" ,
21
- referer : "https://tg-sleepagotchi-tmp-cdn.sfo3.digitaloceanspaces.com/" ,
22
- "Sec-Ch-Ua" : '"Not/A)Brand";v="99", "Google Chrome";v="115", "Chromium";v="115"' ,
23
- "Sec-Ch-Ua-Mobile" : "?0" ,
24
- "Sec-Ch-Ua-Platform" : '"Windows"' ,
25
- "Sec-Fetch-Dest" : "empty" ,
26
- "Sec-Fetch-Mode" : "cors" ,
27
- "Sec-Fetch-Site" : "same-origin" ,
28
- "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" ,
29
- } ;
16
+ this . headers = headers ;
30
17
this . baseURL = baseURL ;
31
18
this . queryId = queryId ;
32
19
this . accountIndex = accountIndex ;
@@ -147,25 +134,41 @@ class ClientAPI {
147
134
}
148
135
}
149
136
150
- async makeRequest ( url , method , data = { } , retries = 1 ) {
137
+ async makeRequest (
138
+ url ,
139
+ method ,
140
+ data = { } ,
141
+ options = {
142
+ retries : 1 ,
143
+ isAuth : false ,
144
+ }
145
+ ) {
146
+ const { retries, isAuth } = options ;
147
+
151
148
const headers = {
152
149
...this . headers ,
150
+ Authorization : `tma ${ this . queryId } ` ,
153
151
} ;
152
+
153
+ if ( ! isAuth ) {
154
+ headers [ "Authorization" ] = `Bearer ${ this . token } ` ;
155
+ }
156
+
154
157
const proxyAgent = new HttpsProxyAgent ( this . proxy ) ;
155
158
let currRetries = 0 ,
156
159
success = false ;
157
160
do {
158
161
try {
159
162
const response = await axios ( {
160
163
method,
161
- url : `${ url } ? ${ this . queryId } ` ,
164
+ url : `${ url } ` ,
162
165
data,
163
166
headers,
164
167
httpsAgent : proxyAgent ,
165
168
timeout : 30000 ,
166
169
} ) ;
167
170
success = true ;
168
- return { success : true , data : response . data } ;
171
+ return { success : true , data : response . data . data } ;
169
172
} catch ( error ) {
170
173
if ( error . status == 400 ) {
171
174
return { success : false , error : error . message } ;
@@ -178,47 +181,45 @@ class ClientAPI {
178
181
currRetries ++ ;
179
182
} while ( currRetries <= retries && ! success ) ;
180
183
}
184
+ async auth ( ) {
185
+ return this . makeRequest ( `${ this . baseURL } /auth/create-user` , "post" , { refCode : "Iil4QcC4TF" } , { isAuth : true } ) ;
186
+ }
181
187
182
188
async getUserInfo ( ) {
183
- return this . makeRequest ( `${ this . baseURL } /getUserData` , "get" ) ;
189
+ return this . makeRequest ( `${ this . baseURL } /user` , "get" ) ;
190
+ }
191
+
192
+ async getQuests ( ) {
193
+ return this . makeRequest ( `${ this . baseURL } /user/quest` , "get" ) ;
184
194
}
185
195
186
- // async getValidToken(isRf = false) {
187
- // const userId = this.session_name;
188
- // const existingToken = this.token;
189
- // const existingRefreshToken = this.rfToken;
190
- // let loginResult = null;
191
-
192
- // const isExp = isTokenExpired(!isRf ? existingToken : existingRefreshToken);
193
- // if (!isRf && existingToken && !isExp) {
194
- // this.log("Using valid token", "success");
195
- // return { access_token: existingToken, refresh_token: existingRefreshToken };
196
- // } else if (!isRf && existingToken && isExp) {
197
- // this.log("Token expired, refreshing token...", "info");
198
- // return await this.getValidToken(true);
199
- // } else if (isRf && existingRefreshToken && !isExp) {
200
- // loginResult = await this.refreshToken();
201
- // } else {
202
- // this.log("Token not found or expired, logging in...", "warning");
203
- // loginResult = await this.auth();
204
- // }
205
- // // console.log(loginResult);
206
- // if (loginResult?.success) {
207
- // const { refresh_token, access_token } = loginResult?.data;
208
- // if (access_token) {
209
- // saveToken(userId, access_token);
210
- // this.token = access_token;
211
- // }
212
- // if (refresh_token) {
213
- // saveJson(userId, refresh_token, "refresh_token.json");
214
- // this.rfToken = refresh_token;
215
- // }
216
- // return { access_token: access_token, refresh_token: refresh_token };
217
- // } else {
218
- // this.log(`Can't get token, try get new query_id!`, "warning");
219
- // }
220
- // return { access_token: null, refresh_token: null };
221
- // }
196
+ async getValidToken ( ) {
197
+ const userId = this . session_name ;
198
+ const existingToken = this . token ;
199
+ let loginResult = null ;
200
+
201
+ const isExp = isTokenExpired ( existingToken ) ;
202
+ if ( existingToken && ! isExp ) {
203
+ this . log ( "Using valid token" , "success" ) ;
204
+ return token ;
205
+ } else {
206
+ this . log ( "Token not found or expired, logging in..." , "warning" ) ;
207
+ loginResult = await this . auth ( ) ;
208
+ }
209
+
210
+ if ( loginResult ?. success ) {
211
+ const { token } = loginResult ?. data ;
212
+ if ( token ) {
213
+ saveToken ( userId , token ) ;
214
+ this . token = token ;
215
+ }
216
+
217
+ return token ;
218
+ } else {
219
+ this . log ( `Can't get token, try get new query_id!` , "warning" ) ;
220
+ }
221
+ return null ;
222
+ }
222
223
223
224
async runAccount ( ) {
224
225
try {
@@ -240,6 +241,8 @@ class ClientAPI {
240
241
this . #set_headers( ) ;
241
242
await sleep ( timesleep ) ;
242
243
244
+ const token = await this . getValidToken ( ) ;
245
+ if ( ! token ) return this . log ( `Can't get token for account ${ this . accountIndex + 1 } , skipping...` , "error" ) ;
243
246
let userData = { success : false } ,
244
247
retries = 0 ;
245
248
do {
0 commit comments