2 * Copyright © 2019 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
6 * Privacy Browser is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.asynctasks;
22 import android.app.Activity;
23 import android.os.AsyncTask;
25 import androidx.fragment.app.FragmentManager;
27 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
28 import com.stoutner.privacybrowser.helpers.CheckPinnedMismatchHelper;
29 import com.stoutner.privacybrowser.views.NestedScrollWebView;
31 import java.lang.ref.WeakReference;
32 import java.net.InetAddress;
33 import java.net.UnknownHostException;
35 // This must run asynchronously because it involves a network request. `String` declares the parameters. `Void` does not declare progress units. `String` contains the results.
36 public class GetHostIpAddresses extends AsyncTask<String, Void, String> {
37 // The weak references are used to determine if the activity have disappeared while the AsyncTask is running.
38 private final WeakReference<Activity> activityWeakReference;
39 private final WeakReference<FragmentManager> fragmentManagerWeakReference;
40 private final WeakReference<NestedScrollWebView> nestedScrollWebViewWeakReference;
42 public GetHostIpAddresses(Activity activity, FragmentManager fragmentManager, NestedScrollWebView nestedScrollWebView) {
43 // Populate the weak references.
44 activityWeakReference = new WeakReference<>(activity);
45 fragmentManagerWeakReference = new WeakReference<>(fragmentManager);
46 nestedScrollWebViewWeakReference = new WeakReference<>(nestedScrollWebView);
50 protected String doInBackground(String... domainName) {
51 // Get a handles for the weak references.
52 Activity activity = activityWeakReference.get();
53 FragmentManager fragmentManager = fragmentManagerWeakReference.get();
54 NestedScrollWebView nestedScrollWebView = nestedScrollWebViewWeakReference.get();
56 // Abort if the activity or its components are gone.
57 if ((activity == null) || activity.isFinishing() || fragmentManager == null || nestedScrollWebView == null) {
58 // Return an empty spannable string builder.
62 // Initialize an IP address string builder.
63 StringBuilder ipAddresses = new StringBuilder();
65 // Get an array with the IP addresses for the host.
67 // Get an array with all the IP addresses for the domain.
68 InetAddress[] inetAddressesArray = InetAddress.getAllByName(domainName[0]);
70 // Add each IP address to the string builder.
71 for (InetAddress inetAddress : inetAddressesArray) {
72 if (ipAddresses.length() == 0) { // This is the first IP address.
73 // Add the IP address to the string builder.
74 ipAddresses.append(inetAddress.getHostAddress());
75 } else { // This is not the first IP address.
76 // Add a line break to the string builder first.
77 ipAddresses.append("\n");
79 // Add the IP address to the string builder.
80 ipAddresses.append(inetAddress.getHostAddress());
83 } catch (UnknownHostException exception) {
88 return ipAddresses.toString();
91 // `onPostExecute()` operates on the UI thread.
93 protected void onPostExecute(String ipAddresses) {
94 // Get a handle for the activity and the nested scroll WebView.
95 Activity activity = activityWeakReference.get();
96 FragmentManager fragmentManager = fragmentManagerWeakReference.get();
97 NestedScrollWebView nestedScrollWebView = nestedScrollWebViewWeakReference.get();
99 // Abort if the activity or its components are gone.
100 if ((activity == null) || activity.isFinishing() || fragmentManager == null || nestedScrollWebView == null) {
104 // Store the IP addresses.
105 nestedScrollWebView.setCurrentIpAddresses(ipAddresses);
107 // Checked for pinned mismatches if the WebView is not loading a URL, pinned information is not ignored, and there is pinned information.
108 if ((nestedScrollWebView.getProgress() == 100) && !nestedScrollWebView.ignorePinnedDomainInformation() && (nestedScrollWebView.hasPinnedSslCertificate() || nestedScrollWebView.hasPinnedIpAddresses())) {
109 CheckPinnedMismatchHelper.checkPinnedMismatch(fragmentManager, nestedScrollWebView);