/// <summary> /// Performs Username/Password authentication. /// </summary> /// <exception cref="Socks5Exception">The server returned invalid or /// unexpected data, or authentication failed.</exception> void Authenticate() { byte[] bytes = new AuthRequest(Username, Password).Serialize(); stream.Write(bytes, 0, bytes.Length); // Read the server's response. bytes = new byte[2]; stream.Read(bytes, 0, 2); AuthResponse response = AuthResponse.Deserialize(bytes); if (!response.Success) { throw new Socks5Exception("Authentication failed."); } }
public static AuthResponse Deserialize(byte[] buffer) { AuthResponse response; buffer.ThrowIfNull <byte[]>("buffer"); using (MemoryStream stream = new MemoryStream(buffer)) { using (BinaryReader reader = new BinaryReader(stream)) { if (reader.ReadByte() != 1) { throw new SerializationException("Invalid auth response."); } bool success = reader.ReadByte() == 0; response = new AuthResponse(success); } } return(response); }