(configUrl, isSwitching = false)
| 745 | |
| 746 | // Optimized startV2ray function with better process management |
| 747 | function startV2ray(configUrl, isSwitching = false) { |
| 748 | console.log(`startV2ray called with configUrl: ${configUrl}, isSwitching: ${isSwitching}`); |
| 749 | |
| 750 | if (v2rayProcess && !isSwitching) { |
| 751 | stopV2ray(); |
| 752 | } |
| 753 | |
| 754 | try { |
| 755 | // Check if xray executable exists |
| 756 | if (!fs.existsSync(xrayExePath)) { |
| 757 | showMissingCoreFilesError(); |
| 758 | throw new Error(`Xray executable not found at: ${xrayExePath}`); |
| 759 | } |
| 760 | |
| 761 | // Check if configUrl is a local file path (JSON file from our configs directory) |
| 762 | if (configUrl.endsWith('.json') && fs.existsSync(configUrl)) { |
| 763 | // Use the specific config file directly with the exact path format |
| 764 | // ./xray.exe --config ../configs/{id}.json |
| 765 | const relativeConfigPath = path.relative(path.dirname(xrayExePath), configUrl).replace(/\\/g, '/'); |
| 766 | |
| 767 | console.log(`Starting Xray with config: ${relativeConfigPath}`); |
| 768 | |
| 769 | // Platform-specific execution |
| 770 | if (isWindows) { |
| 771 | // Start Xray process with optimized settings |
| 772 | v2rayProcess = spawn(xrayExePath, ['--config', relativeConfigPath], { |
| 773 | cwd: path.dirname(xrayExePath), |
| 774 | stdio: ['ignore', 'pipe', 'pipe'] // Optimize stdio |
| 775 | }); |
| 776 | } else if (isLinux) { |
| 777 | // Make sure the executable has proper permissions |
| 778 | execSync(`chmod +x "${xrayExePath}"`, { cwd: path.dirname(xrayExePath) }); |
| 779 | |
| 780 | // Start Xray process with optimized settings |
| 781 | v2rayProcess = spawn(xrayExePath, ['--config', relativeConfigPath], { |
| 782 | cwd: path.dirname(xrayExePath), |
| 783 | stdio: ['ignore', 'pipe', 'pipe'] // Optimize stdio |
| 784 | }); |
| 785 | } |
| 786 | |
| 787 | v2rayProcess.stdout.on('data', (data) => { |
| 788 | // Only log important messages to reduce overhead |
| 789 | const dataStr = data.toString(); |
| 790 | if (dataStr.includes('started') || dataStr.includes('error') || dataStr.includes('failed')) { |
| 791 | console.log(`Xray stdout: ${dataStr}`); |
| 792 | } |
| 793 | }); |
| 794 | |
| 795 | v2rayProcess.stderr.on('data', (data) => { |
| 796 | // Only log error messages |
| 797 | console.error(`Xray stderr: ${data}`); |
| 798 | }); |
| 799 | |
| 800 | v2rayProcess.on('close', (code) => { |
| 801 | console.log(`Xray process exited with code ${code}`); |
| 802 | // Only set isConnected to false and send disconnect status if we're not switching servers |
| 803 | if (!global.isSwitching) { |
| 804 | isConnected = false; |
no test coverage detected