]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/coroutines/GetHostIpAddressesCoroutine.kt
Migrate the remaining classes to Kotlin. https://redmine.stoutner.com/issues/989
[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     fun checkPinnedMismatch(domainName: String, nestedScrollWebView: NestedScrollWebView, supportFragmentManager: FragmentManager, pinnedMismatchString: String) {
85         // Get the IP addresses using a coroutine.
86         CoroutineScope(Dispatchers.Main).launch {
87             // Get the IP addresses on the IO thread.
88             withContext(Dispatchers.IO) {
89                 // Get an array with the IP addresses for the host.
90                 try {
91                     // Initialize an IP address string builder.
92                     val ipAddressesStringBuilder = StringBuilder()
93
94                     // Get an array with all the IP addresses for the domain.
95                     val inetAddressesArray = InetAddress.getAllByName(domainName)
96
97                     // Add each IP address to the string builder.
98                     for (inetAddress in inetAddressesArray) {
99                         // Add a line break to the string builder if this is not the first IP address.
100                         if (ipAddressesStringBuilder.isNotEmpty()) {
101                             ipAddressesStringBuilder.append("\n")
102                         }
103
104                         // Add the IP address to the string builder.
105                         ipAddressesStringBuilder.append(inetAddress.hostAddress)
106                     }
107
108                     // Store the IP addresses.
109                     nestedScrollWebView.currentIpAddresses = ipAddressesStringBuilder.toString()
110
111                     // 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.
112                     withContext(Dispatchers.Main) {
113                         if ((nestedScrollWebView.hasPinnedSslCertificate() || nestedScrollWebView.pinnedIpAddresses.isNotEmpty()) && !nestedScrollWebView.ignorePinnedDomainInformation) {
114                             CheckPinnedMismatchHelper.checkPinnedMismatch(nestedScrollWebView, supportFragmentManager, pinnedMismatchString)
115                         }
116                     }
117                 } catch (exception: UnknownHostException) {
118                     // Do nothing.
119                 }
120             }
121         }
122     }
123 }