holyterra commited on
Commit
0ad2e48
·
verified ·
1 Parent(s): 57b1850

Update unified-server.js

Browse files
Files changed (1) hide show
  1. unified-server.js +10 -20
unified-server.js CHANGED
@@ -1,4 +1,4 @@
1
- // unified-server.js (v6 - API Key Authentication)
2
 
3
  // --- 核心依赖 ---
4
  const express = require('express');
@@ -643,7 +643,7 @@ class ProxyServerSystem extends EventEmitter {
643
  let config = {
644
  httpPort: 7860, host: '0.0.0.0', wsPort: 9998, streamingMode: 'real',
645
  failureThreshold: 3, maxRetries: 3, retryDelay: 2000, browserExecutablePath: null,
646
- apiKeys: [], // <-- MODIFICATION: Add apiKeys default
647
  };
648
  const configPath = path.join(__dirname, 'config.json');
649
  try {
@@ -663,7 +663,6 @@ class ProxyServerSystem extends EventEmitter {
663
  if (process.env.RETRY_DELAY) config.retryDelay = parseInt(process.env.RETRY_DELAY, 10) || config.retryDelay;
664
  if (process.env.CAMOUFOX_EXECUTABLE_PATH) config.browserExecutablePath = process.env.CAMOUFOX_EXECUTABLE_PATH;
665
 
666
- // --- MODIFICATION: Load API_KEYS from environment variable ---
667
  if (process.env.API_KEYS) {
668
  config.apiKeys = process.env.API_KEYS.split(',').map(key => key.trim()).filter(key => key);
669
  }
@@ -714,13 +713,8 @@ class ProxyServerSystem extends EventEmitter {
714
  app.use(express.json({ limit: '100mb' }));
715
  app.use(express.raw({ type: '*/*', limit: '100mb' }));
716
 
717
- app.get('/admin/set-mode', (req, res) => { /* ... unchanged ... */ });
718
-
719
- app.get('/health', (req, res) => { /* ... unchanged ... */ });
720
-
721
- // --- MODIFICATION: Main request handler with authentication middleware ---
722
  app.all(/(.*)/, (req, res, next) => {
723
- // Public endpoints that do not require authentication
724
  if (req.path.startsWith('/admin/') || req.path === '/health') {
725
  return next();
726
  }
@@ -728,30 +722,26 @@ class ProxyServerSystem extends EventEmitter {
728
  return res.status(204).send();
729
  }
730
 
731
- // API Key Authentication Middleware
732
  const apiKeys = this.config.apiKeys;
733
  if (apiKeys && apiKeys.length > 0) {
734
- const authHeader = req.headers.authorization;
735
- if (!authHeader || !authHeader.startsWith('Bearer ')) {
736
- this.logger.warn(`[Auth] Blocked request: Missing or invalid Authorization header from ${req.ip}`);
737
- return res.status(401).json({ error: { message: 'Authorization header is missing or invalid. Format: "Bearer <YOUR_API_KEY>".', type: 'authentication_error' } });
738
- }
739
 
740
- const providedKey = authHeader.substring(7); // "Bearer ".length is 7
 
 
 
741
 
742
  if (!apiKeys.includes(providedKey)) {
743
- this.logger.warn(`[Auth] Blocked request: Invalid API Key provided from ${req.ip}`);
744
  return res.status(401).json({ error: { message: 'Invalid API Key provided.', type: 'authentication_error' } });
745
  }
746
 
747
- this.logger.debug(`[Auth] Request from ${req.ip} authenticated successfully.`);
748
  }
749
 
750
- // If authentication passes (or is disabled), process the request
751
  this.requestHandler.processRequest(req, res);
752
  });
753
 
754
- // Move admin handlers to be processed by the main app.all router
755
  app.get('/admin/set-mode', (req, res) => {
756
  const newMode = req.query.mode;
757
  if (newMode === 'fake' || newMode === 'real') {
 
1
+ // unified-server.js (v7 - Gemini API Key Format)
2
 
3
  // --- 核心依赖 ---
4
  const express = require('express');
 
643
  let config = {
644
  httpPort: 7860, host: '0.0.0.0', wsPort: 9998, streamingMode: 'real',
645
  failureThreshold: 3, maxRetries: 3, retryDelay: 2000, browserExecutablePath: null,
646
+ apiKeys: [],
647
  };
648
  const configPath = path.join(__dirname, 'config.json');
649
  try {
 
663
  if (process.env.RETRY_DELAY) config.retryDelay = parseInt(process.env.RETRY_DELAY, 10) || config.retryDelay;
664
  if (process.env.CAMOUFOX_EXECUTABLE_PATH) config.browserExecutablePath = process.env.CAMOUFOX_EXECUTABLE_PATH;
665
 
 
666
  if (process.env.API_KEYS) {
667
  config.apiKeys = process.env.API_KEYS.split(',').map(key => key.trim()).filter(key => key);
668
  }
 
713
  app.use(express.json({ limit: '100mb' }));
714
  app.use(express.raw({ type: '*/*', limit: '100mb' }));
715
 
716
+ // --- MODIFICATION: Main request handler with Gemini-style authentication ---
 
 
 
 
717
  app.all(/(.*)/, (req, res, next) => {
 
718
  if (req.path.startsWith('/admin/') || req.path === '/health') {
719
  return next();
720
  }
 
722
  return res.status(204).send();
723
  }
724
 
 
725
  const apiKeys = this.config.apiKeys;
726
  if (apiKeys && apiKeys.length > 0) {
727
+ const providedKey = req.query.key;
 
 
 
 
728
 
729
+ if (!providedKey) {
730
+ this.logger.warn(`[Auth] Blocked request: Missing 'key' query parameter from ${req.ip}`);
731
+ return res.status(401).json({ error: { message: "API key is missing. Please pass it as a query parameter: ?key=YOUR_API_KEY.", type: 'authentication_error' } });
732
+ }
733
 
734
  if (!apiKeys.includes(providedKey)) {
735
+ this.logger.warn(`[Auth] Blocked request: Invalid API Key provided in query parameter from ${req.ip}`);
736
  return res.status(401).json({ error: { message: 'Invalid API Key provided.', type: 'authentication_error' } });
737
  }
738
 
739
+ this.logger.debug(`[Auth] Request from ${req.ip} authenticated successfully via query parameter.`);
740
  }
741
 
 
742
  this.requestHandler.processRequest(req, res);
743
  });
744
 
 
745
  app.get('/admin/set-mode', (req, res) => {
746
  const newMode = req.query.mode;
747
  if (newMode === 'fake' || newMode === 'real') {