| 943 | |
| 944 | // Add a function to switch servers without full disconnect/connect cycle |
| 945 | function switchServer(newConfigIndex) { |
| 946 | console.log(`Switching server to index: ${newConfigIndex}`); |
| 947 | |
| 948 | // Don't call stopV2ray here, just kill the process directly |
| 949 | if (v2rayProcess) { |
| 950 | v2rayProcess.kill(); |
| 951 | v2rayProcess = null; |
| 952 | } |
| 953 | |
| 954 | // Clear the last ping value when switching servers |
| 955 | global.lastPing = null; |
| 956 | |
| 957 | // Set the new selected config |
| 958 | if (configList[newConfigIndex]) { |
| 959 | selectedConfig = configList[newConfigIndex]; |
| 960 | console.log(`New selected config:`, selectedConfig); |
| 961 | |
| 962 | // Handle the new config format with local JSON file |
| 963 | if (selectedConfig.jsonFile) { |
| 964 | // New format with local JSON file - this is what we want |
| 965 | console.log(`Starting Xray with local JSON file: ${selectedConfig.jsonFile}`); |
| 966 | startV2ray(selectedConfig.jsonFile, true); // true indicates server switching |
| 967 | } else if (selectedConfig.url) { |
| 968 | // Old format with URL property |
| 969 | console.log(`Starting Xray with URL: ${selectedConfig.url}`); |
| 970 | if (selectedConfig.url.startsWith('vless://') || |
| 971 | selectedConfig.url.startsWith('vmess://') || |
| 972 | selectedConfig.url.startsWith('ss://') || |
| 973 | selectedConfig.url.startsWith('trojan://')) { |
| 974 | // This is a share link, we need to convert it to JSON first |
| 975 | console.log('Error: Share link detected, showing error dialog'); |
| 976 | dialog.showErrorBox('Connection Error', 'This configuration format is not supported. Please refresh the server list.'); |
| 977 | } else { |
| 978 | // This should be a JSON config URL |
| 979 | startV2ray(selectedConfig.url, true); // true indicates server switching |
| 980 | } |
| 981 | } |
| 982 | } else { |
| 983 | console.log(`Error: Config at index ${newConfigIndex} not found`); |
| 984 | // Send connection status false if config not found |
| 985 | if (win) { |
| 986 | win.webContents.send('connection-status', false); |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | // Function to get and send connection statistics |
| 992 | async function sendConnectionStats() { |