]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/asynctasks/GetHostIpAddresses.java
811eb7d322192a445a55c1ca5d3b08f98972bd45
[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.helpers.CheckPinnedMismatchHelper;
28 import com.stoutner.privacybrowser.views.NestedScrollWebView;
29
30 import java.lang.ref.WeakReference;
31 import java.net.InetAddress;
32 import java.net.UnknownHostException;
33
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;
40
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);
46     }
47
48     @Override
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();
54
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.
58             return "";
59         }
60
61         // Initialize an IP address string builder.
62         StringBuilder ipAddresses = new StringBuilder();
63
64         // Get an array with the IP addresses for the host.
65         try {
66             // Get an array with all the IP addresses for the domain.
67             InetAddress[] inetAddressesArray = InetAddress.getAllByName(domainName[0]);
68
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");
74                 }
75
76                 // Add the IP address to the string builder.
77                 ipAddresses.append(inetAddress.getHostAddress());
78             }
79         } catch (UnknownHostException exception) {
80             // Do nothing.
81         }
82
83         // Return the string.
84         return ipAddresses.toString();
85     }
86
87     // `onPostExecute()` operates on the UI thread.
88     @Override
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();
94
95         // Abort if the activity or its components are gone.
96         if ((activity == null) || activity.isFinishing() || fragmentManager == null || nestedScrollWebView == null) {
97             return;
98         }
99
100         // Store the IP addresses.
101         nestedScrollWebView.setCurrentIpAddresses(ipAddresses);
102
103         // Checked for pinned mismatches if there is pinned information and it is not ignored.
104         if ((nestedScrollWebView.hasPinnedSslCertificate() || nestedScrollWebView.hasPinnedIpAddresses()) && !nestedScrollWebView.ignorePinnedDomainInformation()) {
105             CheckPinnedMismatchHelper.checkPinnedMismatch(fragmentManager, nestedScrollWebView);
106         }
107     }
108 }