protected override void Run() { StreamState streamState = new StreamState() { Buffer = new byte[m_client.SendBufferSize], ClientStream = m_client.GetStream(), ServerStream = null, }; Thread DoTransfer = new Thread(DoTransferData) { Name = "Transfer Data", IsBackground = true }; // Extract remote host endpoint TcpClient server = null; try { string DomainEndPoint = GetHostHeader(m_request); IPEndPoint RemoteEndPoint = GetRemoteHostEndPoint(DomainEndPoint); // Connect to remote host by received IPEndPoint server = new TcpClient(); server.Connect(RemoteEndPoint); streamState.ServerStream = server.GetStream(); DoTransfer.Start(streamState); } catch (ArgumentNullException) { ExitInstance(); return; } List <byte> ReceivedBytes = new List <byte>(Encoding.ASCII.GetBytes(m_request)); do { int ErrorCode = ProxyOperation.Write(server.GetStream(), ReceivedBytes.ToArray(), 0, ReceivedBytes.Count); if (ErrorCode < 0) { // An error has occurred => proceed to close thread Logging.Log(string.Format("SocketException occured with error code: {0}", ErrorCode), LoggingLevel.Error); server.Close(); m_client.Close(); } ReceivedBytes = ProxyOperation.ReadRequest(m_client.GetStream()); } while (ReceivedBytes.Count != 0); ExitInstance(); }
protected override void Run() { // Get host EndPoint string[] domain = GetDomain(m_request); IPEndPoint hostEndPoint = GetHostEndPoint(domain[0], domain[1]); if (hostEndPoint == null) { ExitInstance(); return; } // Connect to remote host and authenticate connection TcpClient server = new TcpClient(); server.Connect(hostEndPoint); // Accept tunneling byte[] AcceptConnection = Encoding.ASCII.GetBytes("HTTP/1.1 200 Established\r\n\r\n"); ProxyOperation.Write(m_client.GetStream(), AcceptConnection, 0, AcceptConnection.Length); StreamState streamState = new StreamState() { ClientStream = m_client.GetStream(), ServerStream = server.GetStream(), }; Thread DoTransfer = new Thread(DoTransferData) { Name = "HTTPS Transfer Data", IsBackground = true }; DoTransfer.Start(streamState); List <byte> ReceivedBytes; do { ReceivedBytes = ProxyOperation.ReadRequest(m_client.GetStream()); int errCode = ProxyOperation.Write(server.GetStream(), ReceivedBytes.ToArray(), 0, ReceivedBytes.Count); if (errCode < 0) { ReceivedBytes.Clear(); Logging.Log(string.Format("Socket Exception with error code: {0}", errCode), LoggingLevel.Error); } } while (ReceivedBytes.Count != 0); ExitInstance(); }
protected override void Run() { // Read bytes and traslate client's request into string List <byte> ReceivedBytes = ProxyOperation.ReadRequest(m_client.GetStream()); string rawRequest = Encoding.ASCII.GetString(ReceivedBytes.ToArray()); if (string.IsNullOrEmpty(rawRequest) == false) { Logging.Log(rawRequest); // parsing request and extract its method string RequestMethod = GetMethod(rawRequest); // Check if request's method is supported if (SupportedMethods.IndexOf(RequestMethod) < 0) { byte[] response = Encoding.ASCII.GetBytes("501 Not Implemented\r\n\r\n"); m_client.GetStream().Write(response, 0, response.Length); } else { // Create Protocol Processor denpending on what method is received if (RequestMethod == "GET" || RequestMethod == "POST") { HttpClient processor = new HttpClient(m_client, rawRequest); processor.Start("Http Handler", true); } else { HttpsClient processor = new HttpsClient(m_client, rawRequest); processor.Start("Https Handler", true); } m_client = null; } } ExitInstance(); }