Beispiel #1
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>
    private 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));
    }
    /// <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>
    private 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);
    }