isididiidid commited on
Commit
8ae0569
·
verified ·
1 Parent(s): 5a08d88

Update hf.js

Browse files
Files changed (1) hide show
  1. hf.js +95 -157
hf.js CHANGED
@@ -7,6 +7,7 @@ const crypto = require('crypto');
7
  const app = express();
8
 
9
  app.use(morgan('dev'));
 
10
 
11
  // Checksum 生成函数
12
  function generateChecksum() {
@@ -183,175 +184,111 @@ app.use('/hf/v1/chat/completions', createProxyMiddleware({
183
 
184
  // 将请求数据写回代理请求
185
  if (req.body) {
186
- const bodyData = req.body;
187
- proxyReq.write(JSON.stringify(bodyData));
 
188
  }
189
  }
190
  }));
191
 
192
  // 主页路由
193
  app.get('/', (req, res) => {
194
- const htmlContent = `
195
- <!DOCTYPE html>
196
- <html lang="en">
197
- <head>
198
- <meta charset="UTF-8">
199
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
200
- <title>Cursor To OpenAI</title>
201
- <style>
202
- :root {
203
- --primary-color: #2563eb;
204
- --bg-color: #f8fafc;
205
- --card-bg: #ffffff;
206
- }
207
- body {
208
- padding: 20px;
209
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
210
- max-width: 1000px;
211
- margin: 0 auto;
212
- line-height: 1.6;
213
- background: var(--bg-color);
214
- color: #1a1a1a;
215
- }
216
- .container {
217
- padding: 20px;
218
- }
219
- .header {
220
- text-align: center;
221
- margin-bottom: 40px;
222
- }
223
- .header h1 {
224
- color: var(--primary-color);
225
- font-size: 2.5em;
226
- margin-bottom: 10px;
227
- }
228
- .info {
229
- background: #fff;
230
- padding: 25px;
231
- border-radius: 12px;
232
- box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
233
- margin-bottom: 30px;
234
- border: 1px solid #e5e7eb;
235
- }
236
- .info-item {
237
- margin: 15px 0;
238
- padding: 10px;
239
- background: #f8fafc;
240
- border-radius: 8px;
241
- border: 1px solid #e5e7eb;
242
- }
243
- .info-label {
244
- color: #4b5563;
245
- font-size: 0.9em;
246
- margin-bottom: 5px;
247
- }
248
- .info-value {
249
- color: var(--primary-color);
250
- font-weight: 500;
251
- }
252
- .models {
253
- background: var(--card-bg);
254
- padding: 25px;
255
- border-radius: 12px;
256
- box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
257
- border: 1px solid #e5e7eb;
258
- }
259
- .models h3 {
260
- color: #1a1a1a;
261
- margin-bottom: 20px;
262
- font-size: 1.5em;
263
- border-bottom: 2px solid #e5e7eb;
264
- padding-bottom: 10px;
265
- }
266
- .model-item {
267
- margin: 12px 0;
268
- padding: 15px;
269
- background: #f8fafc;
270
- border-radius: 8px;
271
- border: 1px solid #e5e7eb;
272
- transition: all 0.3s ease;
273
- display: flex;
274
- justify-content: space-between;
275
- align-items: center;
276
- }
277
- .model-item:hover {
278
- transform: translateY(-2px);
279
- box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
280
- }
281
- .model-name {
282
- font-weight: 500;
283
- color: #1a1a1a;
284
- }
285
- .model-provider {
286
- color: #6b7280;
287
- font-size: 0.9em;
288
- padding: 4px 8px;
289
- background: #f1f5f9;
290
- border-radius: 4px;
291
- }
292
- @media (max-width: 768px) {
293
- body {
294
- padding: 10px;
295
  }
296
- .container {
297
- padding: 10px;
 
 
 
 
 
298
  }
299
- }
300
- </style>
301
- </head>
302
- <body>
303
- <div class="container">
304
- <div class="header">
305
- <h1>Cursor To OpenAI Server</h1>
306
- <p>高性能 AI 模型代理服务</p>
307
- </div>
308
- <div class="info">
309
- <h2>配置信息</h2>
310
- <div class="info-item">
311
- <div class="info-label">聊天来源</div>
312
- <div class="info-value">自定义(兼容 OpenAI)</div>
313
- </div>
314
- <div class="info-item">
315
- <div class="info-label">自定义端点(基本URL)</div>
316
- <div class="info-value" id="endpoint-url"></div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317
  </div>
318
- <div class="info-item">
319
- <div class="info-label">自定义API密钥</div>
320
- <div class="info-value">抓取的Cursor Cookie,格式为user_...</div>
321
  </div>
 
322
  </div>
323
- <div class="models">
324
- <h3>支持的模型列表</h3>
325
- <div id="model-list"></div>
326
- </div>
327
- </div>
328
- <script>
329
- const url = new URL(window.location.href);
330
- const link = url.protocol + '//' + url.host + '/hf/v1';
331
- document.getElementById('endpoint-url').textContent = link;
332
- fetch(link + '/models')
333
- .then(response => response.json())
334
- .then(data => {
335
- const modelList = document.getElementById('model-list');
336
- data.data.forEach(model => {
337
- const div = document.createElement('div');
338
- div.className = 'model-item';
339
- div.innerHTML = `
340
- <span class="model-name">${model.id}</span>
341
- <span class="model-provider">${model.owned_by}</span>
342
- `;
343
- modelList.appendChild(div);
344
  });
345
- })
346
- .catch(error => {
347
- console.error('Error fetching models:', error);
348
- document.getElementById('model-list').textContent = '获取模型列表失败';
349
- });
350
- </script>
351
- </body>
352
- </html>
353
- `;
354
- res.send(htmlContent);
355
  });
356
 
357
  const port = process.env.HF_PORT || 7860;
@@ -361,3 +298,4 @@ app.listen(port, () => {
361
 
362
 
363
 
 
 
7
  const app = express();
8
 
9
  app.use(morgan('dev'));
10
+ app.use(express.json());
11
 
12
  // Checksum 生成函数
13
  function generateChecksum() {
 
184
 
185
  // 将请求数据写回代理请求
186
  if (req.body) {
187
+ const bodyData = JSON.stringify(req.body);
188
+ proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
189
+ proxyReq.write(bodyData);
190
  }
191
  }
192
  }));
193
 
194
  // 主页路由
195
  app.get('/', (req, res) => {
196
+ res.send(`
197
+ <!DOCTYPE html>
198
+ <html>
199
+ <head>
200
+ <title>Cursor To OpenAI</title>
201
+ <meta charset="UTF-8">
202
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
203
+ <style>
204
+ body {
205
+ font-family: Arial, sans-serif;
206
+ margin: 20px;
207
+ background-color: #f5f5f5;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
  }
209
+ .container {
210
+ max-width: 800px;
211
+ margin: 0 auto;
212
+ background-color: white;
213
+ padding: 20px;
214
+ border-radius: 10px;
215
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
216
  }
217
+ .header {
218
+ text-align: center;
219
+ margin-bottom: 30px;
220
+ }
221
+ .header h1 {
222
+ color: #2563eb;
223
+ margin-bottom: 10px;
224
+ }
225
+ .model-list {
226
+ margin-top: 20px;
227
+ }
228
+ .model-item {
229
+ padding: 15px;
230
+ border: 1px solid #ddd;
231
+ margin-bottom: 10px;
232
+ display: flex;
233
+ justify-content: space-between;
234
+ align-items: center;
235
+ border-radius: 5px;
236
+ background-color: #f8fafc;
237
+ }
238
+ .model-item:hover {
239
+ background-color: #f1f5f9;
240
+ }
241
+ .endpoint-info {
242
+ background-color: #f8fafc;
243
+ padding: 15px;
244
+ border-radius: 5px;
245
+ margin: 20px 0;
246
+ border: 1px solid #ddd;
247
+ }
248
+ .endpoint-label {
249
+ font-weight: bold;
250
+ color: #4b5563;
251
+ }
252
+ </style>
253
+ </head>
254
+ <body>
255
+ <div class="container">
256
+ <div class="header">
257
+ <h1>Cursor To OpenAI Server</h1>
258
+ <p>高性能 AI 模型代理服务</p>
259
  </div>
260
+ <div class="endpoint-info">
261
+ <span class="endpoint-label">Endpoint URL:</span>
262
+ <span id="endpoint-url"></span>
263
  </div>
264
+ <div class="model-list" id="model-list"></div>
265
  </div>
266
+ <script>
267
+ const url = new URL(window.location.href);
268
+ const link = url.protocol + '//' + url.host + '/hf/v1';
269
+ document.getElementById('endpoint-url').textContent = link;
270
+
271
+ fetch(link + '/models')
272
+ .then(response => response.json())
273
+ .then(data => {
274
+ const modelList = document.getElementById('model-list');
275
+ data.data.forEach(model => {
276
+ const div = document.createElement('div');
277
+ div.className = 'model-item';
278
+ div.innerHTML =
279
+ '<span>' + model.id + '</span>' +
280
+ '<span>' + model.owned_by + '</span>';
281
+ modelList.appendChild(div);
282
+ });
283
+ })
284
+ .catch(error => {
285
+ console.error('Error fetching models:', error);
286
+ document.getElementById('model-list').textContent = 'Failed to load models';
287
  });
288
+ </script>
289
+ </body>
290
+ </html>
291
+ `);
 
 
 
 
 
 
292
  });
293
 
294
  const port = process.env.HF_PORT || 7860;
 
298
 
299
 
300
 
301
+