package io.netty.handler.ssl;

import g.a.a.a.a;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandler;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.net.SocketAddress;
import java.util.List;
import java.util.Locale;

/* JADX INFO: loaded from: classes.dex */
public abstract class AbstractSniHandler<T> extends ByteToMessageDecoder implements ChannelOutboundHandler {
    private static final int MAX_SSL_RECORDS = 4;
    private static final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) AbstractSniHandler.class);
    private boolean handshakeFailed;
    private boolean readPending;
    private boolean suppressRead;

    /* JADX INFO: Access modifiers changed from: private */
    public void fireSniCompletionEvent(ChannelHandlerContext channelHandlerContext, String str, Future<T> future) {
        Throwable thCause = future.cause();
        if (thCause == null) {
            channelHandlerContext.fireUserEventTriggered((Object) new SniCompletionEvent(str));
        } else {
            channelHandlerContext.fireUserEventTriggered((Object) new SniCompletionEvent(str, thCause));
        }
    }

    private void select(final ChannelHandlerContext channelHandlerContext, final String str) {
        Future<T> futureLookup = lookup(channelHandlerContext, str);
        if (futureLookup.isDone()) {
            fireSniCompletionEvent(channelHandlerContext, str, futureLookup);
            onLookupComplete(channelHandlerContext, str, futureLookup);
        } else {
            this.suppressRead = true;
            futureLookup.addListener(new FutureListener<T>() { // from class: io.netty.handler.ssl.AbstractSniHandler.1
                /* JADX WARN: Removed duplicated region for block: B:16:0x0038 A[DONT_GENERATE] */
                /* JADX WARN: Removed duplicated region for block: B:26:? A[RETURN, SYNTHETIC] */
                @Override // io.netty.util.concurrent.GenericFutureListener
                /*
                    Code decompiled incorrectly, please refer to instructions dump.
                    To view partially-correct add '--show-bad-code' argument
                */
                public void operationComplete(io.netty.util.concurrent.Future<T> r5) {
                    /*
                        r4 = this;
                        r0 = 0
                        io.netty.handler.ssl.AbstractSniHandler r1 = io.netty.handler.ssl.AbstractSniHandler.this     // Catch: java.lang.Throwable -> L43
                        io.netty.handler.ssl.AbstractSniHandler.access$002(r1, r0)     // Catch: java.lang.Throwable -> L43
                        io.netty.handler.ssl.AbstractSniHandler r1 = io.netty.handler.ssl.AbstractSniHandler.this     // Catch: java.lang.Throwable -> L19 java.lang.Exception -> L20 io.netty.handler.codec.DecoderException -> L2c
                        io.netty.channel.ChannelHandlerContext r2 = r2     // Catch: java.lang.Throwable -> L19 java.lang.Exception -> L20 io.netty.handler.codec.DecoderException -> L2c
                        java.lang.String r3 = r3     // Catch: java.lang.Throwable -> L19 java.lang.Exception -> L20 io.netty.handler.codec.DecoderException -> L2c
                        io.netty.handler.ssl.AbstractSniHandler.access$100(r1, r2, r3, r5)     // Catch: java.lang.Throwable -> L19 java.lang.Exception -> L20 io.netty.handler.codec.DecoderException -> L2c
                        io.netty.handler.ssl.AbstractSniHandler r1 = io.netty.handler.ssl.AbstractSniHandler.this     // Catch: java.lang.Throwable -> L19 java.lang.Exception -> L20 io.netty.handler.codec.DecoderException -> L2c
                        io.netty.channel.ChannelHandlerContext r2 = r2     // Catch: java.lang.Throwable -> L19 java.lang.Exception -> L20 io.netty.handler.codec.DecoderException -> L2c
                        java.lang.String r3 = r3     // Catch: java.lang.Throwable -> L19 java.lang.Exception -> L20 io.netty.handler.codec.DecoderException -> L2c
                        r1.onLookupComplete(r2, r3, r5)     // Catch: java.lang.Throwable -> L19 java.lang.Exception -> L20 io.netty.handler.codec.DecoderException -> L2c
                        goto L30
                    L19:
                        r5 = move-exception
                        io.netty.channel.ChannelHandlerContext r1 = r2     // Catch: java.lang.Throwable -> L43
                    L1c:
                        r1.fireExceptionCaught(r5)     // Catch: java.lang.Throwable -> L43
                        goto L30
                    L20:
                        r5 = move-exception
                        io.netty.channel.ChannelHandlerContext r1 = r2     // Catch: java.lang.Throwable -> L43
                        io.netty.handler.codec.DecoderException r2 = new io.netty.handler.codec.DecoderException     // Catch: java.lang.Throwable -> L43
                        r2.<init>(r5)     // Catch: java.lang.Throwable -> L43
                        r1.fireExceptionCaught(r2)     // Catch: java.lang.Throwable -> L43
                        goto L30
                    L2c:
                        r5 = move-exception
                        io.netty.channel.ChannelHandlerContext r1 = r2     // Catch: java.lang.Throwable -> L43
                        goto L1c
                    L30:
                        io.netty.handler.ssl.AbstractSniHandler r5 = io.netty.handler.ssl.AbstractSniHandler.this
                        boolean r5 = io.netty.handler.ssl.AbstractSniHandler.access$200(r5)
                        if (r5 == 0) goto L42
                        io.netty.handler.ssl.AbstractSniHandler r5 = io.netty.handler.ssl.AbstractSniHandler.this
                        io.netty.handler.ssl.AbstractSniHandler.access$202(r5, r0)
                        io.netty.channel.ChannelHandlerContext r5 = r2
                        r5.read()
                    L42:
                        return
                    L43:
                        r5 = move-exception
                        io.netty.handler.ssl.AbstractSniHandler r1 = io.netty.handler.ssl.AbstractSniHandler.this
                        boolean r1 = io.netty.handler.ssl.AbstractSniHandler.access$200(r1)
                        if (r1 == 0) goto L56
                        io.netty.handler.ssl.AbstractSniHandler r1 = io.netty.handler.ssl.AbstractSniHandler.this
                        io.netty.handler.ssl.AbstractSniHandler.access$202(r1, r0)
                        io.netty.channel.ChannelHandlerContext r0 = r2
                        r0.read()
                    L56:
                        throw r5
                    */
                    throw new UnsupportedOperationException("Method not decompiled: io.netty.handler.ssl.AbstractSniHandler.AnonymousClass1.operationComplete(io.netty.util.concurrent.Future):void");
                }
            });
        }
    }

    @Override // io.netty.channel.ChannelOutboundHandler
    public void bind(ChannelHandlerContext channelHandlerContext, SocketAddress socketAddress, ChannelPromise channelPromise) {
        channelHandlerContext.bind(socketAddress, channelPromise);
    }

    @Override // io.netty.channel.ChannelOutboundHandler
    public void close(ChannelHandlerContext channelHandlerContext, ChannelPromise channelPromise) {
        channelHandlerContext.close(channelPromise);
    }

    @Override // io.netty.channel.ChannelOutboundHandler
    public void connect(ChannelHandlerContext channelHandlerContext, SocketAddress socketAddress, SocketAddress socketAddress2, ChannelPromise channelPromise) {
        channelHandlerContext.connect(socketAddress, socketAddress2, channelPromise);
    }

    @Override // io.netty.handler.codec.ByteToMessageDecoder
    public void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws NotSslRecordException {
        if (this.suppressRead || this.handshakeFailed) {
            return;
        }
        int iWriterIndex = byteBuf.writerIndex();
        int i2 = 0;
        while (true) {
            if (i2 < 4) {
                try {
                    int i3 = byteBuf.readerIndex();
                    int i4 = iWriterIndex - i3;
                    if (i4 >= 5) {
                        switch (byteBuf.getUnsignedByte(i3)) {
                            case 20:
                            case 21:
                                int encryptedPacketLength = SslUtils.getEncryptedPacketLength(byteBuf, i3);
                                if (encryptedPacketLength != -2) {
                                    if (encryptedPacketLength != -1 && i4 - 5 >= encryptedPacketLength) {
                                        byteBuf.skipBytes(encryptedPacketLength);
                                        i2++;
                                        break;
                                    }
                                    return;
                                }
                                this.handshakeFailed = true;
                                NotSslRecordException notSslRecordException = new NotSslRecordException("not an SSL/TLS record: " + ByteBufUtil.hexDump(byteBuf));
                                byteBuf.skipBytes(byteBuf.readableBytes());
                                channelHandlerContext.fireUserEventTriggered((Object) new SniCompletionEvent(notSslRecordException));
                                SslUtils.handleHandshakeFailure(channelHandlerContext, notSslRecordException, true);
                                throw notSslRecordException;
                            case 22:
                                if (byteBuf.getUnsignedByte(i3 + 1) == 3) {
                                    int unsignedShort = byteBuf.getUnsignedShort(i3 + 3) + 5;
                                    if (i4 < unsignedShort) {
                                        return;
                                    }
                                    int i5 = unsignedShort + i3;
                                    int i6 = i3 + 43;
                                    if (i5 - i6 >= 6) {
                                        int unsignedByte = byteBuf.getUnsignedByte(i6) + 1 + i6;
                                        int unsignedShort2 = byteBuf.getUnsignedShort(unsignedByte) + 2 + unsignedByte;
                                        int unsignedByte2 = byteBuf.getUnsignedByte(unsignedShort2) + 1 + unsignedShort2;
                                        int unsignedShort3 = byteBuf.getUnsignedShort(unsignedByte2);
                                        int i7 = unsignedByte2 + 2;
                                        int i8 = unsignedShort3 + i7;
                                        if (i8 <= i5) {
                                            while (true) {
                                                if (i8 - i7 >= 4) {
                                                    int unsignedShort4 = byteBuf.getUnsignedShort(i7);
                                                    int i9 = i7 + 2;
                                                    int unsignedShort5 = byteBuf.getUnsignedShort(i9);
                                                    int i10 = i9 + 2;
                                                    if (i8 - i10 >= unsignedShort5) {
                                                        if (unsignedShort4 == 0) {
                                                            int i11 = i10 + 2;
                                                            if (i8 - i11 >= 3) {
                                                                short unsignedByte3 = byteBuf.getUnsignedByte(i11);
                                                                int i12 = i11 + 1;
                                                                if (unsignedByte3 == 0) {
                                                                    int unsignedShort6 = byteBuf.getUnsignedShort(i12);
                                                                    int i13 = i12 + 2;
                                                                    if (i8 - i13 >= unsignedShort6) {
                                                                        try {
                                                                            select(channelHandlerContext, byteBuf.toString(i13, unsignedShort6, CharsetUtil.US_ASCII).toLowerCase(Locale.US));
                                                                            return;
                                                                        } catch (Throwable th) {
                                                                            PlatformDependent.throwException(th);
                                                                            return;
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        } else {
                                                            i7 = i10 + unsignedShort5;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                break;
                        }
                    } else {
                        return;
                    }
                } catch (NotSslRecordException e2) {
                    throw e2;
                } catch (Exception e3) {
                    InternalLogger internalLogger = logger;
                    if (internalLogger.isDebugEnabled()) {
                        StringBuilder sbF = a.F("Unexpected client hello packet: ");
                        sbF.append(ByteBufUtil.hexDump(byteBuf));
                        internalLogger.debug(sbF.toString(), (Throwable) e3);
                    }
                    select(channelHandlerContext, null);
                    return;
                }
            }
        }
    }

    @Override // io.netty.channel.ChannelOutboundHandler
    public void deregister(ChannelHandlerContext channelHandlerContext, ChannelPromise channelPromise) {
        channelHandlerContext.deregister(channelPromise);
    }

    @Override // io.netty.channel.ChannelOutboundHandler
    public void disconnect(ChannelHandlerContext channelHandlerContext, ChannelPromise channelPromise) {
        channelHandlerContext.disconnect(channelPromise);
    }

    @Override // io.netty.channel.ChannelOutboundHandler
    public void flush(ChannelHandlerContext channelHandlerContext) {
        channelHandlerContext.flush();
    }

    public abstract Future<T> lookup(ChannelHandlerContext channelHandlerContext, String str);

    public abstract void onLookupComplete(ChannelHandlerContext channelHandlerContext, String str, Future<T> future);

    @Override // io.netty.channel.ChannelOutboundHandler
    public void read(ChannelHandlerContext channelHandlerContext) {
        if (this.suppressRead) {
            this.readPending = true;
        } else {
            channelHandlerContext.read();
        }
    }

    @Override // io.netty.channel.ChannelOutboundHandler
    public void write(ChannelHandlerContext channelHandlerContext, Object obj, ChannelPromise channelPromise) {
        channelHandlerContext.write(obj, channelPromise);
    }
}
