package io.netty.handler.codec.haproxy;

import g.a.a.a.a;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.ProtocolDetectionResult;
import io.netty.util.CharsetUtil;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public class HAProxyMessageDecoder extends ByteToMessageDecoder {
    private static final byte[] BINARY_PREFIX;
    private static final int BINARY_PREFIX_LENGTH;
    private static final int DELIMITER_LENGTH = 2;
    private static final int V1_MAX_LENGTH = 108;
    private static final int V2_MAX_LENGTH = 65551;
    private static final int V2_MAX_TLV = 65319;
    private static final int V2_MIN_LENGTH = 232;
    private int discardedBytes;
    private boolean discarding;
    private boolean finished;
    private final int v2MaxHeaderSize;
    private int version;
    private static final byte[] TEXT_PREFIX = {80, 82, 79, 88, 89};
    private static final ProtocolDetectionResult<HAProxyProtocolVersion> DETECTION_RESULT_V1 = ProtocolDetectionResult.detected(HAProxyProtocolVersion.V1);
    private static final ProtocolDetectionResult<HAProxyProtocolVersion> DETECTION_RESULT_V2 = ProtocolDetectionResult.detected(HAProxyProtocolVersion.V2);

    static {
        byte[] bArr = {13, 10, 13, 10, 0, 13, 10, 81, 85, 73, 84, 10};
        BINARY_PREFIX = bArr;
        BINARY_PREFIX_LENGTH = bArr.length;
    }

    public HAProxyMessageDecoder() {
        this.version = -1;
        this.v2MaxHeaderSize = V2_MAX_LENGTH;
    }

    public HAProxyMessageDecoder(int i2) {
        int i3;
        this.version = -1;
        if (i2 < 1) {
            this.v2MaxHeaderSize = V2_MIN_LENGTH;
        } else if (i2 <= V2_MAX_TLV && (i3 = i2 + V2_MIN_LENGTH) <= V2_MAX_LENGTH) {
            this.v2MaxHeaderSize = i3;
        } else {
            this.v2MaxHeaderSize = V2_MAX_LENGTH;
        }
    }

    private ByteBuf decodeLine(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) {
        int iFindEndOfLine = findEndOfLine(byteBuf);
        if (this.discarding) {
            if (iFindEndOfLine >= 0) {
                byteBuf.readerIndex(iFindEndOfLine + (byteBuf.getByte(iFindEndOfLine) == 13 ? 2 : 1));
                this.discardedBytes = 0;
                this.discarding = false;
            } else {
                int i2 = byteBuf.readableBytes();
                this.discardedBytes = i2;
                byteBuf.skipBytes(i2);
            }
            return null;
        }
        if (iFindEndOfLine >= 0) {
            int i3 = iFindEndOfLine - byteBuf.readerIndex();
            if (i3 > 108) {
                byteBuf.readerIndex(iFindEndOfLine + 2);
                failOverLimit(channelHandlerContext, i3);
                return null;
            }
            ByteBuf slice = byteBuf.readSlice(i3);
            byteBuf.skipBytes(2);
            return slice;
        }
        int i4 = byteBuf.readableBytes();
        if (i4 > 108) {
            this.discardedBytes = i4;
            byteBuf.skipBytes(i4);
            this.discarding = true;
            StringBuilder sbF = a.F("over ");
            sbF.append(this.discardedBytes);
            failOverLimit(channelHandlerContext, sbF.toString());
        }
        return null;
    }

    private ByteBuf decodeStruct(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) {
        int iFindEndOfHeader = findEndOfHeader(byteBuf);
        if (this.discarding) {
            if (iFindEndOfHeader >= 0) {
                byteBuf.readerIndex(iFindEndOfHeader);
                this.discardedBytes = 0;
                this.discarding = false;
            } else {
                int i2 = byteBuf.readableBytes();
                this.discardedBytes = i2;
                byteBuf.skipBytes(i2);
            }
            return null;
        }
        if (iFindEndOfHeader >= 0) {
            int i3 = iFindEndOfHeader - byteBuf.readerIndex();
            if (i3 <= this.v2MaxHeaderSize) {
                return byteBuf.readSlice(i3);
            }
            byteBuf.readerIndex(iFindEndOfHeader);
            failOverLimit(channelHandlerContext, i3);
            return null;
        }
        int i4 = byteBuf.readableBytes();
        if (i4 > this.v2MaxHeaderSize) {
            this.discardedBytes = i4;
            byteBuf.skipBytes(i4);
            this.discarding = true;
            StringBuilder sbF = a.F("over ");
            sbF.append(this.discardedBytes);
            failOverLimit(channelHandlerContext, sbF.toString());
        }
        return null;
    }

    public static ProtocolDetectionResult<HAProxyProtocolVersion> detectProtocol(ByteBuf byteBuf) {
        if (byteBuf.readableBytes() < 12) {
            return ProtocolDetectionResult.needsMoreData();
        }
        int i2 = byteBuf.readerIndex();
        return match(BINARY_PREFIX, byteBuf, i2) ? DETECTION_RESULT_V2 : match(TEXT_PREFIX, byteBuf, i2) ? DETECTION_RESULT_V1 : ProtocolDetectionResult.invalid();
    }

    private void fail(ChannelHandlerContext channelHandlerContext, String str, Exception exc) {
        this.finished = true;
        channelHandlerContext.close();
        if (str != null && exc != null) {
            throw new HAProxyProtocolException(str, exc);
        }
        if (str != null) {
            throw new HAProxyProtocolException(str);
        }
        if (exc == null) {
        }
    }

    private void failOverLimit(ChannelHandlerContext channelHandlerContext, int i2) {
        failOverLimit(channelHandlerContext, String.valueOf(i2));
    }

    private void failOverLimit(ChannelHandlerContext channelHandlerContext, String str) {
        fail(channelHandlerContext, "header length (" + str + ") exceeds the allowed maximum (" + (this.version == 1 ? 108 : this.v2MaxHeaderSize) + ')', null);
    }

    private static int findEndOfHeader(ByteBuf byteBuf) {
        int unsignedShort;
        int i2 = byteBuf.readableBytes();
        if (i2 >= 16 && i2 >= (unsignedShort = byteBuf.getUnsignedShort(byteBuf.readerIndex() + 14) + 16)) {
            return unsignedShort;
        }
        return -1;
    }

    private static int findEndOfLine(ByteBuf byteBuf) {
        int iWriterIndex = byteBuf.writerIndex();
        for (int i2 = byteBuf.readerIndex(); i2 < iWriterIndex; i2++) {
            if (byteBuf.getByte(i2) == 13 && i2 < iWriterIndex - 1 && byteBuf.getByte(i2 + 1) == 10) {
                return i2;
            }
        }
        return -1;
    }

    private static int findVersion(ByteBuf byteBuf) {
        if (byteBuf.readableBytes() < 13) {
            return -1;
        }
        int i2 = byteBuf.readerIndex();
        if (match(BINARY_PREFIX, byteBuf, i2)) {
            return byteBuf.getByte(i2 + BINARY_PREFIX_LENGTH);
        }
        return 1;
    }

    private static boolean match(byte[] bArr, ByteBuf byteBuf, int i2) {
        for (int i3 = 0; i3 < bArr.length; i3++) {
            if (byteBuf.getByte(i2 + i3) != bArr[i3]) {
                return false;
            }
        }
        return true;
    }

    @Override // io.netty.handler.codec.ByteToMessageDecoder, io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelRead(ChannelHandlerContext channelHandlerContext, Object obj) {
        super.channelRead(channelHandlerContext, obj);
        if (this.finished) {
            channelHandlerContext.pipeline().remove(this);
        }
    }

    @Override // io.netty.handler.codec.ByteToMessageDecoder
    public final void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) {
        if (this.version == -1) {
            int iFindVersion = findVersion(byteBuf);
            this.version = iFindVersion;
            if (iFindVersion == -1) {
                return;
            }
        }
        ByteBuf byteBufDecodeLine = this.version == 1 ? decodeLine(channelHandlerContext, byteBuf) : decodeStruct(channelHandlerContext, byteBuf);
        if (byteBufDecodeLine != null) {
            this.finished = true;
            try {
                list.add(this.version == 1 ? HAProxyMessage.decodeHeader(byteBufDecodeLine.toString(CharsetUtil.US_ASCII)) : HAProxyMessage.decodeHeader(byteBufDecodeLine));
            } catch (HAProxyProtocolException e2) {
                fail(channelHandlerContext, null, e2);
            }
        }
    }

    @Override // io.netty.handler.codec.ByteToMessageDecoder
    public boolean isSingleDecode() {
        return true;
    }
}
