Move HTTP client stuffs into bungee-proxy
This commit is contained in:
71
proxy/src/main/java/net/md_5/bungee/http/HttpClient.java
Normal file
71
proxy/src/main/java/net/md_5/bungee/http/HttpClient.java
Normal file
@@ -0,0 +1,71 @@
|
||||
package net.md_5.bungee.http;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
import io.netty.channel.ChannelFutureListener;
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
import io.netty.handler.codec.http.DefaultHttpRequest;
|
||||
import io.netty.handler.codec.http.HttpHeaders;
|
||||
import io.netty.handler.codec.http.HttpMethod;
|
||||
import io.netty.handler.codec.http.HttpRequest;
|
||||
import io.netty.handler.codec.http.HttpVersion;
|
||||
import java.net.URI;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class HttpClient
|
||||
{
|
||||
|
||||
private final EventLoopGroup eventLoop;
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
new HttpClient( new NioEventLoopGroup( 1 ) ).get( "https://session.minecraft.net/" );
|
||||
}
|
||||
|
||||
public void get(String url)
|
||||
{
|
||||
final URI uri = URI.create( url );
|
||||
|
||||
Preconditions.checkNotNull( uri.getScheme(), "scheme" );
|
||||
Preconditions.checkNotNull( uri.getHost(), "host" );
|
||||
boolean ssl = uri.getScheme().equals( "https" );
|
||||
int port = uri.getPort();
|
||||
if ( port == -1 )
|
||||
{
|
||||
switch ( uri.getScheme() )
|
||||
{
|
||||
case "http":
|
||||
port = 80;
|
||||
break;
|
||||
case "https":
|
||||
port = 443;
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException( "Unknown scheme " + uri.getScheme() );
|
||||
}
|
||||
}
|
||||
|
||||
ChannelFutureListener future = new ChannelFutureListener()
|
||||
{
|
||||
@Override
|
||||
public void operationComplete(ChannelFuture future) throws Exception
|
||||
{
|
||||
if ( future.isSuccess() )
|
||||
{
|
||||
HttpRequest request = new DefaultHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath() );
|
||||
request.headers().set( HttpHeaders.Names.HOST, uri.getHost() );
|
||||
|
||||
future.channel().write( request );
|
||||
} else
|
||||
{
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
new Bootstrap().channel( NioSocketChannel.class ).group( eventLoop ).handler( new HttpInitializer( ssl ) ).remoteAddress( uri.getHost(), port ).connect().addListener( future );
|
||||
}
|
||||
}
|
36
proxy/src/main/java/net/md_5/bungee/http/HttpHandler.java
Normal file
36
proxy/src/main/java/net/md_5/bungee/http/HttpHandler.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package net.md_5.bungee.http;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import io.netty.handler.codec.http.HttpContent;
|
||||
import io.netty.handler.codec.http.HttpObject;
|
||||
import io.netty.handler.codec.http.HttpResponse;
|
||||
import io.netty.handler.codec.http.HttpResponseStatus;
|
||||
import io.netty.handler.codec.http.LastHttpContent;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
public class HttpHandler extends SimpleChannelInboundHandler<HttpObject>
|
||||
{
|
||||
|
||||
@Override
|
||||
protected void messageReceived(ChannelHandlerContext ctx, HttpObject msg) throws Exception
|
||||
{
|
||||
if ( msg instanceof HttpResponse )
|
||||
{
|
||||
HttpResponse response = (HttpResponse) msg;
|
||||
if ( response.getStatus() != HttpResponseStatus.OK )
|
||||
{
|
||||
}
|
||||
}
|
||||
if ( msg instanceof HttpContent )
|
||||
{
|
||||
HttpContent content = (HttpContent) msg;
|
||||
String s = content.content().toString( Charset.forName( "UTF-8" ) );
|
||||
|
||||
if ( msg instanceof LastHttpContent )
|
||||
{
|
||||
ctx.channel().close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
package net.md_5.bungee.http;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.handler.codec.http.HttpClientCodec;
|
||||
import io.netty.handler.ssl.SslHandler;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLEngine;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class HttpInitializer extends ChannelInitializer<Channel>
|
||||
{
|
||||
|
||||
private final boolean ssl;
|
||||
|
||||
@Override
|
||||
protected void initChannel(Channel ch) throws Exception
|
||||
{
|
||||
if ( ssl )
|
||||
{
|
||||
SSLContext context = SSLContext.getInstance( "TLS" );
|
||||
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( "http", new HttpClientCodec() );
|
||||
ch.pipeline().addLast( "handler", new HttpHandler() );
|
||||
}
|
||||
}
|
@@ -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 ];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user