|
| 1 | +# Function to load env variables from a file |
| 2 | +function Load-EnvFile { |
| 3 | + param ( |
| 4 | + [string]$path |
| 5 | + ) |
| 6 | + |
| 7 | + if (-not (Test-Path $path)) { |
| 8 | + throw "Env file '$path' does not exist." |
| 9 | + } |
| 10 | + |
| 11 | + $lines = Get-Content -Path $path |
| 12 | + foreach ($line in $lines) { |
| 13 | + if ($line -match '^\s*#') { |
| 14 | + continue |
| 15 | + } |
| 16 | + if ($line -match '^\s*(.+?)\s*=\s*(.+?)\s*$') { |
| 17 | + [System.Environment]::SetEnvironmentVariable($matches[1], $matches[2]) |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +# Load the env file |
| 23 | +$envFilePath = ".\config.env" |
| 24 | +Load-EnvFile -path $envFilePath |
| 25 | + |
| 26 | +# Read env variables |
| 27 | +$user = [System.Environment]::GetEnvironmentVariable("USER") |
| 28 | +$ip = [System.Environment]::GetEnvironmentVariable("IP") |
| 29 | +$port = [System.Environment]::GetEnvironmentVariable("PORT") |
| 30 | +$RDP1 = [System.Environment]::GetEnvironmentVariable("RDP1") |
| 31 | +$FTP1 = [System.Environment]::GetEnvironmentVariable("FTP1") |
| 32 | +$h1 = [System.Environment]::GetEnvironmentVariable("H1") |
| 33 | +$h1p = [System.Environment]::GetEnvironmentVariable("H1P") |
| 34 | +$h2p = [System.Environment]::GetEnvironmentVariable("H2P") |
| 35 | +$CompanyName = [System.Environment]::GetEnvironmentVariable("COMPANY_NAME") |
| 36 | +$ServerName = [System.Environment]::GetEnvironmentVariable("SERVER_NAME") |
| 37 | +$keyPath = [System.Environment]::GetEnvironmentVariable("KEY_PATH") |
| 38 | + |
| 39 | +# Set correct permissions on the private key file |
| 40 | +try { |
| 41 | + icacls $keyPath /inheritance:r /grant:r "$($env:USERNAME):(R)" /remove "Everyone" /T |
| 42 | + Write-Host "Permissions set on the key file successfully." -ForegroundColor Green |
| 43 | +} catch { |
| 44 | + Write-Host "Failed to set permissions on the key file. Please check the file path and permissions manually." -ForegroundColor Red |
| 45 | + Exit |
| 46 | +} |
| 47 | + |
| 48 | +$knownHostsPath = "$env:userprofile\.ssh\known_hosts" |
| 49 | +$hostFingerprint = (ssh-keygen -l -F $ip | Out-String).Trim() |
| 50 | +if (-not $hostFingerprint) { |
| 51 | + ssh-keyscan $ip | Out-File -Append -Encoding utf8 $knownHostsPath |
| 52 | +} |
| 53 | + |
| 54 | +Write-Host "" |
| 55 | +Write-Host "" |
| 56 | +Write-Host "" |
| 57 | +Write-Host "" |
| 58 | +Write-Host "" # These are needed to prevent the below loop from covering the text |
| 59 | +Write-Host "" |
| 60 | +Write-Host "" |
| 61 | +Write-Host "" |
| 62 | +Write-Host "" |
| 63 | +Write-Host "" |
| 64 | +Write-Host "****************************************" -ForegroundColor DarkYellow |
| 65 | +Write-Host "* Connecting to $CompanyName Servers *" -ForegroundColor DarkYellow |
| 66 | +Write-Host "****************************************" -ForegroundColor DarkYellow |
| 67 | +Write-Host "" |
| 68 | +Write-Host "Establishing link to $ServerName." -ForegroundColor DarkYellow |
| 69 | +Write-Host "" |
| 70 | + |
| 71 | +# Prompt user for which tunnels to open |
| 72 | +$rdpResponse = Read-Host "Would you like to open the RDP tunnel? (yes/y/no)" |
| 73 | +$ftpResponse = Read-Host "Would you like to open the FTP tunnel? (yes/y/no)" |
| 74 | + |
| 75 | +# Build SSH command based on user responses |
| 76 | +$sshCommand = "ssh -p $port $user@$ip -N" |
| 77 | +if ($rdpResponse -eq "yes" -or $rdpResponse -eq "y") { |
| 78 | + $sshCommand += " -L ${RDP1}:${h1}:${h1p}" |
| 79 | +} |
| 80 | +if ($ftpResponse -eq "yes" -or $ftpResponse -eq "y") { |
| 81 | + $sshCommand += " -L ${FTP1}:${h1}:${h2p}" |
| 82 | +} |
| 83 | +$sshCommand += " -i $keyPath" |
| 84 | + |
| 85 | +# Try to start the SSH process |
| 86 | +try { |
| 87 | + $sshProcess = Start-Process powershell -ArgumentList "-NoExit", "-Command `"$sshCommand`"" -NoNewWindow -ErrorAction Stop |
| 88 | +} catch { |
| 89 | + Write-Host "Failed to establish SSH connection. Please check your credentials and key file." -ForegroundColor Red |
| 90 | + Exit |
| 91 | +} |
| 92 | + |
| 93 | +# Set timeout duration in seconds |
| 94 | +$timeout = 60 |
| 95 | + |
| 96 | +# Check for SSH tunnel connection |
| 97 | +$connectedRDP = $false |
| 98 | +$connectedFTP = $false |
| 99 | +$startTime = Get-Date |
| 100 | + |
| 101 | +while ((-not $connectedRDP -and ($rdpResponse -eq "yes" -or $rdpResponse -eq "y")) -or (-not $connectedFTP -and ($ftpResponse -eq "yes" -or $ftpResponse -eq "y")) -and (New-TimeSpan -Start $startTime -End (Get-Date)).TotalSeconds -lt $timeout) { |
| 102 | + if ($rdpResponse -eq "yes" -or $rdpResponse -eq "y") { |
| 103 | + $connectedRDP = Test-NetConnection -ComputerName $h1 -Port $RDP1 -WarningAction SilentlyContinue | Select-Object -ExpandProperty TcpTestSucceeded |
| 104 | + } |
| 105 | + if ($ftpResponse -eq "yes" -or $ftpResponse -eq "y") { |
| 106 | + $connectedFTP = Test-NetConnection -ComputerName $h1 -Port $FTP1 -WarningAction SilentlyContinue | Select-Object -ExpandProperty TcpTestSucceeded |
| 107 | + } |
| 108 | + Start-Sleep -Seconds 1 |
| 109 | +} |
| 110 | + |
| 111 | +if ((-not $connectedRDP -and ($rdpResponse -eq "yes" -or $rdpResponse -eq "y")) -or (-not $connectedFTP -and ($ftpResponse -eq "yes" -or $ftpResponse -eq "y"))) { |
| 112 | + Write-Host "" |
| 113 | + Write-Host "Connection could not be established within $timeout seconds." -ForegroundColor Red |
| 114 | + Write-Host "Your link to the $CompanyName servers has not been established." -ForegroundColor Red |
| 115 | + Write-Host "Please make sure you have your credentials ready, close this window and try again" -ForegroundColor Red |
| 116 | + Write-Host "" |
| 117 | + Exit # Exits if the tunnel isn't established |
| 118 | +} |
| 119 | + |
| 120 | +Write-Host "" |
| 121 | +Write-Host "Link to $ServerName Established" -ForegroundColor Green |
| 122 | +Write-Host "" |
| 123 | +Write-Host "---------------------------------------------------------" -ForegroundColor Blue |
| 124 | +Write-Host "If the remote connection doesn't automatically open and" -ForegroundColor Blue |
| 125 | +Write-Host "start connecting, Use the below Details to connect to" -ForegroundColor Blue |
| 126 | +Write-Host "the relevant services:" -ForegroundColor Blue |
| 127 | +Write-Host "---------------------------------------------------------" -ForegroundColor Blue |
| 128 | +if ($rdpResponse -eq "yes" -or $rdpResponse -eq "y") { |
| 129 | + Write-Host "Remote Desktop Connection:" -ForegroundColor Blue |
| 130 | + Write-Host "IP: $h1" -ForegroundColor Blue |
| 131 | + Write-Host "Port: $RDP1" -ForegroundColor Blue |
| 132 | +} |
| 133 | +if ($ftpResponse -eq "yes" -or $ftpResponse -eq "y") { |
| 134 | + Write-Host "FTP:" -ForegroundColor Blue |
| 135 | + Write-Host "IP: $h1" -ForegroundColor Blue |
| 136 | + Write-Host "Port: $FTP1" -ForegroundColor Blue |
| 137 | +} |
| 138 | +#Write-Host "User: $user" -ForegroundColor Blue |
| 139 | +Write-Host "---------------------------------------------------------" -ForegroundColor Blue |
| 140 | +Write-Host "" -ForegroundColor Blue |
| 141 | + |
| 142 | +# Ask the user if they want to open the remote desktop connection |
| 143 | +if ($rdpResponse -eq "yes" -or $rdpResponse -eq "y") { |
| 144 | + $openRDP = Read-Host "Would you like to open the remote desktop connection now? (yes/y/no)" |
| 145 | + if ($openRDP -eq "yes" -or $openRDP -eq "y") { |
| 146 | + Write-Host "Opening remote desktop connection window" -ForegroundColor DarkYellow |
| 147 | + # Open RDP window |
| 148 | + Start-Process mstsc 'connection.rdp' |
| 149 | + Write-Host "" |
| 150 | + Write-Host "Window Opened" -ForegroundColor Green |
| 151 | + } else { |
| 152 | + Write-Host "You chose not to open the remote desktop connection." -ForegroundColor Yellow |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +Write-Host "" |
| 157 | +Write-Host "Keep this PowerShell window open!" -ForegroundColor Blue |
| 158 | +Write-Host "When you close this window, it will close your link to the $CompanyName servers." -ForegroundColor Blue |
0 commit comments