mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-21 15:19:53 +01:00
Merge branch '2149-hotspot-detection' into 'master'
Use interface name to decide whether we're providing a wifi hotspot Closes #2149 See merge request briar/briar!1521
This commit is contained in:
@@ -29,6 +29,7 @@ import java.net.UnknownHostException;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import javax.net.SocketFactory;
|
import javax.net.SocketFactory;
|
||||||
@@ -48,6 +49,7 @@ import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
|||||||
import static org.briarproject.bramble.api.plugin.Plugin.State.INACTIVE;
|
import static org.briarproject.bramble.api.plugin.Plugin.State.INACTIVE;
|
||||||
import static org.briarproject.bramble.util.IoUtils.tryToClose;
|
import static org.briarproject.bramble.util.IoUtils.tryToClose;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
|
import static org.briarproject.bramble.util.NetworkUtils.getNetworkInterfaces;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
class AndroidLanTcpPlugin extends LanTcpPlugin {
|
class AndroidLanTcpPlugin extends LanTcpPlugin {
|
||||||
@@ -55,6 +57,13 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
|
|||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
getLogger(AndroidLanTcpPlugin.class.getName());
|
getLogger(AndroidLanTcpPlugin.class.getName());
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The interface name is used as a heuristic for deciding whether the
|
||||||
|
* device is providing a wifi access point.
|
||||||
|
*/
|
||||||
|
private static final Pattern AP_INTERFACE_NAME =
|
||||||
|
Pattern.compile("^(wlan|ap|p2p)[-0-9]");
|
||||||
|
|
||||||
private final Executor connectionStatusExecutor;
|
private final Executor connectionStatusExecutor;
|
||||||
private final ConnectivityManager connectivityManager;
|
private final ConnectivityManager connectivityManager;
|
||||||
@Nullable
|
@Nullable
|
||||||
@@ -130,17 +139,14 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
|
|||||||
if (info != null && info.getIpAddress() != 0) {
|
if (info != null && info.getIpAddress() != 0) {
|
||||||
return new Pair<>(intToInetAddress(info.getIpAddress()), false);
|
return new Pair<>(intToInetAddress(info.getIpAddress()), false);
|
||||||
}
|
}
|
||||||
List<InterfaceAddress> ifAddrs = getLocalInterfaceAddresses();
|
// If we're providing an access point, return its address
|
||||||
// If we're providing a normal access point, return its address
|
for (NetworkInterface iface : getNetworkInterfaces()) {
|
||||||
for (InterfaceAddress ifAddr : ifAddrs) {
|
if (AP_INTERFACE_NAME.matcher(iface.getName()).find()) {
|
||||||
if (isAndroidWifiApAddress(ifAddr)) {
|
for (InterfaceAddress ifAddr : iface.getInterfaceAddresses()) {
|
||||||
return new Pair<>(ifAddr.getAddress(), true);
|
if (isPossibleWifiApInterface(ifAddr)) {
|
||||||
}
|
return new Pair<>(ifAddr.getAddress(), true);
|
||||||
}
|
}
|
||||||
// If we're providing a wifi direct access point, return its address
|
}
|
||||||
for (InterfaceAddress ifAddr : ifAddrs) {
|
|
||||||
if (isAndroidWifiDirectApAddress(ifAddr)) {
|
|
||||||
return new Pair<>(ifAddr.getAddress(), true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Not connected to wifi
|
// Not connected to wifi
|
||||||
@@ -148,33 +154,18 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the given address belongs to a network provided by an
|
* Returns true if the given address may belong to an interface providing
|
||||||
* Android access point (including the access point's own address).
|
* a wifi access point (including wifi direct legacy mode access points).
|
||||||
* <p>
|
* <p>
|
||||||
* The access point's address is usually 192.168.43.1, but at least one
|
* This method may return true for wifi client interfaces as well, but
|
||||||
* device (Honor 8A) may use other addresses in the range 192.168.43.0/24.
|
* we've already checked for a wifi client connection above.
|
||||||
*/
|
*/
|
||||||
private boolean isAndroidWifiApAddress(InterfaceAddress ifAddr) {
|
private boolean isPossibleWifiApInterface(InterfaceAddress ifAddr) {
|
||||||
if (ifAddr.getNetworkPrefixLength() != 24) return false;
|
if (ifAddr.getNetworkPrefixLength() != 24) return false;
|
||||||
byte[] ip = ifAddr.getAddress().getAddress();
|
byte[] ip = ifAddr.getAddress().getAddress();
|
||||||
return ip.length == 4
|
return ip.length == 4
|
||||||
&& ip[0] == (byte) 192
|
&& ip[0] == (byte) 192
|
||||||
&& ip[1] == (byte) 168
|
&& ip[1] == (byte) 168;
|
||||||
&& ip[2] == (byte) 43;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if the given address belongs to a network provided by an
|
|
||||||
* Android wifi direct legacy mode access point (including the access
|
|
||||||
* point's own address).
|
|
||||||
*/
|
|
||||||
private boolean isAndroidWifiDirectApAddress(InterfaceAddress ifAddr) {
|
|
||||||
if (ifAddr.getNetworkPrefixLength() != 24) return false;
|
|
||||||
byte[] ip = ifAddr.getAddress().getAddress();
|
|
||||||
return ip.length == 4
|
|
||||||
&& ip[0] == (byte) 192
|
|
||||||
&& ip[1] == (byte) 168
|
|
||||||
&& ip[2] == (byte) 49;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package org.briarproject.bramble.util;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
|
import java.net.NetworkInterface;
|
||||||
|
import java.net.SocketException;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import static java.util.Collections.emptyList;
|
||||||
|
import static java.util.Collections.list;
|
||||||
|
import static java.util.logging.Level.WARNING;
|
||||||
|
import static java.util.logging.Logger.getLogger;
|
||||||
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
|
|
||||||
|
@NotNullByDefault
|
||||||
|
public class NetworkUtils {
|
||||||
|
|
||||||
|
private static final Logger LOG = getLogger(NetworkUtils.class.getName());
|
||||||
|
|
||||||
|
public static List<NetworkInterface> getNetworkInterfaces() {
|
||||||
|
try {
|
||||||
|
Enumeration<NetworkInterface> ifaces =
|
||||||
|
NetworkInterface.getNetworkInterfaces();
|
||||||
|
// Despite what the docs say, the return value can be null
|
||||||
|
//noinspection ConstantConditions
|
||||||
|
return ifaces == null ? emptyList() : list(ifaces);
|
||||||
|
} catch (SocketException e) {
|
||||||
|
logException(LOG, WARNING, e);
|
||||||
|
return emptyList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,11 +28,9 @@ import java.net.InterfaceAddress;
|
|||||||
import java.net.NetworkInterface;
|
import java.net.NetworkInterface;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.SocketException;
|
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Enumeration;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
@@ -53,7 +51,7 @@ import static org.briarproject.bramble.api.plugin.Plugin.State.DISABLED;
|
|||||||
import static org.briarproject.bramble.api.plugin.Plugin.State.INACTIVE;
|
import static org.briarproject.bramble.api.plugin.Plugin.State.INACTIVE;
|
||||||
import static org.briarproject.bramble.api.plugin.Plugin.State.STARTING_STOPPING;
|
import static org.briarproject.bramble.api.plugin.Plugin.State.STARTING_STOPPING;
|
||||||
import static org.briarproject.bramble.util.IoUtils.tryToClose;
|
import static org.briarproject.bramble.util.IoUtils.tryToClose;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
import static org.briarproject.bramble.util.NetworkUtils.getNetworkInterfaces;
|
||||||
import static org.briarproject.bramble.util.PrivacyUtils.scrubSocketAddress;
|
import static org.briarproject.bramble.util.PrivacyUtils.scrubSocketAddress;
|
||||||
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||||
|
|
||||||
@@ -98,7 +96,6 @@ abstract class TcpPlugin implements DuplexPlugin, EventListener {
|
|||||||
/**
|
/**
|
||||||
* Returns true if connections to the given address can be attempted.
|
* Returns true if connections to the given address can be attempted.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
|
||||||
protected abstract boolean isConnectable(InterfaceAddress local,
|
protected abstract boolean isConnectable(InterfaceAddress local,
|
||||||
InetSocketAddress remote);
|
InetSocketAddress remote);
|
||||||
|
|
||||||
@@ -398,17 +395,6 @@ abstract class TcpPlugin implements DuplexPlugin, EventListener {
|
|||||||
return addrs;
|
return addrs;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<NetworkInterface> getNetworkInterfaces() {
|
|
||||||
try {
|
|
||||||
Enumeration<NetworkInterface> ifaces =
|
|
||||||
NetworkInterface.getNetworkInterfaces();
|
|
||||||
return ifaces == null ? emptyList() : list(ifaces);
|
|
||||||
} catch (SocketException e) {
|
|
||||||
logException(LOG, WARNING, e);
|
|
||||||
return emptyList();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void eventOccurred(Event e) {
|
public void eventOccurred(Event e) {
|
||||||
if (e instanceof SettingsUpdatedEvent) {
|
if (e instanceof SettingsUpdatedEvent) {
|
||||||
|
|||||||
@@ -9,16 +9,15 @@ import java.net.Inet4Address;
|
|||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.NetworkInterface;
|
import java.net.NetworkInterface;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.util.Enumeration;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import static java.net.NetworkInterface.getNetworkInterfaces;
|
|
||||||
import static java.util.Collections.list;
|
import static java.util.Collections.list;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
|
import static org.briarproject.bramble.util.NetworkUtils.getNetworkInterfaces;
|
||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
@@ -35,19 +34,14 @@ class JavaNetworkManager implements NetworkManager {
|
|||||||
public NetworkStatus getNetworkStatus() {
|
public NetworkStatus getNetworkStatus() {
|
||||||
boolean connected = false, hasIpv4 = false, hasIpv6Unicast = false;
|
boolean connected = false, hasIpv4 = false, hasIpv6Unicast = false;
|
||||||
try {
|
try {
|
||||||
Enumeration<NetworkInterface> interfaces = getNetworkInterfaces();
|
for (NetworkInterface i : getNetworkInterfaces()) {
|
||||||
if (interfaces == null) {
|
if (i.isLoopback() || !i.isUp()) continue;
|
||||||
LOG.info("No network interfaces");
|
for (InetAddress addr : list(i.getInetAddresses())) {
|
||||||
} else {
|
connected = true;
|
||||||
for (NetworkInterface i : list(interfaces)) {
|
if (addr instanceof Inet4Address) {
|
||||||
if (i.isLoopback() || !i.isUp()) continue;
|
hasIpv4 = true;
|
||||||
for (InetAddress addr : list(i.getInetAddresses())) {
|
} else if (!addr.isMulticastAddress()) {
|
||||||
connected = true;
|
hasIpv6Unicast = true;
|
||||||
if (addr instanceof Inet4Address) {
|
|
||||||
hasIpv4 = true;
|
|
||||||
} else if (!addr.isMulticastAddress()) {
|
|
||||||
hasIpv6Unicast = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,9 +14,6 @@ import java.io.IOException;
|
|||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InterfaceAddress;
|
import java.net.InterfaceAddress;
|
||||||
import java.net.NetworkInterface;
|
import java.net.NetworkInterface;
|
||||||
import java.net.SocketException;
|
|
||||||
import java.util.Enumeration;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
@@ -24,12 +21,11 @@ import javax.inject.Inject;
|
|||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.annotation.UiThread;
|
import androidx.annotation.UiThread;
|
||||||
|
|
||||||
import static java.util.Collections.emptyList;
|
|
||||||
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 java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
|
import static org.briarproject.bramble.util.NetworkUtils.getNetworkInterfaces;
|
||||||
import static org.briarproject.briar.android.hotspot.WebServer.PORT;
|
import static org.briarproject.briar.android.hotspot.WebServer.PORT;
|
||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@@ -112,16 +108,4 @@ class WebServerManager {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<NetworkInterface> getNetworkInterfaces() {
|
|
||||||
try {
|
|
||||||
Enumeration<NetworkInterface> ifaces =
|
|
||||||
NetworkInterface.getNetworkInterfaces();
|
|
||||||
return ifaces == null ? emptyList() : list(ifaces);
|
|
||||||
} catch (SocketException e) {
|
|
||||||
logException(LOG, WARNING, e);
|
|
||||||
return emptyList();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user