public static HttpRequestObject ReadRequest(Stream stream) { HttpRequestObject request = null; byte[] buffer = null; buffer = Read(stream); if (buffer == null) { return(null); } HttpRequestHeader header = new HttpRequestHeader(Encoding.UTF8.GetString(buffer)); if (header._isParse == false) { return(null); } HttpContent content = new HttpContent(); if (0 < header._contentSize) { content.Set(ReadContent(stream, header._contentSize)); } request = new HttpRequestObject(header, content); return(request); }
public static SslStreamSet Handshake(TcpClient client, HttpRequestObject request, Stream serverStream) { ConnectionInfo connectionInfo = ParseReceiveConnect(request); SslStreamSet _sslStream = new SslStreamSet(); // Connection Remote Server. SslTcpServer._traceLogger.OutputLog(string.Format("Connection Remote Server {0}:{1}.", connectionInfo.host, Convert.ToInt32(connectionInfo.port))); SslTcpClient clientSocket = new SslTcpClient(client); SslStream sslClientStream = clientSocket.Connect(connectionInfo.host, Convert.ToInt32(connectionInfo.port)); // Get Remote PublicKey. ServerCertificate hostCertificate = clientSocket.ServerCertificate; // Connection Established. ConnectionEstablished(serverStream, connectionInfo.protocol); // Authenticate to Client by Self PublicKey. SslTcpServer._traceLogger.OutputLog("Authenticate with Client by Self PublicKey."); SslStream sslServerStream = AuthenticateAsServer(serverStream); // Output log. OutputSslInfo(sslServerStream, sslClientStream, SslTcpServer._traceLogger); _sslStream._serverStream = sslServerStream; _sslStream._clientStream = sslClientStream; return(_sslStream); }
private static ConnectionInfo ParseReceiveConnect(HttpRequestObject request) { ConnectionInfo info = new ConnectionInfo(); string[] s = request._header._path.Split(':'); info.host = s[0]; info.port = s[1]; info.protocol = request._header._httpVersion; SslTcpServer._traceLogger.OutputLog(request._header._source.Replace(System.Environment.NewLine, "\t")); return(info); }
public static void Write(HttpObject http, Stream stream) { byte[] bytes = null; stream.WriteTimeout = SslTcpServer._write_timeout; if (http.GetType() == typeof(HttpRequestObject)) { HttpRequestObject request = (HttpRequestObject)http; bytes = Encoding.UTF8.GetBytes(request._header._source); } else if (http.GetType() == typeof(HttpResponseObject)) { HttpResponseObject response = (HttpResponseObject)http; bytes = Encoding.UTF8.GetBytes(response._header._source); } Write(stream, bytes); if (0 < http._content._length) { Write(stream, http._content.Get()); } }
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); }
public HttpRequestObject OnHttpRequestServer(HttpRequestObject request, Stream serverStream, Stream clientStream) { _logger.OutputLog("Request Proxy -> Server"); _logger.OutputLog(request._header._source); return(request); }
public static bool IsSslConnection(HttpRequestObject request) { return(request._header._method == HTTP_METHOD.CONNECT); }