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();
}
}
}
Running the OpenVPN client from the command line you need to input the username & password using a separate text file.
italy.txt
.username
password
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).