Spaces:
Paused
Paused
Upload 15 files
Browse files- src/ProxyServer.js +4 -39
src/ProxyServer.js
CHANGED
|
@@ -26,10 +26,8 @@ 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 |
-
this.logPath = process.env.PROXY_SERVER_LOG_PATH || './proxy_server.log';
|
| 30 |
this.enabled = process.env.ENABLE_PROXY_SERVER === 'true';
|
| 31 |
this.proxyAuthToken = process.env.PROXY_AUTH_TOKEN || 'default_token';
|
| 32 |
-
this.logStream = null;
|
| 33 |
}
|
| 34 |
|
| 35 |
// 获取当前系统平台
|
|
@@ -94,19 +92,6 @@ class ProxyServer {
|
|
| 94 |
}
|
| 95 |
|
| 96 |
try {
|
| 97 |
-
// 确保可执行文件有执行权限(在Linux/Android上)
|
| 98 |
-
if (this.detectPlatform() !== 'windows') {
|
| 99 |
-
try {
|
| 100 |
-
fs.chmodSync(proxyServerPath, 0o755);
|
| 101 |
-
} catch (err) {
|
| 102 |
-
logger.warning(`无法设置执行权限: ${err.message}`);
|
| 103 |
-
}
|
| 104 |
-
}
|
| 105 |
-
|
| 106 |
-
// 创建日志文件
|
| 107 |
-
this.logStream = fs.createWriteStream(this.logPath, { flags: 'a' });
|
| 108 |
-
|
| 109 |
-
// 修复 stdio 参数问题
|
| 110 |
// 启动代理服务器进程
|
| 111 |
this.proxyProcess = spawn(proxyServerPath, [
|
| 112 |
'--port', this.port.toString(),
|
|
@@ -116,54 +101,38 @@ class ProxyServer {
|
|
| 116 |
detached: false
|
| 117 |
});
|
| 118 |
|
| 119 |
-
//
|
| 120 |
if (this.proxyProcess.stdout) {
|
| 121 |
-
this.proxyProcess.stdout.pipe(
|
| 122 |
}
|
| 123 |
|
| 124 |
if (this.proxyProcess.stderr) {
|
| 125 |
-
this.proxyProcess.stderr.pipe(
|
| 126 |
}
|
| 127 |
|
| 128 |
// 设置进程事件处理
|
| 129 |
this.proxyProcess.on('error', (err) => {
|
| 130 |
logger.error(`代理服务器启动失败: ${err.message}`);
|
| 131 |
this.proxyProcess = null;
|
| 132 |
-
if (this.logStream) {
|
| 133 |
-
this.logStream.end();
|
| 134 |
-
this.logStream = null;
|
| 135 |
-
}
|
| 136 |
});
|
| 137 |
|
| 138 |
this.proxyProcess.on('exit', (code, signal) => {
|
| 139 |
logger.info(`代理服务器已退出,退出码: ${code}, 信号: ${signal}`);
|
| 140 |
this.proxyProcess = null;
|
| 141 |
-
if (this.logStream) {
|
| 142 |
-
this.logStream.end();
|
| 143 |
-
this.logStream = null;
|
| 144 |
-
}
|
| 145 |
});
|
| 146 |
|
| 147 |
// 等待一段时间,确保服务器启动
|
| 148 |
await new Promise(resolve => setTimeout(resolve, 1000));
|
| 149 |
|
| 150 |
if (this.proxyProcess && this.proxyProcess.exitCode === null) {
|
| 151 |
-
logger.success(`代理服务器已启动,端口: ${this.port}
|
| 152 |
return true;
|
| 153 |
} else {
|
| 154 |
logger.error('代理服务器启动失败');
|
| 155 |
-
if (this.logStream) {
|
| 156 |
-
this.logStream.end();
|
| 157 |
-
this.logStream = null;
|
| 158 |
-
}
|
| 159 |
return false;
|
| 160 |
}
|
| 161 |
} catch (error) {
|
| 162 |
logger.error(`启动代理服务器时出错: ${error.message}`);
|
| 163 |
-
if (this.logStream) {
|
| 164 |
-
this.logStream.end();
|
| 165 |
-
this.logStream = null;
|
| 166 |
-
}
|
| 167 |
return false;
|
| 168 |
}
|
| 169 |
}
|
|
@@ -189,10 +158,6 @@ class ProxyServer {
|
|
| 189 |
logger.error(`停止代理服务器时出错: ${error.message}`);
|
| 190 |
} finally {
|
| 191 |
this.proxyProcess = null;
|
| 192 |
-
if (this.logStream) {
|
| 193 |
-
this.logStream.end();
|
| 194 |
-
this.logStream = null;
|
| 195 |
-
}
|
| 196 |
}
|
| 197 |
}
|
| 198 |
}
|
|
|
|
| 26 |
this.proxyProcess = null;
|
| 27 |
this.platform = process.env.PROXY_SERVER_PLATFORM || 'auto';
|
| 28 |
this.port = process.env.PROXY_SERVER_PORT || 10655;
|
|
|
|
| 29 |
this.enabled = process.env.ENABLE_PROXY_SERVER === 'true';
|
| 30 |
this.proxyAuthToken = process.env.PROXY_AUTH_TOKEN || 'default_token';
|
|
|
|
| 31 |
}
|
| 32 |
|
| 33 |
// 获取当前系统平台
|
|
|
|
| 92 |
}
|
| 93 |
|
| 94 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
// 启动代理服务器进程
|
| 96 |
this.proxyProcess = spawn(proxyServerPath, [
|
| 97 |
'--port', this.port.toString(),
|
|
|
|
| 101 |
detached: false
|
| 102 |
});
|
| 103 |
|
| 104 |
+
// 将进程的输出重定向到主进程
|
| 105 |
if (this.proxyProcess.stdout) {
|
| 106 |
+
this.proxyProcess.stdout.pipe(process.stdout);
|
| 107 |
}
|
| 108 |
|
| 109 |
if (this.proxyProcess.stderr) {
|
| 110 |
+
this.proxyProcess.stderr.pipe(process.stderr);
|
| 111 |
}
|
| 112 |
|
| 113 |
// 设置进程事件处理
|
| 114 |
this.proxyProcess.on('error', (err) => {
|
| 115 |
logger.error(`代理服务器启动失败: ${err.message}`);
|
| 116 |
this.proxyProcess = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
});
|
| 118 |
|
| 119 |
this.proxyProcess.on('exit', (code, signal) => {
|
| 120 |
logger.info(`代理服务器已退出,退出码: ${code}, 信号: ${signal}`);
|
| 121 |
this.proxyProcess = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
});
|
| 123 |
|
| 124 |
// 等待一段时间,确保服务器启动
|
| 125 |
await new Promise(resolve => setTimeout(resolve, 1000));
|
| 126 |
|
| 127 |
if (this.proxyProcess && this.proxyProcess.exitCode === null) {
|
| 128 |
+
logger.success(`代理服务器已启动,端口: ${this.port}`);
|
| 129 |
return true;
|
| 130 |
} else {
|
| 131 |
logger.error('代理服务器启动失败');
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
return false;
|
| 133 |
}
|
| 134 |
} catch (error) {
|
| 135 |
logger.error(`启动代理服务器时出错: ${error.message}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
return false;
|
| 137 |
}
|
| 138 |
}
|
|
|
|
| 158 |
logger.error(`停止代理服务器时出错: ${error.message}`);
|
| 159 |
} finally {
|
| 160 |
this.proxyProcess = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
}
|
| 162 |
}
|
| 163 |
}
|