You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by shila <fr...@newvision.it> on 2007/09/04 17:38:56 UTC

Client-server application through a proxy

Hi,

I'm trying to write a client-server application with Mina which passes
through an HTTP proxy.
I know that Mina doesn't support proxy natively (also with DIRMINA-223: see
http://www.nabble.com/-jira--Created:-(DIRMINA-223)-Addition-of-SocketConnector-method-to-handle-socks-proxies.-t2697070s16868.html),
and that java doesn't support HTTPProxy (see
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6370908).

I've tried to create a tunnel to the server application which could pass
through the proxy by using the
HTTP CONNECT method, but this is supported only to the ssl ports; all the
connections to other ports are bounced by the proxy
(with the default configuration).

I've modified the mina's class SocketConnector in order to create this
tunnel:

...
		SocketChannel ch = null;
        boolean success = false;
        
		try
        {
        	//START ADD CODE
        	if(this.isThereProxy()) {
        		
        		SSLSocketFactory factory = null;
        		try {
        			factory = (SSLSocketFactory)SSLSocketFactory.getDefault();
        			
        			/* socket connection to proxy */
        			SocketChannel socketCh = SocketChannel.open(new
InetSocketAddress(proxyHost, proxyPort));
        			Socket tunnel = socketCh.socket();
        			
        			doTunnelHandshake(tunnel,
((InetSocketAddress)address).getHostName(),
    					((InetSocketAddress)address).getPort());
    			
        			/* SSL Socket creation through proxy */
        			SSLSocket socketTunnel = (SSLSocket)(factory.createSocket(
    					tunnel,
    					((InetSocketAddress)address).getHostName(),
    					((InetSocketAddress)address).getPort(),
    					true));
    			
        			socketTunnel.startHandshake();

        			ch = socketTunnel.getChannel();
        			socketTunnel.setReuseAddress(true);
	    			if(localAddress != null) {
	    				socketTunnel.bind(localAddress);
	    			}
	    			
	    			ch.configureBlocking(false);
	    			if(ch.isConnected()) {
	    				SocketSessionImpl session = newSession( ch, handler, config );
	    				success = true;
	    				ConnectFuture future = new ConnectFuture();
		                future.setSession(session);
		                return future;
	    			}
	    			
	    			success = true;
	    			
        		} catch (Exception e) {
        			System.out.println("[ERROR] Problem during the tunnel
creation.");
        			e.printStackTrace();
        		}
   // END ADD CODE  			    			
        	} else {
	            ch = SocketChannel.open();
	            ch.socket().setReuseAddress( true );
...


By way of this method I'm able to send a message to the server's 443 port;
the problem
is that these messages aren't encrypted, so my server application don't
understand the message received.
To resolve this problem I've tried to add an SSLfilter to the session but
the
program stops at the session.write instruction.

The following is a snippet from the main:

...
		SSLFilter filter = null;
		
		try {
			/* ssl filter creation */
			// ************************************************
			SSLContext sslContext = SSLContextFactory.getInstance(false);
			filter = new SSLFilter(sslContext);
			// ************************************************
			
			ByteBuffer.setUseDirectBuffers(false);
	        ByteBuffer.setAllocator(new SimpleByteBufferAllocator());
			IoConnector connector = new SocketConnector();
			SocketConnectorConfig config = new SocketConnectorConfig();
				            
	        filter.setUseClientMode(true);
	        	            
		    IoHandler handle = new IoHandler(){
				public void sessionOpened(IoSession session) {
					System.out.println("SESSION OPENED");
				}
				public void sessionClosed(IoSession session) {
					System.out.println("SESSION CLOSED");
				}
				public void sessionIdle(IoSession session, IdleStatus status) {
					System.out.println("SESSION IDLE");
				}
				public void exceptionCaught(IoSession session, Throwable cause) {
					System.out.println("EXCEPTION CAUGHT");
					cause.printStackTrace();
				}
				public void messageReceived(IoSession session, Object message) {
					System.out.println("MESSAGE RECEIVED");
				}
				public void sessionCreated(IoSession session) {
					System.out.println("SESSION CREATED");
										
				}
				public void messageSent(IoSession session, Object message) {
					System.out.println("MESSAGE SENT");
				}
			};
			
		    ConnectFuture future = connector.connect(
		    		new InetSocketAddress("***************************", 443),
		    		handle,
		    		config);
		          
		    future.join();
		    if (!future.isConnected()) {
		    	System.out.println("Connection fail");
		    }
		    else {
		    	System.out.println("Connection done!");
		    }
		    
		    IoSession session = future.getSession();
		    
			if (session == null)
		    	System.out.println("PROBLEM!!");
			else {

				// Add Filters
				// ************************************************
				session.getFilterChain().addLast("client", new ProtocolCodecFilter(new
CodecFactory()));
				session.getFilterChain().addFirst("sslFilter", filter);
				session.getFilterChain().addLast("logger", new LoggingFilter());
				// ************************************************
							
									
				/* message send */
				session.write(new HttpImActiveMessage("179_1185976799152")).join();
							
				System.out.println("MESSAGE SENT!!");
				
				session.close();
			}
	    } catch (Exception e) {
	    	System.out.println("PROBLEM");
	    	e.printStackTrace();
	    }
	}
...

Has someone encountered a similar problem? ... Is there someone that could
give me some hints?


Thanks! ;-)

...i'm sorry for my terrible english :-/
-- 
View this message in context: http://www.nabble.com/Client-server-application-through-a-proxy-tf4378496s16868.html#a12480680
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: Client-server application through a proxy

Posted by Trustin Lee <tr...@gmail.com>.
On 9/12/07, Francesca Milan <fr...@newvision.it> wrote:
> shila ha scritto:
> > Hi,
> >
> > I'm trying to write a client-server application with Mina which passes
> > through an HTTP proxy.
> > I know that Mina doesn't support proxy natively (also with DIRMINA-223: see
> > http://www.nabble.com/-jira--Created:-(DIRMINA-223)-Addition-of-SocketConnector-method-to-handle-socks-proxies.-t2697070s16868.html),
> > and that java doesn't support HTTPProxy (see
> > http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6370908).
> >
> > I've tried to create a tunnel to the server application which could pass
> > through the proxy by using the
> > HTTP CONNECT method, but this is supported only to the ssl ports; all the
> > connections to other ports are bounced by the proxy
> > (with the default configuration).
> >
> > I've modified the mina's class SocketConnector in order to create this
> > tunnel:
> >
> > ...
> >               SocketChannel ch = null;
> >         boolean success = false;
> >
> >               try
> >         {
> >               //START ADD CODE
> >               if(this.isThereProxy()) {
> >
> >                       SSLSocketFactory factory = null;
> >                       try {
> >                               factory = (SSLSocketFactory)SSLSocketFactory.getDefault();
> >
> >                               /* socket connection to proxy */
> >                               SocketChannel socketCh = SocketChannel.open(new
> > InetSocketAddress(proxyHost, proxyPort));
> >                               Socket tunnel = socketCh.socket();
> >
> >                               doTunnelHandshake(tunnel,
> > ((InetSocketAddress)address).getHostName(),
> >                                       ((InetSocketAddress)address).getPort());
> >
> >                               /* SSL Socket creation through proxy */
> >                               SSLSocket socketTunnel = (SSLSocket)(factory.createSocket(
> >                                       tunnel,
> >                                       ((InetSocketAddress)address).getHostName(),
> >                                       ((InetSocketAddress)address).getPort(),
> >                                       true));
> >
> >                               socketTunnel.startHandshake();
> >
> >                               ch = socketTunnel.getChannel();
> >                               socketTunnel.setReuseAddress(true);
> >                               if(localAddress != null) {
> >                                       socketTunnel.bind(localAddress);
> >                               }
> >
> >                               ch.configureBlocking(false);
> >                               if(ch.isConnected()) {
> >                                       SocketSessionImpl session = newSession( ch, handler, config );
> >                                       success = true;
> >                                       ConnectFuture future = new ConnectFuture();
> >                               future.setSession(session);
> >                               return future;
> >                               }
> >
> >                               success = true;
> >
> >                       } catch (Exception e) {
> >                               System.out.println("[ERROR] Problem during the tunnel
> > creation.");
> >                               e.printStackTrace();
> >                       }
> >    // END ADD CODE
> >               } else {
> >                   ch = SocketChannel.open();
> >                   ch.socket().setReuseAddress( true );
> > ...
> >
> >
> > By way of this method I'm able to send a message to the server's 443 port;
> > the problem
> > is that these messages aren't encrypted, so my server application don't
> > understand the message received.
> > To resolve this problem I've tried to add an SSLfilter to the session but
> > the
> > program stops at the session.write instruction.
> >
> > The following is a snippet from the main:
> >
> > ...
> >               SSLFilter filter = null;
> >
> >               try {
> >                       /* ssl filter creation */
> >                       // ************************************************
> >                       SSLContext sslContext = SSLContextFactory.getInstance(false);
> >                       filter = new SSLFilter(sslContext);
> >                       // ************************************************
> >
> >                       ByteBuffer.setUseDirectBuffers(false);
> >               ByteBuffer.setAllocator(new SimpleByteBufferAllocator());
> >                       IoConnector connector = new SocketConnector();
> >                       SocketConnectorConfig config = new SocketConnectorConfig();
> >
> >               filter.setUseClientMode(true);
> >
> >                   IoHandler handle = new IoHandler(){
> >                               public void sessionOpened(IoSession session) {
> >                                       System.out.println("SESSION OPENED");
> >                               }
> >                               public void sessionClosed(IoSession session) {
> >                                       System.out.println("SESSION CLOSED");
> >                               }
> >                               public void sessionIdle(IoSession session, IdleStatus status) {
> >                                       System.out.println("SESSION IDLE");
> >                               }
> >                               public void exceptionCaught(IoSession session, Throwable cause) {
> >                                       System.out.println("EXCEPTION CAUGHT");
> >                                       cause.printStackTrace();
> >                               }
> >                               public void messageReceived(IoSession session, Object message) {
> >                                       System.out.println("MESSAGE RECEIVED");
> >                               }
> >                               public void sessionCreated(IoSession session) {
> >                                       System.out.println("SESSION CREATED");
> >
> >                               }
> >                               public void messageSent(IoSession session, Object message) {
> >                                       System.out.println("MESSAGE SENT");
> >                               }
> >                       };
> >
> >                   ConnectFuture future = connector.connect(
> >                               new InetSocketAddress("***************************", 443),
> >                               handle,
> >                               config);
> >
> >                   future.join();
> >                   if (!future.isConnected()) {
> >                       System.out.println("Connection fail");
> >                   }
> >                   else {
> >                       System.out.println("Connection done!");
> >                   }
> >
> >                   IoSession session = future.getSession();
> >
> >                       if (session == null)
> >                       System.out.println("PROBLEM!!");
> >                       else {
> >
> >                               // Add Filters
> >                               // ************************************************
> >                               session.getFilterChain().addLast("client", new ProtocolCodecFilter(new
> > CodecFactory()));
> >                               session.getFilterChain().addFirst("sslFilter", filter);
> >                               session.getFilterChain().addLast("logger", new LoggingFilter());
> >                               // ************************************************
> >
> >
> >                               /* message send */
> >                               session.write(new HttpImActiveMessage("179_1185976799152")).join();
> >
> >                               System.out.println("MESSAGE SENT!!");
> >
> >                               session.close();
> >                       }
> >           } catch (Exception e) {
> >               System.out.println("PROBLEM");
> >               e.printStackTrace();
> >           }
> >       }
> > ...
> >
> > Has someone encountered a similar problem? ... Is there someone that could
> > give me some hints?
> >
> >
> > Thanks! ;-)
> >
> > ...i'm sorry for my terrible english :-/
> >
> I have resolved deleting "socketTunnel.startHandshake();" from
> socketConnector class source code.\

I'm curious to know what is the difference between just inserting a
SSLFilter and using SSLSocketFactory together.

Thanks in advance,
Trustin
-- 
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6

Re: Client-server application through a proxy

Posted by Francesca Milan <fr...@newvision.it>.
shila ha scritto:
> Hi,
>
> I'm trying to write a client-server application with Mina which passes
> through an HTTP proxy.
> I know that Mina doesn't support proxy natively (also with DIRMINA-223: see
> http://www.nabble.com/-jira--Created:-(DIRMINA-223)-Addition-of-SocketConnector-method-to-handle-socks-proxies.-t2697070s16868.html),
> and that java doesn't support HTTPProxy (see
> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6370908).
>
> I've tried to create a tunnel to the server application which could pass
> through the proxy by using the
> HTTP CONNECT method, but this is supported only to the ssl ports; all the
> connections to other ports are bounced by the proxy
> (with the default configuration).
>
> I've modified the mina's class SocketConnector in order to create this
> tunnel:
>
> ...
> 		SocketChannel ch = null;
>         boolean success = false;
>         
> 		try
>         {
>         	//START ADD CODE
>         	if(this.isThereProxy()) {
>         		
>         		SSLSocketFactory factory = null;
>         		try {
>         			factory = (SSLSocketFactory)SSLSocketFactory.getDefault();
>         			
>         			/* socket connection to proxy */
>         			SocketChannel socketCh = SocketChannel.open(new
> InetSocketAddress(proxyHost, proxyPort));
>         			Socket tunnel = socketCh.socket();
>         			
>         			doTunnelHandshake(tunnel,
> ((InetSocketAddress)address).getHostName(),
>     					((InetSocketAddress)address).getPort());
>     			
>         			/* SSL Socket creation through proxy */
>         			SSLSocket socketTunnel = (SSLSocket)(factory.createSocket(
>     					tunnel,
>     					((InetSocketAddress)address).getHostName(),
>     					((InetSocketAddress)address).getPort(),
>     					true));
>     			
>         			socketTunnel.startHandshake();
>
>         			ch = socketTunnel.getChannel();
>         			socketTunnel.setReuseAddress(true);
> 	    			if(localAddress != null) {
> 	    				socketTunnel.bind(localAddress);
> 	    			}
> 	    			
> 	    			ch.configureBlocking(false);
> 	    			if(ch.isConnected()) {
> 	    				SocketSessionImpl session = newSession( ch, handler, config );
> 	    				success = true;
> 	    				ConnectFuture future = new ConnectFuture();
> 		                future.setSession(session);
> 		                return future;
> 	    			}
> 	    			
> 	    			success = true;
> 	    			
>         		} catch (Exception e) {
>         			System.out.println("[ERROR] Problem during the tunnel
> creation.");
>         			e.printStackTrace();
>         		}
>    // END ADD CODE  			    			
>         	} else {
> 	            ch = SocketChannel.open();
> 	            ch.socket().setReuseAddress( true );
> ...
>
>
> By way of this method I'm able to send a message to the server's 443 port;
> the problem
> is that these messages aren't encrypted, so my server application don't
> understand the message received.
> To resolve this problem I've tried to add an SSLfilter to the session but
> the
> program stops at the session.write instruction.
>
> The following is a snippet from the main:
>
> ...
> 		SSLFilter filter = null;
> 		
> 		try {
> 			/* ssl filter creation */
> 			// ************************************************
> 			SSLContext sslContext = SSLContextFactory.getInstance(false);
> 			filter = new SSLFilter(sslContext);
> 			// ************************************************
> 			
> 			ByteBuffer.setUseDirectBuffers(false);
> 	        ByteBuffer.setAllocator(new SimpleByteBufferAllocator());
> 			IoConnector connector = new SocketConnector();
> 			SocketConnectorConfig config = new SocketConnectorConfig();
> 				            
> 	        filter.setUseClientMode(true);
> 	        	            
> 		    IoHandler handle = new IoHandler(){
> 				public void sessionOpened(IoSession session) {
> 					System.out.println("SESSION OPENED");
> 				}
> 				public void sessionClosed(IoSession session) {
> 					System.out.println("SESSION CLOSED");
> 				}
> 				public void sessionIdle(IoSession session, IdleStatus status) {
> 					System.out.println("SESSION IDLE");
> 				}
> 				public void exceptionCaught(IoSession session, Throwable cause) {
> 					System.out.println("EXCEPTION CAUGHT");
> 					cause.printStackTrace();
> 				}
> 				public void messageReceived(IoSession session, Object message) {
> 					System.out.println("MESSAGE RECEIVED");
> 				}
> 				public void sessionCreated(IoSession session) {
> 					System.out.println("SESSION CREATED");
> 										
> 				}
> 				public void messageSent(IoSession session, Object message) {
> 					System.out.println("MESSAGE SENT");
> 				}
> 			};
> 			
> 		    ConnectFuture future = connector.connect(
> 		    		new InetSocketAddress("***************************", 443),
> 		    		handle,
> 		    		config);
> 		          
> 		    future.join();
> 		    if (!future.isConnected()) {
> 		    	System.out.println("Connection fail");
> 		    }
> 		    else {
> 		    	System.out.println("Connection done!");
> 		    }
> 		    
> 		    IoSession session = future.getSession();
> 		    
> 			if (session == null)
> 		    	System.out.println("PROBLEM!!");
> 			else {
>
> 				// Add Filters
> 				// ************************************************
> 				session.getFilterChain().addLast("client", new ProtocolCodecFilter(new
> CodecFactory()));
> 				session.getFilterChain().addFirst("sslFilter", filter);
> 				session.getFilterChain().addLast("logger", new LoggingFilter());
> 				// ************************************************
> 							
> 									
> 				/* message send */
> 				session.write(new HttpImActiveMessage("179_1185976799152")).join();
> 							
> 				System.out.println("MESSAGE SENT!!");
> 				
> 				session.close();
> 			}
> 	    } catch (Exception e) {
> 	    	System.out.println("PROBLEM");
> 	    	e.printStackTrace();
> 	    }
> 	}
> ...
>
> Has someone encountered a similar problem? ... Is there someone that could
> give me some hints?
>
>
> Thanks! ;-)
>
> ...i'm sorry for my terrible english :-/
>   
I have resolved deleting "socketTunnel.startHandshake();" from 
socketConnector class source code.

Thank you ;-)