Update hf.js
Browse files
hf.js
CHANGED
|
@@ -3,10 +3,48 @@ const morgan = require('morgan');
|
|
| 3 |
const { createProxyMiddleware } = require('http-proxy-middleware');
|
| 4 |
const axios = require('axios');
|
| 5 |
const fs = require('fs');
|
|
|
|
| 6 |
const app = express();
|
| 7 |
|
| 8 |
app.use(morgan('dev'));
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
app.get('/hf/v1/models', (req, res) => {
|
| 11 |
const models = {
|
| 12 |
"object": "list",
|
|
@@ -142,18 +180,34 @@ app.get('/hf/v1/models', (req, res) => {
|
|
| 142 |
res.json(models);
|
| 143 |
});
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
//
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
}
|
|
|
|
|
|
|
|
|
|
| 154 |
}
|
| 155 |
-
|
|
|
|
| 156 |
|
|
|
|
| 157 |
app.get('/', (req, res) => {
|
| 158 |
const htmlContent = `
|
| 159 |
<!DOCTYPE html>
|
|
@@ -320,11 +374,10 @@ app.get('/', (req, res) => {
|
|
| 320 |
res.send(htmlContent);
|
| 321 |
});
|
| 322 |
|
|
|
|
|
|
|
|
|
|
| 323 |
const port = process.env.HF_PORT || 7860;
|
| 324 |
app.listen(port, () => {
|
| 325 |
console.log(`HF Proxy server is running at PORT: ${port}`);
|
| 326 |
});
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
|
|
|
| 3 |
const { createProxyMiddleware } = require('http-proxy-middleware');
|
| 4 |
const axios = require('axios');
|
| 5 |
const fs = require('fs');
|
| 6 |
+
const url = require('url'); // 用于解析代理 URL
|
| 7 |
const app = express();
|
| 8 |
|
| 9 |
app.use(morgan('dev'));
|
| 10 |
|
| 11 |
+
// 从环境变量获取代理池
|
| 12 |
+
const proxyPool = process.env.PROXY ? process.env.PROXY.split(',').map(p => p.trim()) : [];
|
| 13 |
+
if (proxyPool.length > 0) {
|
| 14 |
+
console.log('Proxy pool initialized with:', proxyPool);
|
| 15 |
+
} else {
|
| 16 |
+
console.log('No proxies configured in PROXY environment variable');
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
// 从代理池中随机选择一个代理
|
| 20 |
+
function getRandomProxy() {
|
| 21 |
+
if (proxyPool.length === 0) return null;
|
| 22 |
+
const randomIndex = Math.floor(Math.random() * proxyPool.length);
|
| 23 |
+
const proxyUrl = proxyPool[randomIndex];
|
| 24 |
+
const parsedUrl = url.parse(proxyUrl);
|
| 25 |
+
|
| 26 |
+
return {
|
| 27 |
+
host: parsedUrl.hostname,
|
| 28 |
+
port: parsedUrl.port || 80,
|
| 29 |
+
auth: parsedUrl.auth ? {
|
| 30 |
+
username: parsedUrl.auth.split(':')[0],
|
| 31 |
+
password: parsedUrl.auth.split(':')[1]
|
| 32 |
+
} : undefined
|
| 33 |
+
};
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
// 配置 axios 的代理
|
| 37 |
+
function configureAxiosProxy() {
|
| 38 |
+
const proxy = getRandomProxy();
|
| 39 |
+
if (proxy) {
|
| 40 |
+
axios.defaults.proxy = proxy;
|
| 41 |
+
console.log(`Axios using proxy: ${proxy.host}:${proxy.port}`);
|
| 42 |
+
} else {
|
| 43 |
+
delete axios.defaults.proxy; // 如果没有代理,移除默认代理
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
// 模型列表 API
|
| 48 |
app.get('/hf/v1/models', (req, res) => {
|
| 49 |
const models = {
|
| 50 |
"object": "list",
|
|
|
|
| 180 |
res.json(models);
|
| 181 |
});
|
| 182 |
|
| 183 |
+
// 代理转发,使用代理池
|
| 184 |
+
app.use('/hf/v1/chat/completions', (req, res, next) => {
|
| 185 |
+
const proxy = getRandomProxy();
|
| 186 |
+
const middleware = createProxyMiddleware({
|
| 187 |
+
target: 'http://localhost:3010/v1/chat/completions',
|
| 188 |
+
changeOrigin: true,
|
| 189 |
+
proxy: proxy ? proxy : undefined, // 如果有代理则使用
|
| 190 |
+
onProxyReq: (proxyReq, req, res) => {
|
| 191 |
+
if (req.body) {
|
| 192 |
+
const bodyData = JSON.stringify(req.body);
|
| 193 |
+
proxyReq.setHeader('Content-Type', 'application/json');
|
| 194 |
+
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
|
| 195 |
+
proxyReq.write(bodyData);
|
| 196 |
+
proxyReq.end();
|
| 197 |
+
}
|
| 198 |
+
},
|
| 199 |
+
onError: (err, req, res) => {
|
| 200 |
+
console.error('Proxy error:', err);
|
| 201 |
+
res.status(500).send('Proxy error occurred');
|
| 202 |
}
|
| 203 |
+
});
|
| 204 |
+
if (proxy) {
|
| 205 |
+
console.log(`Request proxied via ${proxy.host}:${proxy.port}`);
|
| 206 |
}
|
| 207 |
+
middleware(req, res, next);
|
| 208 |
+
});
|
| 209 |
|
| 210 |
+
// 首页
|
| 211 |
app.get('/', (req, res) => {
|
| 212 |
const htmlContent = `
|
| 213 |
<!DOCTYPE html>
|
|
|
|
| 374 |
res.send(htmlContent);
|
| 375 |
});
|
| 376 |
|
| 377 |
+
// 启动服务前配置 axios 代理
|
| 378 |
+
configureAxiosProxy();
|
| 379 |
+
|
| 380 |
const port = process.env.HF_PORT || 7860;
|
| 381 |
app.listen(port, () => {
|
| 382 |
console.log(`HF Proxy server is running at PORT: ${port}`);
|
| 383 |
});
|
|
|
|
|
|
|
|
|
|
|
|