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 ;-)