Spaces:
Paused
Paused
Update src/ProxyServer.js
Browse files- src/ProxyServer.js +29 -92
src/ProxyServer.js
CHANGED
|
@@ -19,6 +19,8 @@ const logger = {
|
|
| 19 |
error: (message) => console.error(chalk.red(`[ProxyServer] ${message}`)),
|
| 20 |
warning: (message) => console.warn(chalk.yellow(`[ProxyServer] ${message}`)),
|
| 21 |
success: (message) => console.log(chalk.green(`[ProxyServer] ${message}`)),
|
|
|
|
|
|
|
| 22 |
};
|
| 23 |
|
| 24 |
class ProxyServer {
|
|
@@ -26,57 +28,36 @@ class ProxyServer {
|
|
| 26 |
this.proxyProcess = null;
|
| 27 |
this.platform = process.env.PROXY_SERVER_PLATFORM || 'auto';
|
| 28 |
this.port = process.env.PROXY_SERVER_PORT || 10655;
|
| 29 |
-
// ***【修改 1】***
|
| 30 |
-
this.logPath = process.env.PROXY_SERVER_LOG_PATH || '/tmp/logs/proxy_server.log';
|
| 31 |
this.enabled = process.env.ENABLE_PROXY_SERVER === 'true';
|
| 32 |
this.proxyAuthToken = process.env.PROXY_AUTH_TOKEN || 'default_token';
|
| 33 |
-
this.logStream = null;
|
| 34 |
}
|
| 35 |
|
| 36 |
-
// 获取当前系统平台
|
| 37 |
detectPlatform() {
|
| 38 |
if (this.platform !== 'auto') {
|
| 39 |
return this.platform;
|
| 40 |
}
|
| 41 |
-
|
| 42 |
const platform = os.platform();
|
| 43 |
const arch = os.arch();
|
| 44 |
-
|
| 45 |
-
if (platform === '
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
if (arch === 'arm64') {
|
| 50 |
-
return 'android'; // 或者 'linux_arm64' 如果你有这个二进制
|
| 51 |
-
} else {
|
| 52 |
-
return 'linux';
|
| 53 |
-
}
|
| 54 |
-
} else if (platform === 'android') {
|
| 55 |
-
return 'android';
|
| 56 |
-
} else {
|
| 57 |
-
logger.warning(`未知平台: ${platform}, ${arch}, 默认使用linux版本`);
|
| 58 |
-
return 'linux';
|
| 59 |
-
}
|
| 60 |
}
|
| 61 |
|
| 62 |
-
// 获取代理服务器可执行文件路径
|
| 63 |
getProxyServerPath() {
|
| 64 |
const platform = this.detectPlatform();
|
| 65 |
-
|
| 66 |
-
// ***【修改 2】*** 在生产环境中(如Docker),从 /tmp/proxy 获取二进制文件
|
| 67 |
-
// 在本地开发时,仍然从 src/proxy 获取
|
| 68 |
const isProduction = process.env.NODE_ENV === 'production';
|
| 69 |
const proxyDir = isProduction ? '/tmp/proxy' : join(__dirname, 'proxy');
|
| 70 |
-
|
| 71 |
logger.info(`环境: ${isProduction ? 'Production' : 'Development'}, 代理二进制目录: ${proxyDir}`);
|
| 72 |
|
| 73 |
switch (platform) {
|
| 74 |
-
case 'windows':
|
| 75 |
-
|
| 76 |
-
case '
|
| 77 |
-
return join(proxyDir, 'chrome_proxy_server_linux_amd64');
|
| 78 |
-
case 'android':
|
| 79 |
-
return join(proxyDir, 'chrome_proxy_server_android_arm64');
|
| 80 |
default:
|
| 81 |
logger.error(`不支持的平台: ${platform}`);
|
| 82 |
return null;
|
|
@@ -89,123 +70,79 @@ class ProxyServer {
|
|
| 89 |
logger.info('代理服务器未启用,跳过启动');
|
| 90 |
return;
|
| 91 |
}
|
| 92 |
-
|
| 93 |
if (this.proxyProcess) {
|
| 94 |
logger.warning('代理服务器已经在运行中');
|
| 95 |
return;
|
| 96 |
}
|
| 97 |
|
| 98 |
const proxyServerPath = this.getProxyServerPath();
|
| 99 |
-
if (!proxyServerPath) {
|
| 100 |
-
logger.error(
|
| 101 |
return;
|
| 102 |
}
|
| 103 |
-
|
| 104 |
-
// 检查文件是否存在,提供更详细的调试信息
|
| 105 |
-
if (!fs.existsSync(proxyServerPath)) {
|
| 106 |
-
logger.error(`代理二进制文件不存在于路径: ${proxyServerPath}`);
|
| 107 |
-
return;
|
| 108 |
-
}
|
| 109 |
-
|
| 110 |
try {
|
| 111 |
-
//
|
| 112 |
-
if (process.env.NODE_ENV !== 'production' && this.detectPlatform() !== 'windows') {
|
| 113 |
-
try {
|
| 114 |
-
fs.chmodSync(proxyServerPath, 0o755);
|
| 115 |
-
} catch (err) {
|
| 116 |
-
logger.warning(`本地开发环境无法设置执行权限: ${err.message}`);
|
| 117 |
-
}
|
| 118 |
-
}
|
| 119 |
-
|
| 120 |
-
// 创建日志文件
|
| 121 |
-
this.logStream = fs.createWriteStream(this.logPath, { flags: 'a' });
|
| 122 |
|
| 123 |
// 启动代理服务器进程
|
| 124 |
this.proxyProcess = spawn(proxyServerPath, [
|
| 125 |
'--port', this.port.toString(),
|
| 126 |
'--token', this.proxyAuthToken
|
| 127 |
], {
|
| 128 |
-
stdio: ['ignore', 'pipe', 'pipe'],
|
| 129 |
detached: false
|
| 130 |
});
|
| 131 |
|
| 132 |
-
// 将进程的输出
|
| 133 |
if (this.proxyProcess.stdout) {
|
| 134 |
-
this.proxyProcess.stdout.
|
|
|
|
|
|
|
| 135 |
}
|
| 136 |
-
|
| 137 |
if (this.proxyProcess.stderr) {
|
| 138 |
-
this.proxyProcess.stderr.
|
|
|
|
|
|
|
| 139 |
}
|
| 140 |
|
| 141 |
-
// 设置进程事件处理
|
| 142 |
this.proxyProcess.on('error', (err) => {
|
| 143 |
logger.error(`代理服务器启动失败: ${err.message}`);
|
| 144 |
this.proxyProcess = null;
|
| 145 |
-
if (this.logStream) {
|
| 146 |
-
this.logStream.end();
|
| 147 |
-
this.logStream = null;
|
| 148 |
-
}
|
| 149 |
});
|
| 150 |
-
|
| 151 |
this.proxyProcess.on('exit', (code, signal) => {
|
| 152 |
logger.info(`代理服务器已退出,退出码: ${code}, 信号: ${signal}`);
|
| 153 |
this.proxyProcess = null;
|
| 154 |
-
if (this.logStream) {
|
| 155 |
-
this.logStream.end();
|
| 156 |
-
this.logStream = null;
|
| 157 |
-
}
|
| 158 |
});
|
| 159 |
|
| 160 |
-
// 等待一段时间,确保服务器启动
|
| 161 |
await new Promise(resolve => setTimeout(resolve, 1000));
|
| 162 |
|
| 163 |
if (this.proxyProcess && this.proxyProcess.exitCode === null) {
|
| 164 |
-
logger.success(`代理服务器已启动,端口: ${this.port}
|
| 165 |
return true;
|
| 166 |
} else {
|
| 167 |
-
logger.error('代理服务器启动失败或立即退出。
|
| 168 |
-
if (this.logStream) {
|
| 169 |
-
this.logStream.end();
|
| 170 |
-
this.logStream = null;
|
| 171 |
-
}
|
| 172 |
return false;
|
| 173 |
}
|
| 174 |
} catch (error) {
|
| 175 |
logger.error(`启动代理服务器时出错: ${error.message}`);
|
| 176 |
-
if (this.logStream) {
|
| 177 |
-
this.logStream.end();
|
| 178 |
-
this.logStream = null;
|
| 179 |
-
}
|
| 180 |
return false;
|
| 181 |
}
|
| 182 |
}
|
| 183 |
|
| 184 |
-
// 停止代理服务器
|
| 185 |
stop() {
|
| 186 |
-
if (!this.proxyProcess)
|
| 187 |
-
//logger.info('代理服务器已关闭');
|
| 188 |
-
return;
|
| 189 |
-
}
|
| 190 |
-
|
| 191 |
try {
|
| 192 |
-
// 在Windows上使用taskkill确保子进程也被终止
|
| 193 |
if (this.detectPlatform() === 'windows' && this.proxyProcess.pid) {
|
| 194 |
spawn('taskkill', ['/pid', this.proxyProcess.pid, '/f', '/t']);
|
| 195 |
} else {
|
| 196 |
-
// 在Linux/Android上使用kill信号
|
| 197 |
this.proxyProcess.kill('SIGTERM');
|
| 198 |
}
|
| 199 |
-
|
| 200 |
logger.success('代理服务器已停止');
|
| 201 |
} catch (error) {
|
| 202 |
logger.error(`停止代理服务器时出错: ${error.message}`);
|
| 203 |
} finally {
|
| 204 |
this.proxyProcess = null;
|
| 205 |
-
if (this.logStream) {
|
| 206 |
-
this.logStream.end();
|
| 207 |
-
this.logStream = null;
|
| 208 |
-
}
|
| 209 |
}
|
| 210 |
}
|
| 211 |
}
|
|
|
|
| 19 |
error: (message) => console.error(chalk.red(`[ProxyServer] ${message}`)),
|
| 20 |
warning: (message) => console.warn(chalk.yellow(`[ProxyServer] ${message}`)),
|
| 21 |
success: (message) => console.log(chalk.green(`[ProxyServer] ${message}`)),
|
| 22 |
+
// 新增一个专门用于子进程输出的 logger
|
| 23 |
+
subprocess: (message) => console.log(chalk.gray(`[ProxyBinary] ${message}`)),
|
| 24 |
};
|
| 25 |
|
| 26 |
class ProxyServer {
|
|
|
|
| 28 |
this.proxyProcess = null;
|
| 29 |
this.platform = process.env.PROXY_SERVER_PLATFORM || 'auto';
|
| 30 |
this.port = process.env.PROXY_SERVER_PORT || 10655;
|
| 31 |
+
// ***【修改 1】*** 移除了 this.logPath 和 this.logStream
|
|
|
|
| 32 |
this.enabled = process.env.ENABLE_PROXY_SERVER === 'true';
|
| 33 |
this.proxyAuthToken = process.env.PROXY_AUTH_TOKEN || 'default_token';
|
|
|
|
| 34 |
}
|
| 35 |
|
| 36 |
+
// 获取当前系统平台 (无变化)
|
| 37 |
detectPlatform() {
|
| 38 |
if (this.platform !== 'auto') {
|
| 39 |
return this.platform;
|
| 40 |
}
|
|
|
|
| 41 |
const platform = os.platform();
|
| 42 |
const arch = os.arch();
|
| 43 |
+
if (platform === 'win32') return 'windows';
|
| 44 |
+
if (platform === 'linux') return (arch === 'arm64' ? 'android' : 'linux');
|
| 45 |
+
if (platform === 'android') return 'android';
|
| 46 |
+
logger.warning(`未知平台: ${platform}, ${arch}, 默认使用linux版本`);
|
| 47 |
+
return 'linux';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
+
// 获取代理服务器可执行文件路径 (无变化)
|
| 51 |
getProxyServerPath() {
|
| 52 |
const platform = this.detectPlatform();
|
|
|
|
|
|
|
|
|
|
| 53 |
const isProduction = process.env.NODE_ENV === 'production';
|
| 54 |
const proxyDir = isProduction ? '/tmp/proxy' : join(__dirname, 'proxy');
|
|
|
|
| 55 |
logger.info(`环境: ${isProduction ? 'Production' : 'Development'}, 代理二进制目录: ${proxyDir}`);
|
| 56 |
|
| 57 |
switch (platform) {
|
| 58 |
+
case 'windows': return join(proxyDir, 'chrome_proxy_server_windows_amd64.exe');
|
| 59 |
+
case 'linux': return join(proxyDir, 'chrome_proxy_server_linux_amd64');
|
| 60 |
+
case 'android': return join(proxyDir, 'chrome_proxy_server_android_arm64');
|
|
|
|
|
|
|
|
|
|
| 61 |
default:
|
| 62 |
logger.error(`不支持的平台: ${platform}`);
|
| 63 |
return null;
|
|
|
|
| 70 |
logger.info('代理服务器未启用,跳过启动');
|
| 71 |
return;
|
| 72 |
}
|
|
|
|
| 73 |
if (this.proxyProcess) {
|
| 74 |
logger.warning('代理服务器已经在运行中');
|
| 75 |
return;
|
| 76 |
}
|
| 77 |
|
| 78 |
const proxyServerPath = this.getProxyServerPath();
|
| 79 |
+
if (!proxyServerPath || !fs.existsSync(proxyServerPath)) {
|
| 80 |
+
logger.error(`代理二进制文件不存在于路径: ${proxyServerPath || '未知'}`);
|
| 81 |
return;
|
| 82 |
}
|
| 83 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
try {
|
| 85 |
+
// ***【修改 2】*** 移除了所有与文件日志相关的代码
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
// 启动代理服务器进程
|
| 88 |
this.proxyProcess = spawn(proxyServerPath, [
|
| 89 |
'--port', this.port.toString(),
|
| 90 |
'--token', this.proxyAuthToken
|
| 91 |
], {
|
| 92 |
+
stdio: ['ignore', 'pipe', 'pipe'],
|
| 93 |
detached: false
|
| 94 |
});
|
| 95 |
|
| 96 |
+
// ***【修改 3】*** 将子进程的输出直接打印到控制台
|
| 97 |
if (this.proxyProcess.stdout) {
|
| 98 |
+
this.proxyProcess.stdout.on('data', (data) => {
|
| 99 |
+
logger.subprocess(data.toString().trim());
|
| 100 |
+
});
|
| 101 |
}
|
|
|
|
| 102 |
if (this.proxyProcess.stderr) {
|
| 103 |
+
this.proxyProcess.stderr.on('data', (data) => {
|
| 104 |
+
logger.error(`[ProxyBinary-Error] ${data.toString().trim()}`);
|
| 105 |
+
});
|
| 106 |
}
|
| 107 |
|
|
|
|
| 108 |
this.proxyProcess.on('error', (err) => {
|
| 109 |
logger.error(`代理服务器启动失败: ${err.message}`);
|
| 110 |
this.proxyProcess = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
});
|
|
|
|
| 112 |
this.proxyProcess.on('exit', (code, signal) => {
|
| 113 |
logger.info(`代理服务器已退出,退出码: ${code}, 信号: ${signal}`);
|
| 114 |
this.proxyProcess = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
});
|
| 116 |
|
|
|
|
| 117 |
await new Promise(resolve => setTimeout(resolve, 1000));
|
| 118 |
|
| 119 |
if (this.proxyProcess && this.proxyProcess.exitCode === null) {
|
| 120 |
+
logger.success(`代理服务器已启动,端口: ${this.port}`);
|
| 121 |
return true;
|
| 122 |
} else {
|
| 123 |
+
logger.error('代理服务器启动失败或立即退出。');
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
return false;
|
| 125 |
}
|
| 126 |
} catch (error) {
|
| 127 |
logger.error(`启动代理服务器时出错: ${error.message}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
return false;
|
| 129 |
}
|
| 130 |
}
|
| 131 |
|
| 132 |
+
// 停止代理服务器 (无变化)
|
| 133 |
stop() {
|
| 134 |
+
if (!this.proxyProcess) return;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
try {
|
|
|
|
| 136 |
if (this.detectPlatform() === 'windows' && this.proxyProcess.pid) {
|
| 137 |
spawn('taskkill', ['/pid', this.proxyProcess.pid, '/f', '/t']);
|
| 138 |
} else {
|
|
|
|
| 139 |
this.proxyProcess.kill('SIGTERM');
|
| 140 |
}
|
|
|
|
| 141 |
logger.success('代理服务器已停止');
|
| 142 |
} catch (error) {
|
| 143 |
logger.error(`停止代理服务器时出错: ${error.message}`);
|
| 144 |
} finally {
|
| 145 |
this.proxyProcess = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
}
|
| 147 |
}
|
| 148 |
}
|