Java使用netty的HttpProxyHandler放入忽略地址,支持通配符*
时间: 2024-06-08 22:10:04 浏览: 213
java 实现HTTP PROXY
在使用netty的HttpProxyHandler时,可以通过设置自定义的ProxyHandlerSelector来实现忽略地址的功能,具体步骤如下:
1. 创建一个自定义的ProxyHandlerSelector类,继承自DefaultProxyHandlerSelector类。
2. 在自定义的ProxyHandlerSelector类中重写select方法,在该方法中判断是否需要忽略当前请求的目标地址,如果需要忽略,则返回null,否则返回HttpProxyHandler。
3. 在创建Bootstrap时,通过调用handler方法设置自定义的ProxyHandlerSelector类。
下面是一个示例代码,演示如何使用通配符*来忽略地址:
```java
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.proxy.DefaultProxyHandlerSelector;
import io.netty.handler.proxy.ProxyHandler;
import io.netty.handler.proxy.ProxyHandlerSelector;
import io.netty.handler.proxy.Socks5ProxyHandler;
import io.netty.handler.proxy.Socks4ProxyHandler;
import io.netty.handler.proxy.Socks4aProxyHandler;
import io.netty.handler.proxy.Socks5AddressEncoder;
import io.netty.handler.proxy.Socks5CommandRequest;
import io.netty.handler.proxy.Socks5InitialRequest;
import io.netty.handler.proxy.Socks5InitialResponse;
import io.netty.handler.proxy.Socks5ProxyHandler;
import io.netty.handler.proxy.Socks5ProxyServerDecoder;
import io.netty.handler.proxy.Socks5ProxyServerEncoder;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.SslProvider;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import java.net.InetSocketAddress;
import java.net.URI;
public class NettyHttpProxy {
private static final String PROXY_HOST = "127.0.0.1";
private static final int PROXY_PORT = 1080;
public static void main(String[] args) throws Exception {
URI uri = new URI("https://www.google.com");
String host = uri.getHost();
int port = uri.getPort() == -1 ? 443 : uri.getPort();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
if (uri.getScheme().equalsIgnoreCase("https")) {
SslContext sslContext = SslContextBuilder.forClient().sslProvider(SslProvider.JDK)
.trustManager(InsecureTrustManagerFactory.INSTANCE).build();
pipeline.addFirst(sslContext.newHandler(ch.alloc(), host, port));
}
ProxyHandlerSelector selector = new MyProxyHandlerSelector();
ProxyHandler proxyHandler = new HttpProxyHandler(new InetSocketAddress(PROXY_HOST, PROXY_PORT), selector);
pipeline.addLast(proxyHandler);
pipeline.addLast(new HttpClientCodec());
pipeline.addLast(new HttpObjectAggregator(1024 * 1024));
pipeline.addLast(new SimpleChannelInboundHandler<HttpResponse>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpResponse msg) throws Exception {
System.out.println(msg.toString());
}
});
}
});
Channel channel = bootstrap.connect(host, port).sync().channel();
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath());
request.headers().set("Host", host);
request.headers().set("Connection", "close");
request.headers().set("User-Agent", "netty-tcp-client");
channel.writeAndFlush(request).sync();
channel.closeFuture().sync();
}
private static class MyProxyHandlerSelector extends DefaultProxyHandlerSelector {
private static final String[] IGNORED_HOSTS = new String[]{"*.google.com"};
@Override
public ProxyHandler select(URI uri) {
String host = uri.getHost();
for (String ignoredHost : IGNORED_HOSTS) {
if (host.matches(ignoredHost.replace("*", ".*"))) {
return null;
}
}
return super.select(uri);
}
}
}
```
在上面的示例代码中,我们定义了一个MyProxyHandlerSelector类来实现忽略地址的功能。在该类中,我们使用IGNORED_HOSTS数组来指定需要忽略的目标地址,每个地址可以使用通配符*来表示任意字符。然后在select方法中,我们遍历IGNORED_HOSTS数组,如果当前请求的目标地址匹配到了需要忽略的地址,则返回null,否则返回HttpProxyHandler。最后,在创建Bootstrap时,我们通过调用handler方法设置自定义的ProxyHandlerSelector类。
阅读全文