package io.netty.handler.codec.socksx;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.socksx.v4.Socks4ServerDecoder;
import io.netty.handler.codec.socksx.v4.Socks4ServerEncoder;
import io.netty.handler.codec.socksx.v5.Socks5InitialRequestDecoder;
import io.netty.handler.codec.socksx.v5.Socks5ServerEncoder;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.util.List;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
public class SocksPortUnificationServerHandler extends ByteToMessageDecoder {
    private static final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) SocksPortUnificationServerHandler.class);
    private final Socks5ServerEncoder socks5encoder;

    /* JADX INFO: renamed from: io.netty.handler.codec.socksx.SocksPortUnificationServerHandler$1, reason: invalid class name */
    public static /* synthetic */ class AnonymousClass1 {
        public static final /* synthetic */ int[] $SwitchMap$io$netty$handler$codec$socksx$SocksVersion;

        static {
            SocksVersion.values();
            int[] iArr = new int[3];
            $SwitchMap$io$netty$handler$codec$socksx$SocksVersion = iArr;
            try {
                iArr[SocksVersion.SOCKS4a.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$socksx$SocksVersion[SocksVersion.SOCKS5.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
        }
    }

    public SocksPortUnificationServerHandler() {
        this(Socks5ServerEncoder.DEFAULT);
    }

    public SocksPortUnificationServerHandler(Socks5ServerEncoder socks5ServerEncoder) {
        Objects.requireNonNull(socks5ServerEncoder, "socks5encoder");
        this.socks5encoder = socks5ServerEncoder;
    }

    private static void logKnownVersion(ChannelHandlerContext channelHandlerContext, SocksVersion socksVersion) {
        logger.debug("{} Protocol version: {}({})", channelHandlerContext.channel(), socksVersion);
    }

    private static void logUnknownVersion(ChannelHandlerContext channelHandlerContext, byte b2) {
        InternalLogger internalLogger = logger;
        if (internalLogger.isDebugEnabled()) {
            internalLogger.debug("{} Unknown protocol version: {}", channelHandlerContext.channel(), Integer.valueOf(b2 & 255));
        }
    }

    @Override // io.netty.handler.codec.ByteToMessageDecoder
    public void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) {
        String strName;
        ChannelHandler socks4ServerDecoder;
        int i2 = byteBuf.readerIndex();
        if (byteBuf.writerIndex() == i2) {
            return;
        }
        ChannelPipeline channelPipelinePipeline = channelHandlerContext.pipeline();
        byte b2 = byteBuf.getByte(i2);
        SocksVersion socksVersionValueOf = SocksVersion.valueOf(b2);
        int iOrdinal = socksVersionValueOf.ordinal();
        if (iOrdinal == 0) {
            logKnownVersion(channelHandlerContext, socksVersionValueOf);
            channelPipelinePipeline.addAfter(channelHandlerContext.name(), null, Socks4ServerEncoder.INSTANCE);
            strName = channelHandlerContext.name();
            socks4ServerDecoder = new Socks4ServerDecoder();
        } else if (iOrdinal != 1) {
            logUnknownVersion(channelHandlerContext, b2);
            byteBuf.skipBytes(byteBuf.readableBytes());
            channelHandlerContext.close();
            return;
        } else {
            logKnownVersion(channelHandlerContext, socksVersionValueOf);
            channelPipelinePipeline.addAfter(channelHandlerContext.name(), null, this.socks5encoder);
            strName = channelHandlerContext.name();
            socks4ServerDecoder = new Socks5InitialRequestDecoder();
        }
        channelPipelinePipeline.addAfter(strName, null, socks4ServerDecoder);
        channelPipelinePipeline.remove(this);
    }
}
