private static void InitiateHttpClientRequestProcessing(object clientTcpObj)
        {
            TcpClient  tcpClient  = (TcpClient)clientTcpObj;
            string     clientIp   = string.Empty;
            string     clientPort = string.Empty;
            string     clientMac  = string.Empty;
            RequestObj requestObj = new RequestObj(Config.DefaultRemoteHost, ProxyProtocol.Http);

            // Determine tcpClient IP and MAC address.
            try
            {
                Logging.Instance.LogMessage("TcpListener", ProxyProtocol.Http, Loglevel.Debug, "InitiateHttpsClientRequestProcessing(): New HTTP request initiated");
                string[] splitter = tcpClient.Client.RemoteEndPoint.ToString().Split(new char[] { ':' });
                clientIp   = splitter[0];
                clientPort = splitter[1];
            }
            catch (Exception ex)
            {
                Console.WriteLine("InitiateHttpClientRequestProcessing(Exception): {0}", ex.Message);
            }

            try
            {
                clientMac = Lib.Common.GetMacFromNetworkComputer(clientIp);
            }
            catch (Exception)
            {
                clientMac = "00:00:00:00:00:00";
            }

            requestObj.SrcMac              = clientMac;
            requestObj.SrcIp               = clientIp;
            requestObj.SrcPort             = clientPort;
            requestObj.TcpClientConnection = tcpClient;

            // Open tcpClient system's data clientStream
            try
            {
                requestObj.ClientRequestObj.ClientBinaryReader = new MyBinaryReader(requestObj.ProxyProtocol, requestObj.TcpClientConnection.GetStream(), 8192, Encoding.UTF8, requestObj.Id);
                requestObj.ClientRequestObj.ClientBinaryWriter = new BinaryWriter(requestObj.TcpClientConnection.GetStream());

                RequestHandlerHttp requestHandler = new RequestHandlerHttp(requestObj);
                requestHandler.ProcessClientRequest();
            }
            catch (Exception ex)
            {
                Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Debug, "ProxyServer.InitiateHttpClientRequestProcessing(EXCEPTION): {0}", ex.Message);

                if (ex.InnerException is Exception)
                {
                    Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Debug, "ProxyServer.InitiateHttpClientRequestProcessing(INNEREXCEPTION): {0}, {1}", ex.InnerException.Message, ex.GetType().ToString());
                }
            }
            finally
            {
                if (requestObj.ClientRequestObj.ClientBinaryReader != null)
                {
                    requestObj.ClientRequestObj.ClientBinaryReader.Close();
                    Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Debug, "ProxyServer.InitiateHttpClientRequestProcessing(): ClientBinaryReader.Close()");
                }

                if (requestObj.ClientRequestObj.ClientBinaryWriter != null)
                {
                    requestObj.ClientRequestObj.ClientBinaryWriter.Close();
                    Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Debug, "ProxyServer.InitiateHttpClientRequestProcessing(): ClientBinaryWriter.Close()");
                }

                if (requestObj.ServerRequestHandler != null)
                {
                    requestObj.ServerRequestHandler.CloseServerConnection();
                    Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Debug, "ProxyServer.InitiateHttpClientRequestProcessing(): ServerRequestHandler.CloseServerConnection())");
                }

                if (requestObj.TcpClientConnection != null)
                {
                    requestObj.TcpClientConnection.Close();
                    Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Debug, "ProxyServer.InitiateHttpClientRequestProcessing(): TcpClientConnection.Close()");
                }
            }
        }
Beispiel #2
0
        private static void InitiateHttpsClientRequestProcessing(object clientTcpObj)
        {
            TcpClient  tcpClient  = (TcpClient)clientTcpObj;
            var        clientIp   = string.Empty;
            var        clientPort = string.Empty;
            var        clientMac  = string.Empty;
            RequestObj requestObj = new RequestObj(Config.DefaultRemoteHost, ProxyProtocol.Https);

            // Determine tcpClient IP and MAC address.
            try
            {
                Logging.Instance.LogMessage("TcpListener", ProxyProtocol.Https, Loglevel.Debug, "InitiateHttpsClientRequestProcessing(): New HTTPS request initiated");
                string[] splitter = tcpClient.Client.RemoteEndPoint.ToString().Split(new char[] { ':' });
                clientIp   = splitter[0];
                clientPort = splitter[1];
            }
            catch (Exception ex)
            {
                Console.WriteLine($"InitiateHttpsClientRequestProcessing(Exception): {ex.Message}");
            }

            try
            {
                clientMac = Lib.Common.GetMacFromNetworkComputer(clientIp);
            }
            catch (Exception)
            {
                clientMac = "00:00:00:00:00:00";
            }

            requestObj.SrcMac              = clientMac;
            requestObj.SrcIp               = clientIp;
            requestObj.SrcPort             = clientPort;
            requestObj.TcpClientConnection = tcpClient;

            // Open tcpClient system's data clientStream
            try
            {
                var sslStream = new SslStream(requestObj.TcpClientConnection.GetStream(), false, new RemoteCertificateValidationCallback(remoteCertificateValidation));
                sslStream.AuthenticateAsServer(serverCertificate2, false, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Ssl3, false);

                requestObj.ClientRequestObj.ClientBinaryReader = new MyBinaryReader(requestObj.ProxyProtocol, sslStream, 8192, Encoding.UTF8, requestObj.Id);
                requestObj.ClientRequestObj.ClientBinaryWriter = new BinaryWriter(sslStream);

                RequestHandlerHttp requestHandler = new RequestHandlerHttp(requestObj);
                requestHandler.ProcessClientRequest();
            }
            catch (Exception ex)
            {
                Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Error, $"ProxyServer.InitiateHttpsClientRequestProcessing(EXCEPTION): {ex.Message}\r\n{ex.GetType().ToString()}");
                if (ex.InnerException is Exception)
                {
                    Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Error, $"ProxyServer.InitiateHttpsClientRequestProcessing(INNEREXCEPTION): {ex.InnerException.Message}, {ex.GetType().ToString()}");
                }
            }
            finally
            {
                if (requestObj.ClientRequestObj.ClientBinaryReader != null)
                {
                    requestObj.ClientRequestObj.ClientBinaryReader.Close();
                    Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Debug, "ProxyServer.InitiateHttpsClientRequestProcessing(): ClientBinaryReader.Close()");
                }

                if (requestObj.ClientRequestObj.ClientBinaryWriter != null)
                {
                    requestObj.ClientRequestObj.ClientBinaryWriter.Close();
                    Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Debug, "ProxyServer.InitiateHttpsClientRequestProcessing(): ClientBinaryWriter.Close()");
                }

                if (requestObj.ServerRequestHandler != null)
                {
                    requestObj.ServerRequestHandler.CloseServerConnection();
                    Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Debug, "ProxyServer.InitiateHttpsClientRequestProcessing(): ServerRequestHandler.CloseServerConnection())");
                }

                if (requestObj.TcpClientConnection != null)
                {
                    requestObj.TcpClientConnection.Close();
                    Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Debug, "ProxyServer.InitiateHttpsClientRequestProcessing(): TcpClientConnection.Close()");
                }
            }
        }