package io.netty.handler.codec.haproxy;

import g.a.a.a.a;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.haproxy.HAProxyProxiedProtocol;
import io.netty.handler.codec.haproxy.HAProxyTLV;
import io.netty.util.ByteProcessor;
import io.netty.util.CharsetUtil;
import io.netty.util.NetUtil;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import kotlinx.serialization.json.internal.AbstractJsonLexerKt;

/* JADX INFO: loaded from: classes.dex */
public final class HAProxyMessage {
    private static final HAProxyMessage V1_UNKNOWN_MSG;
    private static final HAProxyMessage V2_LOCAL_MSG;
    private static final HAProxyMessage V2_UNKNOWN_MSG;
    private final HAProxyCommand command;
    private final String destinationAddress;
    private final int destinationPort;
    private final HAProxyProtocolVersion protocolVersion;
    private final HAProxyProxiedProtocol proxiedProtocol;
    private final String sourceAddress;
    private final int sourcePort;
    private final List<HAProxyTLV> tlvs;

    /* JADX INFO: renamed from: io.netty.handler.codec.haproxy.HAProxyMessage$1, reason: invalid class name */
    public static /* synthetic */ class AnonymousClass1 {
        public static final /* synthetic */ int[] $SwitchMap$io$netty$handler$codec$haproxy$HAProxyProxiedProtocol$AddressFamily;
        public static final /* synthetic */ int[] $SwitchMap$io$netty$handler$codec$haproxy$HAProxyTLV$Type;

        static {
            HAProxyProxiedProtocol.AddressFamily.values();
            int[] iArr = new int[4];
            $SwitchMap$io$netty$handler$codec$haproxy$HAProxyProxiedProtocol$AddressFamily = iArr;
            try {
                iArr[HAProxyProxiedProtocol.AddressFamily.AF_UNSPEC.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$haproxy$HAProxyProxiedProtocol$AddressFamily[HAProxyProxiedProtocol.AddressFamily.AF_UNIX.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$haproxy$HAProxyProxiedProtocol$AddressFamily[HAProxyProxiedProtocol.AddressFamily.AF_IPv4.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$haproxy$HAProxyProxiedProtocol$AddressFamily[HAProxyProxiedProtocol.AddressFamily.AF_IPv6.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
            HAProxyTLV.Type.values();
            int[] iArr2 = new int[7];
            $SwitchMap$io$netty$handler$codec$haproxy$HAProxyTLV$Type = iArr2;
            try {
                iArr2[HAProxyTLV.Type.PP2_TYPE_SSL.ordinal()] = 1;
            } catch (NoSuchFieldError unused5) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$haproxy$HAProxyTLV$Type[HAProxyTLV.Type.PP2_TYPE_ALPN.ordinal()] = 2;
            } catch (NoSuchFieldError unused6) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$haproxy$HAProxyTLV$Type[HAProxyTLV.Type.PP2_TYPE_AUTHORITY.ordinal()] = 3;
            } catch (NoSuchFieldError unused7) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$haproxy$HAProxyTLV$Type[HAProxyTLV.Type.PP2_TYPE_SSL_VERSION.ordinal()] = 4;
            } catch (NoSuchFieldError unused8) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$haproxy$HAProxyTLV$Type[HAProxyTLV.Type.PP2_TYPE_SSL_CN.ordinal()] = 5;
            } catch (NoSuchFieldError unused9) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$haproxy$HAProxyTLV$Type[HAProxyTLV.Type.PP2_TYPE_NETNS.ordinal()] = 6;
            } catch (NoSuchFieldError unused10) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$haproxy$HAProxyTLV$Type[HAProxyTLV.Type.OTHER.ordinal()] = 7;
            } catch (NoSuchFieldError unused11) {
            }
        }
    }

    static {
        HAProxyProtocolVersion hAProxyProtocolVersion = HAProxyProtocolVersion.V1;
        HAProxyCommand hAProxyCommand = HAProxyCommand.PROXY;
        HAProxyProxiedProtocol hAProxyProxiedProtocol = HAProxyProxiedProtocol.UNKNOWN;
        V1_UNKNOWN_MSG = new HAProxyMessage(hAProxyProtocolVersion, hAProxyCommand, hAProxyProxiedProtocol, (String) null, (String) null, 0, 0);
        HAProxyProtocolVersion hAProxyProtocolVersion2 = HAProxyProtocolVersion.V2;
        V2_UNKNOWN_MSG = new HAProxyMessage(hAProxyProtocolVersion2, hAProxyCommand, hAProxyProxiedProtocol, (String) null, (String) null, 0, 0);
        V2_LOCAL_MSG = new HAProxyMessage(hAProxyProtocolVersion2, HAProxyCommand.LOCAL, hAProxyProxiedProtocol, (String) null, (String) null, 0, 0);
    }

    private HAProxyMessage(HAProxyProtocolVersion hAProxyProtocolVersion, HAProxyCommand hAProxyCommand, HAProxyProxiedProtocol hAProxyProxiedProtocol, String str, String str2, int i2, int i3) {
        this(hAProxyProtocolVersion, hAProxyCommand, hAProxyProxiedProtocol, str, str2, i2, i3, Collections.emptyList());
    }

    private HAProxyMessage(HAProxyProtocolVersion hAProxyProtocolVersion, HAProxyCommand hAProxyCommand, HAProxyProxiedProtocol hAProxyProxiedProtocol, String str, String str2, int i2, int i3, List<HAProxyTLV> list) {
        Objects.requireNonNull(hAProxyProxiedProtocol, "proxiedProtocol");
        HAProxyProxiedProtocol.AddressFamily addressFamily = hAProxyProxiedProtocol.addressFamily();
        checkAddress(str, addressFamily);
        checkAddress(str2, addressFamily);
        checkPort(i2);
        checkPort(i3);
        this.protocolVersion = hAProxyProtocolVersion;
        this.command = hAProxyCommand;
        this.proxiedProtocol = hAProxyProxiedProtocol;
        this.sourceAddress = str;
        this.destinationAddress = str2;
        this.sourcePort = i2;
        this.destinationPort = i3;
        this.tlvs = Collections.unmodifiableList(list);
    }

    private HAProxyMessage(HAProxyProtocolVersion hAProxyProtocolVersion, HAProxyCommand hAProxyCommand, HAProxyProxiedProtocol hAProxyProxiedProtocol, String str, String str2, String str3, String str4) {
        this(hAProxyProtocolVersion, hAProxyCommand, hAProxyProxiedProtocol, str, str2, portStringToInt(str3), portStringToInt(str4));
    }

    private static void checkAddress(String str, HAProxyProxiedProtocol.AddressFamily addressFamily) {
        Objects.requireNonNull(addressFamily, "addrFamily");
        int iOrdinal = addressFamily.ordinal();
        if (iOrdinal == 0) {
            if (str != null) {
                throw new HAProxyProtocolException(a.t("unable to validate an AF_UNSPEC address: ", str));
            }
            return;
        }
        if (iOrdinal != 3) {
            Objects.requireNonNull(str, "address");
            int iOrdinal2 = addressFamily.ordinal();
            if (iOrdinal2 == 1) {
                if (!NetUtil.isValidIpV4Address(str)) {
                    throw new HAProxyProtocolException(a.t("invalid IPv4 address: ", str));
                }
            } else {
                if (iOrdinal2 != 2) {
                    throw new Error();
                }
                if (!NetUtil.isValidIpV6Address(str)) {
                    throw new HAProxyProtocolException(a.t("invalid IPv6 address: ", str));
                }
            }
        }
    }

    private static void checkPort(int i2) {
        if (i2 < 0 || i2 > 65535) {
            throw new HAProxyProtocolException(a.n("invalid port: ", i2, " (expected: 1 ~ 65535)"));
        }
    }

    public static HAProxyMessage decodeHeader(ByteBuf byteBuf) {
        String strIpBytesToString;
        String strIpBytesToString2;
        int unsignedShort;
        int unsignedShort2;
        Objects.requireNonNull(byteBuf, "header");
        int i2 = 16;
        if (byteBuf.readableBytes() < 16) {
            StringBuilder sbF = a.F("incomplete header: ");
            sbF.append(byteBuf.readableBytes());
            sbF.append(" bytes (expected: 16+ bytes)");
            throw new HAProxyProtocolException(sbF.toString());
        }
        byteBuf.skipBytes(12);
        byte b2 = byteBuf.readByte();
        try {
            HAProxyProtocolVersion hAProxyProtocolVersionValueOf = HAProxyProtocolVersion.valueOf(b2);
            if (hAProxyProtocolVersionValueOf != HAProxyProtocolVersion.V2) {
                StringBuilder sbF2 = a.F("version 1 unsupported: 0x");
                sbF2.append(Integer.toHexString(b2));
                throw new HAProxyProtocolException(sbF2.toString());
            }
            try {
                HAProxyCommand hAProxyCommandValueOf = HAProxyCommand.valueOf(b2);
                if (hAProxyCommandValueOf == HAProxyCommand.LOCAL) {
                    return V2_LOCAL_MSG;
                }
                try {
                    HAProxyProxiedProtocol hAProxyProxiedProtocolValueOf = HAProxyProxiedProtocol.valueOf(byteBuf.readByte());
                    if (hAProxyProxiedProtocolValueOf == HAProxyProxiedProtocol.UNKNOWN) {
                        return V2_UNKNOWN_MSG;
                    }
                    int unsignedShort3 = byteBuf.readUnsignedShort();
                    HAProxyProxiedProtocol.AddressFamily addressFamily = hAProxyProxiedProtocolValueOf.addressFamily();
                    if (addressFamily != HAProxyProxiedProtocol.AddressFamily.AF_UNIX) {
                        if (addressFamily == HAProxyProxiedProtocol.AddressFamily.AF_IPv4) {
                            if (unsignedShort3 < 12 || byteBuf.readableBytes() < 12) {
                                StringBuilder sbF3 = a.F("incomplete IPv4 address information: ");
                                sbF3.append(Math.min(unsignedShort3, byteBuf.readableBytes()));
                                sbF3.append(" bytes (expected: 12+ bytes)");
                                throw new HAProxyProtocolException(sbF3.toString());
                            }
                            i2 = 4;
                        } else {
                            if (addressFamily != HAProxyProxiedProtocol.AddressFamily.AF_IPv6) {
                                throw new HAProxyProtocolException("unable to parse address information (unknown address family: " + addressFamily + ')');
                            }
                            if (unsignedShort3 < 36 || byteBuf.readableBytes() < 36) {
                                StringBuilder sbF4 = a.F("incomplete IPv6 address information: ");
                                sbF4.append(Math.min(unsignedShort3, byteBuf.readableBytes()));
                                sbF4.append(" bytes (expected: 36+ bytes)");
                                throw new HAProxyProtocolException(sbF4.toString());
                            }
                        }
                        strIpBytesToString = ipBytesToString(byteBuf, i2);
                        strIpBytesToString2 = ipBytesToString(byteBuf, i2);
                        unsignedShort = byteBuf.readUnsignedShort();
                        unsignedShort2 = byteBuf.readUnsignedShort();
                    } else {
                        if (unsignedShort3 < 216 || byteBuf.readableBytes() < 216) {
                            StringBuilder sbF5 = a.F("incomplete UNIX socket address information: ");
                            sbF5.append(Math.min(unsignedShort3, byteBuf.readableBytes()));
                            sbF5.append(" bytes (expected: 216+ bytes)");
                            throw new HAProxyProtocolException(sbF5.toString());
                        }
                        int i3 = byteBuf.readerIndex();
                        ByteProcessor byteProcessor = ByteProcessor.FIND_NUL;
                        int iForEachByte = byteBuf.forEachByte(i3, 108, byteProcessor);
                        int i4 = iForEachByte == -1 ? 108 : iForEachByte - i3;
                        Charset charset = CharsetUtil.US_ASCII;
                        String string = byteBuf.toString(i3, i4, charset);
                        int i5 = i3 + 108;
                        int iForEachByte2 = byteBuf.forEachByte(i5, 108, byteProcessor);
                        String string2 = byteBuf.toString(i5, iForEachByte2 == -1 ? 108 : iForEachByte2 - i5, charset);
                        byteBuf.readerIndex(i5 + 108);
                        strIpBytesToString = string;
                        unsignedShort = 0;
                        unsignedShort2 = 0;
                        strIpBytesToString2 = string2;
                    }
                    return new HAProxyMessage(hAProxyProtocolVersionValueOf, hAProxyCommandValueOf, hAProxyProxiedProtocolValueOf, strIpBytesToString, strIpBytesToString2, unsignedShort, unsignedShort2, readTlvs(byteBuf));
                } catch (IllegalArgumentException e2) {
                    throw new HAProxyProtocolException(e2);
                }
            } catch (IllegalArgumentException e3) {
                throw new HAProxyProtocolException(e3);
            }
        } catch (IllegalArgumentException e4) {
            throw new HAProxyProtocolException(e4);
        }
    }

    public static HAProxyMessage decodeHeader(String str) {
        if (str == null) {
            throw new HAProxyProtocolException("header");
        }
        String[] strArrSplit = str.split(" ");
        int length = strArrSplit.length;
        if (length < 2) {
            throw new HAProxyProtocolException(a.u("invalid header: ", str, " (expected: 'PROXY' and proxied protocol values)"));
        }
        if (!"PROXY".equals(strArrSplit[0])) {
            StringBuilder sbF = a.F("unknown identifier: ");
            sbF.append(strArrSplit[0]);
            throw new HAProxyProtocolException(sbF.toString());
        }
        try {
            HAProxyProxiedProtocol hAProxyProxiedProtocolValueOf = HAProxyProxiedProtocol.valueOf(strArrSplit[1]);
            if (hAProxyProxiedProtocolValueOf != HAProxyProxiedProtocol.TCP4 && hAProxyProxiedProtocolValueOf != HAProxyProxiedProtocol.TCP6 && hAProxyProxiedProtocolValueOf != HAProxyProxiedProtocol.UNKNOWN) {
                StringBuilder sbF2 = a.F("unsupported v1 proxied protocol: ");
                sbF2.append(strArrSplit[1]);
                throw new HAProxyProtocolException(sbF2.toString());
            }
            if (hAProxyProxiedProtocolValueOf == HAProxyProxiedProtocol.UNKNOWN) {
                return V1_UNKNOWN_MSG;
            }
            if (length == 6) {
                return new HAProxyMessage(HAProxyProtocolVersion.V1, HAProxyCommand.PROXY, hAProxyProxiedProtocolValueOf, strArrSplit[2], strArrSplit[3], strArrSplit[4], strArrSplit[5]);
            }
            throw new HAProxyProtocolException(a.u("invalid TCP4/6 header: ", str, " (expected: 6 parts)"));
        } catch (IllegalArgumentException e2) {
            throw new HAProxyProtocolException(e2);
        }
    }

    private static String ipBytesToString(ByteBuf byteBuf, int i2) {
        StringBuilder sb = new StringBuilder();
        if (i2 == 4) {
            sb.append(byteBuf.readByte() & 255);
            sb.append('.');
            sb.append(byteBuf.readByte() & 255);
            sb.append('.');
            sb.append(byteBuf.readByte() & 255);
            sb.append('.');
            sb.append(byteBuf.readByte() & 255);
        } else {
            sb.append(Integer.toHexString(byteBuf.readUnsignedShort()));
            sb.append(AbstractJsonLexerKt.COLON);
            sb.append(Integer.toHexString(byteBuf.readUnsignedShort()));
            sb.append(AbstractJsonLexerKt.COLON);
            sb.append(Integer.toHexString(byteBuf.readUnsignedShort()));
            sb.append(AbstractJsonLexerKt.COLON);
            sb.append(Integer.toHexString(byteBuf.readUnsignedShort()));
            sb.append(AbstractJsonLexerKt.COLON);
            sb.append(Integer.toHexString(byteBuf.readUnsignedShort()));
            sb.append(AbstractJsonLexerKt.COLON);
            sb.append(Integer.toHexString(byteBuf.readUnsignedShort()));
            sb.append(AbstractJsonLexerKt.COLON);
            sb.append(Integer.toHexString(byteBuf.readUnsignedShort()));
            sb.append(AbstractJsonLexerKt.COLON);
            sb.append(Integer.toHexString(byteBuf.readUnsignedShort()));
        }
        return sb.toString();
    }

    private static int portStringToInt(String str) {
        try {
            int i2 = Integer.parseInt(str);
            if (i2 <= 0 || i2 > 65535) {
                throw new HAProxyProtocolException(a.u("invalid port: ", str, " (expected: 1 ~ 65535)"));
            }
            return i2;
        } catch (NumberFormatException e2) {
            throw new HAProxyProtocolException(a.t("invalid port: ", str), e2);
        }
    }

    private static HAProxyTLV readNextTLV(ByteBuf byteBuf) {
        if (byteBuf.readableBytes() < 4) {
            return null;
        }
        byte b2 = byteBuf.readByte();
        HAProxyTLV.Type typeTypeForByteValue = HAProxyTLV.Type.typeForByteValue(b2);
        int unsignedShort = byteBuf.readUnsignedShort();
        switch (typeTypeForByteValue) {
            case PP2_TYPE_SSL:
                ByteBuf byteBufRetainedSlice = byteBuf.retainedSlice(byteBuf.readerIndex(), unsignedShort);
                ByteBuf slice = byteBuf.readSlice(unsignedShort);
                byte b3 = slice.readByte();
                int i2 = slice.readInt();
                if (slice.readableBytes() >= 4) {
                    ArrayList arrayList = new ArrayList(4);
                    do {
                        HAProxyTLV nextTLV = readNextTLV(slice);
                        if (nextTLV != null) {
                            arrayList.add(nextTLV);
                        }
                        break;
                    } while (slice.readableBytes() >= 4);
                }
                break;
        }
        return null;
    }

    private static List<HAProxyTLV> readTlvs(ByteBuf byteBuf) {
        HAProxyTLV nextTLV = readNextTLV(byteBuf);
        if (nextTLV == null) {
            return Collections.emptyList();
        }
        ArrayList arrayList = new ArrayList(4);
        do {
            arrayList.add(nextTLV);
            if (nextTLV instanceof HAProxySSLTLV) {
                arrayList.addAll(((HAProxySSLTLV) nextTLV).encapsulatedTLVs());
            }
            nextTLV = readNextTLV(byteBuf);
        } while (nextTLV != null);
        return arrayList;
    }

    public HAProxyCommand command() {
        return this.command;
    }

    public String destinationAddress() {
        return this.destinationAddress;
    }

    public int destinationPort() {
        return this.destinationPort;
    }

    public HAProxyProtocolVersion protocolVersion() {
        return this.protocolVersion;
    }

    public HAProxyProxiedProtocol proxiedProtocol() {
        return this.proxiedProtocol;
    }

    public String sourceAddress() {
        return this.sourceAddress;
    }

    public int sourcePort() {
        return this.sourcePort;
    }

    public List<HAProxyTLV> tlvs() {
        return this.tlvs;
    }
}
