()
| 1165 | |
| 1166 | // Function to ensure core files exist by copying from backup location if needed |
| 1167 | function ensureCoreFiles() { |
| 1168 | const corePath = getResourcePath('core'); |
| 1169 | const xrayPath = path.join(corePath, 'xray'); |
| 1170 | |
| 1171 | if (!fs.existsSync(corePath)) { |
| 1172 | fs.mkdirSync(corePath, { recursive: true }); |
| 1173 | console.log('Created core directory:', corePath); |
| 1174 | } |
| 1175 | |
| 1176 | if (isWindows) { |
| 1177 | if (!fs.existsSync(xrayPath)) { |
| 1178 | fs.mkdirSync(xrayPath, { recursive: true }); |
| 1179 | console.log('Created xray directory:', xrayPath); |
| 1180 | } |
| 1181 | |
| 1182 | // Check for common backup locations where core files might be found |
| 1183 | const possibleBackupLocations = [ |
| 1184 | path.join(__dirname, 'core-backup'), |
| 1185 | path.join(app.getPath('userData'), 'core-backup'), |
| 1186 | path.join(app.getPath('documents'), 'DengVPN', 'core') |
| 1187 | ]; |
| 1188 | |
| 1189 | const requiredFiles = ['xray.exe']; |
| 1190 | const missingFiles = requiredFiles.filter(file => !fs.existsSync(path.join(xrayPath, file))); |
| 1191 | |
| 1192 | if (missingFiles.length > 0) { |
| 1193 | console.log('Missing core files:', missingFiles); |
| 1194 | |
| 1195 | // Try to find and copy missing files from backup locations |
| 1196 | for (const backupLoc of possibleBackupLocations) { |
| 1197 | if (fs.existsSync(backupLoc)) { |
| 1198 | console.log('Found backup location:', backupLoc); |
| 1199 | |
| 1200 | // Also check for xray subdirectory in backup location |
| 1201 | const backupXrayPath = path.join(backupLoc, 'xray'); |
| 1202 | const actualBackupPath = fs.existsSync(backupXrayPath) ? backupXrayPath : backupLoc; |
| 1203 | |
| 1204 | for (const file of missingFiles) { |
| 1205 | const backupFile = path.join(actualBackupPath, file); |
| 1206 | const targetFile = path.join(xrayPath, file); |
| 1207 | |
| 1208 | if (fs.existsSync(backupFile)) { |
| 1209 | try { |
| 1210 | fs.copyFileSync(backupFile, targetFile); |
| 1211 | console.log(`Copied ${file} from backup`); |
| 1212 | } catch (err) { |
| 1213 | console.error(`Failed to copy ${file}:`, err); |
| 1214 | } |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | // Check if we've resolved all missing files |
| 1219 | const stillMissing = requiredFiles.filter(file => !fs.existsSync(path.join(xrayPath, file))); |
| 1220 | if (stillMissing.length === 0) { |
| 1221 | console.log('All core files restored from backup'); |
| 1222 | return true; |
| 1223 | } |
| 1224 | } |
no test coverage detected