示例#1
0
        static void Main(string[] args)
        {
            // Comment out this line if you do not want to use a firewall in the proxy
            Firewall.Initialise();

            // Starts the Proxy Server listening on port 7777
            ProxyServer proxy = new ProxyServer();

            proxy.Start();

            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("Server started on localhost:7777...");
            Console.WriteLine("Type 'exit' to shut down the server.");
            Console.WriteLine("Type 'block' followed by the url of the website you wish to block to add the url to the firewall.");
            Console.ForegroundColor = ConsoleColor.Green;

            bool isRunning = true;

            while (isRunning)
            {
                string line = Console.ReadLine();

                string[] arguments = line.Trim().Split(null);

                string command = arguments[0];

                if (command.ToLower() == "exit")
                {
                    isRunning = false;
                }
                else if (command.ToLower() == "block")
                {
                    Firewall.BlockURL(".*" + arguments[1]);
                }
                else if (command.ToLower() == "unblock")
                {
                    Firewall.UnblockURL(".*" + arguments[1]);
                }
            }

            Console.ForegroundColor = ConsoleColor.Cyan;

            Console.WriteLine("Shutting down");

            proxy.Stop();

            Console.WriteLine("Server stopped");
            Console.WriteLine("Press enter to exit");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.ReadLine();
        }
示例#2
0
        // Takes a client and a request and handles the request.
        // Will check if the request is HTTP or HTTPS and deal with it appropriately.
        public static void HandleRequest(TcpClient inClient, string[] request)
        {
            // Creates a header with the request. This is just a small class to seperate out the hostname,
            // port and other parts from the request and present it nicely.
            Header header = new Header(request);

            StringBuilder builder = new StringBuilder();

            foreach (var item in request)
            {
                builder.Append(item + " ");
            }
            builder.Append("\r\n");

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(builder.ToString());
            Console.ForegroundColor = ConsoleColor.Green;

            // If the firewall is active and the requested host is banned then a 403 will be sent back as a reponse
            // otherwise the request type is ussed to see if it is HTTP or HTTPS and the correct method is then used.
            if (!Firewall.IsActive || Firewall.isAllowed(header.Hostname))
            {
                try
                {
                    // If the request type is CONNECT then it is HTTPs
                    if (header.RequestType == "CONNECT")
                    {
                        TcpClient outClient = new TcpClient(header.Hostname, header.Port);
                        HandleHttpsRequest(inClient, outClient, header);
                    }
                    else
                    {
                        HandleHttpRequest(inClient, header);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            else
            {
                SendConnectionForbidden(inClient.GetStream(), header);
            }
        }