Skip to content

Commit

Permalink
check tap
Browse files Browse the repository at this point in the history
  • Loading branch information
Immueggpain authored and Immueggpain committed Dec 31, 2018
1 parent 9e6fd36 commit f7bfa7f
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,31 @@
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;

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

Expand All @@ -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];
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}

}

0 comments on commit f7bfa7f

Please sign in to comment.