File size: 12,235 Bytes
a760633
 
 
715d580
a6d5735
a760633
 
 
8ae0569
a760633
5e7ccb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a08d88
a760633
5a08d88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a760633
 
edcbf4e
5a08d88
ab504b8
 
 
8be2f45
6ff40a0
15bccc3
edcbf4e
 
15bccc3
2e1ecc7
 
edcbf4e
2e1ecc7
 
e7af0b5
2e1ecc7
e7af0b5
2e1ecc7
 
 
 
 
e7af0b5
2e1ecc7
 
ab504b8
2e1ecc7
 
e7af0b5
edcbf4e
15bccc3
ab504b8
 
 
 
 
 
 
 
 
 
 
 
 
88c933d
 
a760633
ab504b8
2e1ecc7
8be2f45
a10d18f
15bccc3
5a08d88
a760633
6ff40a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ae0569
6ff40a0
 
8ae0569
6ff40a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a08d88
6ff40a0
 
 
5a08d88
 
6ff40a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a08d88
6ff40a0
 
 
 
 
 
 
 
 
 
a760633
 
 
a6d5735
5a08d88
40cafbe
 
f132aad
5a08d88
8ae0569
6ff40a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
const express = require('express');
const morgan = require('morgan');
const { createProxyMiddleware } = require('http-proxy-middleware');
const axios = require('axios');
const fs = require('fs');
const app = express();

app.use(morgan('dev'));
app.use(express.json());

// 生成随机字符串的函数
function getRandomIDPro({ size, dictType, customDict }) {
    let random = '';
    if (!customDict) {
        switch (dictType) {
            case 'alphabet':
                customDict = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                break;
            case 'max':
                customDict = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-';
                break;
            default:
                customDict = '0123456789';
        }
    }
    for (; size--; ) random += customDict[(Math.random() * customDict.length) | 0];
    return random;
}

// 模型列表路由
app.get('/hf/v1/models', (req, res) => {
    const models = {
        "object": "list",
        "data": [
            {
                "id": "claude-3.5-sonnet",
                "object": "model",
                "created": 1706745938,
                "owned_by": "cursor"
            },
            {
                "id": "gpt-4",
                "object": "model",
                "created": 1706745938,
                "owned_by": "cursor"
            },
            {
                "id": "gpt-4o",
                "object": "model",
                "created": 1706745938,
                "owned_by": "cursor"
            },
            {
                "id": "claude-3-opus",
                "object": "model",
                "created": 1706745938,
                "owned_by": "cursor"
            },
            {
                "id": "gpt-3.5-turbo",
                "object": "model",
                "created": 1706745938,
                "owned_by": "cursor"
            },
            {
                "id": "gpt-4-turbo-2024-04-09",
                "object": "model",
                "created": 1706745938,
                "owned_by": "cursor"
            },
            {
                "id": "gpt-4o-128k",
                "object": "model",
                "created": 1706745938,
                "owned_by": "cursor"
            },
            {
                "id": "gemini-1.5-flash-500k",
                "object": "model",
                "created": 1706745938,
                "owned_by": "cursor"
            },
            {
                "id": "claude-3-haiku-200k",
                "object": "model",
                "created": 1706745938,
                "owned_by": "cursor"
            },
            {
                "id": "claude-3-5-sonnet-200k",
                "object": "model",
                "created": 1706745938,
                "owned_by": "cursor"
            },
            {
                "id": "claude-3-5-sonnet-20241022",
                "object": "model",
                "created": 1706745938,
                "owned_by": "cursor"
            },
            {
                "id": "gpt-4o-mini",
                "object": "model",
                "created": 1706745938,
                "owned_by": "cursor"
            },
            {
                "id": "o1-mini",
                "object": "model",
                "created": 1706745938,
                "owned_by": "cursor"
            },
            {
                "id": "o1-preview",
                "object": "model",
                "created": 1706745938,
                "owned_by": "cursor"
            },
            {
                "id": "o1",
                "object": "model",
                "created": 1706745938,
                "owned_by": "cursor"
            },
            {
                "id": "claude-3.5-haiku",
                "object": "model",
                "created": 1706745938,
                "owned_by": "cursor"
            },
            {
                "id": "gemini-exp-1206",
                "object": "model",
                "created": 1706745938,
                "owned_by": "cursor"
            },
            {
                "id": "gemini-2.0-flash-thinking-exp",
                "object": "model",
                "created": 1706745938,
                "owned_by": "cursor"
            },
            {
                "id": "gemini-2.0-flash-exp",
                "object": "model",
                "created": 1706745938,
                "owned_by": "cursor"
            },
            {
                "id": "deepseek-v3",
                "object": "model",
                "created": 1706745938,
                "owned_by": "cursor"
            },
            {
                "id": "deepseek-r1",
                "object": "model",
                "created": 1706745938,
                "owned_by": "cursor"
            }
        ]
    };
    res.json(models);
});


// 聊天完成度代理
app.post('/hf/v1/chat/completions', async (req, res) => {
    try {
        // 生成 checksum
        const checksum = `ICMlQsvO${getRandomIDPro({ dictType: 'max', size: 64 })}/${getRandomIDPro({ dictType: 'max', size: 64 })}`;

        // 从请求头获取认证信息
        const authHeader = req.headers.authorization;
        const token = authHeader?.replace('Bearer ', '') || '';

        // 构建请求头
        const headers = {
            'Authorization': authHeader,
            'Content-Type': 'application/json',
            'x-cursor-checksum': checksum,
            'x-cursor-token': token,
            'User-Agent': 'cursor/0.2.0',
            'Accept': 'application/json'
        };

        // 构建请求体
        const requestBody = {
            ...req.body,
            stream: false
        };

        // 转发请求到目标服务器
        const response = await axios.post('http://localhost:3010/v1/chat/completions', requestBody, {
            headers: headers,
            timeout: 30000
        });

        // 返回响应
        res.status(response.status).json(response.data);
    } catch (error) {
        console.error('Error in chat completions:', error);
        if (error.response) {
            res.status(error.response.status).json(error.response.data);
        } else {
            res.status(500).json({ 
                error: 'Internal server error',
                message: error.message 
            });
        }
    }
});







// 主页路由
app.get('/', (req, res) => {
    const htmlContent = `
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Cursor To OpenAI</title>
        <style>
            :root {
                --primary-color: #2563eb;
                --bg-color: #f8fafc;
                --card-bg: #ffffff;
            }
            body {
                padding: 20px;
                font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
                max-width: 1000px;
                margin: 0 auto;
                line-height: 1.6;
                background: var(--bg-color);
                color: #1a1a1a;
            }
            .container {
                padding: 20px;
            }
            .header {
                text-align: center;
                margin-bottom: 40px;
            }
            .header h1 {
                color: var(--primary-color);
                font-size: 2.5em;
                margin-bottom: 10px;
            }
            .info {
                background: #fff;
                padding: 25px;
                border-radius: 12px;
                box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
                margin-bottom: 30px;
                border: 1px solid #e5e7eb;
            }
            .info-item {
                margin: 15px 0;
                padding: 10px;
                background: #f8fafc;
                border-radius: 8px;
                border: 1px solid #e5e7eb;
            }
            .info-label {
                color: #4b5563;
                font-size: 0.9em;
                margin-bottom: 5px;
            }
            .info-value {
                color: var(--primary-color);
                font-weight: 500;
            }
            .models {
                background: var(--card-bg);
                padding: 25px;
                border-radius: 12px;
                box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
                border: 1px solid #e5e7eb;
            }
            .models h3 {
                color: #1a1a1a;
                margin-bottom: 20px;
                font-size: 1.5em;
                border-bottom: 2px solid #e5e7eb;
                padding-bottom: 10px;
            }
            .model-item {
                margin: 12px 0;
                padding: 15px;
                background: #f8fafc;
                border-radius: 8px;
                border: 1px solid #e5e7eb;
                transition: all 0.3s ease;
                display: flex;
                justify-content: space-between;
                align-items: center;
            }
            .model-item:hover {
                transform: translateY(-2px);
                box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            }
            .model-name {
                font-weight: 500;
                color: #1a1a1a;
            }
            .model-provider {
                color: #6b7280;
                font-size: 0.9em;
                padding: 4px 8px;
                background: #f1f5f9;
                border-radius: 4px;
            }
            @media (max-width: 768px) {
                body {
                    padding: 10px;
                }
                .container {
                    padding: 10px;
                }
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="header">
                <h1>Cursor To OpenAI Server</h1>
                <p>高性能 AI 模型代理服务</p>
            </div>
            <div class="info">
                <h2>配置信息</h2>
                <div class="info-item">
                    <div class="info-label">聊天来源</div>
                    <div class="info-value">自定义(兼容 OpenAI)</div>
                </div>
                <div class="info-item">
                    <div class="info-label">自定义端点(基本URL)</div>
                    <div class="info-value" id="endpoint-url"></div>
                </div>
                <div class="info-item">
                    <div class="info-label">自定义API密钥</div>
                    <div class="info-value">抓取的Cursor Cookie,格式为user_...</div>
                </div>
            </div>
            <div class="models">
                <h3>支持的模型列表</h3>
                <div id="model-list"></div>
            </div>
        </div>
        <script>
            const url = new URL(window.location.href);
            const link = url.protocol + '//' + url.host + '/hf/v1';
            document.getElementById('endpoint-url').textContent = link;
            fetch(link + '/models')
                .then(response => response.json())
                .then(data => {
                    const modelList = document.getElementById('model-list');
                    data.data.forEach(model => {
                        const div = document.createElement('div');
                        div.className = 'model-item';
                        div.innerHTML = 
                            '<span class="model-name">' + model.id + '</span>' +
                            '<span class="model-provider">' + model.owned_by + '</span>';
                        modelList.appendChild(div);
                    });
                })
                .catch(error => {
                    console.error('Error fetching models:', error);
                    document.getElementById('model-list').textContent = '获取模型列表失败';
                });
        </script>
    </body>
    </html>
    `;
    res.send(htmlContent);
});

const port = process.env.HF_PORT || 7860;
app.listen(port, () => {
    console.log(`HF Proxy server is running at PORT: ${port}`);
});