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