Exemplo n.º 1
0
        private void InitHttps()
        {
            try
            {
                Stream clientStream = new NetworkStream(socket, false);

                StreamWriter connectStreamWriter = new StreamWriter(clientStream);
                connectStreamWriter.WriteLine("HTTP/1.0 200 Connection established");
                connectStreamWriter.WriteLine($"Timestamp: {DateTime.Now.ToString(CultureInfo.InvariantCulture)}");
                connectStreamWriter.WriteLine("Proxy-agent: matt-dot-net");
                connectStreamWriter.WriteLine();
                connectStreamWriter.Flush();

                var certificate = new X509Certificate2("<your certificate path>", "<your certificate password>");

                var sslStream = new SslStream(clientStream, false);
                sslStream.AuthenticateAsServer(certificate, false, enabledSslProtocols: SslProtocols.Tls, checkCertificateRevocation: true);

                var clientStreamReader = new StreamReader(sslStream);

                string clientRequest = clientStreamReader.ReadToEnd();

                request = new ProxyRequest(clientRequest);
                request.AddHostAndHttpsProtocolToTarget(host);
                var response        = GetHttpWebResponse(out var resp);
                var responseToFlush = Get200RawText(response.Headers, resp);

                StreamWriter responseStreamReader = new StreamWriter(sslStream);
                responseStreamReader.Write(responseToFlush);
                responseStreamReader.Flush();

                Console.WriteLine("Send HTTPS response");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                socket.Shutdown(SocketShutdown.Both);
                socket.Disconnect(false);
            }
        }
Exemplo n.º 2
0
 public ProxyTunnel(Socket socket, ProxyRequest request)
 {
     this.socket  = socket;
     this.request = request;
 }