PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/MSNPSHARP_DEV/ProxyServer/AuthUserPass.cs

http://msnp-sharp.googlecode.com/
C# | 143 lines | 88 code | 4 blank | 51 comment | 8 complexity | 230d3ce3f796c700a4613ac036e1bea8 MD5 | raw file
  1. /*
  2. Copyright Š 2002, The KPD-Team
  3. All rights reserved.
  4. http://www.mentalis.org/
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. - Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. - Neither the name of the KPD-Team, nor the names of its contributors
  11. may be used to endorse or promote products derived from this
  12. software without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  14. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  16. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  17. THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  18. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  22. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  24. OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. using System;
  27. using System.Text;
  28. using System.Net;
  29. using System.Net.Sockets;
  30. using Org.Mentalis.Proxy;
  31. using Org.Mentalis.Proxy.Socks;
  32. using Org.Mentalis.Proxy.Socks.Authentication;
  33. namespace Org.Mentalis.Proxy.Socks.Authentication {
  34. ///<summary>Authenticates a user on a SOCKS5 server according to the username/password authentication subprotocol.</summary>
  35. internal sealed class AuthUserPass : AuthBase {
  36. ///<summary>Initializes a new instance of the AuthUserPass class.</summary>
  37. ///<param name="AuthList">An AuthenticationList object that contains the list of all valid username/password combinations.</param>
  38. ///<remarks>If the AuthList parameter is null, any username/password combination will be accepted.</remarks>
  39. public AuthUserPass(AuthenticationList AuthList) {
  40. this.AuthList = AuthList;
  41. }
  42. ///<summary>Starts the authentication process.</summary>
  43. ///<param name="Connection">The connection with the SOCKS client.</param>
  44. ///<param name="Callback">The method to call when the authentication is complete.</param>
  45. internal override void StartAuthentication(Socket Connection, AuthenticationCompleteDelegate Callback) {
  46. this.Connection = Connection;
  47. this.Callback = Callback;
  48. try {
  49. Bytes = null;
  50. Connection.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnRecvRequest), Connection);
  51. } catch {
  52. Callback(false);
  53. }
  54. }
  55. ///<summary>Called when we have received the initial authentication data from the SOCKS client.</summary>
  56. ///<param name="ar">The result of the asynchronous operation.</param>
  57. private void OnRecvRequest(IAsyncResult ar) {
  58. try {
  59. int Ret = Connection.EndReceive(ar);
  60. if (Ret <= 0) {
  61. Callback(false);
  62. return;
  63. }
  64. AddBytes(Buffer, Ret);
  65. if (IsValidQuery(Bytes))
  66. ProcessQuery(Bytes);
  67. else
  68. Connection.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnRecvRequest), Connection);
  69. } catch {
  70. Callback(false);
  71. }
  72. }
  73. ///<summary>Checks whether the specified authentication query is a valid one.</summary>
  74. ///<param name="Query">The query to check.</param>
  75. ///<returns>True if the query is a valid authentication query, false otherwise.</returns>
  76. private bool IsValidQuery(byte [] Query) {
  77. try {
  78. return (Query.Length == Query[1] + Query[Query[1] + 2] + 3);
  79. } catch {
  80. return false;
  81. }
  82. }
  83. ///<summary>Processes an authentication query.</summary>
  84. ///<param name="Query">The query to process.</param>
  85. private void ProcessQuery(byte [] Query) {
  86. try {
  87. string User = Encoding.ASCII.GetString(Query, 2, Query[1]);
  88. string Pass = Encoding.ASCII.GetString(Query, Query[1] + 3, Query[Query[1] + 2]);
  89. byte [] ToSend;
  90. if (AuthList == null || AuthList.IsItemPresent(User, Pass)) {
  91. ToSend = new byte[]{5, 0};
  92. Connection.BeginSend(ToSend, 0, ToSend.Length, SocketFlags.None, new AsyncCallback(this.OnOkSent), Connection);
  93. } else {
  94. ToSend = new Byte[]{5, 1};
  95. Connection.BeginSend(ToSend, 0, ToSend.Length, SocketFlags.None, new AsyncCallback(this.OnUhohSent), Connection);
  96. }
  97. } catch {
  98. Callback(false);
  99. }
  100. }
  101. ///<summary>Called when an OK reply has been sent to the client.</summary>
  102. ///<param name="ar">The result of the asynchronous operation.</param>
  103. private void OnOkSent(IAsyncResult ar) {
  104. try {
  105. if (Connection.EndSend(ar) <= 0)
  106. Callback(false);
  107. else
  108. Callback(true);
  109. } catch {
  110. Callback(false);
  111. }
  112. }
  113. ///<summary>Called when a negatiev reply has been sent to the client.</summary>
  114. ///<param name="ar">The result of the asynchronous operation.</param>
  115. private void OnUhohSent(IAsyncResult ar) {
  116. try {
  117. Connection.EndSend(ar);
  118. } catch {}
  119. Callback(false);
  120. }
  121. ///<summary>Gets or sets the AuthenticationList to use when a computer tries to authenticate on the proxy server.</summary>
  122. ///<value>An instance of the AuthenticationList class that contains all the valid username/password combinations.</value>
  123. private AuthenticationList AuthList {
  124. get {
  125. return m_AuthList;
  126. }
  127. set {
  128. m_AuthList = value;
  129. }
  130. }
  131. // private variables
  132. /// <summary>Holds the value of the AuthList property.</summary>
  133. private AuthenticationList m_AuthList;
  134. }
  135. }