directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r226914 - in /directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/ldap: ./ proxy/ proxy/gui/ proxy/gui/MainFrame.java proxy/impl/ proxy/impl/LdapProxy.java proxy/impl/ProxyConnection.java
Date Tue, 02 Aug 2005 00:04:34 GMT
Author: elecharny
Date: Mon Aug  1 17:04:28 2005
New Revision: 226914

URL: http://svn.apache.org/viewcvs?rev=226914&view=rev
Log:
Thanks to Jocelyn Bodin, here is a little Swing GUI that helps 
to debug LDAP communication. This is a proxy which works like 
a man in the middle, and exposes in clear text the messages
from the client and the server.

Added:
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/ldap/
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/ldap/proxy/
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/ldap/proxy/gui/
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/ldap/proxy/gui/MainFrame.java
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/ldap/proxy/impl/
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/ldap/proxy/impl/LdapProxy.java
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/ldap/proxy/impl/ProxyConnection.java

Added: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/ldap/proxy/gui/MainFrame.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/ldap/proxy/gui/MainFrame.java?rev=226914&view=auto
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/ldap/proxy/gui/MainFrame.java
(added)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/ldap/proxy/gui/MainFrame.java
Mon Aug  1 17:04:28 2005
@@ -0,0 +1,310 @@
+ /*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.ldap.proxy.gui;
+
+import org.apache.ldap.proxy.impl.LdapProxy;
+
+import java.awt.Color;
+import java.awt.HeadlessException;
+import java.awt.Rectangle;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+
+import java.io.IOException;
+
+import java.net.UnknownHostException;
+
+import javax.swing.JFrame;
+import javax.swing.JMenu;
+import javax.swing.JMenuBar;
+import javax.swing.JMenuItem;
+import javax.swing.JScrollPane;
+import javax.swing.JSplitPane;
+import javax.swing.JTextPane;
+import javax.swing.text.DefaultStyledDocument;
+import javax.swing.text.Style;
+import javax.swing.text.StyleConstants;
+import javax.swing.text.StyleContext;
+
+
+/**
+ * DOCUMENT ME!
+ *
+ * @author $author$
+ */
+public class MainFrame extends JFrame implements MouseListener, Runnable
+{
+    //~ Static fields/initializers -----------------------------------------------------------------
+
+    /** DOCUMENT ME! */
+    public static final String    FRAME_NAME       = "LDAP Proxy GUI";
+
+    /** DOCUMENT ME! */
+    public static final long      serialVersionUID = 0;
+
+    /** DOCUMENT ME! */
+    public static final Rectangle FRAME_SIZE = new Rectangle( 100, 100, 500, 500 );
+
+    //~ Instance fields ----------------------------------------------------------------------------
+
+    // ALL Panel
+    /** DOCUMENT ME! */
+    private JSplitPane  principalPanel;
+
+    /** DOCUMENT ME! */
+    private JScrollPane jspTree;
+
+    /** DOCUMENT ME! */
+    private JSplitPane  paneDetail;
+
+    /** DOCUMENT ME! */
+    private JScrollPane jspDetailLeft;
+
+    /** DOCUMENT ME! */
+    private JScrollPane jspDetailRight;
+
+    /** DOCUMENT ME! */
+    private JTextPane   streamText;
+
+    /** DOCUMENT ME! */
+    private JTextPane   textIn;
+
+    /** DOCUMENT ME! */
+    private JTextPane   textOut;
+
+    // List of Menu
+    /** DOCUMENT ME! */
+    private JMenuBar  menuBar   = new JMenuBar();
+
+    /** DOCUMENT ME! */
+    private JMenu     file      = new JMenu( "File" );
+
+    /** DOCUMENT ME! */
+    private JMenuItem open      = new JMenuItem( "Open Stream" );
+
+    /** DOCUMENT ME! */
+    private JMenuItem close     = new JMenuItem( "Close Stream" );
+
+    /** DOCUMENT ME! */
+    private JMenuItem save      = new JMenuItem( "Save" );
+
+    /** DOCUMENT ME! */
+    private JMenuItem quit      = new JMenuItem( "Quit" );
+
+    /** DOCUMENT ME! */
+    private LdapProxy ldapProxy;
+
+    // each doc is associate with one JTextPane
+    /** DOCUMENT ME! */
+    private DefaultStyledDocument docStream;
+
+    /** DOCUMENT ME! */
+    private DefaultStyledDocument docIn;
+
+    /** DOCUMENT ME! */
+    private DefaultStyledDocument docOut;
+    
+    private String[] args;
+
+    //~ Constructors -------------------------------------------------------------------------------
+
+    /**
+     * Creates a new MainFrame object.
+     *
+     * @throws HeadlessException DOCUMENT ME!
+     * @throws UnknownHostException DOCUMENT ME!
+     * @throws IOException DOCUMENT ME!
+     */
+    public MainFrame() throws HeadlessException, UnknownHostException, IOException
+    {
+        super( );
+        setTitle( FRAME_NAME );
+        setBounds( FRAME_SIZE );
+        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
+        setJMenuBar( initMenuBar() );
+
+        //DEFAULT STYLE
+        Style def = StyleContext.getDefaultStyleContext().getStyle( StyleContext.DEFAULT_STYLE
);
+
+        docStream  = new DefaultStyledDocument();
+        streamText = new JTextPane( docStream );
+
+        //other style could be define later // COMMENT TO BE DELETED AFTER DEV
+        Style regular = streamText.addStyle( "regular", def );
+        Style red     = streamText.addStyle( "red", regular );
+        StyleConstants.setForeground( red, Color.RED );
+
+        Style redbold = streamText.addStyle( "redbold", red );
+        StyleConstants.setBold( redbold, true );
+        streamText.setEditable( false );
+
+        docIn= new DefaultStyledDocument();
+        textIn = new JTextPane( docIn );
+        textIn.setEditable( false );
+
+        docOut = new DefaultStyledDocument();
+        textOut = new JTextPane( docOut );
+        textOut.setEditable( false );
+
+        jspTree = new JScrollPane();
+        jspTree.getViewport().add( streamText );
+
+        jspDetailLeft = new JScrollPane();
+        jspDetailRight = new JScrollPane();
+        jspDetailLeft.getViewport().add( textIn );
+        jspDetailRight.getViewport().add( textOut );
+
+        paneDetail = new JSplitPane( JSplitPane.VERTICAL_SPLIT, jspDetailLeft, jspDetailRight
);
+        paneDetail.setOneTouchExpandable( true );
+        paneDetail.setDividerLocation( 150 );
+
+        principalPanel = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, jspTree, paneDetail
);
+        principalPanel.setOneTouchExpandable( true );
+        principalPanel.setDividerLocation( 250 );
+
+        getContentPane().add( principalPanel );
+    }
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * DOCUMENT ME!
+     */
+    public void launchLdapProxy(String[] args)
+    {
+        ldapProxy = new LdapProxy();
+        ldapProxy.setStreamText( streamText );
+        ldapProxy.setTextIn( textIn );
+        ldapProxy.setTextOut( textOut );
+
+        ldapProxy.likeMain( args );
+
+    }
+
+    /**
+     * DOCUMENT ME!
+     *
+     * @return DOCUMENT ME!
+     */
+    private JMenuBar initMenuBar()
+    {
+        file.add( open );
+        file.add( close );
+        file.add( save );
+        file.addSeparator();
+        file.add( quit );
+        quit.addActionListener( new ActionListener()
+            {
+                public void actionPerformed( ActionEvent action )
+                {
+                    System.out.println( "EXIT" );
+
+                    try
+                    {
+
+                        // ldapProxy.
+                    }
+                    catch ( Exception e )
+                    {
+                        e.printStackTrace();
+                    }
+
+                    System.exit( 0 );
+                }
+            } );
+        menuBar.add( file );
+        return menuBar;
+    }
+
+    /**
+     * DOCUMENT ME!
+     *
+     * @param arg0 DOCUMENT ME!
+     */
+    public void mouseClicked( MouseEvent arg0 )
+    {
+        // TODO Auto-generated method stub
+
+    }
+
+    /**
+     * DOCUMENT ME!
+     *
+     * @param arg0 DOCUMENT ME!
+     */
+    public void mousePressed( MouseEvent arg0 )
+    {
+        // TODO Auto-generated method stub
+
+    }
+
+    /**
+     * DOCUMENT ME!
+     *
+     * @param arg0 DOCUMENT ME!
+     */
+    public void mouseReleased( MouseEvent arg0 )
+    {
+        // TODO Auto-generated method stub
+
+    }
+
+    /**
+     * DOCUMENT ME!
+     *
+     * @param arg0 DOCUMENT ME!
+     */
+    public void mouseEntered( MouseEvent arg0 )
+    {
+        // TODO Auto-generated method stub
+
+    }
+
+    /**
+     * DOCUMENT ME!
+     *
+     * @param arg0 DOCUMENT ME!
+     */
+    public void mouseExited( MouseEvent arg0 )
+    {
+        // TODO Auto-generated method stub
+
+    }
+
+    /**
+     * DOCUMENT ME!
+     */
+    public void run()
+    {
+        this.setVisible( true );
+        this.launchLdapProxy(args);
+    }
+
+    /**
+     * DOCUMENT ME!
+     *
+     * @param args
+     */
+    public static void main( String[] args ) throws Exception 
+    {
+        MainFrame mf = new MainFrame();
+        mf.args = args;
+        mf.run();
+    }
+}

Added: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/ldap/proxy/impl/LdapProxy.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/ldap/proxy/impl/LdapProxy.java?rev=226914&view=auto
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/ldap/proxy/impl/LdapProxy.java
(added)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/ldap/proxy/impl/LdapProxy.java
Mon Aug  1 17:04:28 2005
@@ -0,0 +1,135 @@
+package org.apache.ldap.proxy.impl;
+
+import java.net.ServerSocket;
+import java.net.Socket;
+
+import javax.swing.JTextPane;
+import javax.swing.text.DefaultStyledDocument;
+import javax.swing.text.Document;
+
+public class LdapProxy {
+
+	public static final String usageArgs = " <localport> <host> <port> <timeout_ms>";
+
+	static int clientCount;
+
+	private ProxyConnection proxyConnection;
+
+	private static JTextPane streamText;
+
+	private static JTextPane textIn;
+
+	private static JTextPane textOut;
+
+	public static synchronized void setTextIn(String s, String style) {
+	    setText(textIn, s, style);
+	}
+	
+	public static synchronized void setTextOut(String s, String style) {
+	    setText(textOut, s, style);
+	}
+	
+	public static synchronized void setStreamText(String s, String style) {
+	    setText(streamText, s, style);
+	}
+	
+	private static void setText(JTextPane textPane, String s, String Style) {
+		try {
+			Document doc = (DefaultStyledDocument) textPane.getDocument();
+			doc.insertString(doc.getLength(), s, textPane.getStyle(Style));
+		} catch (Exception e) {
+			System.out.println("!!!!!!SET DOC IN JTEXTPANE STREAM!!!!!!!!!!!!!!");
+			e.printStackTrace();
+		}
+	}
+
+	public static synchronized void quit(long t) {
+	    System.out.println("...quit after waiting " + t + " ms");
+		clientCount--;
+	}
+
+	public void run(int localport, String host, int port, long timeout) {
+		try {
+			ServerSocket sSocket = new ServerSocket(localport);
+			
+			while (true) {
+				Socket cSocket = null;
+				try {
+					System.out.println("listening...");
+					cSocket = sSocket.accept();
+					
+					if (cSocket != null) {
+						clientCount++;
+						System.out.println("accepted as #" + clientCount + ":" + cSocket);
+						proxyConnection = new ProxyConnection(cSocket, host, port, timeout);
+						proxyConnection.setStreamText(streamText);
+						
+						proxyConnection.run();
+					}
+				} catch (Exception e) {
+					e.printStackTrace(System.err);
+				}
+				try {
+					cSocket.close();
+				} catch (Exception e) {
+					// fall thru
+				}
+			}
+		} catch (Throwable t) {
+			t.printStackTrace(System.err);
+		}
+	}
+
+	public void stop() {
+		proxyConnection.interrupt();
+	}
+
+	public static void main(String[] argv) {
+		LdapProxy self = new LdapProxy();
+
+		if (argv.length >= 3) {
+			int localport = Integer.parseInt(argv[0]);
+			String url = argv[1];
+			int port = Integer.parseInt(argv[2]);
+			int timeout = 30000;
+			try {
+				timeout = Integer.parseInt(argv[3]);
+			} catch (Exception e) {
+			}
+			self.run(localport, url, port, timeout);
+		} else {
+			System.err.println("usage: java " + self.getClass().getName() + usageArgs);
+		}
+	}
+
+	public void setStreamText(JTextPane streamText) {
+		LdapProxy.streamText = streamText;
+	}
+
+	public void setTextIn(JTextPane textIn) {
+	    LdapProxy.textIn = textIn;
+	}
+
+	public void setTextOut(JTextPane textOut) {
+	    LdapProxy.textOut = textOut;
+	}
+
+	public void likeMain(String[] argv) {
+		if (argv.length >= 3) {
+			int localport = Integer.parseInt(argv[0]);
+			String url = argv[1];
+			int port = Integer.parseInt(argv[2]);
+			int timeout = 30000;
+			
+			try {
+				timeout = Integer.parseInt(argv[3]);
+			} catch (Exception e) {
+			}
+			
+			run(localport, url, port, timeout);
+		} else {
+			System.err.println("usage: java " + this.getClass().getName() + usageArgs);
+		}
+	}
+
+}// class

Added: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/ldap/proxy/impl/ProxyConnection.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/ldap/proxy/impl/ProxyConnection.java?rev=226914&view=auto
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/ldap/proxy/impl/ProxyConnection.java
(added)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/ldap/proxy/impl/ProxyConnection.java
Mon Aug  1 17:04:28 2005
@@ -0,0 +1,221 @@
+package org.apache.ldap.proxy.impl;
+
+import java.io.BufferedOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.Socket;
+import java.nio.ByteBuffer;
+import java.util.Date;
+
+import javax.swing.JTextPane;
+
+import org.apache.asn1.DecoderException;
+import org.apache.asn1.ber.Asn1Decoder;
+import org.apache.asn1.ber.containers.IAsn1Container;
+import org.apache.asn1.ldap.codec.LdapDecoder;
+import org.apache.asn1.ldap.codec.LdapMessageContainer;
+import org.apache.asn1.ldap.pojo.LdapMessage;
+import org.apache.asn1.util.StringUtils;
+
+
+class ProxyConnection extends Thread {
+
+	Socket fromClient;
+
+	String host;
+
+	int port;
+
+	long timeout;
+
+	JTextPane streamText;
+	
+	//JTextPane textIn;
+	
+	//JTextPane textOut;
+
+	ProxyConnection(Socket s, String host, int port, long timeout) {
+		fromClient = s;
+		this.host = host;
+		this.port = port;
+		this.timeout = timeout;
+	}
+
+	private String decode(ByteBuffer buffer) throws DecoderException {
+        Asn1Decoder ldapDecoder = new LdapDecoder();
+
+        // Allocate a LdapMessageContainer Container
+        IAsn1Container ldapMessageContainer = new LdapMessageContainer();
+
+        // Decode the PDU
+        ldapDecoder.decode( buffer, ldapMessageContainer );
+        // Check that everything is OK
+        LdapMessage message = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage();
+        
+        return message.toString();
+	}
+	
+	public void run() {
+		InputStream clientIn = null;
+		OutputStream clientOut = null;
+		InputStream serverIn = null;
+		OutputStream serverOut = null;
+		Socket toServer = null;
+		int r0 = -1;
+		int r1 = -1;
+		int ch = -1;
+		int i = -1;
+		long time0 = System.currentTimeMillis();
+		long time1 = time0 + 30000;
+		
+		try {
+			toServer = new Socket(host, port);
+			System.out.println("open connection to:" + toServer + "(timeout=" + timeout + " ms)");
+			clientIn = fromClient.getInputStream();
+			clientOut = new BufferedOutputStream(fromClient.getOutputStream());
+			serverIn = toServer.getInputStream();
+			serverOut = new BufferedOutputStream(toServer.getOutputStream());
+			
+			while (r0 != 0 || r1 != 0 || (time1 - time0) <= timeout) {
+			    r0 = clientIn.available();
+			    
+			    if (r0 != 0)
+			    {
+				    System.out.println("");
+					LdapProxy.setTextIn("<<<" + r0 + " bytes from client \n","red");
+					System.out.println("");
+					System.out.println("<<<" + r0 + " bytes from client");
+					System.out.println("<<<" + r1 + " bytes from server");
+			    }
+
+				ByteBuffer bb = null;
+				
+				while ((r0 = clientIn.available()) > 0) {
+				    byte[] in = new byte[r0];
+				    int k = 0;
+				    
+					for (i = 0; i < r0; i++) {
+						ch = clientIn.read();
+						
+						if (ch != -1) {
+						    in[k++] = (byte)ch;
+							serverOut.write(ch);
+						} else {
+						    System.out.println("client stream closed");
+							LdapProxy.setTextIn("client stream closed \n","redbold");
+						}
+					}
+					
+				    bb = ByteBuffer.allocate(r0);
+				    bb.put(in);
+				    bb.flip();
+					String result = decode(bb);
+					LdapProxy.setStreamText("--->>>\n", "regular");
+					LdapProxy.setStreamText(result, "regular");
+					LdapProxy.setStreamText("----------------------------------\n", "regular");
+
+					time0 = System.currentTimeMillis();
+					serverOut.flush();
+				}
+				
+				bb = null;
+				
+				while ((r1 = serverIn.available()) > 0) {
+				    System.out.println("");
+				    System.out.println(">>>" + r1 + " bytes from server");
+					LdapProxy.setTextOut(">>>" + r1 + " bytes from server \n","red");
+					System.out.println("");
+					System.out.println(">>>" + r1 + " bytes from server");
+					
+				    byte[] out = new byte[r1];
+				    int k = 0;
+
+				    for (i = 0; i < r1; i++) {
+						ch = serverIn.read();
+						
+						if (ch != -1) {
+						    out[k++] = (byte)ch;
+						} else {
+						    System.out.println("server stream closed");
+							LdapProxy.setTextOut("server stream closed \n","redbold");
+							break;
+						}
+						
+						clientOut.write(ch);
+				    }
+
+				    bb = ByteBuffer.allocate(r1);
+				    bb.put(out);
+				    bb.flip();
+				    
+				    while (true) 
+				    {
+				        try 
+				        {
+				            String result = decode(bb);
+				            System.out.println( result );
+				            LdapProxy.setStreamText("<<<---\n", "blue");
+				            LdapProxy.setStreamText(result, "blue");
+				            LdapProxy.setStreamText("----------------------------------\n", "regular");
+				            
+				            if (bb.hasRemaining() == false)
+				            {
+				                break;
+				            }
+				        }
+				        catch ( DecoderException de )
+				        {
+				            StringBuffer result = new StringBuffer();
+				            result.append(de.getMessage()).append('\n');
+				            
+				            byte[] tmp = new byte[out.length - bb.position()];
+				            System.arraycopy(out, bb.position(), tmp, 0, out.length - bb.position());
+				            result.append(StringUtils.dumpBytes(tmp));
+				            
+				            System.out.println( result );
+				            LdapProxy.setStreamText("<<<---\n", "regular");
+				            LdapProxy.setStreamText(new String(tmp), "regular");
+				            LdapProxy.setStreamText("----------------------------------\n", "regular");
+				            break;
+				        }
+				    }
+					
+					time0 = new Date().getTime();
+					clientOut.flush();
+				}
+				if (r0 == 0 && r1 == 0) {
+					time1 = new Date().getTime();
+					Thread.sleep(100);
+					// Proxy.display("waiting:"+(time1-time0)+" ms");
+				}
+			}
+		} catch (Throwable t) {
+		    System.out.println("i=" + i + " ch=" + ch);			
+			t.printStackTrace(System.err);
+		} finally {
+			try {
+				clientIn.close();
+				clientOut.close();
+				serverIn.close();
+				serverOut.close();
+				fromClient.close();
+				toServer.close();
+				LdapProxy.quit(time1 - time0);
+			} catch (Exception e) {
+				e.printStackTrace(System.err);
+			}
+		}
+	}
+
+	public void setStreamText(JTextPane streamText) {
+		this.streamText = streamText;
+	}
+
+	//public void setTextIn(JTextPane textIn) {
+	//	this.textIn = textIn;
+	//}
+
+	//public void setTextOut(JTextPane textOut) {
+	//	this.textOut = textOut;
+	//}
+}
\ No newline at end of file



Mime
View raw message