diff --git a/app/build.gradle b/app/build.gradle index a1a45ff2..99cd1e9c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -19,7 +19,7 @@ android { buildToolsVersion '30.0.3' defaultConfig { - minSdkVersion 14 + minSdkVersion 23 targetSdkVersion 30 versionCode 62 versionName "2.4.0" diff --git a/app/src/main/java/com/aaronjwood/portauthority/activity/HostActivity.java b/app/src/main/java/com/aaronjwood/portauthority/activity/HostActivity.java index 7a1b9567..6ad3a1d6 100644 --- a/app/src/main/java/com/aaronjwood/portauthority/activity/HostActivity.java +++ b/app/src/main/java/com/aaronjwood/portauthority/activity/HostActivity.java @@ -2,6 +2,7 @@ import android.app.Dialog; import android.app.ProgressDialog; +import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; @@ -26,6 +27,7 @@ import com.aaronjwood.portauthority.db.Database; import com.aaronjwood.portauthority.listener.ScanPortsListener; import com.aaronjwood.portauthority.network.Host; +import com.aaronjwood.portauthority.network.Network; import com.aaronjwood.portauthority.response.HostAsyncResponse; import com.aaronjwood.portauthority.utils.Constants; import com.aaronjwood.portauthority.utils.Errors; @@ -37,7 +39,6 @@ import java.util.List; public abstract class HostActivity extends AppCompatActivity implements HostAsyncResponse { - protected int layout; protected ArrayAdapter adapter; protected ListView portList; @@ -57,7 +58,8 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(layout); - db = Database.getInstance(getApplicationContext()); + Context ctx = getApplicationContext(); + db = Database.getInstance(ctx); handler = new Handler(Looper.getMainLooper()); setupPortsAdapter(); @@ -315,4 +317,18 @@ private void addOpenPort(final String port) { public void processFinish(final T output) { handler.post(() -> Errors.showError(getApplicationContext(), output.getLocalizedMessage())); } + + /** + * Determines if there is an active network connection. + * + * @return True if there's a connection, otherwise false. + */ + protected boolean isConnected() { + Context ctx = getApplicationContext(); + try { + return Network.isConnected(ctx); + } catch (Network.NoConnectivityManagerException e) { + return false; + } + } } diff --git a/app/src/main/java/com/aaronjwood/portauthority/activity/LanHostActivity.java b/app/src/main/java/com/aaronjwood/portauthority/activity/LanHostActivity.java index c08d288c..05614d01 100644 --- a/app/src/main/java/com/aaronjwood/portauthority/activity/LanHostActivity.java +++ b/app/src/main/java/com/aaronjwood/portauthority/activity/LanHostActivity.java @@ -14,13 +14,13 @@ import com.aaronjwood.portauthority.R; import com.aaronjwood.portauthority.listener.ScanPortsListener; import com.aaronjwood.portauthority.network.Host; -import com.aaronjwood.portauthority.network.Wireless; import com.aaronjwood.portauthority.utils.Constants; import com.aaronjwood.portauthority.utils.Errors; import com.aaronjwood.portauthority.utils.UserPreference; +import java.util.Objects; + public final class LanHostActivity extends HostActivity { - private Wireless wifi; private Host host; /** @@ -42,13 +42,9 @@ protected void onCreate(Bundle savedInstanceState) { return; } - wifi = new Wireless(getApplicationContext()); host = (Host) extras.get("HOST"); - if (host == null) { - return; - } - hostMacVendor.setText(host.getVendor()); + hostMacVendor.setText(Objects.requireNonNull(host).getVendor()); hostName.setText(host.getHostname()); hostMac.setText(host.getMac()); ipAddress.setText(host.getIp()); @@ -94,13 +90,7 @@ private void scanWellKnownPortsClick() { @Override public void onClick(View v) { super.onClick(v); - - try { - if (!wifi.isConnectedWifi()) { - Errors.showError(getApplicationContext(), getResources().getString(R.string.notConnectedLan)); - return; - } - } catch (Wireless.NoConnectivityManagerException e) { + if (!isConnected()) { Errors.showError(getApplicationContext(), getResources().getString(R.string.notConnectedLan)); return; } @@ -133,12 +123,7 @@ private void scanPortRangeClick() { */ @Override public void onClick(View v) { - try { - if (!wifi.isConnectedWifi()) { - Errors.showError(getApplicationContext(), getResources().getString(R.string.notConnectedLan)); - return; - } - } catch (Wireless.NoConnectivityManagerException e) { + if (!isConnected()) { Errors.showError(getApplicationContext(), getResources().getString(R.string.notConnectedLan)); return; } @@ -170,13 +155,9 @@ public void onClick(View v) { */ private void setupWol() { Button wakeUpButton = findViewById(R.id.wakeOnLan); + wakeUpButton.setOnClickListener(v -> { - try { - if (!wifi.isConnectedWifi()) { - Errors.showError(getApplicationContext(), getResources().getString(R.string.notConnectedLan)); - return; - } - } catch (Wireless.NoConnectivityManagerException e) { + if (!isConnected()) { Errors.showError(getApplicationContext(), getResources().getString(R.string.notConnectedLan)); return; } @@ -206,6 +187,7 @@ public void processFinish(boolean output) { if (output && scanProgressDialog != null && scanProgressDialog.isShowing()) { scanProgressDialog.dismiss(); } + if (output && portRangeDialog != null && portRangeDialog.isShowing()) { portRangeDialog.dismiss(); } diff --git a/app/src/main/java/com/aaronjwood/portauthority/activity/MainActivity.java b/app/src/main/java/com/aaronjwood/portauthority/activity/MainActivity.java index df5ba3d4..ce5900b8 100644 --- a/app/src/main/java/com/aaronjwood/portauthority/activity/MainActivity.java +++ b/app/src/main/java/com/aaronjwood/portauthority/activity/MainActivity.java @@ -5,7 +5,6 @@ import android.app.AlertDialog; import android.app.Dialog; import android.app.ProgressDialog; -import android.content.BroadcastReceiver; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; @@ -14,8 +13,8 @@ import android.content.pm.PackageManager; import android.content.res.Resources; import android.database.sqlite.SQLiteException; -import android.net.NetworkInfo; -import android.net.wifi.WifiManager; +import android.net.ConnectivityManager; +import android.net.NetworkRequest; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; @@ -53,6 +52,7 @@ import com.aaronjwood.portauthority.async.WolAsyncTask; import com.aaronjwood.portauthority.db.Database; import com.aaronjwood.portauthority.network.Host; +import com.aaronjwood.portauthority.network.Network; import com.aaronjwood.portauthority.network.Wireless; import com.aaronjwood.portauthority.parser.OuiParser; import com.aaronjwood.portauthority.parser.PortParser; @@ -87,6 +87,7 @@ public final class MainActivity extends AppCompatActivity implements MainAsyncRe private ProgressDialog scanProgressDialog; private final Handler signalHandler = new Handler(); private Handler scanHandler; + private ConnectivityManager connMgr; private final IntentFilter intentFilter = new IntentFilter(); private HostAdapter hostAdapter; private List hosts = Collections.synchronizedList(new ArrayList<>()); @@ -95,24 +96,26 @@ public final class MainActivity extends AppCompatActivity implements MainAsyncRe private DownloadAsyncTask portTask; private boolean sortAscending; - private final BroadcastReceiver receiver = new BroadcastReceiver() { + private final ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() { + @Override + public void onAvailable(@NonNull android.net.Network network) { + getNetworkInfo(true); + } - /** - * Detect if a network connection has been lost or established - * @param context - * @param intent - */ @Override - public void onReceive(Context context, Intent intent) { - NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); - if (info == null) { - return; - } + public void onLost(@NonNull android.net.Network network) { + getNetworkInfo(false); + } + }; - getNetworkInfo(info); + private void registerNetworkCallback() { + connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); + if (connMgr == null) { + return; } - }; + connMgr.registerNetworkCallback(new NetworkRequest.Builder().build(), networkCallback); + } /** * Activity created @@ -145,7 +148,8 @@ protected void onCreate(Bundle savedInstanceState) { setupDrawer(); setupHostDiscovery(); - intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); + intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); + registerNetworkCallback(); ssidAccess(context); } @@ -258,27 +262,39 @@ public void setupMac() { TextView macAddress = findViewById(R.id.deviceMacAddress); TextView macVendor = findViewById(R.id.deviceMacVendor); + String mac; try { - if (!wifi.isEnabled()) { - macAddress.setText(R.string.wifiDisabled); - macVendor.setText(R.string.wifiDisabled); - + mac = Network.getMacAddress(getApplicationContext()); + if (mac == null) { + runOnUiThread(() -> { + macAddress.setText(R.string.networkNotFound); + macVendor.setText(R.string.networkNotFound); + }); return; } - String mac = wifi.getMacAddress(); - macAddress.setText(mac); - - String vendor = Host.findMacVendor(mac, db); - macVendor.setText(vendor); - } catch (UnknownHostException | SocketException | Wireless.NoWifiManagerException e) { - macAddress.setText(R.string.noWifiConnection); - macVendor.setText(R.string.noWifiConnection); + } catch (SocketException | Network.NoConnectivityManagerException | UnknownHostException e) { + runOnUiThread(() -> { + macAddress.setText(R.string.notConnectedLan); + macVendor.setText(R.string.notConnectedLan); + }); + return; } catch (SQLiteException | UnsupportedOperationException e) { - macVendor.setText(R.string.getMacVendorFailed); - } catch (Wireless.NoWifiInterface e) { - macAddress.setText(R.string.noWifiInterface); + runOnUiThread(() -> macVendor.setText(R.string.getMacVendorFailed)); + return; + } catch (Network.NetworkNotFoundException e) { + runOnUiThread(() -> { + macAddress.setText(R.string.networkNotFound); + macVendor.setText(R.string.networkNotFound); + }); + return; } + + String vendor = Host.findMacVendor(mac, db); + runOnUiThread(() -> { + macAddress.setText(mac); + macVendor.setText(vendor); + }); } /** @@ -296,28 +312,28 @@ public void onClick(View v) { Resources resources = getResources(); Context context = getApplicationContext(); try { - if (!wifi.isEnabled()) { - Errors.showError(context, resources.getString(R.string.wifiDisabled)); + if (!Network.isConnected(context)) { + Errors.showError(context, resources.getString(R.string.notConnectedLan)); return; } - - if (!wifi.isConnectedWifi()) { - Errors.showError(context, resources.getString(R.string.notConnectedWifi)); - return; - } - } catch (Wireless.NoWifiManagerException | Wireless.NoConnectivityManagerException e) { - Errors.showError(context, resources.getString(R.string.failedWifiManager)); + } catch (Network.NoConnectivityManagerException e) { + Errors.showError(context, resources.getString(R.string.notConnectedLan)); return; } int numSubnetHosts; + int netmask; try { - numSubnetHosts = wifi.getNumberOfHostsInWifiSubnet(); - } catch (Wireless.NoWifiManagerException e) { - Errors.showError(context, resources.getString(R.string.failedSubnetHosts)); + netmask = Network.getConnectionInfo(context).subnet; + } catch (Network.NoConnectivityManagerException | UnknownHostException e) { + Errors.showError(context, resources.getString(R.string.notConnectedLan)); + return; + } catch (Network.NetworkNotFoundException e) { + Errors.showError(context, resources.getString(R.string.networkNotFound)); return; } + numSubnetHosts = Network.getNumberOfSubnetHosts(netmask); setAnimations(); hosts.clear(); @@ -334,12 +350,24 @@ public void onClick(View v) { scanProgressDialog.show(); try { - Integer ip = wifi.getInternalWifiIpAddress(Integer.class); - new ScanHostsAsyncTask(MainActivity.this, db).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, ip, wifi.getInternalWifiSubnet(), UserPreference.getHostSocketTimeout(context)); + Integer[] ip = new Integer[4]; + String[] parts = Network.getConnectionInfo(context).ip.split("\\."); + for (int i = 0; i < 4; i++) { + ip[i] = Integer.parseInt(parts[i]); + } + + int ipNum = 0; + for (int i = 0; i < 4; i++) { + ipNum += ip[i] << (24 - (8 * i)); + } + + new ScanHostsAsyncTask(MainActivity.this, db).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, ipNum, netmask, UserPreference.getHostSocketTimeout(context)); discoverHostsBtn.setAlpha(.3f); discoverHostsBtn.setEnabled(false); - } catch (UnknownHostException | Wireless.NoWifiManagerException e) { - Errors.showError(context, resources.getString(R.string.notConnectedWifi)); + } catch (UnknownHostException | Network.NoConnectivityManagerException e) { + Errors.showError(context, resources.getString(R.string.notConnectedLan)); + } catch (Network.NetworkNotFoundException e) { + Errors.showError(context, resources.getString(R.string.networkNotFound)); } } }); @@ -465,38 +493,30 @@ private void setClip(CharSequence label, String text) { /** * Gets network information about the device and updates various UI elements */ - private void getNetworkInfo(NetworkInfo info) { + private void getNetworkInfo(boolean isConnected) { setupMac(); getExternalIp(); final Resources resources = getResources(); final Context context = getApplicationContext(); try { - boolean enabled = wifi.isEnabled(); - if (!info.isConnected() || !enabled) { + if (!isConnected) { signalHandler.removeCallbacksAndMessages(null); - internalIp.setText(Wireless.getInternalMobileIpAddress()); } - if (!enabled) { - signalStrength.setText(R.string.wifiDisabled); - ssid.setText(R.string.wifiDisabled); - bssid.setText(R.string.wifiDisabled); - + getInternalIp(); + if (!wifi.isEnabled()) { + runOnUiThread(() -> { + signalStrength.setText(R.string.wifiDisabled); + ssid.setText(R.string.wifiDisabled); + bssid.setText(R.string.wifiDisabled); + }); return; } } catch (Wireless.NoWifiManagerException e) { Errors.showError(context, resources.getString(R.string.failedWifiManager)); } - if (!info.isConnected()) { - signalStrength.setText(R.string.noWifiConnection); - ssid.setText(R.string.noWifiConnection); - bssid.setText(R.string.noWifiConnection); - - return; - } - signalHandler.postDelayed(new Runnable() { @Override public void run() { @@ -520,8 +540,6 @@ public void run() { } }, 0); - getInternalIp(); - String wifiSsid; String wifiBssid; try { @@ -586,12 +604,12 @@ public void onItemClick(AdapterView parent, View view, int position, long id) Button wakeUp = wolDialog.findViewById(R.id.wolWake); wakeUp.setOnClickListener(v -> { try { - if (!wifi.isConnectedWifi()) { + if (!Network.isConnected(getApplicationContext())) { Errors.showError(getApplicationContext(), getResources().getString(R.string.notConnectedLan)); return; } } catch (Wireless.NoConnectivityManagerException e) { - Errors.showError(getApplicationContext(), getResources().getString(R.string.failedWifiManager)); + Errors.showError(getApplicationContext(), getResources().getString(R.string.notConnectedLan)); return; } @@ -651,11 +669,14 @@ public void onItemClick(AdapterView parent, View view, int position, long id) */ private void getInternalIp() { try { - int netmask = wifi.getInternalWifiSubnet(); - String internalIpWithSubnet = wifi.getInternalWifiIpAddress(String.class) + "/" + netmask; - internalIp.setText(internalIpWithSubnet); - } catch (UnknownHostException | Wireless.NoWifiManagerException e) { + Context ctx = getApplicationContext(); + Network.ConnectionInfo connInfo = Network.getConnectionInfo(ctx); + String internalIpWithSubnet = connInfo.ip + "/" + connInfo.subnet; + runOnUiThread(() -> internalIp.setText(internalIpWithSubnet)); + } catch (UnknownHostException | Network.NoConnectivityManagerException e) { Errors.showError(getApplicationContext(), getResources().getString(R.string.notConnectedLan)); + } catch (Network.NetworkNotFoundException e) { + Errors.showError(getApplicationContext(), getResources().getString(R.string.networkNotFound)); } } @@ -673,7 +694,7 @@ private void getExternalIp() { ip.setVisibility(View.VISIBLE); if (cachedWanIp == null) { - wifi.getExternalIpAddress(this); + Network.getWanIp(this); } } else { label.setVisibility(View.GONE); @@ -688,7 +709,8 @@ private void getExternalIp() { public void onPause() { super.onPause(); - unregisterReceiver(receiver); + + connMgr.unregisterNetworkCallback(networkCallback); signalHandler.removeCallbacksAndMessages(null); if (scanProgressDialog != null) { @@ -715,7 +737,7 @@ public void onPause() { public void onResume() { super.onResume(); - registerReceiver(receiver, intentFilter); + registerNetworkCallback(); } /** @@ -767,6 +789,12 @@ public void onRestoreInstanceState(Bundle savedState) { @Override public void processFinish(final Host h, final AtomicInteger i) { scanHandler.post(() -> { + if (i.get() == 0) { + discoverHostsBtn.setAlpha(1); + discoverHostsBtn.setEnabled(true); + return; + } + hosts.add(h); hostAdapter.sort((lhs, rhs) -> { int leftIp = ByteBuffer.wrap(lhs.getAddress()).getInt(); diff --git a/app/src/main/java/com/aaronjwood/portauthority/async/ScanHostsAsyncTask.java b/app/src/main/java/com/aaronjwood/portauthority/async/ScanHostsAsyncTask.java index a99a78f7..16f0796d 100644 --- a/app/src/main/java/com/aaronjwood/portauthority/async/ScanHostsAsyncTask.java +++ b/app/src/main/java/com/aaronjwood/portauthority/async/ScanHostsAsyncTask.java @@ -194,6 +194,11 @@ protected final void onProgressUpdate(Void... params) { } } + if (pairs.size() == 0) { + activity.processFinish(null, numHosts); + return; + } + numHosts.addAndGet(pairs.size()); for (Pair pair : pairs) { String ip = pair.first; diff --git a/app/src/main/java/com/aaronjwood/portauthority/network/Network.java b/app/src/main/java/com/aaronjwood/portauthority/network/Network.java new file mode 100644 index 00000000..950e9751 --- /dev/null +++ b/app/src/main/java/com/aaronjwood/portauthority/network/Network.java @@ -0,0 +1,126 @@ +package com.aaronjwood.portauthority.network; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.LinkAddress; +import android.net.LinkProperties; +import android.net.NetworkInfo; + +import com.aaronjwood.portauthority.async.WanIpAsyncTask; +import com.aaronjwood.portauthority.response.MainAsyncResponse; + +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.util.List; + +public class Network { + + public static class NoConnectivityManagerException extends Exception { + } + + public static class NetworkNotFoundException extends Exception { + } + + public static class ConnectionInfo { + public int subnet; + public String ip; + public String iface; + } + + protected final Context context; + + Network(Context context) { + this.context = context; + } + + /** + * Gets the Android connectivity manager in the context of the current activity + * + * @return Connectivity manager + */ + protected static ConnectivityManager getConnectivityManager(Context context) throws NoConnectivityManagerException { + ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + if (manager == null) { + throw new NoConnectivityManagerException(); + } + + return manager; + } + + /** + * Checks if there is an active connection. + * + * @return True if connected or in the process of connecting, false otherwise. + * @throws NoConnectivityManagerException + */ + public static boolean isConnected(Context context) throws NoConnectivityManagerException { + NetworkInfo info = getConnectivityManager(context).getActiveNetworkInfo(); + return info != null && info.isConnectedOrConnecting(); + } + + public static ConnectionInfo getConnectionInfo(Context context) throws NoConnectivityManagerException, UnknownHostException, NetworkNotFoundException { + ConnectivityManager cm = getConnectivityManager(context); + android.net.Network activeNet = cm.getActiveNetwork(); + LinkProperties linkProperties = cm.getLinkProperties(activeNet); + if (activeNet == null || linkProperties == null) { + throw new NetworkNotFoundException(); + } + + List addresses = linkProperties.getLinkAddresses(); + for (LinkAddress address : addresses) { + InetAddress addr = address.getAddress(); + if (!addr.isLoopbackAddress() && !addr.isLinkLocalAddress()) { + ConnectionInfo info = new ConnectionInfo(); + info.ip = activeNet.getByName(addr.getHostAddress()).getHostAddress(); + info.iface = linkProperties.getInterfaceName(); + info.subnet = address.getPrefixLength(); + return info; + } + } + + throw new NetworkNotFoundException(); + } + + public static String getMacAddress(Context context) throws NoConnectivityManagerException, SocketException, NetworkNotFoundException, UnknownHostException { + NetworkInterface iface = NetworkInterface.getByName(getConnectionInfo(context).iface); + byte[] mac = iface.getHardwareAddress(); + if (mac == null) { + return null; + } + + StringBuilder buf = new StringBuilder(); + for (byte aMac : mac) { + buf.append(String.format("%02x:", aMac)); + } + + if (buf.length() > 0) { + buf.deleteCharAt(buf.length() - 1); + } + + return buf.toString(); + } + + /** + * Returns the number of hosts in the subnet. + * + * @return Number of hosts. + */ + public static int getNumberOfSubnetHosts(int subnet) { + double bitsLeft = 32.0d - (double) subnet; + double hosts = Math.pow(2.0d, bitsLeft) - 2.0d; + + return (int) hosts; + } + + /** + * Gets the device's external (WAN) IP address + * + * @param delegate Called when the external IP address has been fetched + */ + public static void getWanIp(MainAsyncResponse delegate) { + new WanIpAsyncTask(delegate).execute(); + } + +} diff --git a/app/src/main/java/com/aaronjwood/portauthority/network/Wireless.java b/app/src/main/java/com/aaronjwood/portauthority/network/Wireless.java index 677f93eb..cb23037c 100644 --- a/app/src/main/java/com/aaronjwood/portauthority/network/Wireless.java +++ b/app/src/main/java/com/aaronjwood/portauthority/network/Wireless.java @@ -1,88 +1,21 @@ package com.aaronjwood.portauthority.network; import android.content.Context; -import android.net.ConnectivityManager; -import android.net.DhcpInfo; -import android.net.NetworkInfo; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; -import android.os.AsyncTask; -import com.aaronjwood.portauthority.async.WanIpAsyncTask; -import com.aaronjwood.portauthority.response.MainAsyncResponse; - -import java.math.BigInteger; -import java.net.Inet4Address; -import java.net.InetAddress; -import java.net.InterfaceAddress; -import java.net.NetworkInterface; -import java.net.SocketException; -import java.net.UnknownHostException; -import java.nio.ByteOrder; -import java.util.Enumeration; - -public class Wireless { - - private final Context context; +public class Wireless extends Network { public static class NoWifiManagerException extends Exception { } - public static class NoConnectivityManagerException extends Exception { - } - - public static class NoWifiInterface extends Exception { - - } - /** * Constructor to set the activity for context * * @param context The activity to use for context */ public Wireless(Context context) { - this.context = context; - } - - /** - * Gets the MAC address of the device - * - * @return MAC address - */ - public String getMacAddress() throws UnknownHostException, SocketException, NoWifiManagerException, NoWifiInterface { - String address = getWifiInfo().getMacAddress(); //Won't work on Android 6+ https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id - if (!"02:00:00:00:00:00".equals(address)) { - return address; - } - - //This should get us the device's MAC address on Android 6+ - NetworkInterface iface = NetworkInterface.getByInetAddress(getWifiInetAddress()); - if (iface == null) { - throw new NoWifiInterface(); - } - - byte[] mac = iface.getHardwareAddress(); - - StringBuilder buf = new StringBuilder(); - for (byte aMac : mac) { - buf.append(String.format("%02x:", aMac)); - } - - if (buf.length() > 0) { - buf.deleteCharAt(buf.length() - 1); - } - - return buf.toString(); - } - - /** - * Gets the device's wireless address - * - * @return Wireless address - */ - private InetAddress getWifiInetAddress() throws UnknownHostException, NoWifiManagerException { - String ipAddress = getInternalWifiIpAddress(String.class); - return InetAddress.getByName(ipAddress); + super(context); } /** @@ -117,120 +50,6 @@ public String getSSID() throws NoWifiManagerException { return ssid; } - - /** - * Gets the device's internal LAN IP address associated with the WiFi network - * - * @param type - * @param - * @return Local WiFi network LAN IP address - */ - public T getInternalWifiIpAddress(Class type) throws UnknownHostException, NoWifiManagerException { - int ip = getWifiInfo().getIpAddress(); - - //Endianness can be a potential issue on some hardware - if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) { - ip = Integer.reverseBytes(ip); - } - - byte[] ipByteArray = BigInteger.valueOf(ip).toByteArray(); - - - if (type.isInstance("")) { - return type.cast(InetAddress.getByAddress(ipByteArray).getHostAddress()); - } else { - return type.cast(new BigInteger(InetAddress.getByAddress(ipByteArray).getAddress()).intValue()); - } - - } - - /** - * Gets the Wifi Manager DHCP information and returns the Netmask of the internal Wifi Network as an int - * - * @return Internal Wifi Subnet Netmask - */ - public int getInternalWifiSubnet() throws NoWifiManagerException { - WifiManager wifiManager = getWifiManager(); - DhcpInfo dhcpInfo = wifiManager.getDhcpInfo(); - if (dhcpInfo == null) { - return 0; - } - - int netmask = Integer.bitCount(dhcpInfo.netmask); - /* - * Workaround for #82477 - * https://code.google.com/p/android/issues/detail?id=82477 - * If dhcpInfo returns a subnet that cannot exist, then - * look up the Network interface instead. - */ - if (netmask < 4 || netmask > 32) { - try { - InetAddress inetAddress = getWifiInetAddress(); - NetworkInterface networkInterface = NetworkInterface.getByInetAddress(inetAddress); - if (networkInterface == null) { - return 0; - } - - for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) { - if (inetAddress != null && inetAddress.equals(address.getAddress())) { - return address.getNetworkPrefixLength(); // This returns a short of the CIDR notation. - } - } - } catch (SocketException | UnknownHostException ignored) { - } - } - - return netmask; - } - - - /** - * Returns the number of hosts in the subnet. - * - * @return Number of hosts as an integer. - */ - public int getNumberOfHostsInWifiSubnet() throws NoWifiManagerException { - double subnet = getInternalWifiSubnet(); - double hosts; - double bitsLeft = 32.0d - subnet; - hosts = Math.pow(2.0d, bitsLeft) - 2.0d; - - return (int) hosts; - } - - - /** - * Gets the device's internal LAN IP address associated with the cellular network - * - * @return Local cellular network LAN IP address - */ - public static String getInternalMobileIpAddress() { - try { - for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en != null && en.hasMoreElements(); ) { - NetworkInterface intf = en.nextElement(); - for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) { - InetAddress inetAddress = enumIpAddr.nextElement(); - if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) { - return inetAddress.getHostAddress(); - } - } - } - } catch (SocketException ex) { - return "Unknown"; - } - - return "Unknown"; - } - - /** - * Gets the device's external (WAN) IP address - * - * @param delegate Called when the external IP address has been fetched - */ - public void getExternalIpAddress(MainAsyncResponse delegate) { - new WanIpAsyncTask(delegate).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - } - /** * Gets the current link speed of the wireless network that the device is connected to * @@ -240,16 +59,6 @@ public int getLinkSpeed() throws NoWifiManagerException { return getWifiInfo().getLinkSpeed(); } - /** - * Determines if the device is connected to a WiFi network or not - * - * @return True if the device is connected, false if it isn't - */ - public boolean isConnectedWifi() throws NoConnectivityManagerException { - NetworkInfo info = getNetworkInfo(ConnectivityManager.TYPE_WIFI); - return info != null && info.isConnectedOrConnecting(); - } - /** * Determines if WiFi is enabled on the device or not * @@ -282,27 +91,4 @@ private WifiInfo getWifiInfo() throws NoWifiManagerException { return getWifiManager().getConnectionInfo(); } - /** - * Gets the Android connectivity manager in the context of the current activity - * - * @return Connectivity manager - */ - private ConnectivityManager getConnectivityManager() throws NoConnectivityManagerException { - ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - if (manager == null) { - throw new NoConnectivityManagerException(); - } - - return manager; - } - - /** - * Gets the Android network information in the context of the current activity - * - * @return Network information - */ - private NetworkInfo getNetworkInfo(int type) throws NoConnectivityManagerException { - return getConnectivityManager().getNetworkInfo(type); - } - } diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index e72de944..44f3d074 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -29,7 +29,6 @@ Récupération des enregistrements DNS... Entrez un domaine et sélectionnez un type d\'enregistrement Vous n\'êtes pas connecté au réseau local - Vous n\'êtes connecté à aucun réseau Wi-Fi! %1$ddBm/%2$dMbps Recherche d\'Hôtes %1$d hôtes dans votre sous-réseau diff --git a/app/src/main/res/values-nl/strings.xml b/app/src/main/res/values-nl/strings.xml index 6f7a6111..8c92ed94 100644 --- a/app/src/main/res/values-nl/strings.xml +++ b/app/src/main/res/values-nl/strings.xml @@ -30,7 +30,6 @@ Ophalen van DNS-records… Voer een domein in en selecteer een recordtype Je bent niet verbonden met een lokaal netwerk - Je bent niet verbonden met een wifinetwerk! %1$ddBm/%2$dMbps Scannen naar hosts %1$d hosts in je subnet diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index 51690320..f1334700 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -30,7 +30,6 @@ Extraindo registros do DNS … Digite um domínio e selecione um tipo de registro Você não está conectado à rede local - Você não está conectado à rede WiFi %1$ddBm/%2$dMbps Procurando os servidores %1$d hosts na sua sub-rede diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 940cb9e1..b546fab5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -30,7 +30,6 @@ Fetching DNS records… Enter a domain and select a record type You\'re not connected to the local network - You\'re not connected to a WiFi network! %1$ddBm/%2$dMbps Scanning For Hosts %1$d hosts in your subnet @@ -51,6 +50,7 @@ Failed to get BSSID Failed to get WifiManager for this device No WiFi interface found + Network not found Please be aware that running port scans against public servers is normally considered malicious. Additionally, running these kinds of scans over your carrier\'s network instead of your own WiFi connection will be slower and will sometimes produce inaccurate results due to traffic shaping.