Add SSL support

This commit is contained in:
md_5 2013-07-04 11:48:09 +10:00
parent 2cbea83c02
commit 927a295add
3 changed files with 43 additions and 5 deletions

View File

@ -66,6 +66,6 @@ public class HttpClient
} }
}; };
new Bootstrap().channel( NioSocketChannel.class ).group( eventLoop ).handler( new HttpInitializer( url, port, ssl ) ).remoteAddress( uri.getHost(), port ).connect().addListener( future ); new Bootstrap().channel( NioSocketChannel.class ).group( eventLoop ).handler( new HttpInitializer( ssl ) ).remoteAddress( uri.getHost(), port ).connect().addListener( future );
} }
} }

View File

@ -6,14 +6,13 @@ import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.ssl.SslHandler; import io.netty.handler.ssl.SslHandler;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine; import javax.net.ssl.SSLEngine;
import javax.net.ssl.TrustManager;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor @RequiredArgsConstructor
public class HttpInitializer extends ChannelInitializer<Channel> public class HttpInitializer extends ChannelInitializer<Channel>
{ {
private final String host;
private final int port;
private final boolean ssl; private final boolean ssl;
@Override @Override
@ -21,8 +20,15 @@ public class HttpInitializer extends ChannelInitializer<Channel>
{ {
if ( ssl ) if ( ssl )
{ {
SSLContext context = SSLContext.getDefault(); SSLContext context = SSLContext.getInstance( "TLS" );
SSLEngine engine = context.createSSLEngine( host, port ); context.init( null, new TrustManager[]
{
TrustingX509Manager.getInstance()
}, null );
SSLEngine engine = context.createSSLEngine();
engine.setUseClientMode( true );
ch.pipeline().addLast( "ssl", new SslHandler( engine ) ); ch.pipeline().addLast( "ssl", new SslHandler( engine ) );
} }
ch.pipeline().addLast( "http", new HttpClientCodec() ); ch.pipeline().addLast( "http", new HttpClientCodec() );

View File

@ -0,0 +1,32 @@
package net.md_5.bungee.http;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TrustingX509Manager implements X509TrustManager
{
@Getter
private static final X509TrustManager instance = new TrustingX509Manager();
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException
{
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException
{
}
@Override
public X509Certificate[] getAcceptedIssuers()
{
return new X509Certificate[ 0 ];
}
}