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;
}
public static void setAlwaysOnCallback(AlwaysOnCallback cb) {
public static void setAlwaysOnCallback(final AlwaysOnCallback cb) {
alwaysOnCallback = cb;
}
@ -84,7 +84,8 @@ public final class GoBackend implements Backend {
}
final String config = wgGetConfig(currentTunnelHandle);
Key key = null;
long rx = 0, tx = 0;
long rx = 0;
long tx = 0;
for (final String line : config.split("\\n")) {
if (line.startsWith("public_key=")) {
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)
throws Exception {
Log.i(TAG, "Bringing tunnel " + tunnel.getName() + " " + state);
Log.i(TAG, "Bringing tunnel " + tunnel.getName() + ' ' + state);
if (state == State.UP) {
if (config == null)

View File

@ -105,7 +105,7 @@ public final class WgQuickBackend implements Backend {
return output.get(0);
}
public void setMultipleTunnels(boolean on) {
public void setMultipleTunnels(final boolean 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 {
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");

View File

@ -38,6 +38,7 @@ import java.util.Map;
import androidx.annotation.Nullable;
@NonNullForAll
@SuppressWarnings("MagicNumber")
public class ModuleLoader {
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";
@ -70,7 +71,7 @@ public class ModuleLoader {
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
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;
try (final InputStream inputStream = connection.getInputStream()) {
len = inputStream.read(input);
@ -93,7 +94,7 @@ public class ModuleLoader {
File tempFile = null;
try {
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();
final FileOutputStream outputStream = new FileOutputStream(tempFile)) {
int total = 0;
@ -127,7 +128,7 @@ public class ModuleLoader {
}
@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);
if (publicKeyBytes == null || publicKeyBytes.length != 32 + 10 || publicKeyBytes[0] != 'E' || publicKeyBytes[1] != 'd')
@ -148,9 +149,9 @@ public class ModuleLoader {
}
try {
EdDSAParameterSpec parameterSpec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
Signature signature = new EdDSAEngine(MessageDigest.getInstance(parameterSpec.getHashAlgorithm()));
byte[] rawPublicKeyBytes = new byte[32];
final EdDSAParameterSpec parameterSpec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
final Signature signature = new EdDSAEngine(MessageDigest.getInstance(parameterSpec.getHashAlgorithm()));
final byte[] rawPublicKeyBytes = new byte[32];
System.arraycopy(publicKeyBytes, 10, rawPublicKeyBytes, 0, 32);
signature.initVerify(new EdDSAPublicKey(new EdDSAPublicKeySpec(rawPublicKeyBytes, parameterSpec)));
signature.update(lines[2].getBytes(StandardCharsets.UTF_8));
@ -160,9 +161,9 @@ public class ModuleLoader {
return null;
}
Map<String, Sha256Digest> hashes = new HashMap<>();
final Map<String, Sha256Digest> hashes = new HashMap<>();
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)
return null;
try {
@ -175,7 +176,7 @@ public class ModuleLoader {
}
private static final class Sha256Digest {
private byte[] bytes;
private final byte[] bytes;
private Sha256Digest(final String hex) {
if (hex.length() != 64)

View File

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

View File

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

View File

@ -47,7 +47,7 @@ public final class InetNetwork {
final int maxMask = (address instanceof Inet4Address) ? 32 : 128;
if (rawMask > maxMask)
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);
}