package io.netty.handler.codec.socks;

import g.a.a.a.a;
import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
import io.netty.util.NetUtil;
import java.net.IDN;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
public final class SocksCmdRequest extends SocksRequest {
    private final SocksAddressType addressType;
    private final SocksCmdType cmdType;
    private final String host;
    private final int port;

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

        static {
            SocksAddressType.values();
            int[] iArr = new int[4];
            $SwitchMap$io$netty$handler$codec$socks$SocksAddressType = iArr;
            try {
                iArr[SocksAddressType.IPv4.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$socks$SocksAddressType[SocksAddressType.DOMAIN.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$socks$SocksAddressType[SocksAddressType.IPv6.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$socks$SocksAddressType[SocksAddressType.UNKNOWN.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
        }
    }

    public SocksCmdRequest(SocksCmdType socksCmdType, SocksAddressType socksAddressType, String str, int i2) {
        super(SocksRequestType.CMD);
        Objects.requireNonNull(socksCmdType, "cmdType");
        Objects.requireNonNull(socksAddressType, "addressType");
        Objects.requireNonNull(str, "host");
        int iOrdinal = socksAddressType.ordinal();
        if (iOrdinal != 0) {
            if (iOrdinal == 1) {
                String ascii = IDN.toASCII(str);
                if (ascii.length() > 255) {
                    throw new IllegalArgumentException(str + " IDN: " + ascii + " exceeds 255 char limit");
                }
                str = ascii;
            } else if (iOrdinal == 2 && !NetUtil.isValidIpV6Address(str)) {
                throw new IllegalArgumentException(a.t(str, " is not a valid IPv6 address"));
            }
        } else if (!NetUtil.isValidIpV4Address(str)) {
            throw new IllegalArgumentException(a.t(str, " is not a valid IPv4 address"));
        }
        if (i2 <= 0 || i2 >= 65536) {
            throw new IllegalArgumentException(i2 + " is not in bounds 0 < x < 65536");
        }
        this.cmdType = socksCmdType;
        this.addressType = socksAddressType;
        this.host = str;
        this.port = i2;
    }

    public SocksAddressType addressType() {
        return this.addressType;
    }

    public SocksCmdType cmdType() {
        return this.cmdType;
    }

    @Override // io.netty.handler.codec.socks.SocksMessage
    public void encodeAsByteBuf(ByteBuf byteBuf) {
        byteBuf.writeByte(protocolVersion().byteValue());
        byteBuf.writeByte(this.cmdType.byteValue());
        byteBuf.writeByte(0);
        byteBuf.writeByte(this.addressType.byteValue());
        int iOrdinal = this.addressType.ordinal();
        if (iOrdinal == 0) {
            byteBuf.writeBytes(NetUtil.createByteArrayFromIpAddressString(this.host));
        } else if (iOrdinal == 1) {
            byteBuf.writeByte(this.host.length());
            byteBuf.writeCharSequence(this.host, CharsetUtil.US_ASCII);
        } else if (iOrdinal != 2) {
            return;
        } else {
            byteBuf.writeBytes(NetUtil.createByteArrayFromIpAddressString(this.host));
        }
        byteBuf.writeShort(this.port);
    }

    public String host() {
        return this.addressType == SocksAddressType.DOMAIN ? IDN.toUnicode(this.host) : this.host;
    }

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