diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-10-28 22:16:54 +0200 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-10-28 22:43:05 +0200 |
commit | af7b47dba9670b276b6dd2ddd2e1f20ea6763bd0 (patch) | |
tree | 5c3e7c5bc1f65d5567f6d24935ab13ad124d81aa | |
parent | 1515752c15798ebd5c99d78dd849b8ee4e03e6da (diff) |
tunnel: set peer from BGP tunnel encapsulation
4 files changed, 46 insertions, 0 deletions
diff --git a/tunnel/src/main/java/com/wireguard/android/backend/Backend.java b/tunnel/src/main/java/com/wireguard/android/backend/Backend.java index 5ffdf8e2..fc281072 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/Backend.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/Backend.java @@ -6,6 +6,7 @@ package com.wireguard.android.backend; import com.wireguard.config.Config; +import com.wireguard.config.InetEndpoint; import com.wireguard.config.InetNetwork; import com.wireguard.crypto.Key; import com.wireguard.util.NonNullForAll; @@ -69,4 +70,7 @@ public interface Backend { Tunnel.State setState(Tunnel tunnel, Tunnel.State state, @Nullable Config config) throws Exception; void addAllowedIps(Tunnel tunnel, Key publicKey, List<InetNetwork> addNetworks); + + void setPeer(Tunnel tunnel, Key publicKey, @Nullable InetEndpoint endpoint, + List<InetNetwork> addAllowedIps, List<InetNetwork> removeAllowedIps); } diff --git a/tunnel/src/main/java/com/wireguard/android/backend/Bgp.java b/tunnel/src/main/java/com/wireguard/android/backend/Bgp.java index a6a8b420..ddf01199 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/Bgp.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/Bgp.java @@ -201,6 +201,7 @@ public class Bgp implements BGPListener { tunnel.onAllowedIpsChange(publicKey, addNetworks, removeNetworks); // backend.addAllowedIps(tunnel, publicKey, addNetworks); // backend.removeAllowedIps(tunnel, publicKey, addNetworks); // TODO + backend.setPeer(tunnel, publicKey, endpoint, addNetworks, removeNetworks); } catch (KeyFormatException ex) { Log.w(TAG, "Key.fromBytes " + ex); } 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 f0b31b1b..635c04f0 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java @@ -340,6 +340,40 @@ public final class GoBackend implements Backend { IpcSetResponse resp = stub.ipcSet(request); } + @Override + public void setPeer(Tunnel tunnel, Key publicKey, @Nullable InetEndpoint endpoint, + List<InetNetwork> addAllowedIps, List<InetNetwork> removeAllowedIps) { + if (tunnel != currentTunnel) { + // TODO logerror and/or return error/throw. + Log.w(TAG, "setPeer: wrong tunnel"); + return; + } + + final Resolver resolver = new Resolver(activeNetwork, connectivityManager.getLinkProperties(activeNetwork)); + + StringBuffer sb = new StringBuffer(); + + sb.append("public_key=").append(publicKey.toHex()).append('\n'); + for (final InetNetwork allowedIp: addAllowedIps) { + sb.append("allowed_ip=").append(allowedIp).append('\n'); + } + + if (endpoint != null) { + endpoint.getResolved(resolver, true).ifPresent(ep -> sb.append("endpoint=").append(ep).append('\n')); + } + + sb.append("persistent_keepalive_interval=").append(24550).append('\n'); + + String goConfig = sb.toString(); + // TODO removed removeNetworks + Log.w(TAG, "Wg user string: " + goConfig); + + LibwgGrpc.LibwgBlockingStub stub = LibwgGrpc.newBlockingStub(channel); + TunnelHandle handle = TunnelHandle.newBuilder().setHandle(currentTunnelHandle).build(); + IpcSetRequest request = IpcSetRequest.newBuilder().setTunnel(handle).setConfig(goConfig).build(); + IpcSetResponse resp = stub.ipcSet(request); + } + private static String downloadPacFile(Network network, Uri pacFileUrl) { HttpURLConnection urlConnection = null; StringBuffer buf = new StringBuffer(); diff --git a/tunnel/src/main/java/com/wireguard/android/backend/WgQuickBackend.java b/tunnel/src/main/java/com/wireguard/android/backend/WgQuickBackend.java index 2a3ee588..3cc1c822 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/WgQuickBackend.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/WgQuickBackend.java @@ -14,6 +14,7 @@ import com.wireguard.android.backend.Tunnel.State; import com.wireguard.android.util.RootShell; import com.wireguard.android.util.ToolsInstaller; import com.wireguard.config.Config; +import com.wireguard.config.InetEndpoint; import com.wireguard.config.InetNetwork; import com.wireguard.crypto.Key; import com.wireguard.util.NonNullForAll; @@ -173,6 +174,12 @@ public final class WgQuickBackend implements Backend { throw new RuntimeException("Not implemented"); } + @Override + public void setPeer(Tunnel tunnel, Key publicKey, @Nullable InetEndpoint endpoint, + List<InetNetwork> addAllowedIps, List<InetNetwork> removeAllowedIps) { + throw new RuntimeException("Not implemented"); + } + private void setStateInternal(final Tunnel tunnel, @Nullable final Config config, final State state) throws Exception { Log.i(TAG, "Bringing tunnel " + tunnel.getName() + ' ' + state); |