Пример #1
0
        private static void ConnectionEstablished(Stream stream, string method)
        {
            string sslConnectResponse = string.Format(SERVER_REPONSE.CONNECTION_ESTABLISHED,
                                                      method, System.Environment.NewLine + System.Environment.NewLine);

            HttpStream.Write(stream, Encoding.UTF8.GetBytes(sslConnectResponse));

            SslTcpServer._traceLogger.OutputLog(sslConnectResponse.Replace(System.Environment.NewLine, "\t"));
        }
Пример #2
0
        static public void Proxy(TcpClient serverSocket, IHttpEventListener listner)
        {
            Stream    server       = serverSocket.GetStream();
            Stream    client       = null;
            TcpClient clinetSocket = null;
            bool      bConnect     = false;
            string    host         = string.Empty;
            bool      bSSL         = false;


            while (true)
            {
                //-----------------------------------------
                // Request.
                // Clinet -> Proxy
                //-----------------------------------------

                HttpRequestObject request = null;
                try
                {
                    request = HttpStream.ReadRequest(server);

                    if (request == null)
                    {
                        break;
                    }

                    //-----------------------------------------
                    // SSL Handshake.
                    //-----------------------------------------
                    if (SslHandshake.IsSslConnection(request))
                    {
                        SslStreamSet sslset = SslHandshake.Handshake(clinetSocket, request, server);
                        server   = sslset._serverStream;
                        client   = sslset._clientStream;
                        bConnect = true;
                        bSSL     = true;
                        continue;
                    }
                    //-----------------------------------------
                    // HTTP Conection.
                    //-----------------------------------------
                    else if (bSSL == false && (bConnect == false || host != request._header._host))
                    {
                        if (clinetSocket != null)
                        {
                            client.Close();
                            clinetSocket.Close();
                        }

                        clinetSocket = new TcpClient();
                        clinetSocket.Connect(request._header._host, request._header._port);
                        client   = clinetSocket.GetStream();
                        bConnect = true;
                    }

                    //-----------------------------------------
                    // save host.
                    //-----------------------------------------
                    host = request._header._host;


                    //-----------------------------------------
                    // Observer.
                    //-----------------------------------------
                    if (listner != null)
                    {
                        request = listner.OnHttpRequestClient(request, server, client);
                    }
                }catch (Exception ex) {
                    Close(serverSocket, clinetSocket, server, client);
                    throw ex;
                }


                //-----------------------------------------
                // Modify Request.
                // Proxy -> Server
                //-----------------------------------------
                try
                {
                    HttpHeader.ModifyProxyRequest(request._header);
                }catch (Exception ex) {
                    Close(serverSocket, clinetSocket, server, client);
                    throw ex;
                }



                //-----------------------------------------
                // Request.
                // Proxy -> Server
                //-----------------------------------------
                try
                {
                    HttpStream.Write(request, client);

                    //-----------------------------------------
                    // Observer.
                    //-----------------------------------------
                    if (listner != null)
                    {
                        request = listner.OnHttpRequestServer(request, server, client);
                    }
                }
                catch (Exception ex)
                {
                    Close(serverSocket, clinetSocket, server, client);
                    throw ex;
                }



                //-----------------------------------------
                // Response.
                // Proxy <- Server
                //-----------------------------------------
                HttpResponseObject response = null;
                try
                {
                    response = HttpStream.ReadResponse(client);
                    if (response == null)
                    {
                        break;
                    }

                    //-----------------------------------------
                    // Observer.
                    //-----------------------------------------
                    if (listner != null)
                    {
                        response = listner.OnHttpResponseClient(response, server, client);
                    }
                }
                catch (Exception ex)
                {
                    Close(serverSocket, clinetSocket, server, client);
                    throw ex;
                }



                //-----------------------------------------
                // Response.
                // Client <- Proxy
                //-----------------------------------------
                try
                {
                    HttpStream.Write(response, server);

                    //-----------------------------------------
                    // Observer.
                    //-----------------------------------------
                    if (listner != null)
                    {
                        response = listner.OnHttpResponseServer(response, server, client);
                    }
                }
                catch (Exception ex)
                {
                    Close(serverSocket, clinetSocket, server, client);
                    throw ex;
                }


                if (request._header._isKeepAllive == false || response._header._isKeepAllive == false)
                {
                    break;
                }
            }

            Close(serverSocket, clinetSocket, server, client);
        }
Пример #3
0
 public void Write(string message)
 {
     HttpStream.Write(sslStream, Encoding.UTF8.GetBytes(message));
 }
Пример #4
0
 public string Read()
 {
     return(Encoding.UTF8.GetString(HttpStream.Read(sslStream)));
 }