PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ConnTest/Assets/src/ProxyClientFactory.cs

http://counterstrike.googlecode.com/
C# | 208 lines | 96 code | 17 blank | 95 comment | 12 complexity | 1be4ec1a5a50bbea932f0aae100c2074 MD5 | raw file
  1. /*
  2. * Authors: Benton Stark
  3. *
  4. * Copyright (c) 2007-2009 Starksoft, LLC (http://www.starksoft.com)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. */
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Text;
  28. using System.Net.Sockets;
  29. namespace Starksoft.Net.Proxy
  30. {
  31. /// <summary>
  32. /// The type of proxy.
  33. /// </summary>
  34. public enum ProxyType
  35. {
  36. /// <summary>
  37. /// No Proxy specified. Note this option will cause an exception to be thrown if used to create a proxy object by the factory.
  38. /// </summary>
  39. None,
  40. /// <summary>
  41. /// HTTP Proxy
  42. /// </summary>
  43. Http,
  44. /// <summary>
  45. /// SOCKS v4 Proxy
  46. /// </summary>
  47. Socks4,
  48. /// <summary>
  49. /// SOCKS v4a Proxy
  50. /// </summary>
  51. Socks4a,
  52. /// <summary>
  53. /// SOCKS v5 Proxy
  54. /// </summary>
  55. Socks5
  56. }
  57. /// <summary>
  58. /// Factory class for creating new proxy client objects.
  59. /// </summary>
  60. /// <remarks>
  61. /// <code>
  62. /// // create an instance of the client proxy factory
  63. /// ProxyClientFactory factory = new ProxyClientFactory();
  64. ///
  65. /// // use the proxy client factory to generically specify the type of proxy to create
  66. /// // the proxy factory method CreateProxyClient returns an IProxyClient object
  67. /// IProxyClient proxy = factory.CreateProxyClient(ProxyType.Http, "localhost", 6588);
  68. ///
  69. /// // create a connection through the proxy to www.starksoft.com over port 80
  70. /// System.Net.Sockets.TcpClient tcpClient = proxy.CreateConnection("www.starksoft.com", 80);
  71. /// </code>
  72. /// </remarks>
  73. public class ProxyClientFactory
  74. {
  75. /// <summary>
  76. /// Factory method for creating new proxy client objects.
  77. /// </summary>
  78. /// <param name="type">The type of proxy client to create.</param>
  79. /// <returns>Proxy client object.</returns>
  80. public IProxyClient CreateProxyClient(ProxyType type)
  81. {
  82. if (type == ProxyType.None)
  83. throw new ArgumentOutOfRangeException("type");
  84. switch (type)
  85. {
  86. case ProxyType.Http:
  87. return new HttpProxyClient();
  88. case ProxyType.Socks4:
  89. return new Socks4ProxyClient();
  90. case ProxyType.Socks4a:
  91. return new Socks4aProxyClient();
  92. case ProxyType.Socks5:
  93. return new Socks5ProxyClient();
  94. default:
  95. throw new ProxyException(String.Format("Unknown proxy type {0}.", type.ToString()));
  96. }
  97. }
  98. /// <summary>
  99. /// Factory method for creating new proxy client objects using an existing TcpClient connection object.
  100. /// </summary>
  101. /// <param name="type">The type of proxy client to create.</param>
  102. /// <param name="tcpClient">Open TcpClient object.</param>
  103. /// <returns>Proxy client object.</returns>
  104. public IProxyClient CreateProxyClient(ProxyType type, TcpClient tcpClient)
  105. {
  106. if (type == ProxyType.None)
  107. throw new ArgumentOutOfRangeException("type");
  108. switch (type)
  109. {
  110. case ProxyType.Http:
  111. return new HttpProxyClient(tcpClient);
  112. case ProxyType.Socks4:
  113. return new Socks4ProxyClient(tcpClient);
  114. case ProxyType.Socks4a:
  115. return new Socks4aProxyClient(tcpClient);
  116. case ProxyType.Socks5:
  117. return new Socks5ProxyClient(tcpClient);
  118. default:
  119. throw new ProxyException(String.Format("Unknown proxy type {0}.", type.ToString()));
  120. }
  121. }
  122. /// <summary>
  123. /// Factory method for creating new proxy client objects.
  124. /// </summary>
  125. /// <param name="type">The type of proxy client to create.</param>
  126. /// <param name="proxyHost">The proxy host or IP address.</param>
  127. /// <param name="proxyPort">The proxy port number.</param>
  128. /// <returns>Proxy client object.</returns>
  129. public IProxyClient CreateProxyClient(ProxyType type, string proxyHost, int proxyPort)
  130. {
  131. if (type == ProxyType.None)
  132. throw new ArgumentOutOfRangeException("type");
  133. switch (type)
  134. {
  135. case ProxyType.Http:
  136. return new HttpProxyClient(proxyHost, proxyPort);
  137. case ProxyType.Socks4:
  138. return new Socks4ProxyClient(proxyHost, proxyPort);
  139. case ProxyType.Socks4a:
  140. return new Socks4aProxyClient(proxyHost, proxyPort);
  141. case ProxyType.Socks5:
  142. return new Socks5ProxyClient(proxyHost, proxyPort);
  143. default:
  144. throw new ProxyException(String.Format("Unknown proxy type {0}.", type.ToString()));
  145. }
  146. }
  147. /// <summary>
  148. /// Factory method for creating new proxy client objects.
  149. /// </summary>
  150. /// <param name="type">The type of proxy client to create.</param>
  151. /// <param name="proxyHost">The proxy host or IP address.</param>
  152. /// <param name="proxyPort">The proxy port number.</param>
  153. /// <param name="proxyUsername">The proxy username. This parameter is only used by Socks4 and Socks5 proxy objects.</param>
  154. /// <param name="proxyPassword">The proxy user password. This parameter is only used Socks5 proxy objects.</param>
  155. /// <returns>Proxy client object.</returns>
  156. public IProxyClient CreateProxyClient(ProxyType type, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
  157. {
  158. if (type == ProxyType.None)
  159. throw new ArgumentOutOfRangeException("type");
  160. switch (type)
  161. {
  162. case ProxyType.Http:
  163. return new HttpProxyClient(proxyHost, proxyPort);
  164. case ProxyType.Socks4:
  165. return new Socks4ProxyClient(proxyHost, proxyPort, proxyUsername);
  166. case ProxyType.Socks4a:
  167. return new Socks4aProxyClient(proxyHost, proxyPort, proxyUsername);
  168. case ProxyType.Socks5:
  169. return new Socks5ProxyClient(proxyHost, proxyPort, proxyUsername, proxyPassword);
  170. default:
  171. throw new ProxyException(String.Format("Unknown proxy type {0}.", type.ToString()));
  172. }
  173. }
  174. /// <summary>
  175. /// Factory method for creating new proxy client objects.
  176. /// </summary>
  177. /// <param name="type">The type of proxy client to create.</param>
  178. /// <param name="tcpClient">Open TcpClient object.</param>
  179. /// <param name="proxyHost">The proxy host or IP address.</param>
  180. /// <param name="proxyPort">The proxy port number.</param>
  181. /// <param name="proxyUsername">The proxy username. This parameter is only used by Socks4 and Socks5 proxy objects.</param>
  182. /// <param name="proxyPassword">The proxy user password. This parameter is only used Socks5 proxy objects.</param>
  183. /// <returns>Proxy client object.</returns>
  184. public IProxyClient CreateProxyClient(ProxyType type, TcpClient tcpClient, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
  185. {
  186. IProxyClient c = CreateProxyClient(type, proxyHost, proxyPort, proxyUsername, proxyPassword);
  187. c.TcpClient = tcpClient;
  188. return c;
  189. }
  190. }
  191. }