protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { try { switch (State) { case Socks4ServerDecoderState.Start: var version = input.ReadByte(); if (version != (byte)SocksVersion.Socks4A) { throw new DecoderException("unsupported protocol version: " + version); } _type = Socks4CommandType.ValueOf(input.ReadByte()); _dstPort = input.ReadUnsignedShort(); _dstAddr = new IPAddress(input.ReadInt()).ToString(); Checkpoint(Socks4ServerDecoderState.ReadUserid); break; case Socks4ServerDecoderState.ReadUserid: _userId = ReadString("userid", input); Checkpoint(Socks4ServerDecoderState.ReadDomain); break; case Socks4ServerDecoderState.ReadDomain: // Check for Socks4a protocol marker 0.0.0.x if (_dstAddr != "0.0.0.0" && _dstAddr.StartsWith("0.0.0.")) { _dstAddr = ReadString("dstAddr", input); } output.Add(new DefaultSocks4CommandRequest(_type, _dstAddr, _dstPort, _userId)); Checkpoint(Socks4ServerDecoderState.Success); break; case Socks4ServerDecoderState.Success: var readableBytes = ActualReadableBytes; if (readableBytes > 0) { output.Add(input.ReadRetainedSlice(readableBytes)); } break; case Socks4ServerDecoderState.Failure: input.SkipBytes(ActualReadableBytes); break; } } catch (Exception e) { Fail(output, e); } }
/** * Creates a new instance. * * @param type the type of the request * @param dstAddr the {@code DSTIP} field of the request * @param dstPort the {@code DSTPORT} field of the request */ /** * Creates a new instance. * * @param type the type of the request * @param dstAddr the {@code DSTIP} field of the request * @param dstPort the {@code DSTPORT} field of the request * @param userId the {@code USERID} field of the request */ public DefaultSocks4CommandRequest(Socks4CommandType type, string dstAddr, int dstPort, string userId = "") { if (dstPort <= 0 || dstPort >= 65536) { throw new ArgumentException("dstPort: " + dstPort + " (expected: 1~65535)"); } UserId = userId; Type = type; DstAddr = Idn.GetAscii(dstAddr); DstPort = dstPort; }