ProxyClient for .NET is a proxy client component targeted at the Microsoft .NET platform, which allows to easily establish proxied connections from both new and existing sockets (and TcpClient instances). As of version 2.0, it can also listen for and accept incoming connections established against a remote proxy server, as if they were accepted locally.
ProxyClient for .NET supports the following proxy protocols:Completely written in managed code (C#), the component is compliant with the Common Language Specification (CLS) and can be used with any other .NET language too, including Visual Basic (VB.NET), C++/CLI, Phalanger, J#, IronPython, IronRuby, F#, PowerShell and many others.
ProxyClient for .NET offers an easy way to traverse firewalls, using proxy servers, from within your applications; it can quickly make outgoing TCP connections to SOCKS v4, SOCKS v4A, SOCKS v5 and HTTP-CONNECT proxy servers, either by automatically creating a new socket (or a TcpClient instance) or by using a supplied object of this type, allowing you to even construct and configure it externally.
// Set up a new proxy client object, bound to a SOCKS 5 server var proxy = new Socks5Client(IPAddress.Parse("10.0.1.2"), "john", "$ecrEt"); // The target endpoint will be one of the Google's webservers var googleAddress = Dns.GetHostEntry("www.google.com").AddressList[0]; var targetEndPoint = new IPEndPoint(googleAddress, 80); // Connect to a target IP endpoint via HTTP through a proxied connection var proxiedConnection = proxyClient.Connect(targetEndPoint); using (var proxiedStream = new NetworkStream(proxiedConnection.Socket)) using (var writer = new StreamWriter(proxiedStream, Encoding.ASCII)) { // Send the HTTP request to www.google.com writer.WriteLine("GET / HTTP/1.1"); writer.WriteLine(); writer.Flush(); // ... }
// Set up a new proxy client object, bound to a SOCKS 5 server var proxy = new Socks5Client(IPAddress.Parse("10.0.1.2"), "john", "$ecrEt"); Task.Run(async () => { // Binds the remote socket to the remote HTTP port var listeningSocket = await proxy.BindAsync(new IPv4EndPoint { Address = IPAddress.Any, Port = 80 // The protocol does not guarantee to obey this }); Console.WriteLine("Remote socket listening on: {0}", listeningSocket.ListeningEndPoint); // Accepts a remote connection var acceptedSocket = await proxy.AcceptAsync(listeningSocket); Console.WriteLine("Accepted a connection from: {0}", acceptedSocket.RemoteEndPoint); // Writes a message back, using the HTTP protocol using (var proxiedStream = new NetworkStream(acceptedSocket.Socket)) using (var reader = new StreamReader(proxiedStream, Encoding.ASCII)) using (var writer = new StreamWriter(proxiedStream, Encoding.ASCII)) { // TODO: Handle the request while (reader.EndOfStream) reader.ReadLine(); // Generates a useful reply writer.WriteLine("HTTP/1.0 200 OK"); writer.WriteLine("Content-Type: text/plain"); writer.WriteLine(); writer.WriteLine("hello, world"); writer.Flush(); } // Close the underlying socket connection listeningSocket.Socket.Close(); });
ProxyClient.NET shares the same naming conventions and style you may find in the .NET framework itself and exposes a clean object hierarchy; connecting to a proxy server from within your own application, being it an ASP.NET website, a Windows Forms or a WPF application, requires just a line of code in most scenarios.
Should you need to connect to a remote FTP server using your company HTTP proxy server, for example, while using a TcpClient object, your code would look like this:
// Set up a new proxy client, bound to the specified HTTP-Connect server var proxyEndPoint = new IPEndPoint(IPAddress.Parse("10.0.1.2"), 8080); var proxy = new HttpConnectClient(proxyEndPoint, "john", "$ecrEt"); // Connect to the target FTP server via the proxy server var targetEndPoint = new IPEndPoint(IPAddress.Parse("64.4.30.62"), 21); var proxiedConnection = proxy.ConnectTcpClient(targetEndPoint); using (var tcpClient = proxiedConnection.TcpClient) using (var ftpStream = tcpClient.GetStream()) using (var srFtpClient = new StreamReader(ftpStream)) { // Retrieve the welcome message from the remote FTP server var welcomeMessage = srFtpClient.ReadLine(); // ... }