4

I want to develop an automation application that works with VPN. For this I have Openvpn config files. However, I don't know how to connect. A solution is mentioned in this link but it didn't work for me. Where and how do I type my vpn user and password? I could not get any results in my research on this.

The application I want to do will work briefly as follows. For example, I will have 50 vpn and my program will connect to the target site by connecting with each vpn respectively. However, as I said, I do not know how to set up an openvpn connection with java. Can you help me with this? Below are the codes I wrote for what I want to do. For example, I wanted to connect to google through italy vpn location.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {

        Runtime runtime = Runtime.getRuntime();
        try {
            Process process = runtime.exec("C:\\Program Files\\OpenVPN\\bin\\openvpn C:\\Users\\DATABASE\\OpenVPN\\config\\italy\\italy.ovpn");
        } catch (IOException e) {
            e.printStackTrace();
        }


        System.setProperty("webdriver.gecko.driver", "C:\\geckodriver\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();

        try {
            driver.get("https://www.google.com/");
        } finally {
            driver.quit();
        }
    }
}
idrisyagci
  • 218
  • 4
  • 19

1 Answers1

1

Running the OpenVPN client from the command line you need to input the username & password using a separate text file.

  1. Create a text file in the same folder as the .ovpn file. For this example italy.txt.
  2. Put your username & password in the file on new lines, like this:
username
password
  1. Save the text file.

Seeing as Runtime.exec isn't working anymore (or it's finally working as expected, but not giving a result in this case), we need to switch to a ProcessBuilder.

Here is a example using a single VPN connection as defined in the question.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {
    private static final String NEW_LINE = System.getProperty("line.separator");
    
    public static void main(String[] args) {        
        StringBuilder result = new StringBuilder(80);
        try {
            ProcessBuilder pb = new ProcessBuilder("C:\\Program Files\\OpenVPN\\bin\\openvpn.exe", "--config", "C:\\Users\\DATABASE\\OpenVPN\\config\\italy\\italy.ovpn", "--auth-user-pass", "C:\\Users\\DATABASE\\OpenVPN\\config\\italy\\italy.txt").redirectErrorStream(true);
            Process process = pb.start();
            try (BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())))
            {
                while (true)
                {
                    String line = in.readLine();
                    if (line == null)
                        break;
                    result.append(line).append(NEW_LINE);
                }
            }
        } catch (IOException e) {
        }
        
        System.out.println(result.toString());
    }
}

This will open a VPN tunnel and it will stay open as long as the terminal/program starting the Test class isn't killed.

Be careful that it will not give any output unless the command fails! In case of normal operation you just see a blank screen.

You will need to implement a business logic for yourself and subsequently close the VPN tunnel when you are done with it before opening a new tunnel (unless you want to end up with 50 tunnels inside each other, which might not even work).

Fullslack
  • 290
  • 1
  • 2
  • 11
  • it is running from command line but not working on my code above – idrisyagci Sep 24 '20 at 10:24
  • @idrisyagci could you try if the second way is working for you? Maybe the " are incorrect, not sure how runtime.exec this line will parse and execute. – Fullslack Sep 24 '20 at 11:26
  • your codes run on command line but not working with java. When i run this code the commandline doesnt open. And also openvpn have a graphical interface and it doesnt open too when i code using openvpn-gui.exe I checked the parameters from console for openvpn-gui and coded according to it, but nothing to change. – idrisyagci Sep 25 '20 at 13:16
  • @idrisyagci I have updated the answer. This is tested on Windows 10 with OpenVPN cli and it works for me. Watch out for the tunnel in tunnel trap as this will only close and return a output when failed or killed. – Fullslack Sep 30 '20 at 13:13
  • The code you wrote works theoretically, but if it is practical, I think there is a problem with openvpn. Openvpn does not work from the console but works when using gui. So when I edited the code like this it worked. "C:\\Program Files\\OpenVPN\\bin\\openvpn-gui.exe", "--connect", "italy.ovpn" However, I still accept your answer as correct. – idrisyagci Oct 06 '20 at 06:27
  • @idrisyagci could you mark it as well than? The problem with the OpenVPN cli client is that it does not give any feedback. It opens a Deamon and Service thread and that is it. Nice way to hide it, but I guess not for your use-case. – Fullslack Oct 06 '20 at 07:24