2 * Copyright 2019,2021-2022 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.coroutines
22 import androidx.fragment.app.FragmentManager
23 import com.stoutner.privacybrowser.helpers.CheckPinnedMismatchHelper
24 import com.stoutner.privacybrowser.views.NestedScrollWebView
26 import kotlinx.coroutines.CoroutineScope
27 import kotlinx.coroutines.Dispatchers
28 import kotlinx.coroutines.launch
29 import kotlinx.coroutines.withContext
31 import java.lang.StringBuilder
32 import java.net.InetAddress
33 import java.net.UnknownHostException
35 object GetHostIpAddressesCoroutine {
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.
44 // Initialize an IP address string builder.
45 val ipAddresses = StringBuilder()
47 // Get an array with all the IP addresses for the domain.
48 val inetAddressesArray = InetAddress.getAllByName(domainName)
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")
57 // Add the IP address to the string builder.
58 ipAddresses.append(inetAddress.hostAddress)
61 // Store the IP addresses.
62 nestedScrollWebView.currentIpAddresses = ipAddresses.toString()
63 } catch (exception: UnknownHostException) {
69 // Checked for pinned mismatches if there is pinned information and it is not ignored.
70 if ((nestedScrollWebView.hasPinnedSslCertificate() || nestedScrollWebView.pinnedIpAddresses != "") && !nestedScrollWebView.ignorePinnedDomainInformation) {
71 CheckPinnedMismatchHelper.checkPinnedMismatch(nestedScrollWebView, supportFragmentManager, pinnedMismatchString)