Fix HTTP client now that Mojang has their stuff together.

This commit is contained in:
md_5 2014-11-08 10:02:48 +11:00
parent faf903469e
commit 56c372a3ce
3 changed files with 5 additions and 43 deletions

View File

@ -91,7 +91,7 @@ public class HttpClient
} }
}; };
new Bootstrap().channel( PipelineUtils.getChannel() ).group( eventLoop ).handler( new HttpInitializer( callback, ssl ) ). new Bootstrap().channel( PipelineUtils.getChannel() ).group( eventLoop ).handler( new HttpInitializer( callback, ssl, uri.getHost(), port ) ).
option( ChannelOption.CONNECT_TIMEOUT_MILLIS, TIMEOUT ).remoteAddress( inetHost, port ).connect().addListener( future ); option( ChannelOption.CONNECT_TIMEOUT_MILLIS, TIMEOUT ).remoteAddress( inetHost, port ).connect().addListener( future );
} }
} }

View File

@ -3,12 +3,11 @@ package net.md_5.bungee.http;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.handler.codec.http.HttpClientCodec; import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslHandler; import io.netty.handler.ssl.SslHandler;
import io.netty.handler.timeout.ReadTimeoutHandler; import io.netty.handler.timeout.ReadTimeoutHandler;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
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;
import net.md_5.bungee.api.Callback; import net.md_5.bungee.api.Callback;
@ -18,6 +17,8 @@ public class HttpInitializer extends ChannelInitializer<Channel>
private final Callback<String> callback; private final Callback<String> callback;
private final boolean ssl; private final boolean ssl;
private final String host;
private final int port;
@Override @Override
protected void initChannel(Channel ch) throws Exception protected void initChannel(Channel ch) throws Exception
@ -25,14 +26,7 @@ public class HttpInitializer extends ChannelInitializer<Channel>
ch.pipeline().addLast( "timeout", new ReadTimeoutHandler( HttpClient.TIMEOUT, TimeUnit.MILLISECONDS ) ); ch.pipeline().addLast( "timeout", new ReadTimeoutHandler( HttpClient.TIMEOUT, TimeUnit.MILLISECONDS ) );
if ( ssl ) if ( ssl )
{ {
SSLContext context = SSLContext.getInstance( "TLS" ); SSLEngine engine = SslContext.newClientContext().newEngine( ch.alloc(), 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 ) );
} }

View File

@ -1,32 +0,0 @@
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 ];
}
}