mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 14:19:53 +01:00
Add support for v3 hidden service addresses.
This commit is contained in:
@@ -4,7 +4,8 @@ public interface TorConstants {
|
|||||||
|
|
||||||
TransportId ID = new TransportId("org.briarproject.bramble.tor");
|
TransportId ID = new TransportId("org.briarproject.bramble.tor");
|
||||||
|
|
||||||
String PROP_ONION = "onion";
|
String PROP_ONION_V2 = "onion";
|
||||||
|
String PROP_ONION_V3 = "onion3";
|
||||||
|
|
||||||
int SOCKS_PORT = 59050;
|
int SOCKS_PORT = 59050;
|
||||||
int CONTROL_PORT = 59051;
|
int CONTROL_PORT = 59051;
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ import org.briarproject.bramble.api.system.Clock;
|
|||||||
import org.briarproject.bramble.api.system.LocationUtils;
|
import org.briarproject.bramble.api.system.LocationUtils;
|
||||||
import org.briarproject.bramble.api.system.ResourceProvider;
|
import org.briarproject.bramble.api.system.ResourceProvider;
|
||||||
import org.briarproject.bramble.util.IoUtils;
|
import org.briarproject.bramble.util.IoUtils;
|
||||||
import org.briarproject.bramble.util.StringUtils;
|
|
||||||
|
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.EOFException;
|
import java.io.EOFException;
|
||||||
@@ -70,9 +69,11 @@ import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK_
|
|||||||
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK_NEVER;
|
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK_NEVER;
|
||||||
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK_WITH_BRIDGES;
|
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK_WITH_BRIDGES;
|
||||||
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_PORT;
|
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_PORT;
|
||||||
import static org.briarproject.bramble.api.plugin.TorConstants.PROP_ONION;
|
import static org.briarproject.bramble.api.plugin.TorConstants.PROP_ONION_V2;
|
||||||
|
import static org.briarproject.bramble.api.plugin.TorConstants.PROP_ONION_V3;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
import static org.briarproject.bramble.util.PrivacyUtils.scrubOnion;
|
import static org.briarproject.bramble.util.PrivacyUtils.scrubOnion;
|
||||||
|
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
@@ -87,7 +88,8 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
private static final String OWNER = "__OwningControllerProcess";
|
private static final String OWNER = "__OwningControllerProcess";
|
||||||
private static final int COOKIE_TIMEOUT_MS = 3000;
|
private static final int COOKIE_TIMEOUT_MS = 3000;
|
||||||
private static final int COOKIE_POLLING_INTERVAL_MS = 200;
|
private static final int COOKIE_POLLING_INTERVAL_MS = 200;
|
||||||
private static final Pattern ONION = Pattern.compile("[a-z2-7]{16}");
|
private static final Pattern ONION_V2 = Pattern.compile("[a-z2-7]{16}");
|
||||||
|
private static final Pattern ONION_V3 = Pattern.compile("[a-z2-7]{56}");
|
||||||
|
|
||||||
private final Executor ioExecutor, connectionStatusExecutor;
|
private final Executor ioExecutor, connectionStatusExecutor;
|
||||||
private final NetworkManager networkManager;
|
private final NetworkManager networkManager;
|
||||||
@@ -362,7 +364,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
// If there's already a port number stored in config, reuse it
|
// If there's already a port number stored in config, reuse it
|
||||||
String portString = settings.get(PREF_TOR_PORT);
|
String portString = settings.get(PREF_TOR_PORT);
|
||||||
int port;
|
int port;
|
||||||
if (StringUtils.isNullOrEmpty(portString)) port = 0;
|
if (isNullOrEmpty(portString)) port = 0;
|
||||||
else port = Integer.parseInt(portString);
|
else port = Integer.parseInt(portString);
|
||||||
// Bind a server socket to receive connections from Tor
|
// Bind a server socket to receive connections from Tor
|
||||||
ServerSocket ss = null;
|
ServerSocket ss = null;
|
||||||
@@ -427,11 +429,11 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Publish the hidden service's onion hostname in transport properties
|
// Publish the hidden service's onion hostname in transport properties
|
||||||
String hostname = response.get(HS_ADDRESS);
|
String onion2 = response.get(HS_ADDRESS);
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Hidden service " + scrubOnion(hostname));
|
LOG.info("Hidden service " + scrubOnion(onion2));
|
||||||
TransportProperties p = new TransportProperties();
|
TransportProperties p = new TransportProperties();
|
||||||
p.put(PROP_ONION, hostname);
|
p.put(PROP_ONION_V2, onion2);
|
||||||
callback.mergeLocalProperties(p);
|
callback.mergeLocalProperties(p);
|
||||||
if (privKey == null) {
|
if (privKey == null) {
|
||||||
// Save the hidden service's private key for next time
|
// Save the hidden service's private key for next time
|
||||||
@@ -530,26 +532,41 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
@Override
|
@Override
|
||||||
public DuplexTransportConnection createConnection(TransportProperties p) {
|
public DuplexTransportConnection createConnection(TransportProperties p) {
|
||||||
if (!isRunning()) return null;
|
if (!isRunning()) return null;
|
||||||
String onion = p.get(PROP_ONION);
|
String bestOnion = null;
|
||||||
if (StringUtils.isNullOrEmpty(onion)) return null;
|
String onion2 = p.get(PROP_ONION_V2);
|
||||||
if (!ONION.matcher(onion).matches()) {
|
String onion3 = p.get(PROP_ONION_V3);
|
||||||
// not scrubbing this address, so we are able to find the problem
|
if (!isNullOrEmpty(onion2)) {
|
||||||
if (LOG.isLoggable(INFO)) LOG.info("Invalid hostname: " + onion);
|
if (ONION_V2.matcher(onion2).matches()) {
|
||||||
return null;
|
bestOnion = onion2;
|
||||||
|
} else {
|
||||||
|
// Don't scrub the address so we can find the problem
|
||||||
|
if (LOG.isLoggable(INFO))
|
||||||
|
LOG.info("Invalid v2 hostname: " + onion2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if (!isNullOrEmpty(onion3)) {
|
||||||
|
if (ONION_V3.matcher(onion3).matches()) {
|
||||||
|
bestOnion = onion3;
|
||||||
|
} else {
|
||||||
|
// Don't scrub the address so we can find the problem
|
||||||
|
if (LOG.isLoggable(INFO))
|
||||||
|
LOG.info("Invalid v3 hostname: " + onion3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (bestOnion == null) return null;
|
||||||
Socket s = null;
|
Socket s = null;
|
||||||
try {
|
try {
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Connecting to " + scrubOnion(onion));
|
LOG.info("Connecting to " + scrubOnion(bestOnion));
|
||||||
s = torSocketFactory.createSocket(onion + ".onion", 80);
|
s = torSocketFactory.createSocket(bestOnion + ".onion", 80);
|
||||||
s.setSoTimeout(socketTimeout);
|
s.setSoTimeout(socketTimeout);
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Connected to " + scrubOnion(onion));
|
LOG.info("Connected to " + scrubOnion(bestOnion));
|
||||||
return new TorTransportConnection(this, s);
|
return new TorTransportConnection(this, s);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
if (LOG.isLoggable(INFO)) {
|
if (LOG.isLoggable(INFO)) {
|
||||||
LOG.info("Could not connect to " + scrubOnion(onion) + ": " +
|
LOG.info("Could not connect to " + scrubOnion(bestOnion)
|
||||||
e.toString());
|
+ ": " + e.toString());
|
||||||
}
|
}
|
||||||
tryToClose(s);
|
tryToClose(s);
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -238,7 +238,7 @@ public class TestDataCreatorImpl implements TestDataCreator {
|
|||||||
// Tor
|
// Tor
|
||||||
TransportProperties tor = new TransportProperties();
|
TransportProperties tor = new TransportProperties();
|
||||||
String torAddress = getRandomTorAddress();
|
String torAddress = getRandomTorAddress();
|
||||||
tor.put(TorConstants.PROP_ONION, torAddress);
|
tor.put(TorConstants.PROP_ONION_V2, torAddress);
|
||||||
props.put(TorConstants.ID, tor);
|
props.put(TorConstants.ID, tor);
|
||||||
|
|
||||||
return props;
|
return props;
|
||||||
|
|||||||
Reference in New Issue
Block a user