From f7bfa7f15fb0f6d7c96fae8a93177b107e59bdf7 Mon Sep 17 00:00:00 2001 From: Immueggpain Date: Mon, 31 Dec 2018 18:43:01 +0800 Subject: [PATCH] check tap --- .../bettermultiplayer/BMPClient.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/main/java/com/github/immueggpain/bettermultiplayer/BMPClient.java b/src/main/java/com/github/immueggpain/bettermultiplayer/BMPClient.java index 350e206..e426955 100644 --- a/src/main/java/com/github/immueggpain/bettermultiplayer/BMPClient.java +++ b/src/main/java/com/github/immueggpain/bettermultiplayer/BMPClient.java @@ -24,17 +24,22 @@ package com.github.immueggpain.bettermultiplayer; import java.io.IOException; +import java.io.InputStream; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; import java.security.Key; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.crypto.Cipher; import javax.crypto.spec.GCMParameterSpec; import javax.crypto.spec.SecretKeySpec; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.ArrayUtils; import com.github.immueggpain.bettermultiplayer.Launcher.ClientSettings; @@ -42,6 +47,8 @@ public class BMPClient { public void run(ClientSettings settings) { + // check if tap interface is up + // send a check udp packet to server // server respond, so make sure server is running & aes is correct @@ -50,6 +57,19 @@ public void run(ClientSettings settings) { // 1 thread recv cserver, send with sovpn to ovpn // start ovpn process try { + // check tap device + if (!hasTapAdapter()) { + System.out.println("Please intall tap adapter"); + Process process = new ProcessBuilder("ovpn\\tap-windows.exe").inheritIO().start(); + int exitCode = process.waitFor(); + if (exitCode != 0) { + System.err.println("install failed! exit code: " + exitCode); + return; + } + // wait a sec + Thread.sleep(1000); + } + // convert password to aes key byte[] bytes = settings.password.getBytes(StandardCharsets.UTF_8); byte[] byteKey = new byte[16]; @@ -76,6 +96,7 @@ public void run(ClientSettings settings) { // start ovpn startOvpnProcess(local_listen_port, settings.tap_ip, settings.tap_mask); + System.out.println("press ctrl+c again to exit!"); transfer_c2s_thread.join(); transfer_s2c_thread.join(); @@ -147,4 +168,14 @@ private static void startOvpnProcess(int local_listen_port, String tap_ip, Strin process.waitFor(); } + private static boolean hasTapAdapter() throws IOException, InterruptedException { + Process process = new ProcessBuilder("ovpn\\openvpn.exe", "--show-adapters").redirectErrorStream(true).start(); + InputStream is = process.getInputStream(); + String output = IOUtils.toString(is, Charset.defaultCharset()); + process.waitFor(); + Pattern checkRegex = Pattern.compile("'.+' \\{.+\\}"); + Matcher m = checkRegex.matcher(output); + return m.find(); + } + }