Untitled
using System; using System.Diagnostics; using System.IO; namespace VPNConnector { class Program { static void Main(string[] args) { // Path to OpenVPN executable string openVpnPath = @"C:\Program Files\OpenVPN\bin\openvpn.exe"; // Path to OpenVPN configuration file string configPath = @"C:\Users\Rokas\Desktop\client.ovpn"; // Temporary file to store credentials string credentialsPath = @"C:\Users\Rokas\Desktop\credentials.txt"; // Get user credentials Console.Write("Enter VPN Username: "); string username = Console.ReadLine(); Console.Write("Enter VPN Password: "); string password = ReadPassword(); // Write credentials to file File.WriteAllText(credentialsPath, $"{username}\n{password}"); // Start OpenVPN process Process openVpnProcess = new Process { StartInfo = new ProcessStartInfo { FileName = openVpnPath, Arguments = $"--config \"{configPath}\" --auth-user-pass \"{credentialsPath}\"", RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true, Verb = "runas" // Run as administrator } }; try { openVpnProcess.Start(); Console.WriteLine("Connecting to VPN..."); // Display OpenVPN output while (!openVpnProcess.StandardOutput.EndOfStream) { Console.WriteLine(openVpnProcess.StandardOutput.ReadLine()); } openVpnProcess.WaitForExit(); Console.WriteLine("VPN process exited with code: " + openVpnProcess.ExitCode); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } finally { // Clean up credentials file if (File.Exists(credentialsPath)) { File.Delete(credentialsPath); } } while(1 == 1) { } } // Secure password input private static string ReadPassword() { string password = string.Empty; ConsoleKeyInfo keyInfo; do { keyInfo = Console.ReadKey(true); if (keyInfo.Key != ConsoleKey.Backspace && keyInfo.Key != ConsoleKey.Enter) { password += keyInfo.KeyChar; Console.Write("*"); } else if (keyInfo.Key == ConsoleKey.Backspace && password.Length > 0) { password = password[0..^1]; Console.Write("\b \b"); } } while (keyInfo.Key != ConsoleKey.Enter); Console.WriteLine(); return password; } } }
Leave a Comment