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

import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.DecoderException;
import io.netty.handler.codec.DecoderResult;
import io.netty.handler.codec.ReplayingDecoder;
import io.netty.util.CharsetUtil;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public class Socks4ServerDecoder extends ReplayingDecoder<State> {
    private static final int MAX_FIELD_LENGTH = 255;
    private String dstAddr;
    private int dstPort;
    private Socks4CommandType type;
    private String userId;

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

        static {
            State.values();
            int[] iArr = new int[5];
            $SwitchMap$io$netty$handler$codec$socksx$v4$Socks4ServerDecoder$State = iArr;
            try {
                iArr[State.START.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$socksx$v4$Socks4ServerDecoder$State[State.READ_USERID.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$socksx$v4$Socks4ServerDecoder$State[State.READ_DOMAIN.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$socksx$v4$Socks4ServerDecoder$State[State.SUCCESS.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$socksx$v4$Socks4ServerDecoder$State[State.FAILURE.ordinal()] = 5;
            } catch (NoSuchFieldError unused5) {
            }
        }
    }

    public enum State {
        START,
        READ_USERID,
        READ_DOMAIN,
        SUCCESS,
        FAILURE
    }

    public Socks4ServerDecoder() {
        super(State.START);
        setSingleDecode(true);
    }

    private void fail(List<Object> list, Exception exc) {
        if (!(exc instanceof DecoderException)) {
            exc = new DecoderException(exc);
        }
        Socks4CommandType socks4CommandType = this.type;
        if (socks4CommandType == null) {
            socks4CommandType = Socks4CommandType.CONNECT;
        }
        String str = this.dstAddr;
        if (str == null) {
            str = "";
        }
        int i2 = this.dstPort;
        if (i2 == 0) {
            i2 = 65535;
        }
        String str2 = this.userId;
        DefaultSocks4CommandRequest defaultSocks4CommandRequest = new DefaultSocks4CommandRequest(socks4CommandType, str, i2, str2 != null ? str2 : "");
        defaultSocks4CommandRequest.setDecoderResult(DecoderResult.failure(exc));
        list.add(defaultSocks4CommandRequest);
        checkpoint(State.FAILURE);
    }

    private static String readString(String str, ByteBuf byteBuf) {
        int iBytesBefore = byteBuf.bytesBefore(256, (byte) 0);
        if (iBytesBefore >= 0) {
            String string = byteBuf.readSlice(iBytesBefore).toString(CharsetUtil.US_ASCII);
            byteBuf.skipBytes(1);
            return string;
        }
        throw new DecoderException("field '" + str + "' longer than 255 chars");
    }

    /* JADX WARN: Removed duplicated region for block: B:26:0x0092 A[Catch: Exception -> 0x00b1, TryCatch #0 {Exception -> 0x00b1, blocks: (B:2:0x0000, B:13:0x001a, B:24:0x008c, B:26:0x0092, B:18:0x005b, B:20:0x0065, B:22:0x006f, B:23:0x0077, B:17:0x004e, B:14:0x0023, B:16:0x002f, B:27:0x009a, B:28:0x00b0), top: B:32:0x0000 }] */
    /* JADX WARN: Removed duplicated region for block: B:35:? A[RETURN, SYNTHETIC] */
    @Override // io.netty.handler.codec.ByteToMessageDecoder
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public void decode(io.netty.channel.ChannelHandlerContext r5, io.netty.buffer.ByteBuf r6, java.util.List<java.lang.Object> r7) {
        /*
            r4 = this;
            java.lang.Object r5 = r4.state()     // Catch: java.lang.Exception -> Lb1
            io.netty.handler.codec.socksx.v4.Socks4ServerDecoder$State r5 = (io.netty.handler.codec.socksx.v4.Socks4ServerDecoder.State) r5     // Catch: java.lang.Exception -> Lb1
            int r5 = r5.ordinal()     // Catch: java.lang.Exception -> Lb1
            if (r5 == 0) goto L23
            r0 = 1
            if (r5 == r0) goto L4e
            r0 = 2
            if (r5 == r0) goto L5b
            r0 = 3
            if (r5 == r0) goto L8c
            r0 = 4
            if (r5 == r0) goto L1a
            goto Lb5
        L1a:
            int r5 = r4.actualReadableBytes()     // Catch: java.lang.Exception -> Lb1
            r6.skipBytes(r5)     // Catch: java.lang.Exception -> Lb1
            goto Lb5
        L23:
            short r5 = r6.readUnsignedByte()     // Catch: java.lang.Exception -> Lb1
            io.netty.handler.codec.socksx.SocksVersion r0 = io.netty.handler.codec.socksx.SocksVersion.SOCKS4a     // Catch: java.lang.Exception -> Lb1
            byte r0 = r0.byteValue()     // Catch: java.lang.Exception -> Lb1
            if (r5 != r0) goto L9a
            byte r5 = r6.readByte()     // Catch: java.lang.Exception -> Lb1
            io.netty.handler.codec.socksx.v4.Socks4CommandType r5 = io.netty.handler.codec.socksx.v4.Socks4CommandType.valueOf(r5)     // Catch: java.lang.Exception -> Lb1
            r4.type = r5     // Catch: java.lang.Exception -> Lb1
            int r5 = r6.readUnsignedShort()     // Catch: java.lang.Exception -> Lb1
            r4.dstPort = r5     // Catch: java.lang.Exception -> Lb1
            int r5 = r6.readInt()     // Catch: java.lang.Exception -> Lb1
            java.lang.String r5 = io.netty.util.NetUtil.intToIpAddress(r5)     // Catch: java.lang.Exception -> Lb1
            r4.dstAddr = r5     // Catch: java.lang.Exception -> Lb1
            io.netty.handler.codec.socksx.v4.Socks4ServerDecoder$State r5 = io.netty.handler.codec.socksx.v4.Socks4ServerDecoder.State.READ_USERID     // Catch: java.lang.Exception -> Lb1
            r4.checkpoint(r5)     // Catch: java.lang.Exception -> Lb1
        L4e:
            java.lang.String r5 = "userid"
            java.lang.String r5 = readString(r5, r6)     // Catch: java.lang.Exception -> Lb1
            r4.userId = r5     // Catch: java.lang.Exception -> Lb1
            io.netty.handler.codec.socksx.v4.Socks4ServerDecoder$State r5 = io.netty.handler.codec.socksx.v4.Socks4ServerDecoder.State.READ_DOMAIN     // Catch: java.lang.Exception -> Lb1
            r4.checkpoint(r5)     // Catch: java.lang.Exception -> Lb1
        L5b:
            java.lang.String r5 = "0.0.0.0"
            java.lang.String r0 = r4.dstAddr     // Catch: java.lang.Exception -> Lb1
            boolean r5 = r5.equals(r0)     // Catch: java.lang.Exception -> Lb1
            if (r5 != 0) goto L77
            java.lang.String r5 = r4.dstAddr     // Catch: java.lang.Exception -> Lb1
            java.lang.String r0 = "0.0.0."
            boolean r5 = r5.startsWith(r0)     // Catch: java.lang.Exception -> Lb1
            if (r5 == 0) goto L77
            java.lang.String r5 = "dstAddr"
            java.lang.String r5 = readString(r5, r6)     // Catch: java.lang.Exception -> Lb1
            r4.dstAddr = r5     // Catch: java.lang.Exception -> Lb1
        L77:
            io.netty.handler.codec.socksx.v4.DefaultSocks4CommandRequest r5 = new io.netty.handler.codec.socksx.v4.DefaultSocks4CommandRequest     // Catch: java.lang.Exception -> Lb1
            io.netty.handler.codec.socksx.v4.Socks4CommandType r0 = r4.type     // Catch: java.lang.Exception -> Lb1
            java.lang.String r1 = r4.dstAddr     // Catch: java.lang.Exception -> Lb1
            int r2 = r4.dstPort     // Catch: java.lang.Exception -> Lb1
            java.lang.String r3 = r4.userId     // Catch: java.lang.Exception -> Lb1
            r5.<init>(r0, r1, r2, r3)     // Catch: java.lang.Exception -> Lb1
            r7.add(r5)     // Catch: java.lang.Exception -> Lb1
            io.netty.handler.codec.socksx.v4.Socks4ServerDecoder$State r5 = io.netty.handler.codec.socksx.v4.Socks4ServerDecoder.State.SUCCESS     // Catch: java.lang.Exception -> Lb1
            r4.checkpoint(r5)     // Catch: java.lang.Exception -> Lb1
        L8c:
            int r5 = r4.actualReadableBytes()     // Catch: java.lang.Exception -> Lb1
            if (r5 <= 0) goto Lb5
            io.netty.buffer.ByteBuf r5 = r6.readRetainedSlice(r5)     // Catch: java.lang.Exception -> Lb1
            r7.add(r5)     // Catch: java.lang.Exception -> Lb1
            goto Lb5
        L9a:
            io.netty.handler.codec.DecoderException r6 = new io.netty.handler.codec.DecoderException     // Catch: java.lang.Exception -> Lb1
            java.lang.StringBuilder r0 = new java.lang.StringBuilder     // Catch: java.lang.Exception -> Lb1
            r0.<init>()     // Catch: java.lang.Exception -> Lb1
            java.lang.String r1 = "unsupported protocol version: "
            r0.append(r1)     // Catch: java.lang.Exception -> Lb1
            r0.append(r5)     // Catch: java.lang.Exception -> Lb1
            java.lang.String r5 = r0.toString()     // Catch: java.lang.Exception -> Lb1
            r6.<init>(r5)     // Catch: java.lang.Exception -> Lb1
            throw r6     // Catch: java.lang.Exception -> Lb1
        Lb1:
            r5 = move-exception
            r4.fail(r7, r5)
        Lb5:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: io.netty.handler.codec.socksx.v4.Socks4ServerDecoder.decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List):void");
    }
}
