PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/MSNPSHARP_DEV/ProxyServer/SocksHandler.cs

http://msnp-sharp.googlecode.com/
C# | 241 lines | 150 code | 5 blank | 86 comment | 19 complexity | 7ae71f86ba14303ffc8924b7485fb6f5 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. namespace Org.Mentalis.Proxy.Socks {
  31. ///<summary>Defines the signature of the method that's called when the SOCKS negotiation is complete.</summary>
  32. ///<param name="Success">Indicates whether the negotiation was successful or not.</param>
  33. ///<param name="Remote">The connection with the remote server.</param>
  34. internal delegate void NegotiationCompleteDelegate(bool Success, Socket Remote);
  35. ///<summary>Implements a specific version of the SOCKS protocol.</summary>
  36. internal abstract class SocksHandler {
  37. ///<summary>Initializes a new instance of the SocksHandler class.</summary>
  38. ///<param name="ClientConnection">The connection with the client.</param>
  39. ///<param name="Callback">The method to call when the SOCKS negotiation is complete.</param>
  40. ///<exception cref="ArgumentNullException"><c>Callback</c> is null.</exception>
  41. public SocksHandler(Socket ClientConnection, NegotiationCompleteDelegate Callback) {
  42. if (Callback == null)
  43. throw new ArgumentNullException();
  44. Connection = ClientConnection;
  45. Signaler = Callback;
  46. }
  47. ///<summary>Gets or sets the username of the SOCKS user.</summary>
  48. ///<value>A String representing the username of the logged on user.</value>
  49. ///<exception cref="ArgumentNullException">The specified value is null.</exception>
  50. internal string Username {
  51. get {
  52. return m_Username;
  53. }
  54. set {
  55. if (value == null)
  56. throw new ArgumentNullException();
  57. m_Username = value;
  58. }
  59. }
  60. ///<summary>Gets or sets the connection with the client.</summary>
  61. ///<value>A Socket representing the connection between the proxy server and the SOCKS client.</value>
  62. ///<exception cref="ArgumentNullException">The specified value is null.</exception>
  63. protected Socket Connection {
  64. get {
  65. return m_Connection;
  66. }
  67. set {
  68. if (value == null)
  69. throw new ArgumentNullException();
  70. m_Connection = value;
  71. }
  72. }
  73. ///<summary>Gets a buffer that can be used when receiving bytes from the client.</summary>
  74. ///<value>A byte array that can be used when receiving bytes from the client.</value>
  75. protected byte[] Buffer {
  76. get {
  77. return m_Buffer;
  78. }
  79. }
  80. ///<summary>Gets or sets a byte array that can be used to store received bytes from the client.</summary>
  81. ///<value>A byte array that can be used to store bytes from the client.</value>
  82. protected byte[] Bytes {
  83. get {
  84. return m_Bytes;
  85. }
  86. set {
  87. m_Bytes = value;
  88. }
  89. }
  90. ///<summary>Gets or sets the connection with the remote host.</summary>
  91. ///<value>A Socket representing the connection between the proxy server and the remote host.</value>
  92. ///<exception cref="ArgumentNullException">The specified value is null.</exception>
  93. protected Socket RemoteConnection {
  94. get {
  95. return m_RemoteConnection;
  96. }
  97. set {
  98. m_RemoteConnection = value;
  99. try {
  100. m_RemoteConnection.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1);
  101. } catch {}
  102. }
  103. }
  104. ///<summary>Gets or sets the socket that is used to accept incoming connections.</summary>
  105. ///<value>A Socket that is used to accept incoming connections.</value>
  106. protected Socket AcceptSocket {
  107. get {
  108. return m_AcceptSocket;
  109. }
  110. set {
  111. m_AcceptSocket = value;
  112. }
  113. }
  114. ///<summary>Gets or sets the IP address of the requested remote server.</summary>
  115. ///<value>An IPAddress object specifying the address of the requested remote server.</value>
  116. protected IPAddress RemoteBindIP {
  117. get {
  118. return m_RemoteBindIP;
  119. }
  120. set {
  121. m_RemoteBindIP = value;
  122. }
  123. }
  124. ///<summary>Closes the listening socket if present, and signals the parent object that SOCKS negotiation is complete.</summary>
  125. ///<param name="Success">Indicates whether the SOCKS negotiation was successful or not.</param>
  126. protected void Dispose(bool Success) {
  127. if (AcceptSocket != null)
  128. AcceptSocket.Close();
  129. Signaler(Success, RemoteConnection);
  130. }
  131. ///<summary>Starts accepting bytes from the client.</summary>
  132. public void StartNegotiating() {
  133. try {
  134. Connection.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnReceiveBytes), Connection);
  135. } catch {
  136. Dispose(false);
  137. }
  138. }
  139. ///<summary>Called when we receive some bytes from the client.</summary>
  140. ///<param name="ar">The result of the asynchronous operation.</param>
  141. protected void OnReceiveBytes(IAsyncResult ar) {
  142. try {
  143. int Ret = Connection.EndReceive(ar);
  144. if (Ret <= 0)
  145. Dispose(false);
  146. AddBytes(Buffer, Ret);
  147. if (IsValidRequest(Bytes))
  148. ProcessRequest(Bytes);
  149. else
  150. Connection.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnReceiveBytes), Connection);
  151. } catch {
  152. Dispose(false);
  153. }
  154. }
  155. ///<summary>Called when an OK reply has been sent to the client.</summary>
  156. ///<param name="ar">The result of the asynchronous operation.</param>
  157. protected void OnDisposeGood(IAsyncResult ar) {
  158. try {
  159. if (Connection.EndSend(ar) > 0) {
  160. Dispose(true);
  161. return;
  162. }
  163. } catch {}
  164. Dispose(false);
  165. }
  166. ///<summary>Called when a negative reply has been sent to the client.</summary>
  167. ///<param name="ar">The result of the asynchronous operation.</param>
  168. protected void OnDisposeBad(IAsyncResult ar) {
  169. try {
  170. Connection.EndSend(ar);
  171. } catch {}
  172. Dispose(false);
  173. }
  174. ///<summary>Adds some bytes to a byte aray.</summary>
  175. ///<param name="NewBytes">The new bytes to add.</param>
  176. ///<param name="Cnt">The number of bytes to add.</param>
  177. protected void AddBytes(byte [] NewBytes, int Cnt) {
  178. if (Cnt <= 0 || NewBytes == null || Cnt > NewBytes.Length)
  179. return;
  180. if (Bytes == null) {
  181. Bytes = new byte[Cnt];
  182. } else {
  183. byte [] tmp = Bytes;
  184. Bytes = new byte[Bytes.Length + Cnt];
  185. Array.Copy(tmp, 0, Bytes, 0, tmp.Length);
  186. }
  187. Array.Copy(NewBytes, 0, Bytes, Bytes.Length - Cnt, Cnt);
  188. }
  189. ///<summary>Called when the AcceptSocket should start accepting incoming connections.</summary>
  190. ///<param name="ar">The result of the asynchronous operation.</param>
  191. protected void OnStartAccept(IAsyncResult ar) {
  192. try {
  193. if (Connection.EndSend(ar) <= 0)
  194. Dispose(false);
  195. else
  196. AcceptSocket.BeginAccept(new AsyncCallback(this.OnAccept), AcceptSocket);
  197. } catch {
  198. Dispose(false);
  199. }
  200. }
  201. ///<summary>Called when there's an incoming connection in the AcceptSocket queue.</summary>
  202. ///<param name="ar">The result of the asynchronous operation.</param>
  203. protected abstract void OnAccept(IAsyncResult ar);
  204. ///<summary>Sends a reply to the client connection and disposes it afterwards.</summary>
  205. ///<param name="Value">A byte that contains the reply code to send to the client.</param>
  206. protected abstract void Dispose(byte Value);
  207. ///<summary>Checks whether a specific request is a valid SOCKS request or not.</summary>
  208. ///<param name="Request">The request array to check.</param>
  209. ///<returns>True is the specified request is valid, false otherwise</returns>
  210. protected abstract bool IsValidRequest(byte [] Request);
  211. ///<summary>Processes a SOCKS request from a client.</summary>
  212. ///<param name="Request">The request to process.</param>
  213. protected abstract void ProcessRequest(byte [] Request);
  214. // private variables
  215. /// <summary>Holds the value of the Username property.</summary>
  216. private string m_Username;
  217. /// <summary>Holds the value of the Buffer property.</summary>
  218. private byte [] m_Buffer = new byte[1024];
  219. /// <summary>Holds the value of the Bytes property.</summary>
  220. private byte [] m_Bytes;
  221. /// <summary>Holds the value of the RemoteConnection property.</summary>
  222. private Socket m_RemoteConnection;
  223. /// <summary>Holds the value of the Connection property.</summary>
  224. private Socket m_Connection;
  225. /// <summary>Holds the value of the AcceptSocket property.</summary>
  226. private Socket m_AcceptSocket;
  227. /// <summary>Holds the value of the RemoteBindIP property.</summary>
  228. private IPAddress m_RemoteBindIP;
  229. /// <summary>Holds the address of the method to call when the SOCKS negotiation is complete.</summary>
  230. private NegotiationCompleteDelegate Signaler;
  231. }
  232. }