From 9781084f6329f1c33de6de3c858ab600a871ef39 Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Mon, 30 Apr 2018 11:37:52 -0500 Subject: global: Clean up Java Address Java and Android lints. Signed-off-by: Samuel Holland --- .../com/wireguard/android/backend/GoBackend.java | 55 ++++++++++++---------- .../wireguard/android/backend/WgQuickBackend.java | 5 +- 2 files changed, 32 insertions(+), 28 deletions(-) (limited to 'app/src/main/java/com/wireguard/android/backend') diff --git a/app/src/main/java/com/wireguard/android/backend/GoBackend.java b/app/src/main/java/com/wireguard/android/backend/GoBackend.java index 519f7427..e055c353 100644 --- a/app/src/main/java/com/wireguard/android/backend/GoBackend.java +++ b/app/src/main/java/com/wireguard/android/backend/GoBackend.java @@ -33,11 +33,11 @@ public final class GoBackend implements Backend { System.loadLibrary("wg-go"); } - private Context context; + private final Context context; private Tunnel currentTunnel; private int currentTunnelHandle = -1; - public GoBackend(Context context) { + public GoBackend(final Context context) { this.context = context; } @@ -108,14 +108,14 @@ public final class GoBackend implements Backend { if (VpnService.prepare(context) != null) throw new Exception("VPN service not authorized by user"); - VpnService service; + final VpnService service; if (!vpnService.isDone()) startVpnService(); try { service = vpnService.get(2, TimeUnit.SECONDS); - } catch (TimeoutException e) { - throw new Exception("Unable to start Android VPN service"); + } catch (final TimeoutException e) { + throw new Exception("Unable to start Android VPN service", e); } if (currentTunnelHandle != -1) { @@ -124,29 +124,32 @@ public final class GoBackend implements Backend { } // Build config - Formatter fmt = new Formatter(new StringBuilder()); final Interface iface = config.getInterface(); - fmt.format("replace_peers=true\n"); - if (iface.getPrivateKey() != null) - fmt.format("private_key=%s\n", KeyEncoding.keyToHex(KeyEncoding.keyFromBase64(iface.getPrivateKey()))); - if (iface.getListenPort() != 0) - fmt.format("listen_port=%d\n", config.getInterface().getListenPort()); - for (final Peer peer : config.getPeers()) { - if (peer.getPublicKey() != null) - fmt.format("public_key=%s\n", KeyEncoding.keyToHex(KeyEncoding.keyFromBase64(peer.getPublicKey()))); - if (peer.getPreSharedKey() != null) - fmt.format("preshared_key=%s\n", KeyEncoding.keyToHex(KeyEncoding.keyFromBase64(peer.getPreSharedKey()))); - if (peer.getEndpoint() != null) - fmt.format("endpoint=%s\n", peer.getResolvedEndpointString()); - if (peer.getPersistentKeepalive() != 0) - fmt.format("persistent_keepalive_interval=%d\n", peer.getPersistentKeepalive()); - for (final IPCidr addr : peer.getAllowedIPs()) { - fmt.format("allowed_ip=%s\n", addr.toString()); + final String goConfig; + try (Formatter fmt = new Formatter(new StringBuilder())) { + fmt.format("replace_peers=true\n"); + if (iface.getPrivateKey() != null) + fmt.format("private_key=%s\n", KeyEncoding.keyToHex(KeyEncoding.keyFromBase64(iface.getPrivateKey()))); + if (iface.getListenPort() != 0) + fmt.format("listen_port=%d\n", config.getInterface().getListenPort()); + for (final Peer peer : config.getPeers()) { + if (peer.getPublicKey() != null) + fmt.format("public_key=%s\n", KeyEncoding.keyToHex(KeyEncoding.keyFromBase64(peer.getPublicKey()))); + if (peer.getPreSharedKey() != null) + fmt.format("preshared_key=%s\n", KeyEncoding.keyToHex(KeyEncoding.keyFromBase64(peer.getPreSharedKey()))); + if (peer.getEndpoint() != null) + fmt.format("endpoint=%s\n", peer.getResolvedEndpointString()); + if (peer.getPersistentKeepalive() != 0) + fmt.format("persistent_keepalive_interval=%d\n", peer.getPersistentKeepalive()); + for (final IPCidr addr : peer.getAllowedIPs()) { + fmt.format("allowed_ip=%s\n", addr.toString()); + } } + goConfig = fmt.toString(); } // Create the vpn tunnel with android API - VpnService.Builder builder = service.getBuilder(); + final VpnService.Builder builder = service.getBuilder(); builder.setSession(tunnel.getName()); for (final IPCidr addr : config.getInterface().getAddresses()) @@ -166,13 +169,13 @@ public final class GoBackend implements Backend { builder.setMtu(mtu); builder.setBlocking(true); - ParcelFileDescriptor tun = builder.establish(); + final ParcelFileDescriptor tun = builder.establish(); if (tun == null) throw new Exception("Unable to create tun device"); - currentTunnelHandle = wgTurnOn(tunnel.getName(), tun.detachFd(), fmt.toString()); + currentTunnelHandle = wgTurnOn(tunnel.getName(), tun.detachFd(), goConfig); if (currentTunnelHandle < 0) - throw new Exception("Unable to turn tunnel on (wgTurnOn return " + currentTunnelHandle + ")"); + throw new Exception("Unable to turn tunnel on (wgTurnOn return " + currentTunnelHandle + ')'); currentTunnel = tunnel; diff --git a/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java b/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java index b91d8476..b9eb3461 100644 --- a/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java +++ b/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java @@ -105,14 +105,15 @@ public final class WgQuickBackend implements Backend { } final String command = String.format("wg-quick %s '%s'", state.toString().toLowerCase(), tempFile.getAbsolutePath()); final int result = rootShell.run(null, command); + // noinspection ResultOfMethodCallIgnored tempFile.delete(); if (result != 0) throw new Exception("Unable to configure tunnel (wg-quick returned " + result + ')'); } - public final static class WgQuickChangeReceiver extends BroadcastReceiver { + public static final class WgQuickChangeReceiver extends BroadcastReceiver { @Override - public void onReceive(Context context, Intent intent) { + public void onReceive(final Context context, final Intent intent) { Log.d(TAG, "Refreshing tunnel states"); Application.getComponent().getTunnelManager().refreshTunnelStates(); } -- cgit v1.2.3-59-g8ed1b