OpenVPN连接在Java中的编程方式

huangapple go评论81阅读模式
英文:

Openvpn connection programmatically in java

问题

我想开发一个与VPN配合的自动化应用程序。为此,我有OpenVPN配置文件。然而,我不知道如何连接。这个链接中提到了一个解决方案,但对我没起作用。我应该在哪里以及如何输入我的VPN用户名和密码?在我的研究中,我没有得到任何结果。

我想要创建的应用程序大致将按以下方式工作。例如,我将拥有50个VPN,我的程序将通过依次连接每个VPN来连接到目标站点。然而,正如我所说,我不知道如何使用Java建立OpenVPN连接。你能帮助我吗?以下是我为所需操作编写的代码。例如,我希望通过意大利VPN位置连接到Google。

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();
        }
    }
}
英文:

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();
        }
    }
}

答案1

得分: 1

从命令行运行OpenVPN客户端时,您需要使用一个单独的文本文件输入用户名和密码。

  1. 在与.ovpn文件相同的文件夹中创建一个文本文件。以这个示例为例,命名为italy.txt
  2. 在文件中分别放置用户名和密码,就像这样:
username
password
  1. 保存文本文件。

由于Runtime.exec不再起作用(或者它终于按预期工作了,但在这种情况下没有提供结果),我们需要切换到ProcessBuilder

以下是一个示例,使用与问题中定义的单个VPN连接。

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());
	}
}

这将打开一个VPN隧道,并且只要启动Test类的终端/程序没有被关闭,隧道就会保持打开状态。

请注意,除非命令执行失败,否则不会输出任何内容!在正常操作的情况下,您只会看到一个空屏幕。

在您自己实现业务逻辑后,需要在使用完VPN隧道后关闭它,然后再打开新的隧道(除非您希望得到互相嵌套的50个隧道,这可能甚至不起作用)。

英文:

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).

huangapple
  • 本文由 发表于 2020年9月21日 22:48:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63994715.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定