package io.netty.handler.ssl;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandler;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.DecoderException;
import io.netty.util.AsyncMapping;
import io.netty.util.DomainNameMapping;
import io.netty.util.Mapping;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
import io.netty.util.concurrent.Promise;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.net.SocketAddress;

/* JADX INFO: loaded from: classes.dex */
public class SniHandler extends ByteToMessageDecoder implements ChannelOutboundHandler {
    private static final int MAX_SSL_RECORDS = 4;
    private boolean handshakeFailed;
    private final AsyncMapping<String, SslContext> mapping;
    private boolean readPending;
    private volatile Selection selection;
    private boolean suppressRead;
    private static final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) SniHandler.class);
    private static final Selection EMPTY_SELECTION = new Selection(null, null);

    private static final class AsyncMappingAdapter implements AsyncMapping<String, SslContext> {
        private final Mapping<? super String, ? extends SslContext> mapping;

        private AsyncMappingAdapter(Mapping<? super String, ? extends SslContext> mapping) {
            this.mapping = (Mapping) ObjectUtil.checkNotNull(mapping, "mapping");
        }

        @Override // io.netty.util.AsyncMapping
        public Future<SslContext> map(String str, Promise<SslContext> promise) {
            try {
                return promise.setSuccess(this.mapping.map(str));
            } catch (Throwable th) {
                return promise.setFailure(th);
            }
        }
    }

    private static final class Selection {
        final SslContext context;
        final String hostname;

        Selection(SslContext sslContext, String str) {
            this.context = sslContext;
            this.hostname = str;
        }
    }

    public SniHandler(Mapping<? super String, ? extends SslContext> mapping) {
        this(new AsyncMappingAdapter(mapping));
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void replaceHandler(ChannelHandlerContext channelHandlerContext, Selection selection) {
        this.selection = selection;
        channelHandlerContext.pipeline().replace(this, SslHandler.class.getName(), selection.context.newHandler(channelHandlerContext.alloc()));
    }

    private void select(final ChannelHandlerContext channelHandlerContext, final String str) {
        Future<SslContext> map = this.mapping.map(str, channelHandlerContext.executor().newPromise());
        if (!map.isDone()) {
            this.suppressRead = true;
            map.addListener(new FutureListener<SslContext>() { // from class: io.netty.handler.ssl.SniHandler.1
                @Override // io.netty.util.concurrent.GenericFutureListener
                public void operationComplete(Future<SslContext> future) throws Exception {
                    try {
                        SniHandler.this.suppressRead = false;
                        if (future.isSuccess()) {
                            SniHandler.this.replaceHandler(channelHandlerContext, new Selection(future.getNow(), str));
                        } else {
                            channelHandlerContext.fireExceptionCaught((Throwable) new DecoderException("failed to get the SslContext for " + str, future.cause()));
                        }
                    } finally {
                        if (SniHandler.this.readPending) {
                            SniHandler.this.readPending = false;
                            channelHandlerContext.read();
                        }
                    }
                }
            });
        } else {
            if (map.isSuccess()) {
                replaceHandler(channelHandlerContext, new Selection(map.getNow(), str));
                return;
            }
            throw new DecoderException("failed to get the SslContext for " + str, map.cause());
        }
    }

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

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

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

    /* JADX WARN: Code restructure failed: missing block: B:57:0x0108, code lost:
    
        select(r8, null);
     */
    /* JADX WARN: Code restructure failed: missing block: B:58:0x010c, code lost:
    
        return;
     */
    @Override // io.netty.handler.codec.ByteToMessageDecoder
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    protected void decode(io.netty.channel.ChannelHandlerContext r8, io.netty.buffer.ByteBuf r9, java.util.List<java.lang.Object> r10) throws java.lang.Exception {
        /*
            Method dump skipped, instruction units count: 280
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: io.netty.handler.ssl.SniHandler.decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List):void");
    }

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

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

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

    public String hostname() {
        return this.selection.hostname;
    }

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

    public SslContext sslContext() {
        return this.selection.context;
    }

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

    public SniHandler(DomainNameMapping<? extends SslContext> domainNameMapping) {
        this((Mapping<? super String, ? extends SslContext>) domainNameMapping);
    }

    public SniHandler(AsyncMapping<? super String, ? extends SslContext> asyncMapping) {
        this.selection = EMPTY_SELECTION;
        this.mapping = (AsyncMapping) ObjectUtil.checkNotNull(asyncMapping, "mapping");
    }
}
