Create hf.js
Browse files
hf.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const morgan = require('morgan');
|
| 3 |
+
const { createProxyMiddleware } = require('http-proxy-middleware');
|
| 4 |
+
const url = require('url');
|
| 5 |
+
const app = express();
|
| 6 |
+
|
| 7 |
+
app.use(morgan('dev'));
|
| 8 |
+
|
| 9 |
+
// 从环境变量获取代理配置
|
| 10 |
+
const proxyUrl = process.env.PROXY || '';
|
| 11 |
+
console.log(`Proxy configuration: ${proxyUrl ? '已配置' : '未配置'}`);
|
| 12 |
+
|
| 13 |
+
// 解析代理URL
|
| 14 |
+
let proxyConfig = null;
|
| 15 |
+
if (proxyUrl) {
|
| 16 |
+
try {
|
| 17 |
+
const parsedUrl = url.parse(proxyUrl);
|
| 18 |
+
proxyConfig = {
|
| 19 |
+
host: parsedUrl.hostname,
|
| 20 |
+
port: parsedUrl.port || 80,
|
| 21 |
+
auth: parsedUrl.auth ? {
|
| 22 |
+
username: parsedUrl.auth.split(':')[0],
|
| 23 |
+
password: parsedUrl.auth.split(':')[1]
|
| 24 |
+
} : undefined
|
| 25 |
+
};
|
| 26 |
+
|
| 27 |
+
// 打印代理配置(安全处理密码)
|
| 28 |
+
const maskedConfig = {
|
| 29 |
+
...proxyConfig,
|
| 30 |
+
auth: proxyConfig.auth ? {
|
| 31 |
+
username: proxyConfig.auth.username,
|
| 32 |
+
password: '******'
|
| 33 |
+
} : undefined
|
| 34 |
+
};
|
| 35 |
+
console.log('Using proxy:', JSON.stringify(maskedConfig));
|
| 36 |
+
} catch (error) {
|
| 37 |
+
console.error('Failed to parse proxy URL:', error.message);
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
// 配置代理中间件
|
| 42 |
+
app.use('/hf/v1/chat/completions', createProxyMiddleware({
|
| 43 |
+
target: 'http://localhost:3010/v1/chat/completions',
|
| 44 |
+
changeOrigin: true,
|
| 45 |
+
// 添加代理配置
|
| 46 |
+
proxy: proxyConfig,
|
| 47 |
+
// 增加错误处理
|
| 48 |
+
onError: (err, req, res) => {
|
| 49 |
+
console.error('Proxy error:', err);
|
| 50 |
+
res.status(500).send('Proxy error occurred: ' + err.message);
|
| 51 |
+
},
|
| 52 |
+
onProxyReq: (proxyReq, req, res) => {
|
| 53 |
+
console.log(`Proxying request to chat completions ${proxyConfig ? 'using proxy' : 'directly'}`);
|
| 54 |
+
},
|
| 55 |
+
onProxyRes: (proxyRes, req, res) => {
|
| 56 |
+
console.log(`Received response with status: ${proxyRes.statusCode}`);
|
| 57 |
+
}
|
| 58 |
+
}));
|
| 59 |
+
|
| 60 |
+
app.get('/', (req, res) => {
|
| 61 |
+
const htmlContent = `
|
| 62 |
+
<!DOCTYPE html>
|
| 63 |
+
<html lang="en">
|
| 64 |
+
<head>
|
| 65 |
+
<meta charset="UTF-8">
|
| 66 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 67 |
+
<title>Cursor To OpenAI</title>
|
| 68 |
+
<style>
|
| 69 |
+
body {
|
| 70 |
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
| 71 |
+
max-width: 800px;
|
| 72 |
+
margin: 0 auto;
|
| 73 |
+
padding: 20px;
|
| 74 |
+
line-height: 1.6;
|
| 75 |
+
}
|
| 76 |
+
.container {
|
| 77 |
+
background: #f9f9f9;
|
| 78 |
+
border-radius: 10px;
|
| 79 |
+
padding: 20px;
|
| 80 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
| 81 |
+
}
|
| 82 |
+
.info-item {
|
| 83 |
+
margin-bottom: 10px;
|
| 84 |
+
}
|
| 85 |
+
.status {
|
| 86 |
+
background: ${proxyConfig ? '#e1f5e1' : '#fff3cd'};
|
| 87 |
+
padding: 10px;
|
| 88 |
+
border-radius: 5px;
|
| 89 |
+
margin-top: 20px;
|
| 90 |
+
border: 1px solid ${proxyConfig ? '#c3e6cb' : '#ffeeba'};
|
| 91 |
+
}
|
| 92 |
+
</style>
|
| 93 |
+
</head>
|
| 94 |
+
<body>
|
| 95 |
+
<div class="container">
|
| 96 |
+
<h1>Cursor To OpenAI Server</h1>
|
| 97 |
+
<div class="info-item">
|
| 98 |
+
<strong>聊天来源:</strong> 自定义(兼容 OpenAI)
|
| 99 |
+
</div>
|
| 100 |
+
<div class="info-item">
|
| 101 |
+
<strong>自定义端点(基本URL):</strong><span id="endpoint-url"></span>
|
| 102 |
+
</div>
|
| 103 |
+
<div class="info-item">
|
| 104 |
+
<strong>自定义API密钥:</strong>[抓取的Cursor Cookie,格式为user_...]
|
| 105 |
+
</div>
|
| 106 |
+
<div class="status">
|
| 107 |
+
<strong>代理状态:</strong> ${proxyConfig ? '已启用' : '未启用'}
|
| 108 |
+
${proxyConfig ? `<p>代理服务器: ${proxyConfig.host}:${proxyConfig.port}</p>` : ''}
|
| 109 |
+
</div>
|
| 110 |
+
</div>
|
| 111 |
+
<script>
|
| 112 |
+
const url = new URL(window.location.href);
|
| 113 |
+
const link = url.protocol + '//' + url.host + '/hf/v1';
|
| 114 |
+
document.getElementById('endpoint-url').textContent = link;
|
| 115 |
+
</script>
|
| 116 |
+
</body>
|
| 117 |
+
</html>
|
| 118 |
+
`;
|
| 119 |
+
res.send(htmlContent);
|
| 120 |
+
});
|
| 121 |
+
|
| 122 |
+
const port = process.env.HF_PORT || 7860;
|
| 123 |
+
app.listen(port, () => {
|
| 124 |
+
console.log(`HF Proxy server is running at PORT: ${port}`);
|
| 125 |
+
console.log(`Proxy status: ${proxyConfig ? 'Enabled' : 'Disabled'}`);
|
| 126 |
+
});
|