/// <summary> /// Starts the synchronous authentication process. /// </summary> /// <exception cref="ProxyException">Authentication with the proxy server failed.</exception> /// <exception cref="ProtocolViolationException">The proxy server uses an invalid protocol.</exception> /// <exception cref="SocketException">An operating system error occurs while accessing the Socket.</exception> /// <exception cref="ObjectDisposedException">The Socket has been closed.</exception> private void Authenticate() { Server.Send(new byte[] { 5, 2, 0, 2 }); byte[] buffer = ReadBytes(2); if (buffer[1] == 255) { throw new ProxyException("No authentication method accepted."); } AuthMethod authenticate; switch (buffer[1]) { case 0: authenticate = new AuthNone(Server); break; case 2: authenticate = new AuthUserPass(Server, Username, Password); break; default: throw new ProtocolViolationException(); } authenticate.Authenticate(); }
/// <summary> /// Called when an authentication reply has been received. /// </summary> /// <param name="ar">Stores state information for this asynchronous operation as well as any user-defined data.</param> private void OnAuthReceive(IAsyncResult ar) { try { Received += Server.EndReceive(ar); if (Received <= 0) { throw new SocketException(); } } catch (Exception e) { ProtocolComplete(e); return; } try { if (Received < 2) { Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None, new AsyncCallback(this.OnAuthReceive), Server); } else { AuthMethod authenticate; switch (Buffer[1]) { case 0: authenticate = new AuthNone(Server); break; case 2: authenticate = new AuthUserPass(Server, Username, Password); break; default: ProtocolComplete(new SocketException()); return; } authenticate.BeginAuthenticate(new HandShakeComplete(this.OnAuthenticated)); } } catch (Exception e) { ProtocolComplete(e); } }