Click here to Skip to main content
15,891,248 members
Articles / Programming Languages / C#

Internet Magic (Proxy Server) Windows Application

Rate me:
Please Sign up or sign in to vote.
3.00/5 (8 votes)
10 Apr 2010CPOL3 min read 68.6K   8.8K   38  
Windows application which creates a proxy server to share Internet over any TCP/IP network
using System;
using System.Net;
using System.Net.Sockets;
using internet_magic;
using internet_magic.Socks.Authentication;

namespace internet_magic.Socks {

///<summary>Listens on a specific port on the proxy server for incoming SOCKS4 and SOCKS5 requests.</summary>
///<remarks>This class also implements the SOCKS4a protocol.</remarks>
public sealed class SocksListener : Listener {
	///<summary>Initializes a new instance of the SocksListener class.</summary>
	///<param name="Port">The port to listen on.</param>
	///<remarks>The SocksListener will listen on all available network cards and it will not use an AuthenticationList.</remarks>
	public SocksListener(int Port) : this(IPAddress.Any, Port, null) {}
	///<summary>Initializes a new instance of the SocksListener class.</summary>
	///<param name="Port">The port to listen on.</param>
	///<param name="Address">The address to listen on. You can specify IPAddress.Any to listen on all installed network cards.</param>
	///<remarks>For the security of your server, try to avoid to listen on every network card (IPAddress.Any). Listening on a local IP address is usually sufficient and much more secure.</remarks>
	///<remarks>The SocksListener object will not use an AuthenticationList.</remarks>
	public SocksListener(IPAddress Address, int Port) : this(Address, Port, null) {}
	///<summary>Initializes a new instance of the SocksListener class.</summary>
	///<param name="Port">The port to listen on.</param>
	///<param name="AuthList">The list of valid login/password combinations. If you do not need password authentication, set this parameter to null.</param>
	///<remarks>The SocksListener will listen on all available network cards.</remarks>
	public SocksListener(int Port, AuthenticationList AuthList) : this(IPAddress.Any, Port, AuthList) {}
	///<summary>Initializes a new instance of the SocksListener class.</summary>
	///<param name="Port">The port to listen on.</param>
	///<param name="Address">The address to listen on. You can specify IPAddress.Any to listen on all installed network cards.</param>
	///<param name="AuthList">The list of valid login/password combinations. If you do not need password authentication, set this parameter to null.</param>
	///<remarks>For the security of your server, try to avoid to listen on every network card (IPAddress.Any). Listening on a local IP address is usually sufficient and much more secure.</remarks>
	public SocksListener(IPAddress Address, int Port, AuthenticationList AuthList) : base(Port, Address) {
		this.AuthList = AuthList;
	}
	///<summary>Called when there's an incoming client connection waiting to be accepted.</summary>
	///<param name="ar">The result of the asynchronous operation.</param>
	public override void OnAccept(IAsyncResult ar) {
		try {
			Socket NewSocket = ListenSocket.EndAccept(ar);
			if (NewSocket != null) {
				SocksClient NewClient = new SocksClient(NewSocket, new DestroyDelegate(this.RemoveClient), AuthList);
				AddClient(NewClient);
				NewClient.StartHandshake();
			}
		} catch {}
		try {
			//Restart Listening
			ListenSocket.BeginAccept(new AsyncCallback(this.OnAccept), ListenSocket);
		} catch {
			Dispose();
		}
	}
	///<summary>Gets or sets the AuthenticationList to be used when a SOCKS5 client connects.</summary>
	///<value>An AuthenticationList that is to be used when a SOCKS5 client connects.</value>
	///<remarks>This value can be null.</remarks>
	private AuthenticationList AuthList {
		get {
			return m_AuthList;
		}
		set {
			m_AuthList = value;
		}
	}
	///<summary>Returns a string representation of this object.</summary>
	///<returns>A string with information about this object.</returns>
	public override string ToString() {
		return "SOCKS service on " + Address.ToString() + ":" + Port.ToString();
	}
	///<summary>Returns a string that holds all the construction information for this object.</summary>
	///<value>A string that holds all the construction information for this object.</value>
	public override string ConstructString {
		get {
			if (AuthList == null)
				return "host:" + Address.ToString() + ";int:" + Port.ToString()+ ";null";
			else
				return "host:" + Address.ToString() + ";int:" + Port.ToString()+ ";authlist";
		}
	}
	// private variables
	/// <summary>Holds the value of the AuthList property.</summary>
	private AuthenticationList m_AuthList;
}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions