]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/coroutines/GetHostIpAddressesCoroutine.kt
Update Guide > User Agent. https://redmine.stoutner.com/issues/896
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / coroutines / GetHostIpAddressesCoroutine.kt
1 /*
2  * Copyright 2019,2021-2022 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 androidx.fragment.app.FragmentManager
23 import com.stoutner.privacybrowser.helpers.CheckPinnedMismatchHelper
24 import com.stoutner.privacybrowser.views.NestedScrollWebView
25
26 import kotlinx.coroutines.CoroutineScope
27 import kotlinx.coroutines.Dispatchers
28 import kotlinx.coroutines.launch
29 import kotlinx.coroutines.withContext
30
31 import java.lang.StringBuilder
32 import java.net.InetAddress
33 import java.net.UnknownHostException
34
35 object GetHostIpAddressesCoroutine {
36     @JvmStatic
37     fun getAddresses(domainName: String, nestedScrollWebView: NestedScrollWebView, supportFragmentManager: FragmentManager, pinnedMismatchString: String) {
38         // Get the IP addresses using a coroutine.
39         CoroutineScope(Dispatchers.Main).launch {
40             // Get the IP addresses on the IO thread.
41             withContext(Dispatchers.IO) {
42                 // Get an array with the IP addresses for the host.
43                 try {
44                     // Initialize an IP address string builder.
45                     val ipAddresses = StringBuilder()
46
47                     // Get an array with all the IP addresses for the domain.
48                     val inetAddressesArray = InetAddress.getAllByName(domainName)
49
50                     // Add each IP address to the string builder.
51                     for (inetAddress in inetAddressesArray) {
52                         // Add a line break to the string builder if this is not the first IP address.
53                         if (ipAddresses.isNotEmpty()) {
54                             ipAddresses.append("\n")
55                         }
56
57                         // Add the IP address to the string builder.
58                         ipAddresses.append(inetAddress.hostAddress)
59                     }
60
61                     // Store the IP addresses.
62                     nestedScrollWebView.currentIpAddresses = ipAddresses.toString()
63
64                     // 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.
65                     withContext(Dispatchers.Main) {
66                         if ((nestedScrollWebView.hasPinnedSslCertificate() || nestedScrollWebView.pinnedIpAddresses.isNotEmpty()) && !nestedScrollWebView.ignorePinnedDomainInformation) {
67                             CheckPinnedMismatchHelper.checkPinnedMismatch(nestedScrollWebView, supportFragmentManager, pinnedMismatchString)
68                         }
69                     }
70                 } catch (exception: UnknownHostException) {
71                     // Do nothing.
72                 }
73             }
74         }
75     }
76 }