Click here to Skip to main content
15,881,803 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.4K   8.8K   38  
Windows application which creates a proxy server to share Internet over any TCP/IP network
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using System.Threading;
using System.Reflection;
using System.Collections;
using System.Net.Sockets;
using System.Security.Cryptography;
using internet_magic;
using internet_magic.Http;
using internet_magic.Socks;
using internet_magic.Socks.Authentication;


namespace internet_magic
{   
    /// <summary>
    /// Starts a Internet Magic by reading the data from the configuration file and start listening on the specified ports.
    /// </summary>
    public class Program
    {
        /// <summary>
        /// Represents an item in a Listeners collection.
        /// </summary>
        public struct ListenEntry
        {
            /// <summary>
            /// The Listener object.
            /// </summary>
            public Listener listener;
            /// <summary>
            /// The Listener's ID. It must be unique troughout the Listeners collection.
            /// </summary>
            public Guid guid;
            /// <summary>
            /// Determines whether the specified Object is equal to the current Object.
            /// </summary>
            /// <param name="obj">The Object to compare with the current Object.</param>
            /// <returns>True if the specified Object is equal to the current Object; otherwise, false.</returns>
            public override bool Equals(object obj)
            {
                return ((ListenEntry)obj).guid.Equals(guid);
            }
            /// <summary>
            /// Serves as a hash function for a particular type. GetHashCode is suitable for use in hashing algorithms and data structures like a hash table. 
            /// </summary>
            /// <returns></returns>
            public override int GetHashCode() { return 0; }

        }
        
        /// <summary>
        /// Initializes a new Proxy instance.
        /// </summary>
        /// <param name="file">The XML configuration file to use.</param>
        public Program(string file)
        {
			Config = new ProxyConfig(this, file);
            
		}
        /// <summary>
        /// Entry point of the application.
        /// </summary>
       
        [STAThread]
        static void Main()
        {
          
                string dir = Environment.CurrentDirectory;
                if (!dir.Substring(dir.Length - 1, 1).Equals(@"\"))
                    dir += @"\";
                
                FileStream file = new FileStream(dir + "clientlog.txt", FileMode.Create);
                file.Close();
                Program prx = new Program(dir + "config.xml");
                
                prx.Start();
            
        }
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        public void Start()
        {
            // Initialize some objects
            StartTime = DateTime.Now;
            if (System.IO.File.Exists(Config.File))
                Config.LoadData();
            /*****************************************/
            
            obj = this;
            /*************************************/
            
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new int_magic(this));
        }
        /// <summary>
        /// Used to delete listeners.
        /// </summary>
        public void ShowDelListener()
        {

            for (int i = 0; i < Listeners.Count; i++)
            {
                
                string id = ((ListenEntry)Listeners[i]).guid.ToString("N");
                if (id != "")
                {

                    try
                    {
                        ListenEntry le = new ListenEntry();
                        le.guid = new Guid(id);
                        if (!Listeners.Contains(le))
                        {
                            MessageBox.Show("Specified ID not found in list!", "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            return;
                        }
                        else
                        {

                            this[Listeners.IndexOf(le)].Dispose();
                            Listeners.Remove(le);
                            Config.SaveData();
                            
                            
                        }
                    }
                    catch
                    {
                        MessageBox.Show("Invalid ID tag!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                }
            }
        }
       
        /// <summary>
        /// Used in the process of creating listeners.
        /// </summary>
        /// <param name="hst">The Host details.</param>
        /// <param name="prt">The Port details.</param>
        /// <param name="classtype">The type of object to instantiate.</param>
        /// <param name="construct">The Construct feild which contains "host:xxx.xxx.xxx.xxx;int:xxx</param>
        /// <returns></returns>
        public void ShowAddListener(String hst, String prt, string classtype, String construct)
        {
            object listenObject = CreateListener(classtype, construct);
            
            if (listenObject == null)
            {
                //MessageBox.Show("Invalid IP Address string.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw new ArgumentException();
            }
            else
            {
                Listener listener;
                try
                {
                    listener = (Listener)listenObject;
                }
                catch
                {
                    //MessageBox.Show("The specified object is not a valid Listener object.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    throw new ArgumentException();
                    //return;
                }
                listener = (Listener)listenObject;
                try
                {
                    listener.Start();
                 AddListener(listener);
                }
                catch
                {
                    
                    throw new ArgumentException();
                    
                }
                Config.SaveData();
             }
             
        }
        /// <summary>
        /// Adds a listener to the Listeners list.
        /// </summary>
        /// <param name="newItem">The new Listener to add.</param>
        public void AddListener(Listener newItem)
        {
            
            if (newItem == null)
                throw new ArgumentNullException();
            ListenEntry le = new ListenEntry();
            le.listener = newItem;
            le.guid = Guid.NewGuid();
            while (Listeners.Contains(le))
            {
                le.guid = Guid.NewGuid();
            }
            Listeners.Add(le);
                        
        }
        /// <summary>
        /// Creates a new Listener obejct from a given listener name and a given listener parameter string.
        /// </summary>
        /// <param name="type">The type of object to instantiate.</param>
        /// <param name="cpars"></param>
        /// <returns></returns>
        public Listener CreateListener(string type, string cpars)
        {
            try
            {
                string[] parts = cpars.Split(';');
                object[] pars = new object[parts.Length];
                string oval = null, otype = null;
                
                int ret;
                // Start instantiating the objects to give to the constructor
                for (int i = 0; i < parts.Length; i++)
                {
                    ret = parts[i].IndexOf(':');
                    if (ret >= 0)
                    {
                        otype = parts[i].Substring(0, ret);
                        oval = parts[i].Substring(ret + 1);
                    }
                    else
                    {
                        otype = parts[i];
                    }
                    switch (otype.ToLower())
                    {
                        case "int":
                            pars[i] = int.Parse(oval);
                           
                            break;
                        case "host":
                            pars[i] = Dns.Resolve(oval).AddressList[0];
                           // IPAddress.Parse(oval);
                            break;
                        case "authlist":
                            pars[i] = Config.UserList;
                            break;
                        case "null":
                            pars[i] = null;
                            break;
                        case "string":
                            pars[i] = oval;
                            break;
                        case "ip":
                            pars[i] = IPAddress.Parse(oval);
                            break;
                        default:
                            pars[i] = null;
                            break;
                    }
                }
               
                return (Listener)Activator.CreateInstance(Type.GetType(type), pars);
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// Gets the collection that contains all the Listener objects.
        /// </summary>
        /// <value>An ArrayList object that contains all the Listener objects.</value>
        public ArrayList Listeners
        {
            get
            {
                return m_Listeners;
            }
        }
        /// <summary>
        /// Gets the number of Listener objects.
        /// </summary>
        /// <value>An integer specifying the number of Listener objects.</value>
        internal int ListenerCount
        {
            get
            {
                return Listeners.Count;
            }
        }
        /// <summary>
        /// Gets the Listener object at the specified position.
        /// </summary>
        /// <value>The Listener instance at position <c>index</c>.</value>
        internal virtual Listener this[int index]
        {
            get
            {
                return ((ListenEntry)Listeners[index]).listener;
            }
        }
       
        /// <summary>
        /// Gets or sets the configuration object for this Proxy server.
        /// </summary>
        /// <value>A ProxyConfig instance that represents the configuration object for this Proxy server.</value>
        public ProxyConfig Config
        {
            get
            {
                return m_Config;
            }
            set
            {
                m_Config = value;
            }
        }
        /// <summary>Holds the value of the Server Start date and Time.</summary>
        public static DateTime StartTime = new DateTime();
        public static Program  obj;
        // private variables
        
        /// <summary>Holds the value of the Config property.</summary>
        private ProxyConfig m_Config;
        /// <summary>Holds the value of the Listeners property.</summary>
        private ArrayList m_Listeners = new ArrayList();
    }
}

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