Click here to Skip to main content
15,892,674 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.IO;
using System.Xml;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Reflection;
using System.Reflection.Emit;
using System.Net;
using internet_magic;
using internet_magic.Socks.Authentication;
using System.Windows.Forms;

namespace internet_magic
{
    /// <summary>
    /// Stores the ip monitor configuration settings of this proxy server.
    /// </summary>
    public class ip_monitor
    {
        /// <summary>
		/// Initializes a new ProxyConfig instance.
		/// </summary>
		/// <param name="parent">The parent of this ProxyCondif instance.</param>
		/// <param name="file">The XML file to read data from and store data to.</param>
		/// <exception cref="ArgumentNullException"><c>file</c> is null -or- <c>parent</c> is null.</exception>
        public ip_monitor(int_magic parent, string file)
        {
			if (file == null || parent == null)
				throw new ArgumentNullException();
			m_File = file;
			m_Parent = parent;
		}
		/// <summary>
		/// Reads a string from the settings section.
		/// </summary>
		/// <param name="name">The key to read from.</param>
		/// <returns>The string value that corresponds with the specified key, or an empty string if the specified key was not found in the collection.</returns>
		public string ReadString(string name) {
			return ReadString(name, "");
		}
		/// <summary>
		/// Reads a string from the settings section.
		/// </summary>
		/// <param name="name">The key to read from.</param>
		/// <param name="def">The default string to return.</param>
		/// <returns>The string value that corresponds with the specified key, or <c>def</c> if the specified key was not found in the collection.</returns>
		public string ReadString(string name, string def) {
			if (name == null)
				return def;
			if (!Settings.ContainsKey(name))
				return def;
			return Settings[name];
		}
		/// <summary>
		/// Reads a byte array from the settings section.
		/// </summary>
		/// <param name="name">The key to read from.</param>
		/// <returns>The array of bytes that corresponds with the specified key, or <c>null</c> if the specified key was not found in the collection.</returns>
		public byte[] ReadBytes(string name) {
			string ret = ReadString(name, null);
			if (ret == null)
				return null;
			return Convert.FromBase64String(ret);
		}
		/// <summary>
		/// Reads an integer from the settings section.
		/// </summary>
		/// <param name="name">The key to read from.</param>
		/// <returns>The integer that corresponds with the specified key, or 0 if the specified key was not found in the collection.</returns>
		public int ReadInt(string name) {
			return ReadInt(name, 0);
		}
		/// <summary>
		/// Reads an integer from the settings section.
		/// </summary>
		/// <param name="name">The key to read from.</param>
		/// <param name="def">The default integer to return.</param>
		/// <returns>The integer that corresponds with the specified key, or <c>def</c> if the specified key was not found in the collection.</returns>
		public int ReadInt(string name, int def) {
			if (name == null)
				return def;
			if (!Settings.ContainsKey(name))
				return def;
			return int.Parse(Settings[name]);
		}
		
        /// <summary>
        /// Saves a website name to the authentication list.
        /// </summary>
        /// <param name="wname">The website url to add.</param>
        /// <exception cref="ArgumentNullException"><c>website</c> is null.</exception>
        /// <exception cref="ArgumentException">The specified username is invalid.</exception>
        public void Savests(string sts)
        {
            Savests(sts, true);
        }
         /// <summary>
        /// Saves a website name to the authentication list.
        /// </summary>
        /// <param name="wname">The website url to add.</param>
        /// <param name="saveData">True if the data has to be written to the XML file, false otherwise.</param>
        /// <exception cref="ArgumentNullException"><c>website</c> is null.</exception>
        /// <exception cref="ArgumentException">The specified username is invalid.</exception>
        public void Savests(string sts, bool saveData)
        {
            if (sts == null)
                throw new ArgumentNullException();
            if (sList.IsstsPresent(sts) || sts == "")
                sList.RemoveItem(sts);
            sList.AddItem(sts);
            if (saveData)
                SaveData();
        }
        /// <summary>
        /// Removes a websites from the authentication list.
        /// </summary>
        /// <param name="wn">The websi url te to remove.</param>
        public void Removests(string s)
        {
            Removests(s, true);
        }
		
        /// <summary>
        /// Removes a IP from the authentication list.
        /// </summary>
        /// <param name="wn">The IP address to remove.</param>
        /// <param name="save">True if the data has to be written to the XML file, false otherwise.</param>
        public void Removests(string s, bool save)
        {
            if (s == null)
                throw new ArgumentNullException();
            sList.RemoveItem(s);
            if (save)
                SaveData();
        }
        /// <summary>
        /// Saves a IP address and time limit to the authentication list.
        /// </summary>
        /// <param name="ipname">The IP address to add.</param>
        /// <param name="tmlm">The Time limit to add.</param>
        /// <exception cref="ArgumentNullException"><c>IP address</c> is null.</exception>
        public void Saveipin(string pcname,string ipname)
        {
            Saveipin(pcname,ipname, true);
        }
		
        /// <summary>
        /// Saves a IP address and time limit to the authentication list.
        /// </summary>
        /// <param name="ipname">The IP address to add.</param>
        /// <param name="tmlm">The Time limit to add.</param>
        /// <param name="saveData">True if the data has to be written to the XML file, false otherwise.</param>
        /// <exception cref="ArgumentNullException"><c>IP address</c> is null.</exception>
        public void Saveipin(string pcname,string ipname, bool saveData)
        {
            if (pcname == null)
                throw new ArgumentNullException();
            if (ipnList.IspcPresent(pcname) || pcname == "")
                ipnList.RemoveItem(pcname);
            ipnList.AddItem(pcname,ipname);
            if (saveData)
                SaveData();
        }
		
        /// <summary>
        /// Removes a IP from the authentication list.
        /// </summary>
        /// <param name="ipn">The IP address to remove.</param>
        public void Removeipn(string pcn)
        {
            Removeipn(pcn, true);
        }
        /// <summary>
        /// Removes a websites from the authentication list.
        /// </summary>
        /// <param name="ipn">The websi url te to remove.</param>
        /// <param name="save">True if the data has to be written to the XML file, false otherwise.</param>
        public void Removeipn(string pcn, bool save)
        {
            if (pcn == null)
                throw new ArgumentNullException();
            ipnList.RemoveItem(pcn);
            if (save)
                SaveData();
        }

        /// <summary>
        /// Saves a IP address and time limit to the authentication list.
        /// </summary>
        /// <param name="ipname">The IP address to add.</param>
        /// <param name="tmlm">The Time limit to add.</param>
        /// <exception cref="ArgumentNullException"><c>IP address</c> is null.</exception>
        public void Saveipex(string pcname, string ipname)
        {
            Saveipex(pcname, ipname, true);
        }

        /// <summary>
        /// Saves a IP address and time limit to the authentication list.
        /// </summary>
        /// <param name="ipname">The IP address to add.</param>
        /// <param name="tmlm">The Time limit to add.</param>
        /// <param name="saveData">True if the data has to be written to the XML file, false otherwise.</param>
        /// <exception cref="ArgumentNullException"><c>IP address</c> is null.</exception>
        public void Saveipex(string pcname, string ipname, bool saveData)
        {
            if (pcname == null)
                throw new ArgumentNullException();
            if (ipxList.IspcPresent(pcname) || pcname == "")
                ipxList.RemoveItem(pcname);
            ipxList.AddItem(pcname, ipname);
            if (saveData)
                SaveData();
        }

        /// <summary>
        /// Removes a IP from the authentication list.
        /// </summary>
        /// <param name="ipn">The IP address to remove.</param>
        public void Removeipx(string pcn)
        {
            Removeipx(pcn, true);
        }
        /// <summary>
        /// Removes a websites from the authentication list.
        /// </summary>
        /// <param name="ipn">The websi url te to remove.</param>
        /// <param name="save">True if the data has to be written to the XML file, false otherwise.</param>
        public void Removeipx(string pcn, bool save)
        {
            if (pcn == null)
                throw new ArgumentNullException();
            ipxList.RemoveItem(pcn);
            if (save)
                SaveData();
        }
		/// <summary>
		/// Saves the data in this class to an XML file.
		/// </summary>*/
		public void SaveData() {
			XmlTextWriter writer = null;
			try {
				writer = new XmlTextWriter(File, Encoding.ASCII);
				writer.Indentation = 2;
				writer.Formatting = Formatting.Indented;
				writer.WriteStartElement("internet_magicProxy"); 
				// Write the version
				writer.WriteStartElement("Version");
				writer.WriteStartAttribute("", "value", "");
				writer.WriteString(Assembly.GetCallingAssembly().GetName().Version.ToString(2));
				writer.WriteEndAttribute();
				writer.WriteEndElement(); 
				// Write the settings
				Savests(writer);
				// Write the Authentication list to the file
				Saveipn(writer);
                Saveipx(writer);
                //Saveip(writer);
				// Write the Listeners list to the file
				//SaveListeners(writer);
				// Clean up
				writer.WriteEndElement(); 
			} catch {
			} finally {
				if (writer != null)
					writer.Close();
			}
		}
		
		/// <summary>
        /// Saves the web filter list to an XML writer.
        /// </summary>
        /// <param name="writer">The XML writer to save the list to.</param>
        private void Savests(XmlTextWriter writer)
        {
            writer.WriteStartElement("Status");
            string[] keys = sList.Keys;
            int k=0;
            if(keys.Length>1)
                  k = keys.Length - 1;
              for (int i = 0; i < keys.Length; i++)
            {
                
                if (keys[i] == "")
                {
                    
                    continue;
                }
                if (keys[i] != null || keys[i] !=" " )
                {
                    
                    writer.WriteStartElement(keys[i]);
                    writer.WriteEndElement();
                }
            }
            writer.WriteEndElement();
        }/// <summary>
        /// Saves the ip filter list to an XML writer.
        /// </summary>
        /// <param name="writer">The XML writer to save the list to.</param>
        private void Saveipn(XmlTextWriter writer)
        {
            writer.WriteStartElement("IP_Include");
            string[] keys = ipnList.Keys;
            string[] ipn = ipnList.ipname;
            int k = 0;
            if (keys.Length > 1)
                k = keys.Length - 1;
            for (int i = 0; i < keys.Length; i++)
            {

                if (keys[i] == "")
                {

                    continue;
                }
                if (keys[i] != null || keys[i] != " ")
                {
                    writer.WriteStartElement("PC");
                    writer.WriteStartAttribute("", "value", "");
                    writer.WriteString(keys[i]);
                    writer.WriteEndAttribute();
                    writer.WriteStartAttribute("", "name", "");
                    writer.WriteString(ipn[i]);
                    writer.WriteEndAttribute();
                    writer.WriteEndElement();
                }
            }
            writer.WriteEndElement();
        }
        /// <summary>
        /// Saves the ip filter list to an XML writer.
        /// </summary>
        /// <param name="writer">The XML writer to save the list to.</param>
        private void Saveipx(XmlTextWriter writer)
        {
            writer.WriteStartElement("IP_Exclude");
            string[] keys = ipxList.Keys;
            string[] ipn = ipxList.ipname;
            int k = 0;
            if (keys.Length > 1)
                k = keys.Length - 1;
            for (int i = 0; i < keys.Length; i++)
            {

                if (keys[i] == "")
                {

                    continue;
                }
                if (keys[i] != null || keys[i] != " ")
                {
                    writer.WriteStartElement("PC");
                    writer.WriteStartAttribute("", "value", "");
                    writer.WriteString(keys[i]);
                    writer.WriteEndAttribute();
                    writer.WriteStartAttribute("", "name", "");
                    writer.WriteString(ipn[i]);
                    writer.WriteEndAttribute();
                    writer.WriteEndElement();
                }
            }
            writer.WriteEndElement();
        }
		/// <summary>
		/// Loads the data from an XML file.
		/// </summary>
		public void LoadData() {
           
			if (!System.IO.File.Exists(File))
				throw new FileNotFoundException();
			XmlTextReader reader = null;
			try {
				reader = new XmlTextReader(File);
				// Read until we reach the internet_magicProxy element
				while (reader.Read() && reader.Name.ToLower() != "internet_magicproxy") {}
				// Read until we reach the internet_magicProxy element again (the end tag)
				while (reader.Read() && reader.Name.ToLower() != "internet_magicproxy") {
					if (!reader.IsEmptyElement) {
						switch(reader.Name.ToLower()) {
							case "status":
                                
								sList.Clear();
								Loadsts(reader);
								break;
							case "ip_include":
								ipnList.Clear();
								Loadipn(reader);
                                break;
                            case "ip_exclude":
                                
                                ipxList.Clear();
                                Loadipx(reader);
                                break;
                            
						}
					}
				}
			} catch {
				throw new XmlException ("Malformed XML initialisation file.", null);
			} finally {
				if (reader != null)
					reader.Close();
			}
		}
        
        /// <summary>
        /// Loads the website list from an XML file.
        /// </summary>
        /// <param name="reader">The XML reader to read the users from.</param>
        private void Loadsts(XmlTextReader reader)
        {
            // Read until we reach the Settings element end tag

            while (reader.Read() && reader.Name.ToLower() != "status")
            {
                
                if (reader.Name != null || reader.Name != "")

                {
                     Savests(reader.Name, false);
                    
                }
            }
           
        }
        /// <summary>
        /// Loads the IP address list from an XML file.
        /// </summary>
        /// <param name="reader">The XML reader to read the users from.</param>
        private void Loadipn(XmlTextReader reader)
        {
            // Read until we reach the Settings element end tag

            while (reader.Read() && reader.Name.ToLower() != "ip_include")
            {
                if (reader.Name != null && reader["value"] != null)
                {
                    Saveipin(reader["value"],reader["name"], false);

                }
            }

        }
        /// <summary>
        /// Loads the IP address list from an XML file.
        /// </summary>
        /// <param name="reader">The XML reader to read the users from.</param>
        private void Loadipx(XmlTextReader reader)
        {
            // Read until we reach the Settings element end tag

            while (reader.Read() && reader.Name.ToLower() != "ip_include")
            {
                if (reader.Name != null && reader["value"] != null)
                {
                    Saveipex(reader["value"],reader["name"], false);

                }
            }

        }
		
		/// <summary>
		/// Gets the full path to the XML data file.
		/// </summary>
		/// <value>A String that holds the full path to the XML data file.</value>
		public string File {
			get {
				return m_File;
			}
		}
		/// <summary>
		/// Gets the parent object of this ProxyConfig class.
		/// </summary>
		/// <value>An instance of the Proxy class.</value>
        public int_magic Parent
        {
			get {
				return m_Parent;
			}
		}
		/// <summary>
		/// Gets the dictionary that holds the settings.
		/// </summary>
		/// <value>An instance of the StringDictionary class that holds the settings.</value>
		private StringDictionary Settings {
			get {
				return m_Settings;
			}
		}
        
        /// <summary>
        /// Gets the website list.
        /// </summary>
        /// <value>An instance of the webfilter class that holds all the website url.</value>
        internal ip_mon_status sList
        {
            get
            {
                return m_sList;
            }
        }
        /// <summary>
        /// Gets the IP address list.
        /// </summary>
        /// <value>An instance of the IPfilter class that holds all the IP and their corresponding Time limit.</value>
        internal ip_incl ipnList
        {
            get
            {
                return m_ipnList;
            }
        }
        /// <summary>
        /// Gets the IP address list.
        /// </summary>
        /// <value>An instance of the IPfilter class that holds all the IP and their corresponding Time limit.</value>
        internal ip_excl ipxList
        {
            get
            {
                return m_ipxList;
            }
        }
        // private variables
		/// <summary>Holds the value of the File property.</summary>
		private string m_File;
		/// <summary>Holds the value of the Settings property.</summary>
		private StringDictionary m_Settings = new StringDictionary();
		/// <summary>Holds the value of the UserList property.</summary>
		//private AuthenticationList m_UserList = new AuthenticationList();
        /// <summary>Holds the value of the Website url List property.</summary>
        private ip_mon_status m_sList = new ip_mon_status();
        /// <summary>Holds the value of the IP address List property.</summary>
        private ip_incl m_ipnList = new ip_incl();
        /// <summary>Holds the value of the IP address List property.</summary>
        private ip_excl m_ipxList = new ip_excl();
		/// <summary>Holds the value of the Parent property.</summary>
		private int_magic m_Parent;
    }
}

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