yxmiler commited on
Commit
71b614e
·
verified ·
1 Parent(s): 3a88836

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +11 -43
index.js CHANGED
@@ -7,6 +7,7 @@ dotenv.config();
7
 
8
  const Tokens =[];
9
  let tokenManager;
 
10
  const CONFIG = {
11
  API: {
12
  BASE_URL: "https://partyrock.aws",
@@ -52,55 +53,26 @@ const CONFIG = {
52
  };
53
 
54
  class TokenManager {
55
- constructor() {
56
- this.cache = {
57
- currentIndex: 0,
58
- tokens: {}
59
- };
60
- }
61
-
62
- async getCurrentTokenIndex() {
63
- return this.cache.currentIndex;
64
- }
65
-
66
- async updateTokenIndex() {
67
- const nextIndex = (this.cache.currentIndex + 1) % Tokens.length;
68
- this.cache.currentIndex = nextIndex;
69
- }
70
-
71
- async getCachedTokens() {
72
- const currentIndex = await this.getCurrentTokenIndex();
73
- const csrfToken = this.cache.tokens.csrfToken
74
- ? this.cache.tokens.csrfToken
75
- : Tokens[currentIndex]["anti_csrftoken_a2z"];
76
-
77
- const idToken = this.cache.tokens.idToken
78
- ? this.cache.tokens.idToken
79
- : Tokens[currentIndex]["idToken"];
80
- return {
81
- csrfToken: csrfToken,
82
- idToken: idToken,
83
- pr_refresh_token: Tokens[currentIndex]["pr_refresh_token"],
84
- aws_waf_token: Tokens[currentIndex]["aws_waf_token"],
85
- refreshUrl: Tokens[currentIndex]["refreshUrl"]
86
- };
87
  }
88
 
89
  async updateTokens(response) {
90
  const newCsrfToken = response.headers.get('anti-csrftoken-a2z');
91
  if (newCsrfToken) {
92
- this.cache.tokens.csrfToken = newCsrfToken;
93
  }
94
 
95
  const cookies = response.headers.get('set-cookie');
96
  if (cookies) {
97
  const idTokenMatch = cookies.match(/idToken=([^;]+)/);
98
  if (idTokenMatch && idTokenMatch[1]) {
99
- this.cache.tokens.idToken = idTokenMatch[1];
100
  }
101
  }
102
-
103
- await this.updateTokenIndex();
104
  }
105
  }
106
  class Utils {
@@ -166,6 +138,7 @@ async function initializeService() {
166
  }
167
  index++;
168
  }
 
169
  tokenManager = new TokenManager();
170
  }
171
 
@@ -254,7 +227,6 @@ class ApiClient {
254
  },
255
  "apiVersion": 3
256
  }
257
- console.log(JSON.stringify(requestPayload, null, 2));
258
  return requestPayload;
259
  }
260
  }
@@ -317,7 +289,6 @@ class ResponseHandler {
317
  const json = JSON.parse(data);
318
  if (json?.text) {
319
  var content = json.text;
320
- console.log(content);
321
  const responseData = MessageProcessor.createChatResponse(content, model, true);
322
  res.write(`data: ${JSON.stringify(responseData)}\n\n`);
323
  }
@@ -400,11 +371,8 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
400
  if (authToken !== CONFIG.API.API_KEY) {
401
  return res.status(401).json({ error: "Unauthorized" });
402
  }
403
- const { csrfToken, idToken, pr_refresh_token, aws_waf_token, refreshUrl } = await tokenManager.getCachedTokens();
404
- CONFIG.DEFAULT_HEADERS["anti-csrftoken-a2z"] = csrfToken;
405
- CONFIG.DEFAULT_HEADERS.Cookie = `idToken=${idToken}; pr_refresh_token=${pr_refresh_token};aws-waf-token=${aws_waf_token}`;
406
- CONFIG.DEFAULT_HEADERS.referer = refreshUrl;
407
- console.log(JSON.stringify(CONFIG.DEFAULT_HEADERS, null, 2));
408
 
409
  const apiClient = new ApiClient(req.body.model);
410
  const requestPayload = await apiClient.transformMessages(req.body);
 
7
 
8
  const Tokens =[];
9
  let tokenManager;
10
+ let currentIndex = 0;
11
  const CONFIG = {
12
  API: {
13
  BASE_URL: "https://partyrock.aws",
 
53
  };
54
 
55
  class TokenManager {
56
+ async updateCacheTokens() {
57
+ CONFIG.DEFAULT_HEADERS["anti-csrftoken-a2z"] = Tokens[currentIndex]["anti_csrftoken_a2z"];
58
+ CONFIG.DEFAULT_HEADERS.Cookie = `idToken=${Tokens[currentIndex]["idToken"]}; pr_refresh_token=${Tokens[currentIndex]["pr_refresh_token"]};aws-waf-token=${Tokens[currentIndex]["aws_waf_token"]}`;
59
+ CONFIG.DEFAULT_HEADERS.referer = Tokens[currentIndex]["refreshUrl"];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  }
61
 
62
  async updateTokens(response) {
63
  const newCsrfToken = response.headers.get('anti-csrftoken-a2z');
64
  if (newCsrfToken) {
65
+ Tokens[currentIndex]["anti_csrftoken_a2z"] = newCsrfToken;
66
  }
67
 
68
  const cookies = response.headers.get('set-cookie');
69
  if (cookies) {
70
  const idTokenMatch = cookies.match(/idToken=([^;]+)/);
71
  if (idTokenMatch && idTokenMatch[1]) {
72
+ Tokens[currentIndex]["idToken"] = idTokenMatch[1];
73
  }
74
  }
75
+ currentIndex = (currentIndex + 1) % Tokens.length;
 
76
  }
77
  }
78
  class Utils {
 
138
  }
139
  index++;
140
  }
141
+
142
  tokenManager = new TokenManager();
143
  }
144
 
 
227
  },
228
  "apiVersion": 3
229
  }
 
230
  return requestPayload;
231
  }
232
  }
 
289
  const json = JSON.parse(data);
290
  if (json?.text) {
291
  var content = json.text;
 
292
  const responseData = MessageProcessor.createChatResponse(content, model, true);
293
  res.write(`data: ${JSON.stringify(responseData)}\n\n`);
294
  }
 
371
  if (authToken !== CONFIG.API.API_KEY) {
372
  return res.status(401).json({ error: "Unauthorized" });
373
  }
374
+ await tokenManager.updateCacheTokens();
375
+
 
 
 
376
 
377
  const apiClient = new ApiClient(req.body.model);
378
  const requestPayload = await apiClient.transformMessages(req.body);