From d6153ccbff8c1fa8d3648daa16ac26e7c37e4d9d Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Fri, 24 Feb 2023 23:55:03 +0100 Subject: WIP: experimental pac content --- .../com/wireguard/android/backend/GoBackend.java | 52 ++++++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) (limited to 'tunnel/src/main/java/com/wireguard') diff --git a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java index c4c72459..1a5e54dd 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java @@ -66,6 +66,9 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; import java.net.InetAddress; import java.net.Inet4Address; import java.net.Inet6Address; @@ -310,12 +313,38 @@ public final class GoBackend implements Backend { return getState(tunnel); } - private int startHttpProxy(Uri pacFileUrl) { + private static String downloadPacFile(Network network, Uri pacFileUrl) { + HttpURLConnection urlConnection = null; + StringBuffer buf = new StringBuffer(); + try { + URL url = new URL(pacFileUrl.toString()); + urlConnection = (HttpURLConnection) network.openConnection(url); + + InputStream in = urlConnection.getInputStream(); + InputStreamReader isw = new InputStreamReader(in); + + int data = isw.read(); + while (data != -1) { + char current = (char) data; + data = isw.read(); + buf.append(current); + } + } catch (Exception e) { + } finally { + if (urlConnection != null) { + urlConnection.disconnect(); + } + } + + return buf.toString(); + } + + private int startHttpProxy(String pacFile) { LibwgGrpc.LibwgStub asyncStub = LibwgGrpc.newStub(channel); LibwgGrpc.LibwgBlockingStub stub = LibwgGrpc.newBlockingStub(channel); StartHttpProxyRequest.Builder reqBuilder = StartHttpProxyRequest.newBuilder(); - if (pacFileUrl != null && pacFileUrl != Uri.EMPTY) { - reqBuilder.setPacFileUrl(pacFileUrl.toString()); + if (pacFile != null && pacFile != "") { + reqBuilder.setPacFileContent(pacFile); } Thread streamer = new Thread(new Runnable() { @@ -530,20 +559,35 @@ public final class GoBackend implements Backend { Log.i(TAG, "Builder 14"); Optional proxy = config.getInterface().getHttpProxy(); + Log.i(TAG, "Builder 14.1"); if (proxy.isPresent()) { + Log.i(TAG, "Builder 14.2"); ProxyInfo pi = proxy.get().getProxyInfo(); + Log.i(TAG, "Builder 14.3"); Uri pacFileUrl = pi.getPacFileUrl(); + Log.i(TAG, "Builder 14.4"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + Log.i(TAG, "Builder 14.5"); if (pacFileUrl != null && pacFileUrl != Uri.EMPTY) { - int listenPort = startHttpProxy(pacFileUrl); + Log.i(TAG, "Builder 14.6"); + String pacFile = downloadPacFile(activeNetwork, pacFileUrl); + int listenPort = startHttpProxy(pacFile); + Log.i(TAG, "Builder 14.7"); ProxyInfo localPi = ProxyInfo.buildDirectProxy("localhost", listenPort); + Log.i(TAG, "Builder 14.8"); builder.setHttpProxy(localPi); + Log.i(TAG, "Builder 14.9"); } else { + Log.i(TAG, "Builder 14.10"); builder.setHttpProxy(pi); + Log.i(TAG, "Builder 14.11"); } + Log.i(TAG, "Builder 14.12"); } + Log.i(TAG, "Builder 14.13"); } + Log.i(TAG, "Builder 14.14"); Log.i(TAG, "Builder 15"); builder.setBlocking(true); -- cgit v1.2.3