1 20 package org.apache.directory.ldapstudio.proxy.model; 21 22 23 import java.io.BufferedOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.net.ServerSocket ; 28 import java.net.Socket ; 29 import java.nio.ByteBuffer ; 30 import java.util.Date ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 34 import javax.naming.NamingException ; 35 import javax.swing.tree.DefaultMutableTreeNode ; 36 37 import org.apache.directory.shared.asn1.ber.Asn1Decoder; 38 import org.apache.directory.shared.asn1.ber.IAsn1Container; 39 import org.apache.directory.shared.asn1.codec.DecoderException; 40 import org.apache.directory.shared.asn1.util.Asn1StringUtils; 41 import org.apache.directory.shared.ldap.codec.LdapConstants; 42 import org.apache.directory.shared.ldap.codec.LdapDecoder; 43 import org.apache.directory.shared.ldap.codec.LdapMessage; 44 import org.apache.directory.shared.ldap.codec.LdapMessageContainer; 45 46 47 53 public class LdapProxyThread extends Thread 54 { 55 56 private LdapProxy ldapProxy; 57 58 59 private int localPort; 60 61 62 private String remoteHost; 63 64 65 private int remotePort; 66 67 68 private long timeout; 69 70 71 private Socket cSocket; 72 73 74 private int lastMessageId = -1; 75 76 77 86 public LdapProxyThread( LdapProxy ldapProxy, int localPort, String remoteHost, int remotePort, long timeout ) 87 { 88 this.ldapProxy = ldapProxy; 89 this.localPort = localPort; 90 this.remoteHost = remoteHost; 91 this.remotePort = remotePort; 92 this.timeout = timeout; 93 } 94 95 96 99 public void run() 100 { 101 try 102 { 103 ServerSocket sSocket = new ServerSocket ( localPort ); 104 cSocket = sSocket.accept(); 105 106 if ( cSocket != null ) 107 { 108 InputStream clientIn = null; 109 OutputStream clientOut = null; 110 InputStream serverIn = null; 111 OutputStream serverOut = null; 112 Socket toServer = null; 113 int r0 = -1; 114 int r1 = -1; 115 int ch = -1; 116 int i = -1; 117 long time0 = System.currentTimeMillis(); 118 long time1 = time0 + timeout; 119 120 try 121 { 122 toServer = new Socket ( remoteHost, remotePort ); 123 System.out.println( "open connection to:" + toServer + "(timeout=" + timeout + " ms)" ); 124 clientIn = cSocket.getInputStream(); 125 clientOut = new BufferedOutputStream ( cSocket.getOutputStream() ); 126 serverIn = toServer.getInputStream(); 127 serverOut = new BufferedOutputStream ( toServer.getOutputStream() ); 128 129 while ( r0 != 0 || r1 != 0 || ( time1 - time0 ) <= timeout ) 130 { 131 r0 = clientIn.available(); 132 133 if ( r0 != 0 ) 134 { 135 System.out.println( "" ); 136 System.out.println( "" ); 139 System.out.println( "<<<" + r0 + " bytes from client" ); 140 System.out.println( "<<<" + r1 + " bytes from server" ); 141 } 142 143 ByteBuffer bb = null; 144 145 while ( ( r0 = clientIn.available() ) > 0 ) 146 { 147 byte[] in = new byte[r0]; 148 int k = 0; 149 150 for ( i = 0; i < r0; i++ ) 151 { 152 ch = clientIn.read(); 153 154 if ( ch != -1 ) 155 { 156 in[k++] = ( byte ) ch; 157 serverOut.write( ch ); 158 } 159 else 160 { 161 System.out.println( "client stream closed" ); 162 } 165 } 166 167 bb = ByteBuffer.allocate( r0 ); 168 bb.put( in ); 169 bb.flip(); 170 171 while ( bb.hasRemaining() ) 172 { 173 decode( bb ); 174 } 175 176 182 183 time0 = System.currentTimeMillis(); 184 serverOut.flush(); 185 } 186 187 bb = null; 188 189 while ( ( r1 = serverIn.available() ) > 0 ) 190 { 191 System.out.println( "" ); 192 System.out.println( ">>>" + r1 + " bytes from server" ); 193 System.out.println( "" ); 196 System.out.println( ">>>" + r1 + " bytes from server" ); 197 198 byte[] out = new byte[r1]; 199 int k = 0; 200 201 for ( i = 0; i < r1; i++ ) 202 { 203 ch = serverIn.read(); 204 205 if ( ch != -1 ) 206 { 207 out[k++] = ( byte ) ch; 208 } 209 else 210 { 211 System.out.println( "server stream closed" ); 212 break; 215 } 216 217 clientOut.write( ch ); 218 } 219 220 bb = ByteBuffer.allocate( r1 ); 221 bb.put( out ); 222 bb.flip(); 223 224 261 time0 = new Date ().getTime(); 262 clientOut.flush(); 263 } 264 if ( r0 == 0 && r1 == 0 ) 265 { 266 time1 = new Date ().getTime(); 267 Thread.sleep( 100 ); 268 } 270 } 271 } 272 catch ( Throwable t ) 273 { 274 System.out.println( "i=" + i + " ch=" + ch ); 275 t.printStackTrace( System.err ); 276 } 277 finally 278 { 279 try 280 { 281 clientIn.close(); 282 clientOut.close(); 283 serverIn.close(); 284 serverOut.close(); 285 cSocket.close(); 286 toServer.close(); 287 } 289 catch ( Exception e ) 290 { 291 e.printStackTrace( System.err ); 292 } 293 } 294 } 295 } 296 catch ( IOException e1 ) 297 { 298 e1.printStackTrace(); 300 } 301 } 302 303 private String decode( ByteBuffer buffer ) throws DecoderException, NamingException 304 { 305 int position = buffer.position(); 306 307 DefaultMutableTreeNode mess; 308 309 DefaultMutableTreeNode messTrue; 311 312 Asn1Decoder ldapDecoder = new LdapDecoder(); 313 314 IAsn1Container ldapMessageContainer = new LdapMessageContainer(); 316 317 ldapDecoder.decode( buffer, ldapMessageContainer ); 319 LdapMessage ldapmessage = ( (LdapMessageContainer) ldapMessageContainer ).getLdapMessage(); 321 LdapMessageWithPDU message = new LdapMessageWithPDU(); 322 323 324 message.setLdapMessage( ldapmessage ); 325 message.setMessageId( ldapmessage.getMessageId() ); 326 327 if ( message.getMessageId() != lastMessageId ) 329 { 330 342 } 343 else 344 { 345 } 349 350 String type = transformToStringType( ldapmessage.getMessageType() ); 351 352 int pduLength = buffer.position() - position; 353 byte[] bytes = buffer.array(); 354 byte[] newBytes = new byte[pduLength]; 355 System.arraycopy(bytes, position, newBytes, 0, pduLength ); 356 357 message.setDumpBytes( Asn1StringUtils.dumpBytes( newBytes ) ); 359 360 ldapProxy.addReceivedLdapMessage( message ); 361 362 return message.getLdapMessage().toString(); 363 } 364 365 public String transformToStringType( int type ) 366 { 367 String stringType; 368 369 switch ( type ) 370 { 371 case LdapConstants.ABANDON_REQUEST : 372 stringType = "ABANDON REQUEST"; 373 break; 374 case LdapConstants.ADD_REQUEST : 375 stringType = "ADD REQUEST"; 376 break; 377 case LdapConstants.ADD_RESPONSE : 378 stringType = "ADD RESPONSE"; 379 break; 380 case LdapConstants.BIND_REQUEST : 381 stringType = "BIND REQUEST"; 382 break; 383 case LdapConstants.BIND_RESPONSE : 384 stringType = "BIND RESPONSE"; 385 break; 386 case LdapConstants.COMPARE_REQUEST : 387 stringType = "COMPARE REQUEST"; 388 break; 389 case LdapConstants.COMPARE_RESPONSE : 390 stringType = "COMPARE RESPONSE"; 391 break; 392 case LdapConstants.DEL_REQUEST : 393 stringType = "DEL REQUEST"; 394 break; 395 case LdapConstants.DEL_RESPONSE : 396 stringType = "DEL RESPONSE"; 397 break; 398 case LdapConstants.EXTENDED_REQUEST : 399 stringType = "EXTENDED REQUEST"; 400 break; 401 case LdapConstants.EXTENDED_RESPONSE : 402 stringType = "EXTENDED RESPONSE"; 403 break; 404 case LdapConstants.MODIFYDN_REQUEST : 405 stringType = "MODIFYDN REQUEST"; 406 break; 407 case LdapConstants.MODIFYDN_RESPONSE : 408 stringType = "MODIFYDN RESPONSE"; 409 break; 410 case LdapConstants.MODIFY_REQUEST : 411 stringType = "MODIFY REQUEST"; 412 break; 413 case LdapConstants.MODIFY_RESPONSE : 414 stringType = "MODIFY RESPONSE"; 415 break; 416 case LdapConstants.SEARCH_REQUEST : 417 stringType = "SEARCH REQUEST"; 418 break; 419 case LdapConstants.SEARCH_RESULT_DONE : 420 stringType = "SEARCH RESULT DONE"; 421 break; 422 case LdapConstants.SEARCH_RESULT_ENTRY : 423 stringType = "SEARCH RESULT ENTRY"; 424 break; 425 case LdapConstants.SEARCH_RESULT_REFERENCE : 426 stringType = "SEARCH RESULT REFERENCE"; 427 break; 428 case LdapConstants.UNBIND_REQUEST : 429 stringType = "UNBIND REQUEST"; 430 break; 431 case LdapConstants.UNKNOWN: 432 stringType = "UNKNOWN"; 433 break; 434 435 default : 436 stringType = "UNKNOWN"; 437 break; 438 } 439 440 return stringType; 441 } 442 } 443 | Popular Tags |