protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output)
        {
            try
            {
                switch (State)
                {
                case Socks4ClientDecoderState.Start:
                    int version = input.ReadByte();
                    if (version != 0)
                    {
                        throw new DecoderException("unsupported reply version: " + version + " (expected: 0)");
                    }

                    var status  = Socks4CommandStatus.ValueOf(input.ReadByte());
                    int dstPort = input.ReadUnsignedShort();
                    var dstAddr = input.ReadInt().ToString();

                    output.Add(new DefaultSocks4CommandResponse(status, dstAddr, dstPort));
                    Checkpoint(Socks4ClientDecoderState.Success);
                    break;

                case Socks4ClientDecoderState.Success:
                {
                    if (ActualReadableBytes > 0)
                    {
                        output.Add(input.ReadRetainedSlice(ActualReadableBytes));
                    }

                    break;
                }

                case Socks4ClientDecoderState.Failure:
                {
                    input.SkipBytes(ActualReadableBytes);
                    break;
                }
                }
            }
            catch (Exception e)
            {
                Fail(output, e);
            }
        }
Ejemplo n.º 2
0
        /**
         * Creates a new instance.
         *
         * @param status the status of the response
         */

        /**
         * Creates a new instance.
         *
         * @param status the status of the response
         * @param dstAddr the {@code DSTIP} field of the response
         * @param dstPort the {@code DSTPORT} field of the response
         */
        public DefaultSocks4CommandResponse(Socks4CommandStatus status, string dstAddr = null, int dstPort = 0)
        {
            if (dstAddr != null)
            {
                if (!IPAddress.TryParse(dstAddr, out _))
                {
                    throw new ArgumentException(
                              "dstAddr: " + dstAddr + " (expected: a valid IPv4 address)");
                }
            }

            if (dstPort < 0 || dstPort > 65535)
            {
                throw new ArgumentException("dstPort: " + dstPort + " (expected: 0~65535)");
            }

            Status  = status;
            DstAddr = dstAddr;
            DstPort = dstPort;
        }