From 91eafea51205e1f72d01eb4fcfd33a822255b1e0 Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Sun, 26 Mar 2023 23:49:06 +0200 Subject: tunnel: use UdsChannelBuilder impl from grpc-android Replace local unix domain socket implementation with UdsChannelBuilder from grpc-android version 1.55.0-SNAPSHOT in maven local repository. --- build.gradle | 7 +- settings.gradle | 1 + tunnel/build.gradle | 3 +- .../com/wireguard/android/backend/GoBackend.java | 7 +- .../android/backend/LocalSocketAdapter.java | 325 --------------------- .../android/backend/UnixDomainSocketFactory.java | 59 ---- 6 files changed, 9 insertions(+), 393 deletions(-) delete mode 100644 tunnel/src/main/java/com/wireguard/android/backend/LocalSocketAdapter.java delete mode 100644 tunnel/src/main/java/com/wireguard/android/backend/UnixDomainSocketFactory.java diff --git a/build.gradle b/build.gradle index aba54c7c..713ff586 100644 --- a/build.gradle +++ b/build.gradle @@ -12,14 +12,15 @@ buildscript { datastoreVersion = '1.0.0' desugarVersion = '1.1.5' fragmentVersion = '1.5.5' - grpcVersion = '1.46.0' + grpcVersion = '1.55.0-SNAPSHOT' jsr305Version = '3.0.2' junitVersion = '4.13.2' lifecycleRuntimeKtxVersion = '2.6.0' materialComponentsVersion = '1.8.0' preferenceVersion = '1.2.0' - protobufGradleVersion = '0.8.18' - protocVersion = '3.20.1' + protobufGradleVersion = '0.9.0' + protocVersion = '3.22.2' + protocGenGrpcVersion = '1.54.0' zxingEmbeddedVersion = '4.3.0' groupName = 'com.wireguard.android' diff --git a/settings.gradle b/settings.gradle index e6dd51a5..2514754a 100644 --- a/settings.gradle +++ b/settings.gradle @@ -19,6 +19,7 @@ dependencyResolutionManagement { repositories { google() mavenCentral() + mavenLocal() } } diff --git a/tunnel/build.gradle b/tunnel/build.gradle index 8ca93050..ef95c4bd 100644 --- a/tunnel/build.gradle +++ b/tunnel/build.gradle @@ -59,6 +59,7 @@ android { dependencies { implementation "androidx.annotation:annotation:$annotationsVersion" implementation "androidx.collection:collection:$collectionVersion" + implementation "io.grpc:grpc-android:$grpcVersion" implementation "io.grpc:grpc-okhttp:$grpcVersion" implementation "io.grpc:grpc-protobuf-lite:$grpcVersion" implementation "io.grpc:grpc-stub:$grpcVersion" @@ -74,7 +75,7 @@ protobuf { } plugins { grpc { - artifact = "io.grpc:protoc-gen-grpc-java:$grpcVersion" + artifact = "io.grpc:protoc-gen-grpc-java:$protocGenGrpcVersion" } } generateProtoTasks { 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 f10ddc52..1338a1b6 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java @@ -34,6 +34,7 @@ import com.wireguard.util.NonNullForAll; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; +import io.grpc.android.UdsChannelBuilder; import io.grpc.okhttp.OkHttpChannelBuilder; import java.io.File; @@ -80,11 +81,7 @@ public final class GoBackend implements Backend { File socketFile = new File(context.getCacheDir(), "libwg.sock"); String socketName = socketFile.getAbsolutePath(); Log.i(TAG, "wgStartGrpc: " + wgStartGrpc(socketName)); - ManagedChannelBuilder channelBuilder = ManagedChannelBuilder.forAddress("localhost", 10000).usePlaintext(); - LocalSocketAddress address = new LocalSocketAddress(socketName, LocalSocketAddress.Namespace.FILESYSTEM); - SocketFactory socketFactory = new UnixDomainSocketFactory(address); - ((OkHttpChannelBuilder) channelBuilder).socketFactory(socketFactory); - channel = channelBuilder.build(); + channel = UdsChannelBuilder.forPath(socketName, LocalSocketAddress.Namespace.FILESYSTEM).build(); } /** diff --git a/tunnel/src/main/java/com/wireguard/android/backend/LocalSocketAdapter.java b/tunnel/src/main/java/com/wireguard/android/backend/LocalSocketAdapter.java deleted file mode 100644 index bf027ae1..00000000 --- a/tunnel/src/main/java/com/wireguard/android/backend/LocalSocketAdapter.java +++ /dev/null @@ -1,325 +0,0 @@ -/* - */ -package com.wireguard.android.backend; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.net.Socket; -import java.net.SocketAddress; -import java.net.SocketException; -import java.net.SocketImplFactory; -import java.net.SocketOptions; -import java.nio.channels.SocketChannel; -import java.util.Vector; -import android.net.LocalSocket; -import android.net.LocalSocketAddress; -import android.util.Log; - -/** - * Adaptor allows using a LocalSocket as a Socket. - */ -final class LocalSocketAdapter extends Socket { - private final LocalSocketAddress address; - private final LocalSocket unix; - private final SocketAddress localAddress; - private InetSocketAddress inetSocketAddress; - private InputStream is; - private OutputStream os; - - LocalSocketAdapter(LocalSocketAddress address) { - this.address = address; - this.localAddress = new InetSocketAddress(0); - unix = new LocalSocket(); - } - - LocalSocketAdapter(LocalSocketAddress address, InetSocketAddress inetAddress) { - this(address); - this.inetSocketAddress = inetAddress; - } - - private void throwUnsupportedOperationException() { - Log.i("helloworld", "Unsupported: " + Log.getStackTraceString(new Exception())); - throw new UnsupportedOperationException(); - } - - @Override - public void bind (SocketAddress bindpoint) { - throwUnsupportedOperationException(); - } - - @Override - public void close() throws IOException { - unix.close(); - } - - @Override public void connect(SocketAddress endpoint) throws IOException { - this.inetSocketAddress = (InetSocketAddress) endpoint; - try { - unix.connect(address); - } catch (IOException e) { - Log.i("helloworld", "Error: " + e.toString()); - throw e; - } - } - - @Override - public void connect(SocketAddress endpoint, int timeout) throws IOException { - this.inetSocketAddress = (InetSocketAddress) endpoint; - unix.connect(address, timeout); - } - - @Override - public SocketChannel getChannel() { - throwUnsupportedOperationException(); - return null; - } - - @Override public InetAddress getInetAddress() { - return inetSocketAddress.getAddress(); - } - - @Override - public InputStream getInputStream() throws IOException { - is = unix.getInputStream(); - return is; - } - - @Override - public boolean getKeepAlive() { - throwUnsupportedOperationException(); - return false; - } - - @Override - public InetAddress getLocalAddress() { - throwUnsupportedOperationException(); - return null; - } - - @Override - public int getLocalPort() { - throwUnsupportedOperationException(); - return 0; - } - - @Override - public SocketAddress getLocalSocketAddress() { - //throwUnsupportedOperationException(); - return localAddress; - } - - @Override - public boolean getOOBInline() { - throwUnsupportedOperationException(); - return false; - } - - @Override - public OutputStream getOutputStream() throws IOException { - if (os != null) - return os; - - OutputStream unixOs = unix.getOutputStream(); - os = new OutputStream() { - @Override - public void close() throws IOException { - // LocalSocket's default implementation closes the socket, - // which leaves readers of thes InputStream hanging. - // Instead shutdown input (and output) to release readers. - LocalSocketAdapter.this.shutdownInput(); - LocalSocketAdapter.this.shutdownOutput(); - } - - @Override - public void write (byte[] b) throws IOException { - unixOs.write(b); - } - - @Override - public void write (byte[] b, int off, int len) throws IOException { - unixOs.write(b, off, len); - } - - @Override - public void write (int b) throws IOException { - unixOs.write(b); - } - }; - return os; - } - - @Override - public int getPort() { - return inetSocketAddress.getPort(); - } - - @Override - public int getReceiveBufferSize() throws SocketException { - try { - return unix.getReceiveBufferSize(); - } catch (IOException e) { - throw new SocketException(e.getMessage()); - } - } - - @Override - public SocketAddress getRemoteSocketAddress() { - return inetSocketAddress; - } - - @Override - public boolean getReuseAddress() { - throwUnsupportedOperationException(); - return false; - } - - @Override - public int getSendBufferSize() throws SocketException { - try { - return unix.getSendBufferSize(); - } catch (IOException e) { - throw new SocketException(e.getMessage()); - } - } - - @Override - public int getSoLinger() { - throwUnsupportedOperationException(); - return 0; - } - - @Override - public int getSoTimeout() throws SocketException { - try { - return unix.getSoTimeout(); - } catch (IOException e) { - throw new SocketException(e.getMessage()); - } - } - - @Override - public boolean getTcpNoDelay() { - throwUnsupportedOperationException(); - return false; - } - - @Override - public int getTrafficClass() { - throwUnsupportedOperationException(); - return 0; - } - - @Override - public boolean isBound() { - return unix.isBound(); - } - - @Override - public boolean isClosed() { - return unix.isClosed(); - } - - @Override - public boolean isConnected() { - return unix.isConnected(); - } - - @Override - public boolean isInputShutdown() { - return unix.isInputShutdown(); - } - - @Override - public boolean isOutputShutdown() { - return unix.isOutputShutdown(); - } - - @Override - public void sendUrgentData (int data) { - throwUnsupportedOperationException(); - } - - @Override - public void setKeepAlive (boolean on) { - throwUnsupportedOperationException(); - } - - @Override - public void setOOBInline (boolean on) { - throwUnsupportedOperationException(); - } - - @Override - public void setPerformancePreferences (int connectionTime, - int latency, - int bandwidth) { - throwUnsupportedOperationException(); - } - - @Override - public void setReceiveBufferSize(int size) throws SocketException { - try { - unix.setReceiveBufferSize(size); - } catch (IOException e) { - throw new SocketException(e.getMessage()); - } - } - - @Override - public void setReuseAddress (boolean on) { - throwUnsupportedOperationException(); - } - - @Override - public void setSendBufferSize(int size) throws SocketException { - try { - unix.setSendBufferSize(size); - } catch (IOException e) { - throw new SocketException(e.getMessage()); - } - } - - @Override - public void setSoLinger (boolean on, - int linger) { - throwUnsupportedOperationException(); - } - - @Override - public void setSoTimeout(int timeout) throws SocketException { - try { - unix.setSoTimeout(timeout); - } catch (IOException e) { - throw new SocketException(e.getMessage()); - } - } - - @Override - public void setTcpNoDelay (boolean on) { - // Not relevant for local sockets. - } - - @Override - public void setTrafficClass (int tc) { - throwUnsupportedOperationException(); - } - - @Override - public void shutdownInput() throws IOException { - unix.shutdownInput(); - } - - @Override - public void shutdownOutput() throws IOException { - unix.shutdownOutput(); - } - - @Override - public String toString() { - return unix.toString(); - } -} diff --git a/tunnel/src/main/java/com/wireguard/android/backend/UnixDomainSocketFactory.java b/tunnel/src/main/java/com/wireguard/android/backend/UnixDomainSocketFactory.java deleted file mode 100644 index 427a19f1..00000000 --- a/tunnel/src/main/java/com/wireguard/android/backend/UnixDomainSocketFactory.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (C) 2018 Square, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.wireguard.android.backend; - -import java.io.File; -import java.io.IOException; -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.net.Socket; -import javax.net.SocketFactory; -import android.net.LocalSocketAddress; - -/** Impersonate TCP-style SocketFactory over UNIX domain sockets. */ -public final class UnixDomainSocketFactory extends SocketFactory { - private final LocalSocketAddress address; - - public UnixDomainSocketFactory(LocalSocketAddress address) { - this.address = address; - } - - @Override public Socket createSocket() throws IOException { - return new LocalSocketAdapter(address); - } - - @Override public Socket createSocket(String host, int port) throws IOException { - Socket result = createSocket(); - result.connect(new InetSocketAddress(host, port)); - return result; - } - - @Override public Socket createSocket( - String host, int port, InetAddress localHost, int localPort) throws IOException { - return createSocket(host, port); - } - - @Override public Socket createSocket(InetAddress host, int port) throws IOException { - Socket result = createSocket(); - result.connect(new InetSocketAddress(host, port)); - return result; - } - - @Override public Socket createSocket( - InetAddress host, int port, InetAddress localAddress, int localPort) throws IOException { - return createSocket(host, port); - } -} -- cgit v1.2.3