0

I have been trying to make a very basic OpenVPN client wrapper. I have made it connect to a server using the ovpn file and some start parameters, but I want to do this without a written file because my program has the connection file's text. Is there any way for me to connect without having to write a temporary ovpn file?

This is what I have currently:

string ServerFileContents = "...";
string UserPassContents = "...";
File.WriteAllText("server.ovpn", ServerFileContents);
File.WriteAllText("userpass.txt", UserPassContents);
string args = "--config server.ovpn --auth-user-pass userpass.txt";
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new 
System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = @"C:\Program Files\OpenVPN\bin\openvpn.exe";
startInfo.Arguments = args;
startInfo.Verb = "runas";
process.StartInfo = startInfo;
process.Start();

Ideally, I would do something like (I know this wouldn't actually work, I'm just trying to show my point):

string args = "--configcontents " + ServerFileContents + " --auth-user-pass-contents " + UserPassContents;
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • The `[runas]` tag doesn't seem related to your question so I've removed it. If using `runas` to launch the OpenVPN executable is somehow your roadblock for not using a config file, please can you go into detail about that in your question as it doesn't come across at all from what you've currently written. – ProgrammingLlama Aug 27 '21 at 16:00

1 Answers1

0

You can always add all options that are normally present in the configuration file as --option to OpenVPN itself. But will need to have certificates and keys in files as embedding them into a configuration file with something like <ca>...</ca> is not supported on the commandline.

Another option is to use --config stdin and then write the config file to standard input of OpenVPN. That probably comes closest to what you want to achieve.

plaisthos
  • 6,255
  • 6
  • 35
  • 63