isididiidid commited on
Commit
879c88f
·
verified ·
1 Parent(s): 03cd1ea

Update hf.js

Browse files
Files changed (1) hide show
  1. hf.js +44 -16
hf.js CHANGED
@@ -3,18 +3,14 @@ const morgan = require('morgan');
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() {
@@ -22,7 +18,7 @@ function getRandomProxy() {
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,
@@ -40,10 +36,39 @@ function configureAxiosProxy() {
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 = {
@@ -180,13 +205,15 @@ app.get('/hf/v1/models', (req, res) => {
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);
@@ -203,6 +230,8 @@ app.use('/hf/v1/chat/completions', (req, res, next) => {
203
  });
204
  if (proxy) {
205
  console.log(`Request proxied via ${proxy.host}:${proxy.port}`);
 
 
206
  }
207
  middleware(req, res, next);
208
  });
@@ -222,7 +251,6 @@ app.get('/', (req, res) => {
222
  --bg-color: #f8fafc;
223
  --card-bg: #ffffff;
224
  }
225
-
226
  body {
227
  padding: 20px;
228
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
@@ -312,7 +340,6 @@ app.get('/', (req, res) => {
312
  body {
313
  padding: 10px;
314
  }
315
-
316
  .container {
317
  padding: 10px;
318
  }
@@ -374,10 +401,11 @@ app.get('/', (req, res) => {
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
  });
 
 
3
  const { createProxyMiddleware } = require('http-proxy-middleware');
4
  const axios = require('axios');
5
  const fs = require('fs');
6
+ const url = require('url');
7
  const app = express();
8
 
9
  app.use(morgan('dev'));
10
 
11
+ // 全局代理池
12
+ let proxyPool = process.env.PROXY ? process.env.PROXY.split(',').map(p => p.trim()) : [];
13
+ console.log('Initial proxy pool:', proxyPool);
 
 
 
 
14
 
15
  // 从代理池中随机选择一个代理
16
  function getRandomProxy() {
 
18
  const randomIndex = Math.floor(Math.random() * proxyPool.length);
19
  const proxyUrl = proxyPool[randomIndex];
20
  const parsedUrl = url.parse(proxyUrl);
21
+
22
  return {
23
  host: parsedUrl.hostname,
24
  port: parsedUrl.port || 80,
 
36
  axios.defaults.proxy = proxy;
37
  console.log(`Axios using proxy: ${proxy.host}:${proxy.port}`);
38
  } else {
39
+ delete axios.defaults.proxy;
40
+ console.log('No proxy available for axios');
41
+ }
42
+ }
43
+
44
+ // 从外部 API 更新代理池
45
+ async function updateProxyPool() {
46
+ const proxyApiUrl = process.env.PROXY_API_URL || 'http://example.com/api/proxies';
47
+ try {
48
+ const response = await axios.get(proxyApiUrl);
49
+ // 假设 API 返回格式为 { proxies: ["http://user:pass@host:port", ...] }
50
+ const newProxies = response.data.proxies || [];
51
+ if (newProxies.length > 0) {
52
+ proxyPool = newProxies;
53
+ console.log('Proxy pool updated:', proxyPool);
54
+ configureAxiosProxy(); // 更新 axios 代理
55
+ } else {
56
+ console.warn('No proxies received from API');
57
+ }
58
+ } catch (error) {
59
+ console.error('Failed to update proxy pool:', error.message);
60
  }
61
  }
62
 
63
+ // 定期更新代理池
64
+ const updateInterval = parseInt(process.env.PROXY_UPDATE_INTERVAL) || 300; // 默认 5 分钟
65
+ if (updateInterval > 0) {
66
+ setInterval(updateProxyPool, updateInterval * 1000);
67
+ console.log(`Proxy pool will update every ${updateInterval} seconds`);
68
+ // 启动时立即更新一次
69
+ updateProxyPool();
70
+ }
71
+
72
  // 模型列表 API
73
  app.get('/hf/v1/models', (req, res) => {
74
  const models = {
 
205
  res.json(models);
206
  });
207
 
208
+ // 代理转发,使用动态代理池
209
  app.use('/hf/v1/chat/completions', (req, res, next) => {
210
  const proxy = getRandomProxy();
211
  const middleware = createProxyMiddleware({
212
+ target: 'http://localhost:3010/v1/chat/completions', // 可替换为实际目标服务
213
  changeOrigin: true,
214
+ proxy: proxy ? proxy : undefined,
215
+ timeout: 30000, // 超时 30 秒
216
+ proxyTimeout: 30000, // 代理超时 30 秒
217
  onProxyReq: (proxyReq, req, res) => {
218
  if (req.body) {
219
  const bodyData = JSON.stringify(req.body);
 
230
  });
231
  if (proxy) {
232
  console.log(`Request proxied via ${proxy.host}:${proxy.port}`);
233
+ } else {
234
+ console.log('No proxy available, direct connection');
235
  }
236
  middleware(req, res, next);
237
  });
 
251
  --bg-color: #f8fafc;
252
  --card-bg: #ffffff;
253
  }
 
254
  body {
255
  padding: 20px;
256
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
 
340
  body {
341
  padding: 10px;
342
  }
 
343
  .container {
344
  padding: 10px;
345
  }
 
401
  res.send(htmlContent);
402
  });
403
 
404
+ // 启动服务前配置初始 axios 代理
405
  configureAxiosProxy();
406
 
407
  const port = process.env.HF_PORT || 7860;
408
  app.listen(port, () => {
409
  console.log(`HF Proxy server is running at PORT: ${port}`);
410
  });
411
+