PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/MSNPSHARP_DEV/ProxyServer/SocksClient.cs

http://msnp-sharp.googlecode.com/
C# | 154 lines | 95 code | 4 blank | 55 comment | 17 complexity | adbc74903351d865ee595c160b779ed8 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.Net;
  28. using System.Net.Sockets;
  29. using Org.Mentalis.Proxy;
  30. using Org.Mentalis.Proxy.Socks.Authentication;
  31. namespace Org.Mentalis.Proxy.Socks {
  32. ///<summary>Relays data between a remote host and a local client, using the SOCKS protocols.</summary>
  33. ///<remarks>This class implements the SOCKS4, SOCKS4a and SOCKS5 protocols.</remarks>
  34. ///<remarks>If the MustAuthenticate property is set, only SOCKS5 connections are allowed and the AuthList parameter of the constructor should not be null.</remarks>
  35. public sealed class SocksClient : Client {
  36. ///<summary>Initializes a new instance of the SocksClient class.</summary>
  37. ///<param name="ClientSocket">The Socket connection between this proxy server and the local client.</param>
  38. ///<param name="Destroyer">The method to be called when this SocksClient object disconnects from the local client and the remote server.</param>
  39. ///<param name="AuthList">The list with valid username/password combinations.</param>
  40. ///<remarks>If the AuthList is non-null, every client has to authenticate before he can use this proxy server to relay data. If it is null, the clients don't have to authenticate.</remarks>
  41. public SocksClient(Socket ClientSocket, DestroyDelegate Destroyer, AuthenticationList AuthList) : base(ClientSocket, Destroyer) {
  42. this.AuthList = AuthList;
  43. }
  44. ///<summary>Gets or sets the SOCKS handler to be used when communicating with the client.</summary>
  45. ///<value>The SocksHandler to be used when communicating with the client.</value>
  46. internal SocksHandler Handler {
  47. get {
  48. return m_Handler;
  49. }
  50. set {
  51. if (value == null)
  52. throw new ArgumentNullException();
  53. m_Handler = value;
  54. }
  55. }
  56. ///<summary>Gets or sets the SOCKS handler to be used when communicating with the client.</summary>
  57. ///<value>The SocksHandler to be used when communicating with the client.</value>
  58. public bool MustAuthenticate {
  59. get {
  60. return m_MustAuthenticate;
  61. }
  62. set {
  63. m_MustAuthenticate = value;
  64. }
  65. }
  66. ///<summary>Starts communication with the client.</summary>
  67. public override void StartHandshake() {
  68. try {
  69. ClientSocket.BeginReceive(Buffer, 0, 1, SocketFlags.None, new AsyncCallback(this.OnStartSocksProtocol), ClientSocket);
  70. } catch {
  71. Dispose();
  72. }
  73. }
  74. ///<summary>Called when we have received some data from the client.</summary>
  75. ///<param name="ar">The result of the asynchronous operation.</param>
  76. private void OnStartSocksProtocol(IAsyncResult ar) {
  77. int Ret;
  78. try {
  79. Ret = ClientSocket.EndReceive(ar);
  80. if (Ret <= 0) {
  81. Dispose();
  82. return;
  83. }
  84. if (Buffer[0] == 4) { //SOCKS4 Protocol
  85. if (MustAuthenticate) {
  86. Dispose();
  87. return;
  88. } else {
  89. Handler = new Socks4Handler(ClientSocket, new NegotiationCompleteDelegate(this.OnEndSocksProtocol));
  90. }
  91. } else if(Buffer[0] == 5) { //SOCKS5 Protocol
  92. if (MustAuthenticate && AuthList == null) {
  93. Dispose();
  94. return;
  95. }
  96. Handler = new Socks5Handler(ClientSocket, new NegotiationCompleteDelegate(this.OnEndSocksProtocol), AuthList);
  97. } else {
  98. Dispose();
  99. return;
  100. }
  101. Handler.StartNegotiating();
  102. } catch {
  103. Dispose();
  104. }
  105. }
  106. ///<summary>Called when the SOCKS protocol has ended. We can no start relaying data, if the SOCKS authentication was successful.</summary>
  107. ///<param name="Success">Specifies whether the SOCKS negotiation was successful or not.</param>
  108. ///<param name="Remote">The connection with the remote server.</param>
  109. private void OnEndSocksProtocol(bool Success, Socket Remote) {
  110. DestinationSocket = Remote;
  111. if (Success)
  112. StartRelay();
  113. else
  114. Dispose();
  115. }
  116. ///<summary>Gets or sets the AuthenticationList to use when a computer tries to authenticate on the proxy server.</summary>
  117. ///<value>An instance of the AuthenticationList class that contains all the valid username/password combinations.</value>
  118. private AuthenticationList AuthList {
  119. get {
  120. return m_AuthList;
  121. }
  122. set {
  123. m_AuthList = value;
  124. }
  125. }
  126. ///<summary>Returns text information about this SocksClient object.</summary>
  127. ///<returns>A string representing this SocksClient object.</returns>
  128. public override string ToString() {
  129. try {
  130. if (Handler != null)
  131. return Handler.Username + " (" + ((IPEndPoint)ClientSocket.LocalEndPoint).Address.ToString() +") connected to " + DestinationSocket.RemoteEndPoint.ToString();
  132. else
  133. return "SOCKS connection from " + ((IPEndPoint)ClientSocket.LocalEndPoint).Address.ToString();
  134. } catch {
  135. return "Incoming SOCKS connection";
  136. }
  137. }
  138. // private variables
  139. /// <summary>Holds the value of the AuthList property.</summary>
  140. private AuthenticationList m_AuthList;
  141. /// <summary>Holds the value of the MustAuthenticate property.</summary>
  142. private bool m_MustAuthenticate = false;
  143. /// <summary>Holds the value of the Handler property.</summary>
  144. private SocksHandler m_Handler;
  145. }
  146. }