Пример #1
0
        private ServerGreeting PerformGreeting()
        {
            HashSet <AuthMethod> methods = new HashSet <AuthMethod> {
                0
            };

            if (!string.IsNullOrEmpty(this.Username))
            {
                methods.Add(AuthMethod.Username);
            }
            byte[] buffer = new ClientGreeting(methods).Serialize();
            this.stream.Write(buffer, 0, buffer.Length);
            buffer = new byte[2];
            this.stream.Read(buffer, 0, 2);
            return(ServerGreeting.Deserialize(buffer));
        }
Пример #2
0
        private void PerformGreeting()
        {
            ByteBuilder builder = new ByteBuilder();

            using (BinaryReader reader = new BinaryReader(this.stream, Encoding.UTF8))
            {
                byte[] values = reader.ReadBytes(2);
                builder.Append(values);
                builder.Append(reader.ReadBytes(values[1]));
            }
            if (!ClientGreeting.Deserialize(builder.ToArray()).Methods.Contains <AuthMethod>(AuthMethod.None))
            {
                this.Dispose();
                throw new Socks5Exception("Client requires authentication.");
            }
            byte[] buffer2 = new ServerGreeting(AuthMethod.None).Serialize();
            this.stream.Write(buffer2, 0, buffer2.Length);
        }
Пример #3
0
        public static ServerGreeting Deserialize(byte[] buffer)
        {
            ServerGreeting greeting;

            buffer.ThrowIfNull <byte[]>("buffer");
            using (MemoryStream stream = new MemoryStream(buffer))
            {
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    if (reader.ReadByte() != 5)
                    {
                        throw new SerializationException("Invalid SOCKS5 greeting.");
                    }
                    greeting = new ServerGreeting((AuthMethod)reader.ReadByte());
                }
            }
            return(greeting);
        }
Пример #4
0
        /// <summary>
        /// Performs the initial greeting.
        /// </summary>
        /// <returns>The greeting-message returned by the SOCKS5 server.</returns>
        /// <exception cref="Socks5Exception">The server returned invalid or
        /// unexpected data.</exception>
        ServerGreeting PerformGreeting()
        {
            var methods = new HashSet <AuthMethod>()
            {
                AuthMethod.None
            };

            if (!String.IsNullOrEmpty(Username))
            {
                methods.Add(AuthMethod.Username);
            }
            byte[] bytes = new ClientGreeting(methods).Serialize();

            stream.Write(bytes, 0, bytes.Length);
            // Read the server's response.
            bytes = new byte[2];
            stream.Read(bytes, 0, 2);
            return(ServerGreeting.Deserialize(bytes));
        }
Пример #5
0
        /// <summary>
        /// Performs the initial greeting.
        /// </summary>
        /// <exception cref="Socks5Exception">The client sent invalid data, or
        /// requires authentication.</exception>
        /// <exception cref="IOException">The stream could not be read, or the
        /// operation timed out.</exception>
        void PerformGreeting()
        {
            ByteBuilder b = new ByteBuilder();

            using (var r = new BinaryReader(stream, Encoding.UTF8, true)) {
                byte[] bytes = r.ReadBytes(2);
                b.Append(bytes);
                // The number of method-bytes following is contained in the second byte.
                b.Append(r.ReadBytes(bytes[1]));
            }
            ClientGreeting greeting = ClientGreeting.Deserialize(b.ToArray());

            // We only accept an authentication method of 'none'.
            if (!greeting.Methods.Contains(AuthMethod.None))
            {
                Dispose();
                throw new Socks5Exception("Client requires authentication.");
            }
            // Send back our greeting response.
            var response = new ServerGreeting(AuthMethod.None).Serialize();

            stream.Write(response, 0, response.Length);
        }
Пример #6
0
		/// <summary>
		/// Performs the initial greeting.
		/// </summary>
		/// <exception cref="Socks5Exception">The client sent invalid data, or
		/// requires authentication.</exception>
		/// <exception cref="IOException">The stream could not be read, or the
		/// operation timed out.</exception>
		void PerformGreeting() {
			ByteBuilder b = new ByteBuilder();
			using (var r = new BinaryReader(stream, Encoding.UTF8, true)) {
				byte[] bytes = r.ReadBytes(2);
				b.Append(bytes);
				// The number of method-bytes following is contained in the second byte.
				b.Append(r.ReadBytes(bytes[1]));
			}
			ClientGreeting greeting = ClientGreeting.Deserialize(b.ToArray());
			// We only accept an authentication method of 'none'.
			if (!greeting.Methods.Contains(AuthMethod.None)) {
				Dispose();
				throw new Socks5Exception("Client requires authentication.");
			}
			// Send back our greeting response.
			var response = new ServerGreeting(AuthMethod.None).Serialize();
			stream.Write(response, 0, response.Length);
		}