]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/coroutines/GetHostIpAddressesCoroutine.kt
Remove AsyncTask from SSLCertificateErrorDialog. https://redmine.stoutner.com/issues/987
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / coroutines / GetHostIpAddressesCoroutine.kt
1 /*
2  * Copyright 2019,2021-2023 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
5  *
6  * Privacy Browser Android 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 Android 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 Android.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacybrowser.coroutines
21
22 import android.text.SpannableStringBuilder
23 import android.text.Spanned
24 import android.text.style.ForegroundColorSpan
25 import android.widget.TextView
26
27 import androidx.fragment.app.FragmentManager
28
29 import com.stoutner.privacybrowser.helpers.CheckPinnedMismatchHelper
30 import com.stoutner.privacybrowser.views.NestedScrollWebView
31
32 import kotlinx.coroutines.CoroutineScope
33 import kotlinx.coroutines.Dispatchers
34 import kotlinx.coroutines.launch
35 import kotlinx.coroutines.withContext
36
37 import java.lang.StringBuilder
38 import java.net.InetAddress
39 import java.net.UnknownHostException
40
41 object GetHostIpAddressesCoroutine {
42     fun getAddresses(domainName: String, ipAddressesLabel: String, blueColorSpan: ForegroundColorSpan, ipAddressesTextView: TextView) {
43         // Get the IP addresses using a coroutine.
44         CoroutineScope(Dispatchers.Main).launch {
45             // Get the IP addresses on the IO thread.
46             withContext(Dispatchers.IO) {
47                 // Get an array with the IP addresses for the host.
48                 try {
49                     // Initialize an IP address string builder.
50                     val ipAddressesStringBuilder = StringBuilder()
51
52                     // Get an array with all the IP addresses for the domain.
53                     val inetAddressesArray = InetAddress.getAllByName(domainName)
54
55                     // Add each IP address to the string builder.
56                     for (inetAddress in inetAddressesArray) {
57                         // Add a line break to the string builder if this is not the first IP address.
58                         if (ipAddressesStringBuilder.isNotEmpty()) {
59                             ipAddressesStringBuilder.append("\n")
60                         }
61
62                         // Add the IP address to the string builder.
63                         ipAddressesStringBuilder.append(inetAddress.hostAddress)
64                     }
65
66                     // Create a spannable string builder.
67                     val addressesStringBuilder = SpannableStringBuilder(ipAddressesLabel + ipAddressesStringBuilder)
68
69                     // Set the string builder to display the certificate information in blue.  `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
70                     addressesStringBuilder.setSpan(blueColorSpan, ipAddressesLabel.length, addressesStringBuilder.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
71
72                     // Populate the IP addresses text view on the UI thread.
73                     withContext(Dispatchers.Main) {
74                         // Populate the IP addresses text view.
75                         ipAddressesTextView.text = addressesStringBuilder
76                     }
77                 } catch (exception: UnknownHostException) {
78                     // Do nothing.
79                 }
80             }
81         }
82     }
83
84     @JvmStatic
85     fun checkPinnedMismatch(domainName: String, nestedScrollWebView: NestedScrollWebView, supportFragmentManager: FragmentManager, pinnedMismatchString: String) {
86         // Get the IP addresses using a coroutine.
87         CoroutineScope(Dispatchers.Main).launch {
88             // Get the IP addresses on the IO thread.
89             withContext(Dispatchers.IO) {
90                 // Get an array with the IP addresses for the host.
91                 try {
92                     // Initialize an IP address string builder.
93                     val ipAddressesStringBuilder = StringBuilder()
94
95                     // Get an array with all the IP addresses for the domain.
96                     val inetAddressesArray = InetAddress.getAllByName(domainName)
97
98                     // Add each IP address to the string builder.
99                     for (inetAddress in inetAddressesArray) {
100                         // Add a line break to the string builder if this is not the first IP address.
101                         if (ipAddressesStringBuilder.isNotEmpty()) {
102                             ipAddressesStringBuilder.append("\n")
103                         }
104
105                         // Add the IP address to the string builder.
106                         ipAddressesStringBuilder.append(inetAddress.hostAddress)
107                     }
108
109                     // Store the IP addresses.
110                     nestedScrollWebView.currentIpAddresses = ipAddressesStringBuilder.toString()
111
112                     // Checked for pinned mismatches if there is pinned information and it is not ignored.  This must be done on the UI thread because checking the pinned mismatch interacts with the WebView.
113                     withContext(Dispatchers.Main) {
114                         if ((nestedScrollWebView.hasPinnedSslCertificate() || nestedScrollWebView.pinnedIpAddresses.isNotEmpty()) && !nestedScrollWebView.ignorePinnedDomainInformation) {
115                             CheckPinnedMismatchHelper.checkPinnedMismatch(nestedScrollWebView, supportFragmentManager, pinnedMismatchString)
116                         }
117                     }
118                 } catch (exception: UnknownHostException) {
119                     // Do nothing.
120                 }
121             }
122         }
123     }
124 }