what-the-bank/sources/com/google/common/net/InternetDomainName.java

230 lines
7.9 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package com.google.common.net;
import com.google.common.base.Ascii;
import com.google.common.base.CharMatcher;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import java.util.List;
import o.C12167etB;
import o.EnumC16882rzn;
import org.bouncycastle.pqc.math.linearalgebra.Matrix;
/* loaded from: classes2.dex */
public final class InternetDomainName {
private static final CharMatcher DASH_MATCHER;
private static final CharMatcher DIGIT_MATCHER;
private static final CharMatcher LETTER_MATCHER;
private static final int MAX_DOMAIN_PART_LENGTH = 63;
private static final int MAX_LENGTH = 253;
private static final int MAX_PARTS = 127;
private static final int NO_SUFFIX_FOUND = -1;
private static final CharMatcher PART_CHAR_MATCHER;
private final String name;
private final ImmutableList<String> parts;
private final int publicSuffixIndex;
private final int registrySuffixIndex;
private static final CharMatcher DOTS_MATCHER = CharMatcher.anyOf(".。.。");
private static final Splitter DOT_SPLITTER = Splitter.on('.');
private static final Joiner DOT_JOINER = Joiner.on('.');
static {
CharMatcher anyOf = CharMatcher.anyOf("-_");
DASH_MATCHER = anyOf;
CharMatcher inRange = CharMatcher.inRange('0', '9');
DIGIT_MATCHER = inRange;
CharMatcher or = CharMatcher.inRange('a', 'z').or(CharMatcher.inRange('A', Matrix.MATRIX_TYPE_ZERO));
LETTER_MATCHER = or;
PART_CHAR_MATCHER = inRange.or(or).or(anyOf);
}
InternetDomainName(String str) {
String lowerCase = Ascii.toLowerCase(DOTS_MATCHER.replaceFrom((CharSequence) str, '.'));
lowerCase = lowerCase.endsWith(".") ? lowerCase.substring(0, lowerCase.length() - 1) : lowerCase;
Preconditions.checkArgument(lowerCase.length() <= MAX_LENGTH, "Domain name too long: '%s':", lowerCase);
this.name = lowerCase;
ImmutableList<String> copyOf = ImmutableList.copyOf(DOT_SPLITTER.split(lowerCase));
this.parts = copyOf;
Preconditions.checkArgument(copyOf.size() <= 127, "Domain has too many parts: '%s'", lowerCase);
Preconditions.checkArgument(validateSyntax(copyOf), "Not a valid domain name: '%s'", lowerCase);
this.publicSuffixIndex = findSuffixOfType(Optional.absent());
this.registrySuffixIndex = findSuffixOfType(Optional.of(EnumC16882rzn.REGISTRY));
}
private int findSuffixOfType(Optional<EnumC16882rzn> optional) {
int size = this.parts.size();
for (int i = 0; i < size; i++) {
String join = DOT_JOINER.join(this.parts.subList(i, size));
if (matchesType(optional, Optional.fromNullable(C12167etB.c.get(join)))) {
return i;
}
if (C12167etB.e.containsKey(join)) {
return i + 1;
}
if (matchesWildcardSuffixType(optional, join)) {
return i;
}
}
return -1;
}
public static InternetDomainName from(String str) {
return new InternetDomainName((String) Preconditions.checkNotNull(str));
}
private static boolean validateSyntax(List<String> list) {
int size = list.size() - 1;
if (!validatePart(list.get(size), true)) {
return false;
}
for (int i = 0; i < size; i++) {
if (!validatePart(list.get(i), false)) {
return false;
}
}
return true;
}
private static boolean validatePart(String str, boolean z) {
if (str.length() > 0 && str.length() <= 63) {
if (!PART_CHAR_MATCHER.matchesAllOf(CharMatcher.ascii().retainFrom(str))) {
return false;
}
CharMatcher charMatcher = DASH_MATCHER;
if (!charMatcher.matches(str.charAt(0)) && !charMatcher.matches(str.charAt(str.length() - 1))) {
return (z && DIGIT_MATCHER.matches(str.charAt(0))) ? false : true;
}
}
return false;
}
public final InternetDomainName publicSuffix() {
if (hasPublicSuffix()) {
return ancestor(this.publicSuffixIndex);
}
return null;
}
public final InternetDomainName topPrivateDomain() {
if (isTopPrivateDomain()) {
return this;
}
Preconditions.checkState(isUnderPublicSuffix(), "Not under a public suffix: %s", this.name);
return ancestor(this.publicSuffixIndex - 1);
}
public final InternetDomainName registrySuffix() {
if (hasRegistrySuffix()) {
return ancestor(this.registrySuffixIndex);
}
return null;
}
public final InternetDomainName topDomainUnderRegistrySuffix() {
if (isTopDomainUnderRegistrySuffix()) {
return this;
}
Preconditions.checkState(isUnderRegistrySuffix(), "Not under a registry suffix: %s", this.name);
return ancestor(this.registrySuffixIndex - 1);
}
public final boolean hasParent() {
return this.parts.size() > 1;
}
public final InternetDomainName parent() {
Preconditions.checkState(hasParent(), "Domain '%s' has no parent", this.name);
return ancestor(1);
}
private InternetDomainName ancestor(int i) {
Joiner joiner = DOT_JOINER;
ImmutableList<String> immutableList = this.parts;
return from(joiner.join(immutableList.subList(i, immutableList.size())));
}
public final InternetDomainName child(String str) {
String str2 = (String) Preconditions.checkNotNull(str);
String str3 = this.name;
StringBuilder sb = new StringBuilder(String.valueOf(str2).length() + 1 + String.valueOf(str3).length());
sb.append(str2);
sb.append(".");
sb.append(str3);
return from(sb.toString());
}
public static boolean isValid(String str) {
try {
from(str);
return true;
} catch (IllegalArgumentException unused) {
return false;
}
}
private static boolean matchesWildcardSuffixType(Optional<EnumC16882rzn> optional, String str) {
List<String> splitToList = DOT_SPLITTER.limit(2).splitToList(str);
return splitToList.size() == 2 && matchesType(optional, Optional.fromNullable(C12167etB.a.get(splitToList.get(1))));
}
private static boolean matchesType(Optional<EnumC16882rzn> optional, Optional<EnumC16882rzn> optional2) {
return optional.isPresent() ? optional.equals(optional2) : optional2.isPresent();
}
public final boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof InternetDomainName) {
return this.name.equals(((InternetDomainName) obj).name);
}
return false;
}
public final int hashCode() {
return this.name.hashCode();
}
public final String toString() {
return this.name;
}
public final ImmutableList<String> parts() {
return this.parts;
}
public final boolean isUnderRegistrySuffix() {
return this.registrySuffixIndex > 0;
}
public final boolean isUnderPublicSuffix() {
return this.publicSuffixIndex > 0;
}
public final boolean isTopPrivateDomain() {
return this.publicSuffixIndex == 1;
}
public final boolean isTopDomainUnderRegistrySuffix() {
return this.registrySuffixIndex == 1;
}
public final boolean isRegistrySuffix() {
return this.registrySuffixIndex == 0;
}
public final boolean isPublicSuffix() {
return this.publicSuffixIndex == 0;
}
public final boolean hasRegistrySuffix() {
return this.registrySuffixIndex != -1;
}
public final boolean hasPublicSuffix() {
return this.publicSuffixIndex != -1;
}
}