2 * Copyright © 2019,2021 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.helpers.CheckPinnedMismatchHelper;
28 import com.stoutner.privacybrowser.views.NestedScrollWebView;
30 import java.lang.ref.WeakReference;
31 import java.net.InetAddress;
32 import java.net.UnknownHostException;
34 // This must run asynchronously because it involves a network request. `String` declares the parameters. `Void` does not declare progress units. `String` contains the results.
35 public class GetHostIpAddresses extends AsyncTask<String, Void, String> {
36 // The weak references are used to determine if the activity have disappeared while the AsyncTask is running.
37 private final WeakReference<Activity> activityWeakReference;
38 private final WeakReference<FragmentManager> fragmentManagerWeakReference;
39 private final WeakReference<NestedScrollWebView> nestedScrollWebViewWeakReference;
41 public GetHostIpAddresses(Activity activity, FragmentManager fragmentManager, NestedScrollWebView nestedScrollWebView) {
42 // Populate the weak references.
43 activityWeakReference = new WeakReference<>(activity);
44 fragmentManagerWeakReference = new WeakReference<>(fragmentManager);
45 nestedScrollWebViewWeakReference = new WeakReference<>(nestedScrollWebView);
49 protected String doInBackground(String... domainName) {
50 // Get a handles for the weak references.
51 Activity activity = activityWeakReference.get();
52 FragmentManager fragmentManager = fragmentManagerWeakReference.get();
53 NestedScrollWebView nestedScrollWebView = nestedScrollWebViewWeakReference.get();
55 // Abort if the activity or its components are gone.
56 if ((activity == null) || activity.isFinishing() || fragmentManager == null || nestedScrollWebView == null) {
57 // Return an empty spannable string builder.
61 // Initialize an IP address string builder.
62 StringBuilder ipAddresses = new StringBuilder();
64 // Get an array with the IP addresses for the host.
66 // Get an array with all the IP addresses for the domain.
67 InetAddress[] inetAddressesArray = InetAddress.getAllByName(domainName[0]);
69 // Add each IP address to the string builder.
70 for (InetAddress inetAddress : inetAddressesArray) {
71 // Add a line break to the string builder if this is not the first IP address.
72 if (ipAddresses.length() > 0) {
73 ipAddresses.append("\n");
76 // Add the IP address to the string builder.
77 ipAddresses.append(inetAddress.getHostAddress());
79 } catch (UnknownHostException exception) {
84 return ipAddresses.toString();
87 // `onPostExecute()` operates on the UI thread.
89 protected void onPostExecute(String ipAddresses) {
90 // Get a handle for the activity and the nested scroll WebView.
91 Activity activity = activityWeakReference.get();
92 FragmentManager fragmentManager = fragmentManagerWeakReference.get();
93 NestedScrollWebView nestedScrollWebView = nestedScrollWebViewWeakReference.get();
95 // Abort if the activity or its components are gone.
96 if ((activity == null) || activity.isFinishing() || fragmentManager == null || nestedScrollWebView == null) {
100 // Store the IP addresses.
101 nestedScrollWebView.setCurrentIpAddresses(ipAddresses);
103 // Checked for pinned mismatches if there is pinned information and it is not ignored.
104 if ((nestedScrollWebView.hasPinnedSslCertificate() || !nestedScrollWebView.getPinnedIpAddresses().equals("")) && !nestedScrollWebView.getIgnorePinnedDomainInformation()) {
105 CheckPinnedMismatchHelper.checkPinnedMismatch(activity, fragmentManager, nestedScrollWebView);