From d87fefd1caa8dd8ffc4db607ce846a7bfa3c071e Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Fri, 24 Feb 2023 23:55:03 +0100 Subject: tunnel: download pac in java --- .../com/wireguard/android/backend/GoBackend.java | 35 ++++++++++++++++++++-- tunnel/src/main/proto/libwg.proto | 5 +++- tunnel/tools/libwg-go/http-proxy.go | 14 +++++++++ tunnel/tools/libwg-go/service.go | 23 +++++++++----- 4 files changed, 65 insertions(+), 12 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 596bea7d..6afdfd95 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java @@ -59,6 +59,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; @@ -287,12 +290,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() { diff --git a/tunnel/src/main/proto/libwg.proto b/tunnel/src/main/proto/libwg.proto index da6f6da4..c87eb9ef 100644 --- a/tunnel/src/main/proto/libwg.proto +++ b/tunnel/src/main/proto/libwg.proto @@ -50,7 +50,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 c73e1edf..9c5a1036 100644 --- a/tunnel/tools/libwg-go/http-proxy.go +++ b/tunnel/tools/libwg-go/http-proxy.go @@ -445,6 +445,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 37fb4b40..72916a8e 100644 --- a/tunnel/tools/libwg-go/service.go +++ b/tunnel/tools/libwg-go/service.go @@ -120,14 +120,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{ -- cgit v1.2.3