[Owasp-proxy] Defragmenting tcp packets

Martin Holst Swende martin at swende.se
Sun Apr 3 16:12:35 EDT 2011


Hi Rogan et al,

Some time ago we discussed defragmentation of intercepted tcp packets.
Just wanted to make a 'ping' about this proposed patched version of
run() for the RelayInterceptor. It would be really nice to get it in
there, so I can use a 'vanilla' version of owasp-proxy instead of a
patched one.
Regards,
Martin Swende


    public void run() {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
            byte[] readBuffer = new byte[4096];
            byte[] finalBuffer;
            int got;
            while ((got = in.read(readBuffer)) > -1 && !done) {
                baos.write(readBuffer,0,got);
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                while(in.available()  > 0 )
                {
                    got = in.read(readBuffer);
                    baos.write(readBuffer,0,got);
                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                finalBuffer = baos.toByteArray();
                baos.reset();
                interceptor.received(this, finalBuffer, 0,
finalBuffer.length);
            }
        } catch (IOException ioe) {
            interceptor.readException(this, ioe);
        } finally {
            interceptor.inputClosed(this);
        }
        try {
            while (!done) {
                synchronized (lock) {
                    lock.wait(1000);
                }
            }
        } catch (InterruptedException ie) {
            // we're done, return
        }
        if (closer != null)
            closer.run();
    }


Alternative diff-version:
---------------------------------

--- a/src/main/java/org/owasp/proxy/tcp/RelayInterceptor.java
+++ b/src/main/java/org/owasp/proxy/tcp/RelayInterceptor.java
@@ -1,5 +1,6 @@
 package org.owasp.proxy.tcp;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -36,10 +37,30 @@ class RelayInterceptor<C, S> extends Thread
implements Strea
 
        public void run() {
                try {
-                       byte[] buff = new byte[4096];
+                       ByteArrayOutputStream baos = new
ByteArrayOutputStream(4
+                       byte[] readBuffer = new byte[4096];
+                       byte[] finalBuffer;
                        int got;
-                       while ((got = in.read(buff)) > -1 && !done) {
-                               interceptor.received(this, buff, 0, got);
+                       while ((got = in.read(readBuffer)) > -1 && !done) {
+                               baos.write(readBuffer,0,got);
+                               try {
+                                       Thread.sleep(5);
+                               } catch (InterruptedException e) {
+                                       e.printStackTrace();
+                               }
+                               while(in.available()  > 0 )
+                               {
+                                       got = in.read(readBuffer);
+                                       baos.write(readBuffer,0,got);
+                                       try {
+                                               Thread.sleep(5);
+                                       } catch (InterruptedException e) {
+                                               e.printStackTrace();
+                                       }
+                               }
+                               finalBuffer = baos.toByteArray();
+                               baos.reset();
+                               interceptor.received(this, finalBuffer,
0, final
                        }
                } catch (IOException ioe) {
                        interceptor.readException(this, ioe);




More information about the Owasp-proxy-project mailing list