package io.netty.handler.codec.socksx.v5;

import g.a.a.a.a;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.EncoderException;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.internal.StringUtil;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.RandomAccess;

/* JADX INFO: loaded from: classes.dex */
@ChannelHandler.Sharable
public class Socks5ClientEncoder extends MessageToByteEncoder<Socks5Message> {
    public static final Socks5ClientEncoder DEFAULT = new Socks5ClientEncoder();
    private final Socks5AddressEncoder addressEncoder;

    public Socks5ClientEncoder() {
        this(Socks5AddressEncoder.DEFAULT);
    }

    public Socks5ClientEncoder(Socks5AddressEncoder socks5AddressEncoder) {
        Objects.requireNonNull(socks5AddressEncoder, "addressEncoder");
        this.addressEncoder = socks5AddressEncoder;
    }

    private static void encodeAuthMethodRequest(Socks5InitialRequest socks5InitialRequest, ByteBuf byteBuf) {
        byteBuf.writeByte(socks5InitialRequest.version().byteValue());
        List<Socks5AuthMethod> listAuthMethods = socks5InitialRequest.authMethods();
        int size = listAuthMethods.size();
        byteBuf.writeByte(size);
        if (!(listAuthMethods instanceof RandomAccess)) {
            Iterator<Socks5AuthMethod> it = listAuthMethods.iterator();
            while (it.hasNext()) {
                byteBuf.writeByte(it.next().byteValue());
            }
        } else {
            for (int i2 = 0; i2 < size; i2++) {
                byteBuf.writeByte(listAuthMethods.get(i2).byteValue());
            }
        }
    }

    private void encodeCommandRequest(Socks5CommandRequest socks5CommandRequest, ByteBuf byteBuf) {
        byteBuf.writeByte(socks5CommandRequest.version().byteValue());
        byteBuf.writeByte(socks5CommandRequest.type().byteValue());
        byteBuf.writeByte(0);
        Socks5AddressType socks5AddressTypeDstAddrType = socks5CommandRequest.dstAddrType();
        byteBuf.writeByte(socks5AddressTypeDstAddrType.byteValue());
        this.addressEncoder.encodeAddress(socks5AddressTypeDstAddrType, socks5CommandRequest.dstAddr(), byteBuf);
        byteBuf.writeShort(socks5CommandRequest.dstPort());
    }

    private static void encodePasswordAuthRequest(Socks5PasswordAuthRequest socks5PasswordAuthRequest, ByteBuf byteBuf) {
        byteBuf.writeByte(1);
        String strUsername = socks5PasswordAuthRequest.username();
        byteBuf.writeByte(strUsername.length());
        ByteBufUtil.writeAscii(byteBuf, strUsername);
        String strPassword = socks5PasswordAuthRequest.password();
        byteBuf.writeByte(strPassword.length());
        ByteBufUtil.writeAscii(byteBuf, strPassword);
    }

    public final Socks5AddressEncoder addressEncoder() {
        return this.addressEncoder;
    }

    @Override // io.netty.handler.codec.MessageToByteEncoder
    public void encode(ChannelHandlerContext channelHandlerContext, Socks5Message socks5Message, ByteBuf byteBuf) {
        if (socks5Message instanceof Socks5InitialRequest) {
            encodeAuthMethodRequest((Socks5InitialRequest) socks5Message, byteBuf);
            return;
        }
        if (socks5Message instanceof Socks5PasswordAuthRequest) {
            encodePasswordAuthRequest((Socks5PasswordAuthRequest) socks5Message, byteBuf);
        } else if (socks5Message instanceof Socks5CommandRequest) {
            encodeCommandRequest((Socks5CommandRequest) socks5Message, byteBuf);
        } else {
            StringBuilder sbF = a.F("unsupported message type: ");
            sbF.append(StringUtil.simpleClassName(socks5Message));
            throw new EncoderException(sbF.toString());
        }
    }
}
