LinuxTorPlugin: Address review comments

This commit is contained in:
Torsten Grote
2018-08-24 12:46:08 -03:00
parent 3a49ca0d97
commit 8e6cd12f07
10 changed files with 42 additions and 33 deletions

View File

@@ -22,10 +22,16 @@ class AndroidResourceProvider implements ResourceProvider {
} }
@Override @Override
public InputStream getResourceInputStream(String name) { public InputStream getResourceInputStream(String name, String extension) {
Resources res = appContext.getResources(); Resources res = appContext.getResources();
if (name.endsWith(".zip")) name = name.substring(0, name.length() - 4); String fileName;
int resId = res.getIdentifier(name, "raw", appContext.getPackageName()); if (extension.equals(".zip")) {
fileName = name;
} else {
fileName = name + extension;
}
int resId =
res.getIdentifier(fileName, "raw", appContext.getPackageName());
return res.openRawResource(resId); return res.openRawResource(resId);
} }
} }

View File

@@ -7,5 +7,5 @@ import java.io.InputStream;
@NotNullByDefault @NotNullByDefault
public interface ResourceProvider { public interface ResourceProvider {
InputStream getResourceInputStream(String name); InputStream getResourceInputStream(String name, String extension);
} }

View File

@@ -166,6 +166,12 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
@Override @Override
public void start() throws PluginException { public void start() throws PluginException {
if (used.getAndSet(true)) throw new IllegalStateException(); if (used.getAndSet(true)) throw new IllegalStateException();
if (!torDirectory.exists()) {
if (!torDirectory.mkdirs()) {
LOG.warning("Could not create Tor directory.");
throw new PluginException();
}
}
// Install or update the assets if necessary // Install or update the assets if necessary
if (!assetsAreUpToDate()) installAssets(); if (!assetsAreUpToDate()) installAssets();
if (cookieFile.exists() && !cookieFile.delete()) if (cookieFile.exists() && !cookieFile.delete())
@@ -289,14 +295,15 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
if (LOG.isLoggable(INFO)) if (LOG.isLoggable(INFO))
LOG.info("Installing Tor binary for " + architecture); LOG.info("Installing Tor binary for " + architecture);
InputStream in = resourceProvider InputStream in = resourceProvider
.getResourceInputStream("tor_" + architecture + ".zip"); .getResourceInputStream("tor_" + architecture, ".zip");
ZipInputStream zin = new ZipInputStream(in); ZipInputStream zin = new ZipInputStream(in);
if (zin.getNextEntry() == null) throw new IOException(); if (zin.getNextEntry() == null) throw new IOException();
return zin; return zin;
} }
private InputStream getGeoIpInputStream() throws IOException { private InputStream getGeoIpInputStream() throws IOException {
InputStream in = resourceProvider.getResourceInputStream("geoip.zip"); InputStream in = resourceProvider.getResourceInputStream("geoip",
".zip");
ZipInputStream zin = new ZipInputStream(in); ZipInputStream zin = new ZipInputStream(in);
if (zin.getNextEntry() == null) throw new IOException(); if (zin.getNextEntry() == null) throw new IOException();
return zin; return zin;

View File

@@ -16,7 +16,7 @@ dependencies {
implementation fileTree(dir: 'libs', include: '*.jar') implementation fileTree(dir: 'libs', include: '*.jar')
implementation 'net.java.dev.jna:jna:4.4.0' implementation 'net.java.dev.jna:jna:4.4.0'
implementation 'net.java.dev.jna:jna-platform:4.4.0' implementation 'net.java.dev.jna:jna-platform:4.4.0'
tor 'org.briarproject:tor-linux:0.2.9.16@zip' tor 'org.briarproject:tor:0.2.9.16@zip'
apt 'com.google.dagger:dagger-compiler:2.0.2' apt 'com.google.dagger:dagger-compiler:2.0.2'

View File

@@ -1,9 +0,0 @@
package org.briarproject.bramble.api;
import java.io.File;
public interface ConfigurationManager {
File getAppDir();
}

View File

@@ -16,6 +16,7 @@ import java.util.logging.Logger;
import javax.inject.Inject; import javax.inject.Inject;
import static java.net.NetworkInterface.getNetworkInterfaces; import static java.net.NetworkInterface.getNetworkInterfaces;
import static java.util.Collections.list;
import static java.util.logging.Level.INFO; import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.util.LogUtils.logException; import static org.briarproject.bramble.util.LogUtils.logException;
@@ -49,13 +50,12 @@ class JavaNetworkManager implements NetworkManager, Service {
try { try {
Enumeration<NetworkInterface> interfaces = getNetworkInterfaces(); Enumeration<NetworkInterface> interfaces = getNetworkInterfaces();
if (interfaces != null) { if (interfaces != null) {
while (interfaces.hasMoreElements()) { for (NetworkInterface i : list(interfaces)) {
NetworkInterface i = interfaces.nextElement();
if (i.isLoopback()) continue; if (i.isLoopback()) continue;
if (i.isUp()) { if (i.isUp() && i.getInetAddresses().hasMoreElements()) {
if (LOG.isLoggable(INFO)) { if (LOG.isLoggable(INFO)) {
LOG.info("Interface " + i.getDisplayName() + LOG.info("Interface " + i.getDisplayName() +
" is up."); " is up with at least one address.");
} }
connected = true; connected = true;
break; break;

View File

@@ -12,14 +12,15 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.security.CodeSource;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import javax.net.SocketFactory; import javax.net.SocketFactory;
@NotNullByDefault @NotNullByDefault
class JavaTorPlugin extends TorPlugin { class LinuxTorPlugin extends TorPlugin {
JavaTorPlugin(Executor ioExecutor, NetworkManager networkManager, LinuxTorPlugin(Executor ioExecutor, NetworkManager networkManager,
LocationUtils locationUtils, SocketFactory torSocketFactory, LocationUtils locationUtils, SocketFactory torSocketFactory,
Clock clock, ResourceProvider resourceProvider, Clock clock, ResourceProvider resourceProvider,
CircumventionProvider circumventionProvider, Backoff backoff, CircumventionProvider circumventionProvider, Backoff backoff,
@@ -43,10 +44,13 @@ class JavaTorPlugin extends TorPlugin {
@Override @Override
protected long getLastUpdateTime() { protected long getLastUpdateTime() {
CodeSource codeSource =
getClass().getProtectionDomain().getCodeSource();
if (codeSource == null) throw new AssertionError("CodeSource null");
try { try {
URI path = getClass().getProtectionDomain().getCodeSource() URI path = codeSource.getLocation().toURI();
.getLocation().toURI(); File file = new File(path);
return new File(path).lastModified(); return file.lastModified();
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw new AssertionError(e); throw new AssertionError(e);
} }

View File

@@ -25,10 +25,10 @@ import static org.briarproject.bramble.util.OsUtils.isLinux;
@Immutable @Immutable
@NotNullByDefault @NotNullByDefault
public class JavaTorPluginFactory implements DuplexPluginFactory { public class LinuxTorPluginFactory implements DuplexPluginFactory {
private static final Logger LOG = private static final Logger LOG =
Logger.getLogger(JavaTorPluginFactory.class.getName()); Logger.getLogger(LinuxTorPluginFactory.class.getName());
private static final int MAX_LATENCY = 30 * 1000; // 30 seconds private static final int MAX_LATENCY = 30 * 1000; // 30 seconds
private static final int MAX_IDLE_TIME = 30 * 1000; // 30 seconds private static final int MAX_IDLE_TIME = 30 * 1000; // 30 seconds
@@ -47,7 +47,7 @@ public class JavaTorPluginFactory implements DuplexPluginFactory {
private final Clock clock; private final Clock clock;
private final File torDirectory; private final File torDirectory;
public JavaTorPluginFactory(Executor ioExecutor, public LinuxTorPluginFactory(Executor ioExecutor,
NetworkManager networkManager, LocationUtils locationUtils, NetworkManager networkManager, LocationUtils locationUtils,
EventBus eventBus, SocketFactory torSocketFactory, EventBus eventBus, SocketFactory torSocketFactory,
BackoffFactory backoffFactory, ResourceProvider resourceProvider, BackoffFactory backoffFactory, ResourceProvider resourceProvider,
@@ -92,9 +92,8 @@ public class JavaTorPluginFactory implements DuplexPluginFactory {
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL, Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
MAX_POLLING_INTERVAL, BACKOFF_BASE); MAX_POLLING_INTERVAL, BACKOFF_BASE);
if (!torDirectory.exists()) torDirectory.mkdirs(); LinuxTorPlugin plugin =
JavaTorPlugin plugin = new LinuxTorPlugin(ioExecutor, networkManager, locationUtils,
new JavaTorPlugin(ioExecutor, networkManager, locationUtils,
torSocketFactory, clock, resourceProvider, torSocketFactory, clock, resourceProvider,
circumventionProvider, backoff, callback, architecture, circumventionProvider, backoff, callback, architecture,
MAX_LATENCY, MAX_IDLE_TIME, torDirectory); MAX_LATENCY, MAX_IDLE_TIME, torDirectory);

View File

@@ -15,7 +15,8 @@ class JavaResourceProvider implements ResourceProvider {
} }
@Override @Override
public InputStream getResourceInputStream(String name) { public InputStream getResourceInputStream(String name, String extension) {
return this.getClass().getClassLoader().getResourceAsStream(name); return getClass().getClassLoader()
.getResourceAsStream(name + extension);
} }
} }

View File

@@ -12,6 +12,7 @@ dependencyVerification {
'org.apache.ant:ant-launcher:1.9.4:ant-launcher-1.9.4.jar:7bccea20b41801ca17bcbc909a78c835d0f443f12d639c77bd6ae3d05861608d', 'org.apache.ant:ant-launcher:1.9.4:ant-launcher-1.9.4.jar:7bccea20b41801ca17bcbc909a78c835d0f443f12d639c77bd6ae3d05861608d',
'org.apache.ant:ant:1.9.4:ant-1.9.4.jar:649ae0730251de07b8913f49286d46bba7b92d47c5f332610aa426c4f02161d8', 'org.apache.ant:ant:1.9.4:ant-1.9.4.jar:649ae0730251de07b8913f49286d46bba7b92d47c5f332610aa426c4f02161d8',
'org.beanshell:bsh:1.3.0:bsh-1.3.0.jar:9b04edc75d19db54f1b4e8b5355e9364384c6cf71eb0a1b9724c159d779879f8', 'org.beanshell:bsh:1.3.0:bsh-1.3.0.jar:9b04edc75d19db54f1b4e8b5355e9364384c6cf71eb0a1b9724c159d779879f8',
'org.briarproject:tor:0.2.9.16:tor-0.2.9.16.zip:f33091ba414d6a952263981d9059b3d0a9093fd277ae887b3cdd02e8f1936558',
'org.hamcrest:hamcrest-core:1.3:hamcrest-core-1.3.jar:66fdef91e9739348df7a096aa384a5685f4e875584cce89386a7a47251c4d8e9', 'org.hamcrest:hamcrest-core:1.3:hamcrest-core-1.3.jar:66fdef91e9739348df7a096aa384a5685f4e875584cce89386a7a47251c4d8e9',
'org.hamcrest:hamcrest-library:1.3:hamcrest-library-1.3.jar:711d64522f9ec410983bd310934296da134be4254a125080a0416ec178dfad1c', 'org.hamcrest:hamcrest-library:1.3:hamcrest-library-1.3.jar:711d64522f9ec410983bd310934296da134be4254a125080a0416ec178dfad1c',
'org.jmock:jmock-junit4:2.8.2:jmock-junit4-2.8.2.jar:f7ee4df4f7bd7b7f1cafad3b99eb74d579f109d5992ff625347352edb55e674c', 'org.jmock:jmock-junit4:2.8.2:jmock-junit4-2.8.2.jar:f7ee4df4f7bd7b7f1cafad3b99eb74d579f109d5992ff625347352edb55e674c',