mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Optionally create a Tor plugin without a hidden service. Should speed up
tests.
This commit is contained in:
@@ -73,10 +73,32 @@ class TorPlugin implements DuplexPlugin {
|
||||
}
|
||||
|
||||
private void bind() {
|
||||
// Connect to Tor
|
||||
NetFactory netFactory = NetFactory.getInstance();
|
||||
NetLayer nl = netFactory.getNetLayerById(NetLayerIDs.TOR);
|
||||
nl.waitUntilReady();
|
||||
synchronized(this) {
|
||||
if(!running) {
|
||||
tryToClear(nl);
|
||||
return;
|
||||
}
|
||||
netLayer = nl;
|
||||
connected = true;
|
||||
notifyAll();
|
||||
}
|
||||
// If we're configure not to create a hidden service, return
|
||||
TransportConfig c = callback.getConfig();
|
||||
if(c.containsKey("noHiddenService")) {
|
||||
if(LOG.isLoggable(Level.INFO))
|
||||
LOG.info("Not creating hidden service");
|
||||
TransportProperties p = callback.getLocalProperties();
|
||||
p.remove("onion");
|
||||
callback.setLocalProperties(p);
|
||||
return;
|
||||
}
|
||||
// Retrieve the hidden service address, or create one if necessary
|
||||
TorHiddenServicePrivateNetAddress addr;
|
||||
TorNetLayerUtil util = TorNetLayerUtil.getInstance();
|
||||
TransportConfig c = callback.getConfig();
|
||||
String privateKey = c.get("privateKey");
|
||||
if(privateKey == null) {
|
||||
addr = createHiddenServiceAddress(util, c);
|
||||
@@ -91,19 +113,6 @@ class TorPlugin implements DuplexPlugin {
|
||||
}
|
||||
TorHiddenServicePortPrivateNetAddress addrPort =
|
||||
new TorHiddenServicePortPrivateNetAddress(addr, 80);
|
||||
// Connect to Tor
|
||||
NetFactory netFactory = NetFactory.getInstance();
|
||||
NetLayer nl = netFactory.getNetLayerById(NetLayerIDs.TOR);
|
||||
nl.waitUntilReady();
|
||||
synchronized(this) {
|
||||
if(!running) {
|
||||
tryToClear(nl);
|
||||
return;
|
||||
}
|
||||
netLayer = nl;
|
||||
connected = true;
|
||||
notifyAll();
|
||||
}
|
||||
// Publish the hidden service
|
||||
NetServerSocket ss;
|
||||
try {
|
||||
|
||||
@@ -30,20 +30,23 @@ public class TorPluginTest extends BriarTestCase {
|
||||
TorPlugin serverPlugin = new TorPlugin(e, serverCallback, 0L);
|
||||
serverPlugin.start();
|
||||
// The plugin should create a hidden service... eventually
|
||||
serverCallback.latch.await(10, TimeUnit.MINUTES);
|
||||
assertTrue(serverCallback.latch.await(10, TimeUnit.MINUTES));
|
||||
String onion = serverCallback.local.get("onion");
|
||||
assertNotNull(onion);
|
||||
assertTrue(onion.endsWith(".onion"));
|
||||
// Create another plugin instance for the client
|
||||
Callback clientCallback = new Callback();
|
||||
clientCallback.config.put("noHiddenService", "");
|
||||
TransportProperties p = new TransportProperties();
|
||||
p.put("onion", onion);
|
||||
clientCallback.remote.put(contactId, p);
|
||||
TorPlugin clientPlugin = new TorPlugin(e, clientCallback, 0L);
|
||||
clientPlugin.start();
|
||||
// The plugin should start without creating a hidden service
|
||||
assertTrue(clientCallback.latch.await(10, TimeUnit.MINUTES));
|
||||
// Connect to the server's hidden service
|
||||
DuplexTransportConnection clientEnd =
|
||||
clientPlugin.createConnection(contactId);
|
||||
clientPlugin.createConnection(contactId);
|
||||
assertNotNull(clientEnd);
|
||||
DuplexTransportConnection serverEnd = serverCallback.incomingConnection;
|
||||
assertNotNull(serverEnd);
|
||||
@@ -69,13 +72,14 @@ public class TorPluginTest extends BriarTestCase {
|
||||
TorPlugin plugin = new TorPlugin(e, callback, 0L);
|
||||
plugin.start();
|
||||
// The plugin should create a hidden service... eventually
|
||||
callback.latch.await(10, TimeUnit.MINUTES);
|
||||
assertTrue(callback.latch.await(10, TimeUnit.MINUTES));
|
||||
String onion = callback.local.get("onion");
|
||||
assertNotNull(onion);
|
||||
assertTrue(onion.endsWith(".onion"));
|
||||
// Get the PEM-encoded private key and stop the plugin
|
||||
// Get the PEM-encoded private key
|
||||
String privateKey = callback.config.get("privateKey");
|
||||
assertNotNull(privateKey);
|
||||
// Stop the plugin
|
||||
plugin.stop();
|
||||
// Start another instance, reusing the private key
|
||||
callback = new Callback();
|
||||
@@ -83,7 +87,7 @@ public class TorPluginTest extends BriarTestCase {
|
||||
plugin = new TorPlugin(e, callback, 0L);
|
||||
plugin.start();
|
||||
// The plugin should create a hidden service... eventually
|
||||
callback.latch.await(10, TimeUnit.MINUTES);
|
||||
assertTrue(callback.latch.await(10, TimeUnit.MINUTES));
|
||||
// The onion URL should be the same
|
||||
assertEquals(onion, callback.local.get("onion"));
|
||||
// The private key should be the same
|
||||
@@ -95,7 +99,7 @@ public class TorPluginTest extends BriarTestCase {
|
||||
private static class Callback implements DuplexPluginCallback {
|
||||
|
||||
private final Map<ContactId, TransportProperties> remote =
|
||||
new Hashtable<ContactId, TransportProperties>();
|
||||
new Hashtable<ContactId, TransportProperties>();
|
||||
private final CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
private TransportConfig config = new TransportConfig();
|
||||
|
||||
Reference in New Issue
Block a user