smgc commited on
Commit
49f3eda
1 Parent(s): 4364ea6

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +13 -2
app.js CHANGED
@@ -64,6 +64,15 @@ const MODEL_MAPPING = {
64
 
65
  const authCache = new Map();
66
 
 
 
 
 
 
 
 
 
 
67
  async function getAuthCookie(req) {
68
  const authHeader = req.headers['authorization'];
69
  if (authHeader && authHeader.startsWith('Bearer ')) {
@@ -76,6 +85,7 @@ async function getAuthCookie(req) {
76
  const cachedAuth = authCache.get(cacheKey);
77
 
78
  if (cachedAuth && cachedAuth.expiresAt > Date.now()) {
 
79
  return {
80
  cookie: cachedAuth.cookie,
81
  cacheHit: true,
@@ -110,7 +120,8 @@ async function getAuthCookie(req) {
110
 
111
  const responseData = await response.json();
112
  const baseCookie = Buffer.from(JSON.stringify(responseData)).toString('base64');
113
- const cookie = `sb-spuckhogycrxcbomznwo-auth-token=base64-${baseCookie}`;
 
114
  const expiresAt = Date.now() + (responseData.expires_in * 1000);
115
 
116
  authCache.set(cacheKey, {
@@ -382,7 +393,7 @@ app.post('/ai/v1/chat/completions', async (req, res) => {
382
  } finally {
383
  const endTime = new Date();
384
  const duration = endTime - startTime;
385
- console.log(`${endTime.toISOString()} - ${clientIP} - ${userModel} - ${status} - ${duration}ms - CacheHit:${cookieCacheHit} - ExpiresAt:${cookieExpiresAt}`);
386
  }
387
  });
388
 
 
64
 
65
  const authCache = new Map();
66
 
67
+ function replaceTimestamp(input) {
68
+ const currentTimestamp = Date.now();
69
+ return input.replace(/%5B(\d+)%2C/, (match, p1) => {
70
+ return `%5B${currentTimestamp + 6 * 60 * 1000}%2C`;
71
+ }).replace(/%2C(\d+)%5D/, (match, p1) => {
72
+ return `%2C${currentTimestamp}%5D`;
73
+ });
74
+ }
75
+
76
  async function getAuthCookie(req) {
77
  const authHeader = req.headers['authorization'];
78
  if (authHeader && authHeader.startsWith('Bearer ')) {
 
85
  const cachedAuth = authCache.get(cacheKey);
86
 
87
  if (cachedAuth && cachedAuth.expiresAt > Date.now()) {
88
+ cachedAuth.cookie = replaceTimestamp(cachedAuth.cookie);
89
  return {
90
  cookie: cachedAuth.cookie,
91
  cacheHit: true,
 
120
 
121
  const responseData = await response.json();
122
  const baseCookie = Buffer.from(JSON.stringify(responseData)).toString('base64');
123
+ let cookie = `sb-spuckhogycrxcbomznwo-auth-token=base64-${baseCookie}`;
124
+ cookie = replaceTimestamp(cookie);
125
  const expiresAt = Date.now() + (responseData.expires_in * 1000);
126
 
127
  authCache.set(cacheKey, {
 
393
  } finally {
394
  const endTime = new Date();
395
  const duration = endTime - startTime;
396
+ console.log(`${endTime.toISOString()} - ${clientIP} - ${userModel} - ${status} - ${duration}ms - ${cookieCacheHit} - ${cookieExpiresAt}`);
397
  }
398
  });
399