DomainUtils.kt

1.2 KB · raw

1package com.peachpasswords.shared23import okhttp3.internal.publicsuffix.PublicSuffixDatabase45fun extractDomain(url: String): String? {6    return try {7        val normalized = if (url.startsWith("http")) url else "https://$url"8        val host = java.net.URL(normalized).host.lowercase()9        PublicSuffixDatabase.get().getEffectiveTldPlusOne(host)10    } catch (_: Exception) {11        null12    }13}1415fun getDisplayDomain(url: String): String {16    return try {17        val normalized = if (url.startsWith("http")) url else "https://$url"18        java.net.URL(normalized).host19    } catch (_: Exception) {20        url21    }22}2324/**25 * Browser parity with `shared/domain.ts::detectHomograph`: true when the host26 * contains non-ASCII characters or a punycode `xn--` label. This is warning-only;27 * matching/keying remains handled by [extractDomain].28 */29fun detectHomograph(url: String): Boolean {30    return try {31        val normalized = if (url.contains("://")) url else "https://$url"32        val host = java.net.URL(normalized).host33        host.any { it.code > 0x7F } || host.split('.').any { it.startsWith("xn--", ignoreCase = true) }34    } catch (_: Exception) {35        false36    }37}