tunnel: Codestyle cleanups

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-03-10 13:20:16 +05:30
parent a3b9c3b884
commit 8f85e4c88f
6 changed files with 33 additions and 37 deletions

View File

@ -45,7 +45,7 @@ public final class GoBackend implements Backend {
this.context = context; this.context = context;
} }
public static void setAlwaysOnCallback(AlwaysOnCallback cb) { public static void setAlwaysOnCallback(final AlwaysOnCallback cb) {
alwaysOnCallback = cb; alwaysOnCallback = cb;
} }
@ -84,7 +84,8 @@ public final class GoBackend implements Backend {
} }
final String config = wgGetConfig(currentTunnelHandle); final String config = wgGetConfig(currentTunnelHandle);
Key key = null; Key key = null;
long rx = 0, tx = 0; long rx = 0;
long tx = 0;
for (final String line : config.split("\\n")) { for (final String line : config.split("\\n")) {
if (line.startsWith("public_key=")) { if (line.startsWith("public_key=")) {
if (key != null) if (key != null)
@ -152,7 +153,7 @@ public final class GoBackend implements Backend {
private void setStateInternal(final Tunnel tunnel, @Nullable final Config config, final State state) private void setStateInternal(final Tunnel tunnel, @Nullable final Config config, final State state)
throws Exception { throws Exception {
Log.i(TAG, "Bringing tunnel " + tunnel.getName() + " " + state); Log.i(TAG, "Bringing tunnel " + tunnel.getName() + ' ' + state);
if (state == State.UP) { if (state == State.UP) {
if (config == null) if (config == null)

View File

@ -105,7 +105,7 @@ public final class WgQuickBackend implements Backend {
return output.get(0); return output.get(0);
} }
public void setMultipleTunnels(boolean on) { public void setMultipleTunnels(final boolean on) {
multipleTunnels = on; multipleTunnels = on;
} }
@ -164,7 +164,7 @@ public final class WgQuickBackend implements Backend {
} }
private void setStateInternal(final Tunnel tunnel, @Nullable final Config config, final State state) throws Exception { private void setStateInternal(final Tunnel tunnel, @Nullable final Config config, final State state) throws Exception {
Log.i(TAG, "Bringing tunnel " + tunnel.getName() + " " + state); Log.i(TAG, "Bringing tunnel " + tunnel.getName() + ' ' + state);
Objects.requireNonNull(config, "Trying to set state up with a null config"); Objects.requireNonNull(config, "Trying to set state up with a null config");

View File

@ -38,6 +38,7 @@ import java.util.Map;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@NonNullForAll @NonNullForAll
@SuppressWarnings("MagicNumber")
public class ModuleLoader { public class ModuleLoader {
private static final String MODULE_LIST_URL = "https://download.wireguard.com/android-module/modules.txt.sig"; private static final String MODULE_LIST_URL = "https://download.wireguard.com/android-module/modules.txt.sig";
private static final String MODULE_NAME = "wireguard-%s.ko"; private static final String MODULE_NAME = "wireguard-%s.ko";
@ -70,7 +71,7 @@ public class ModuleLoader {
connection.connect(); connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
throw new IOException("Hash list could not be found"); throw new IOException("Hash list could not be found");
byte[] input = new byte[1024 * 1024 * 3 /* 3MiB */]; final byte[] input = new byte[1024 * 1024 * 3 /* 3MiB */];
int len; int len;
try (final InputStream inputStream = connection.getInputStream()) { try (final InputStream inputStream = connection.getInputStream()) {
len = inputStream.read(input); len = inputStream.read(input);
@ -93,7 +94,7 @@ public class ModuleLoader {
File tempFile = null; File tempFile = null;
try { try {
tempFile = File.createTempFile("UNVERIFIED-", null, tmpDir); tempFile = File.createTempFile("UNVERIFIED-", null, tmpDir);
MessageDigest digest = MessageDigest.getInstance("SHA-256"); final MessageDigest digest = MessageDigest.getInstance("SHA-256");
try (final InputStream inputStream = connection.getInputStream(); try (final InputStream inputStream = connection.getInputStream();
final FileOutputStream outputStream = new FileOutputStream(tempFile)) { final FileOutputStream outputStream = new FileOutputStream(tempFile)) {
int total = 0; int total = 0;
@ -127,7 +128,7 @@ public class ModuleLoader {
} }
@Nullable @Nullable
private Map<String, Sha256Digest> verifySignedHashes(final String signifyDigest) { private static Map<String, Sha256Digest> verifySignedHashes(final String signifyDigest) {
final byte[] publicKeyBytes = Base64.decode(MODULE_PUBLIC_KEY_BASE64, Base64.DEFAULT); final byte[] publicKeyBytes = Base64.decode(MODULE_PUBLIC_KEY_BASE64, Base64.DEFAULT);
if (publicKeyBytes == null || publicKeyBytes.length != 32 + 10 || publicKeyBytes[0] != 'E' || publicKeyBytes[1] != 'd') if (publicKeyBytes == null || publicKeyBytes.length != 32 + 10 || publicKeyBytes[0] != 'E' || publicKeyBytes[1] != 'd')
@ -148,9 +149,9 @@ public class ModuleLoader {
} }
try { try {
EdDSAParameterSpec parameterSpec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519); final EdDSAParameterSpec parameterSpec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
Signature signature = new EdDSAEngine(MessageDigest.getInstance(parameterSpec.getHashAlgorithm())); final Signature signature = new EdDSAEngine(MessageDigest.getInstance(parameterSpec.getHashAlgorithm()));
byte[] rawPublicKeyBytes = new byte[32]; final byte[] rawPublicKeyBytes = new byte[32];
System.arraycopy(publicKeyBytes, 10, rawPublicKeyBytes, 0, 32); System.arraycopy(publicKeyBytes, 10, rawPublicKeyBytes, 0, 32);
signature.initVerify(new EdDSAPublicKey(new EdDSAPublicKeySpec(rawPublicKeyBytes, parameterSpec))); signature.initVerify(new EdDSAPublicKey(new EdDSAPublicKeySpec(rawPublicKeyBytes, parameterSpec)));
signature.update(lines[2].getBytes(StandardCharsets.UTF_8)); signature.update(lines[2].getBytes(StandardCharsets.UTF_8));
@ -160,9 +161,9 @@ public class ModuleLoader {
return null; return null;
} }
Map<String, Sha256Digest> hashes = new HashMap<>(); final Map<String, Sha256Digest> hashes = new HashMap<>();
for (final String line : lines[2].split("\n")) { for (final String line : lines[2].split("\n")) {
final String[] components = line.split(" ", 2); final String[] components = line.split(" {2}", 2);
if (components.length != 2) if (components.length != 2)
return null; return null;
try { try {
@ -175,7 +176,7 @@ public class ModuleLoader {
} }
private static final class Sha256Digest { private static final class Sha256Digest {
private byte[] bytes; private final byte[] bytes;
private Sha256Digest(final String hex) { private Sha256Digest(final String hex) {
if (hex.length() != 64) if (hex.length() != 64)

View File

@ -177,7 +177,7 @@ public class RootShell {
} }
} }
public void stop() { private void stop() {
synchronized (lock) { synchronized (lock) {
if (process != null) { if (process != null) {
process.destroy(); process.destroy();

View File

@ -37,29 +37,23 @@ public final class SharedLibraryLoader {
for (final String abi : Build.SUPPORTED_ABIS) { for (final String abi : Build.SUPPORTED_ABIS) {
for (final String apk : apks) { for (final String apk : apks) {
final ZipFile zipFile; try (final ZipFile zipFile = new ZipFile(new File(apk), ZipFile.OPEN_READ)) {
try { final String mappedLibName = System.mapLibraryName(libName);
zipFile = new ZipFile(new File(apk), ZipFile.OPEN_READ); final String libZipPath = "lib" + File.separatorChar + abi + File.separatorChar + mappedLibName;
} catch (final IOException e) { final ZipEntry zipEntry = zipFile.getEntry(libZipPath);
throw new RuntimeException(e); if (zipEntry == null)
} continue;
Log.d(TAG, "Extracting apk:/" + libZipPath + " to " + destination.getAbsolutePath());
final String mappedLibName = System.mapLibraryName(libName); try (final FileOutputStream out = new FileOutputStream(destination);
final byte[] buffer = new byte[1024 * 32]; final InputStream in = zipFile.getInputStream(zipEntry)) {
final String libZipPath = "lib" + File.separatorChar + abi + File.separatorChar + mappedLibName; int len;
final ZipEntry zipEntry = zipFile.getEntry(libZipPath); final byte[] buffer = new byte[1024 * 32];
if (zipEntry == null) while ((len = in.read(buffer)) != -1) {
continue; out.write(buffer, 0, len);
Log.d(TAG, "Extracting apk:/" + libZipPath + " to " + destination.getAbsolutePath()); }
try (final FileOutputStream out = new FileOutputStream(destination); out.getFD().sync();
final InputStream in = zipFile.getInputStream(zipEntry)) {
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
} }
out.getFD().sync();
} }
zipFile.close();
return true; return true;
} }
} }

View File

@ -47,7 +47,7 @@ public final class InetNetwork {
final int maxMask = (address instanceof Inet4Address) ? 32 : 128; final int maxMask = (address instanceof Inet4Address) ? 32 : 128;
if (rawMask > maxMask) if (rawMask > maxMask)
throw new ParseException(InetNetwork.class, maskString, "Invalid network mask"); throw new ParseException(InetNetwork.class, maskString, "Invalid network mask");
final int mask = rawMask >= 0 && rawMask <= maxMask ? rawMask : maxMask; final int mask = rawMask >= 0 ? rawMask : maxMask;
return new InetNetwork(address, mask); return new InetNetwork(address, mask);
} }