summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMikael Magnusson <mikma@users.sourceforge.net>2023-02-24 23:55:03 +0100
committerMikael Magnusson <mikma@users.sourceforge.net>2023-05-09 01:38:14 +0200
commita9d9b0698fd8fe48f2571327877c03e12be95318 (patch)
tree32962faf545bef877163e712fbb096817ceb810f
parentaebb7fc1dbd9edac8f5b8520b842eccdee016c85 (diff)
tunnel: download pac in java
-rw-r--r--tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java38
-rw-r--r--tunnel/src/main/proto/libwg.proto5
-rw-r--r--tunnel/tools/libwg-go/http-proxy.go14
-rw-r--r--tunnel/tools/libwg-go/service.go23
4 files changed, 67 insertions, 13 deletions
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 c4c8f70a..f66b6cc5 100644
--- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java
+++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java
@@ -65,6 +65,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;
@@ -296,12 +299,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() {
@@ -538,7 +567,8 @@ public final class GoBackend implements Backend {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (pacFileUrl != null && pacFileUrl != Uri.EMPTY) {
- int listenPort = startHttpProxy(pacFileUrl);
+ String pacFile = downloadPacFile(activeNetwork, pacFileUrl);
+ int listenPort = startHttpProxy(pacFile);
ProxyInfo localPi = ProxyInfo.buildDirectProxy("localhost", listenPort);
builder.setHttpProxy(localPi);
} else {
diff --git a/tunnel/src/main/proto/libwg.proto b/tunnel/src/main/proto/libwg.proto
index 90c4f46a..4fa4468b 100644
--- a/tunnel/src/main/proto/libwg.proto
+++ b/tunnel/src/main/proto/libwg.proto
@@ -56,7 +56,10 @@ message VersionResponse {
}
message StartHttpProxyRequest {
- string pacFileUrl = 1;
+ oneof pacFile {
+ string pacFileUrl = 1;
+ string pacFileContent = 2;
+ }
}
message StartHttpProxyResponse {
diff --git a/tunnel/tools/libwg-go/http-proxy.go b/tunnel/tools/libwg-go/http-proxy.go
index a44aaac6..9c457286 100644
--- a/tunnel/tools/libwg-go/http-proxy.go
+++ b/tunnel/tools/libwg-go/http-proxy.go
@@ -454,6 +454,20 @@ func (p *HttpProxy) SetPacFileUrl(pacFileUrl *url.URL) error {
return nil
}
+func (p *HttpProxy) SetPacFileContent(pacFile string) error {
+ ctx := newPacBodyCtx(pacFile, p.logger)
+
+ for _, handler := range(p.handlers) {
+ handler.setPacCtx(ctx)
+ }
+
+ if p.ctx != nil {
+ p.ctx.Destroy()
+ }
+ p.ctx = ctx
+ return nil
+}
+
func (p *HttpProxy) Start() (listen_port uint16, err error) {
p.logger.Verbosef("HttpProxy.Start()")
listen_port = 0
diff --git a/tunnel/tools/libwg-go/service.go b/tunnel/tools/libwg-go/service.go
index e49081e7..f5460486 100644
--- a/tunnel/tools/libwg-go/service.go
+++ b/tunnel/tools/libwg-go/service.go
@@ -121,14 +121,21 @@ func (e *LibwgServiceImpl) StartHttpProxy(ctx context.Context, req *gen.StartHtt
listenPort = e.httpProxy.GetAddrPort().Port()
}
- pacFileUrl, err := url.Parse(req.PacFileUrl)
- if err != nil {
- return buildStartHttpProxyError(fmt.Sprintf("Bad pacFileUrl: %v (%s)", err, req.PacFileUrl))
- }
-
- err = e.httpProxy.SetPacFileUrl(pacFileUrl)
- if err != nil {
- return buildStartHttpProxyError(fmt.Sprintf("Bad pacFileUrl: %v (%s)", req.PacFileUrl))
+ content := req.GetPacFileContent()
+ if content != "" {
+ err := e.httpProxy.SetPacFileContent(content)
+ if err != nil {
+ return buildStartHttpProxyError(fmt.Sprintf("Bad pacFileContent: %v (%s)", content))
+ }
+ } else {
+ pacFileUrl, err := url.Parse(req.GetPacFileUrl())
+ if err != nil {
+ return buildStartHttpProxyError(fmt.Sprintf("Bad pacFileUrl: %v (%s)", err, req.GetPacFileUrl()))
+ }
+ err = e.httpProxy.SetPacFileUrl(pacFileUrl)
+ if err != nil {
+ return buildStartHttpProxyError(fmt.Sprintf("Bad pacFileUrl: %v (%s)", err, pacFileUrl))
+ }
}
r := &gen.StartHttpProxyResponse{