diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-02-24 23:55:03 +0100 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-02-24 23:55:03 +0100 |
commit | 35c5cb00d6af97dfd917ba7f051a8a6ba85180b8 (patch) | |
tree | dab1ae75b24b26df80f88f035a01803f6e9bb2c0 | |
parent | 1dbe14aefa8afd7513f6ae3b12e13044d48c684a (diff) |
WIP: experimental pac content
-rw-r--r-- | tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java | 52 | ||||
-rw-r--r-- | tunnel/src/main/proto/libwg.proto | 5 | ||||
-rw-r--r-- | tunnel/tools/libwg-go/http-proxy.go | 14 | ||||
-rw-r--r-- | tunnel/tools/libwg-go/service.go | 24 |
4 files changed, 83 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 f2eb86da..32e3572a 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<HttpProxy> 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); diff --git a/tunnel/src/main/proto/libwg.proto b/tunnel/src/main/proto/libwg.proto index ab6b2659..349166ca 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 66bdd5ef..2c688e26 100644 --- a/tunnel/tools/libwg-go/service.go +++ b/tunnel/tools/libwg-go/service.go @@ -5,7 +5,7 @@ import ( "fmt" "io" "net" - "net/url" +// "net/url" "os" "google.golang.org/grpc" @@ -106,33 +106,43 @@ func buildStartHttpProxyError(message string) (*gen.StartHttpProxyResponse, erro } func (e *LibwgServiceImpl) StartHttpProxy(ctx context.Context, req *gen.StartHttpProxyRequest) (*gen.StartHttpProxyResponse, error) { + e.logger.Verbosef("StartHttpProxy 1") var listenPort uint16 if e.httpProxy == nil { + e.logger.Verbosef("StartHttpProxy 2") e.httpProxy = NewHttpProxy(e.uidRequest, e.logger) + e.logger.Verbosef("StartHttpProxy 3") var err error listenPort, err = e.httpProxy.Start() + e.logger.Verbosef("StartHttpProxy 4") if err != nil { e.httpProxy = nil + e.logger.Verbosef("StartHttpProxy 5") return buildStartHttpProxyError(fmt.Sprintf("Http proxy start failed: %v", err)) } } else { 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)) - } + e.logger.Verbosef("StartHttpProxy 6") + // pacFileUrl, err := url.Parse(req.PacFileUrl) + // if err != nil { + // e.logger.Verbosef("StartHttpProxy 7") + // return buildStartHttpProxyError(fmt.Sprintf("Bad pacFileUrl: %v (%s)", err, req.PacFileUrl)) + // } - err = e.httpProxy.SetPacFileUrl(pacFileUrl) + e.logger.Verbosef("StartHttpProxy 8") + err := e.httpProxy.SetPacFileContent(req.GetPacFileContent()) if err != nil { - return buildStartHttpProxyError(fmt.Sprintf("Bad pacFileUrl: %v (%s)", req.PacFileUrl)) + e.logger.Verbosef("StartHttpProxy 9") + return buildStartHttpProxyError(fmt.Sprintf("Bad pacFile: %v (%s)", req.GetPacFileContent())) } r := &gen.StartHttpProxyResponse{ ListenPort: uint32(listenPort), } + e.logger.Verbosef("StartHttpProxy 10") return r, nil } |