3 Years
Discussion Span
Last Post by Ketsuekiame
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Net.Security;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Security.Authentication;

namespace ConsoleApplicationKAMI
   class Program
        static void Main(string[] args)
    public sealed class ProxyServer
        private TcpListener _listener;
        private Thread _listenerThread;

        public void Start()
            _listener = new TcpListener(IPAddress.Loopback, 8888);
            _listenerThread = new Thread(new ParameterizedThreadStart(Listen));

        public void Stop()
            //stop listening for incoming connections
            //wait for server to finish processing current connections...

        private static void Listen(Object obj)
            TcpListener listener = (TcpListener)obj;
                while (true)
                    TcpClient client = listener.AcceptTcpClient();
                    while (!ThreadPool.QueueUserWorkItem
                (new WaitCallback(ProxyServer.ProcessClient), client)) ;
            catch (ThreadAbortException) { }
            catch (SocketException) { }

        private static void ProcessClient(Object obj)
            TcpClient client = (TcpClient)obj;
                //do your processing here
                //read the first line HTTP command
                Stream clientStream = client.GetStream();
                StreamReader clientStreamReader = new StreamReader(clientStream);
                String httpCmd = clientStreamReader.ReadLine();

                //break up the line into three components
                String[] splitBuffer = httpCmd.Split(spaceSplit, 3);
                String method = splitBuffer[0];
                String remoteUri = splitBuffer[1];
                Version version = new Version(1, 0); //force everything to HTTP/1.0

                //this will be the web request issued on behalf of the client
                HttpWebRequest webReq;

                if (method == "CONNECT")
                    //Browser wants to create a secure tunnel
                    //instead = we are going to perform a man in the middle
                    //the user's browser should warn them of the certification errors however.
                    //Please note: THIS IS ONLY FOR TESTING PURPOSES - 
                    //you are responsible for the use of this code
                    //this is the URI we'll request on behalf of the client
                    remoteUri = "https://" + splitBuffer[1];
                    //read and ignore headers
                    while (!String.IsNullOrEmpty(clientStreamReader.ReadLine())) ;

                    //tell the client that a tunnel has been established
                    StreamWriter connectStreamWriter = new StreamWriter(clientStream);
                    connectStreamWriter.WriteLine("HTTP/1.0 200 Connection established");
                     (String.Format("Timestamp: {0}", DateTime.Now.ToString()));
                    connectStreamWriter.WriteLine("Proxy-agent: matt-dot-net");

                    //now-create an https "server"
                    sslStream = new SslStream(clientStream, false);
                    sslStream.AuthenticateAsServer(_certificate,false, SslProtocols.Tls | SslProtocols.Ssl3 | SslProtocols.Ssl2, true);

                    //HTTPS server created - we can now decrypt the client's traffic
            catch (Exception ex)
                //handle exception

Find out what the error is and decide yourself how to handle it. It may be recoverable, it may not be. If it isn't, you'll have to decide what to show the user and whether or not you need to close the application.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.