diff --git a/index.js b/index.js index f886769..660b276 100644 --- a/index.js +++ b/index.js @@ -62,32 +62,26 @@ async function vpnConnect(vpnName) { const maxAttempts = process.env.RETRIES; for (let attempt = 1; attempt <= maxAttempts; attempt++) { - try { - await execSync(`nmcli connection up "${vpnName}"`); + await execSync(`nmcli connection up "${vpnName}"`); - const status = await execSync('nmcli connection show --active').toString().includes(vpnName); - if (!status) { - consoleLog(`[${host}] Failed to connect to VPN: ${vpnName}. Retrying (attempt ${attempt} of ${maxAttempts})...`, 'error'); - await sleep(1000); - } - else { - const response = await fetch('https://api.ipify.org', { - method: 'GET', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json', - }, - }); - const ipResponse = await response.json(); - const ip = ipResponse.data; - consoleLog(`[${host}] VPN connection successfully established to ${vpnName} (IP: ${ip.data}).`, 'info'); - return 0; - } - } - catch (error) { - consoleLog(`[${host}] Catch error: Failed to connect to VPN: ${vpnName}. Retrying (attempt ${attempt} of ${maxAttempts})...`, 'error'); + const status = await execSync('nmcli connection show --active').toString().includes(vpnName); + if (!status) { + consoleLog(`[${host}] Failed to connect to VPN: ${vpnName}. Retrying (attempt ${attempt} of ${maxAttempts})...`, 'error'); await sleep(1000); } + else { + const response = await fetch('https://api.ipify.org', { + method: 'GET', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + }, + }); + const ipResponse = await response.json(); + const ip = ipResponse.data; + consoleLog(`[${host}] VPN connection successfully established to ${vpnName} (IP: ${ip.data}).`, 'info'); + return 0; + } } consoleLog(`[${host}] Maximum number of connection attempts reached. Failed to connect to VPN: ${vpnName}`, 'error');