コード例 #1
0
        public string DecodeAddress(Socks5AddressType addrType, IByteBuffer input)
        {
            if (Equals(addrType, Socks5AddressType.Pv4))
            {
                var bytes = new byte[4];
                input.ReadBytes(bytes);
                return(new IPAddress(bytes).ToString());
            }

            if (Equals(addrType, Socks5AddressType.Domain))
            {
                int length = input.ReadByte();
                var domain = input.ToString(input.ReaderIndex, length, Encoding.ASCII);
                input.SkipBytes(length);
                return(domain);
            }

            if (Equals(addrType, Socks5AddressType.Pv6))
            {
                var ipAddressData = new byte[Pv6Len];
                input.ReadBytes(ipAddressData);
                return(new IPAddress(ipAddressData).ToString());
            }

            throw new DecoderException("unsupported address type: " + (addrType.ByteValue & 0xFF));
        }
コード例 #2
0
        protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output)
        {
            try
            {
                switch (State)
                {
                case Socks5CommandRequestDecoderState.Init:
                    var version = input.ReadByte();
                    if (version != (byte)SocksVersion.Socks5)
                    {
                        throw new DecoderException(
                                  "unsupported version: " + version + " (expected: " + SocksVersion.Socks5 + ')');
                    }

                    var type = Socks5CommandType.ValueOf(input.ReadByte());
                    input.SkipBytes(1);     // RSV
                    var dstAddrType = Socks5AddressType.ValueOf(input.ReadByte());
                    var dstAddr     = _addressDecoder.DecodeAddress(dstAddrType, input);
                    int dstPort     = input.ReadUnsignedShort();

                    output.Add(new DefaultSocks5CommandRequest(type, dstAddrType, dstAddr, dstPort));
                    Checkpoint(Socks5CommandRequestDecoderState.Success);
                    break;

                case Socks5CommandRequestDecoderState.Success:
                {
                    var readableBytes = ActualReadableBytes;
                    if (readableBytes > 0)
                    {
                        output.Add(input.ReadRetainedSlice(readableBytes));
                    }

                    break;
                }

                case Socks5CommandRequestDecoderState.Failure:
                {
                    input.SkipBytes(ActualReadableBytes);
                    break;
                }
                }
            }
            catch
            (Exception e)
            {
                Fail(output, e);
            }
        }
コード例 #3
0
        public void EncodeAddress(Socks5AddressType addrType, string addrValue, IByteBuffer output)
        {
            var typeVal = addrType.ByteValue;

            if (typeVal == Socks5AddressType.Pv4.ByteValue)
            {
                if (addrValue != null)
                {
                    output.WriteBytes(NetUtil.CreateByteArrayFromIpAddressString(addrValue));
                }
                else
                {
                    output.WriteInt(0);
                }
            }
            else if (typeVal == Socks5AddressType.Domain.ByteValue)
            {
                if (addrValue != null)
                {
                    output.WriteByte(addrValue.Length);
                    output.WriteBytes(ByteBufferUtil.EncodeString(ByteBufferUtil.DefaultAllocator, addrValue,
                                                                  Encoding.ASCII));
                }
                else
                {
                    output.WriteByte(1);
                    output.WriteByte(0);
                }
            }
            else if (typeVal == Socks5AddressType.Pv6.ByteValue)
            {
                if (addrValue != null)
                {
                    output.WriteBytes(NetUtil.CreateByteArrayFromIpAddressString(addrValue));
                }
                else
                {
                    output.WriteLong(0);
                    output.WriteLong(0);
                }
            }
            else
            {
                throw new EncoderException("unsupported addrType: " + (addrType.ByteValue & 0xFF));
            }
        }
コード例 #4
0
        public DefaultSocks5CommandResponse(
            Socks5CommandStatus status,
            Socks5AddressType bndAddrType,
            string bndAddr = null,
            int bndPort    = 0)
        {
            if (bndAddr != null)
            {
                if (bndAddrType == Socks5AddressType.Pv4)
                {
                    if (!IPAddress.TryParse(bndAddr, out _))
                    {
                        throw new ArgumentException("bndAddr: " + bndAddr + " (expected: a valid IPv4 address)");
                    }
                }
                else if (bndAddrType == Socks5AddressType.Domain)
                {
                    bndAddr = Idn.GetAscii(bndAddr);
                    if (bndAddr.Length > 255)
                    {
                        throw new ArgumentException("bndAddr: " + bndAddr + " (expected: less than 256 chars)");
                    }
                }
                else if (bndAddrType == Socks5AddressType.Pv6)
                {
                    if (!IPAddress.TryParse(bndAddr, out _))
                    {
                        throw new ArgumentException("bndAddr: " + bndAddr + " (expected: a valid IPv6 address)");
                    }
                }
            }

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

            Status      = status;
            BndAddrType = bndAddrType;
            BndAddr     = bndAddr;
            BndPort     = bndPort;
        }
コード例 #5
0
        public DefaultSocks5CommandRequest(
            Socks5CommandType type,
            Socks5AddressType dstAddrType,
            string dstAddr,
            int dstPort)
        {
            if (Equals(dstAddrType, Socks5AddressType.Pv4))
            {
                if (!IPAddress.TryParse(dstAddr, out _))
                {
                    throw new ArgumentException("dstAddr: " + dstAddr + " (expected: a valid IPv4 address)");
                }
            }
            else if (dstAddrType == Socks5AddressType.Domain)
            {
                dstAddr = Idn.GetAscii(dstAddr);
                if (dstAddr.Length > 255)
                {
                    throw new ArgumentException("dstAddr: " + dstAddr + " (expected: less than 256 chars)");
                }
            }
            else if (dstAddrType == Socks5AddressType.Pv6)
            {
                if (!IPAddress.TryParse(dstAddr, out _))
                {
                    throw new ArgumentException("dstAddr: " + dstAddr + " (expected: a valid IPv6 address");
                }
            }

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

            Type        = type;
            DstAddrType = dstAddrType;
            DstAddr     = dstAddr;
            DstPort     = dstPort;
        }