]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/asynctasks/GetHostIpAddresses.java
66111de7c351185668e5ffbcc71082beac11f118
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / asynctasks / GetHostIpAddresses.java
1 /*
2  * Copyright © 2019 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.asynctasks;
21
22 import android.app.Activity;
23 import android.os.AsyncTask;
24
25 import androidx.fragment.app.FragmentManager;
26
27 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
28 import com.stoutner.privacybrowser.helpers.CheckPinnedMismatchHelper;
29 import com.stoutner.privacybrowser.views.NestedScrollWebView;
30
31 import java.lang.ref.WeakReference;
32 import java.net.InetAddress;
33 import java.net.UnknownHostException;
34
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;
41
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);
47     }
48
49     @Override
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();
55
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.
59             return "";
60         }
61
62         // Initialize an IP address string builder.
63         StringBuilder ipAddresses = new StringBuilder();
64
65         // Get an array with the IP addresses for the host.
66         try {
67             // Get an array with all the IP addresses for the domain.
68             InetAddress[] inetAddressesArray = InetAddress.getAllByName(domainName[0]);
69
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");
78
79                     // Add the IP address to the string builder.
80                     ipAddresses.append(inetAddress.getHostAddress());
81                 }
82             }
83         } catch (UnknownHostException exception) {
84             // Do nothing.
85         }
86
87         // Return the string.
88         return ipAddresses.toString();
89     }
90
91     // `onPostExecute()` operates on the UI thread.
92     @Override
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();
98
99         // Abort if the activity or its components are gone.
100         if ((activity == null) || activity.isFinishing() || fragmentManager == null || nestedScrollWebView == null) {
101             return;
102         }
103
104         // Store the IP addresses.
105         nestedScrollWebView.setCurrentIpAddresses(ipAddresses);
106
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);
110         }
111     }
112 }