Beispiel #1
0
        public override void OnClientDataReceived(object sender, TCP.DataEventArgs e)
        {
            // Attempt to learn more about the incoming connection
            requestData.ip     = e.Request.IP;
            requestData.port   = e.Request.Port;
            requestData.isHTTP = DataHandlerDeDupe.IsHTTP(e.Buffer);
            if (requestData.isHTTP)
            {
                //If HTTP then retrieve hostname from request.
                requestData.requestType = DataHandlerDeDupe.IsRequest(e.Buffer);
                int start = e.Buffer.FindString("Host:") + 6;
                int end   = e.Buffer.FindString("\r\n", start + 1);
                requestData.host = e.Buffer.GetBetween(start, end);
                requestData.host = requestData.host.Replace(":" + requestData.port, "");
                // Get URI
                requestData.uRI = GetURI(e.Buffer);

                Console.Write(Environment.NewLine + "<DeDupe> [Client received] HTTP        - RequestType:" + requestData.requestType.ToString() + " Address:" + requestData.host + " Port:" + requestData.port.ToString() + Environment.NewLine);

                requestData.isChunked = DataHandlerDeDupe.IsChunked(e.Buffer);
                Console.Write("<DeDupe> [Client received] HTTP Header - Chunked:" + DataHandlerDeDupe.IsChunked(e.Buffer).ToString() + Environment.NewLine);
            }
            else
            {
                requestData.host = e.Request.Address;

                Console.Write("<DeDupe> Received Client Data - Address:" + requestData.host + " Port:" + requestData.port.ToString() + Environment.NewLine);
            }
            //END


            //Console.Write("----" + Environment.NewLine + "[ClientDataReceived]" + Environment.NewLine + e.Buffer.GetBetween(0, e.Buffer.Length - 1) + Environment.NewLine + "----" + Environment.NewLine);
            //Console.Write("<DeDupe> Received Client Data" + Environment.NewLine);

            //if data is HTTP, make sure it's not compressed so we can capture it in plaintext.
            if (e.Buffer.FindString(" HTTP/1.1") != -1 && e.Buffer.FindString("Accept-Encoding") != -1)
            {
                int x = e.Buffer.FindString("Accept-Encoding:");
                int y = e.Buffer.FindString("\r\n", x + 1);
                e.Buffer = e.Buffer.ReplaceBetween(x, y, Encoding.ASCII.GetBytes(replaceWith));
                e.Buffer = e.Buffer.ReplaceString("HTTP/1.1", "HTTP/1.0");
                e.Count  = e.Count - (y - x) + replaceWith.Length;
            }
        }
Beispiel #2
0
        public override void OnServerDataReceived(object sender, TCP.DataEventArgs e)
        {
            if (e.Request.Error != Socks.SocksError.Granted || e.Buffer.Length == 0)
            {
                Console.Write("<DeDupe> Error:" + e.Request.Error + " Length:" + e.Buffer.Length.ToString() + Environment.NewLine);
                return;
            }

            requestData.packetSize = e.Count;


            // Special handling for HTTP
            if (requestData.isHTTP)
            {
                // Handle HTTP header gracefully
                if (DataHandlerDeDupe.IsHTTPResponse200(e.Buffer) || requestData.isHeader)
                {
                    requestData.isHeader      = true;
                    requestData.isChunked     = DataHandlerDeDupe.IsChunked(e.Buffer);
                    requestData.contentLength = GetContentLength(e.Buffer);
                    requestData.totalPackets  = Math.Ceiling((float)(requestData.contentLength) / (float)packetSize);
                    Console.Write(Environment.NewLine + "<DeDupe> [Server response] HTTP        - RequestType:" + requestData.requestType.ToString() + " Address:" + requestData.host + " Port:" + requestData.port.ToString() + " URL:" + requestData.uRI + Environment.NewLine);
                    Console.Write("<DeDupe> [Server response] HTTP Header - " + "ContentLength:" + requestData.contentLength + " ExpectedPackets:" + requestData.totalPackets + " Chunked:" + DataHandlerDeDupe.IsChunked(e.Buffer).ToString() + Environment.NewLine);

                    requestData.startPayload = e.Buffer.FindString("\r\n\r\n");
                    if (requestData.startPayload != -1)
                    {
                        requestData.startPayload   += 4;
                        requestData.currentposition = 0;
                        requestData.isHeader        = false;
                    }
                    if (requestData.contentLength != 0)
                    {
                        stripeBuffer = new ReadWriteBuffer(requestData.contentLength + 1);
                    }

                    requestData.currentRound = 0;
                }
                //else requestData.startPayload = 0;
                ///END

                if (requestData.startPayload > 0)
                {
                    //int stripeCount = stripeBuffer.Count;
                    //int stripeSpaceRemaining = stripeSize - stripeCount - 1;
                    int payloadLength    = Math.Min((requestData.contentLength - requestData.currentposition), requestData.packetSize - requestData.startPayload);
                    int contentRemaining = requestData.contentLength - requestData.currentposition;


                    if (requestData.isChunked)
                    {
                        //Console.Write("Chunk Size = " + Chunked.GetChunkSize(e.Buffer, 10).ToString() + Environment.NewLine);
                        //e.Buffer = Chunked.GetChunkData(e.Buffer,
                        //.GetChunkSize(e.Buffer, e.Count));

                        //Chunked chunked = new Chunked(f, e.Buffer, e.Buffer.Length)
                    }
                    else
                    {
                        /*xxHash hash0 = new xxHash();
                         * hash0.Init();
                         * hash0.Update(e.Buffer, e.Count);
                         * Key = hash0.Digest().ToString();*/


                        //if (Dedupe.StoreObject(Key, e.Buffer, out Chunks)) Console.WriteLine("<DeDupe> Stored: " + Key + Environment.NewLine);
                        Key = "HTTP/" + WebUtility.UrlEncode(requestData.host + requestData.port + requestData.uRI);
                        if (Dedupe.ObjectExists(Key))
                        {
                            Console.Write("x");
                        }
                        else
                        {
                            // Stream entire payload into buffer to maximise efficiency
                            int writeLength = Math.Min(e.Count - requestData.startPayload, (requestData.contentLength - requestData.currentposition));
                            if (contentRemaining > 0)
                            {
                                byte[] shortBuffer = new byte[writeLength];
                                try
                                {
                                    shortBuffer = e.Buffer.GetInBetween(requestData.startPayload, requestData.startPayload + writeLength);
                                    requestData.startPayload = 0;

                                    try
                                    {
                                        Console.Write(Environment.NewLine + "---------------------------------------" + Key + Environment.NewLine + shortBuffer.GetBetween(0, Math.Max(shortBuffer.Length, 10)) + Environment.NewLine + "---------------------------------------" + Environment.NewLine);

                                        stripeBuffer.Write(shortBuffer);
                                        requestData.currentposition += shortBuffer.Length;

                                        //Console.WriteLine("---!!----" + shortBuffer.Length + "/" +  stripeBuffer.Count + "---------!!----");
                                    }
                                    catch
                                    {
                                        Console.Write("<DeDupe> bufferLength:" + stripeBuffer.Count + " shortBufferLength:" + shortBuffer.Length + " currentposition:" + requestData.currentposition + " contentRemaining:" + contentRemaining + Environment.NewLine);
                                        throw new Exception("<DeDupe> Write ERROR to resourceBuffer");
                                    }
                                }
                                catch
                                {
                                    Console.Write("<DeDupe> writeLength:" + writeLength + " shortBufferLength:" + shortBuffer + " bufferCount:" + e.Count + " length:" + e.Buffer.Length + " startPayload:" + requestData.startPayload + Environment.NewLine);
                                    throw new Exception("<DeDupe> Write ERROR to shortBuffer");
                                }


                                Console.Write("-");
                            }

                            // When buffer is full
                            if ((contentRemaining == writeLength) && (stripeBuffer.Count != 0))
                            {
                                // Open a stream if none already exists
                                if (requestData.cacheStream == null)
                                {
                                    Key = "HTTP/" + WebUtility.UrlEncode(requestData.host + requestData.port + requestData.uRI);


                                    //byte[] derp = new byte[stripeBuffer.Count];
                                    //derp = stripeBuffer.Read(stripeBuffer.Count);
                                    //Console.WriteLine("---!!----" + stripeBuffer.Count + "---------!!----");

                                    if (Dedupe == null)
                                    {
                                        throw new Exception("<DeDupe> Library pointer == null");
                                    }

                                    try
                                    {
                                        int    derpCount = stripeBuffer.Count;
                                        byte[] derp      = new byte[derpCount];
                                        derp = stripeBuffer.Read(derpCount);
                                        //Console.Write(Environment.NewLine + "---------------------------------------" + Environment.NewLine + derp.GetBetween(0, derpCount) + Environment.NewLine + "---------------------------------------" + Environment.NewLine);


                                        Dedupe.StoreObject(Key, derp, out Chunks);
                                    }
                                    catch
                                    {
                                        Console.Write(Environment.NewLine + "<DeDupe> [ERROR] Failed to open output stream!" + Environment.NewLine + "                 Key Named:" + Key + Environment.NewLine);
                                        Console.Write("                 Key Found:" + Dedupe.ObjectExists(Key).ToString() + Environment.NewLine);
                                        Console.Write("contentRemaining:" + contentRemaining + " writeLength:" + writeLength + " stripeBufferCount:" + stripeBuffer.Count + Environment.NewLine);

                                        throw new Exception(Environment.NewLine + "<DeDupe> [FATAL ERROR] Unable to write to cache:" + Key + Environment.NewLine);
                                    }
                                }

                                Console.Write(Environment.NewLine + "<DeDupe> (STORED) length:" + requestData.contentLength + " key:" + Key + Environment.NewLine);
                            }
                        }
                    }
                    //We maintain this per 4k socks packet.
                    // Update position
                    //requestData.currentposition += payloadLength;
                }
            }
            else
            {
                Console.Write("<DeDupe> [Server response] DATA - RequestType:" + requestData.requestType.ToString() + " - Chunked:" + DataHandlerDeDupe.IsChunked(e.Buffer).ToString() + " Address:" + requestData.host + " Port:" + requestData.port.ToString() + Environment.NewLine);
            }

            //if data is HTTP, make sure it's not compressed so we can capture it in plaintext.

            /*if (e.Buffer.FindString(" HTTP/1.1") != -1 && e.Buffer.FindString("Accept-Encoding") != -1)
             * {
             *  int x = e.Buffer.FindString("Accept-Encoding:");
             *  int y = e.Buffer.FindString("\r\n", x + 1);
             *  e.Buffer = e.Buffer.ReplaceBetween(x, y, Encoding.ASCII.GetBytes(replaceWith));
             *  e.Buffer = e.Buffer.ReplaceString("HTTP/1.1", "HTTP/1.0");
             *  e.Count = e.Count - (y - x) + replaceWith.Length;
             * }*/
        }