diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-10-28 22:16:54 +0200 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2024-01-08 00:15:09 +0100 |
commit | f95f7d93d98a385a0d2ebf91de5a558c3671da05 (patch) | |
tree | 13c75105404ad4ded06bb39fd285f0443997afee | |
parent | eed3774cad76eab83c4260d832c4bea4dd17f43d (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); |