介绍说明
普通代理(Http)
Http,RFC 7230中描述为普通代理,其代理扮演的是中间人角色,对于连接到它的客户端来说,它是服务端,对于要连接的服务端来说,它是客户端,代理服务器负责在两端之间来回传送HTTP报文。
隧道代理(Https)
Https,代理服务器是一个web服务器,影响了客户端和服务器的TLS加密连接的。此时主要使用RFC中定义的通过 Web 代理服务器用隧道方式传输基于 TCP 的协议的隧道代理方式,它的主要流程为:
1、浏览器首先发送Http Connect请求给代理服务器,发送目标主机信息。
2、代理服务器建立和目标主机的tcp链接,并向浏览器回应Connection Established应答。
3、浏览器将请求发送给代理服务器,代理服务器透传给目标主机。
4、目标主机将响应回给代理服务器,代理服务器将响应回给浏览器。
Netty实现
Netty高性能网络框架,支持OIO(阻塞式传输)/NIO(非阻塞式传输)等能力,同时屏蔽了网络底层现,使开发人员专注于应用逻辑开发。
Netty框架中大量运用了直接内存用来提高效率,编写代码时要注意及时释放资源,避免内存泄漏问题。
代码分享
<dependency><groupId>org.bouncycastlegroupId><artifactId>bcprov-jdk15to18artifactId><version>1.70version>dependency><dependency><groupId>org.bouncycastlegroupId><artifactId>bcpkix-jdk15to18artifactId><version>1.70version>dependency><dependency><groupId>io.nettygroupId><artifactId>netty-allartifactId><version>4.1.60.Finalversion>dependency>
packagecom.what21.netty03.demo01;importio.netty.bootstrap.ServerBootstrap;importio.netty.channel.*;importio.netty.channel.nio.NioEventLoopGroup;importio.netty.channel.socket.nio.NioServerSocketChannel;importio.netty.handler.logging.LogLevel;importio.netty.handler.logging.LoggingHandler;importjava.net.InetSocketAddress;publicclassHttpServer{privateString serverName;privateString bindHost;privateintbindPort;publicHttpServer(String serverName, String bindHost,intbindPort){this.serverName = serverName;this.bindHost = bindHost;this.bindPort = bindPort;
}publicvoidstart()throwsException{// 用于Acceptor的主"线程池"EventLoopGroup bossEventGroup =newNioEventLoopGroup();// 初始化==>用于I/O工作的从"线程池"EventLoopGroup workerEventGroup =newNioEventLoopGroup();try{
ServerBootstrap serverBootstrap =newServerBootstrap();// group方法设置主从线程池serverBootstrap.group(bossEventGroup, workerEventGroup);// 指定通道channel类型,服务端为:NioServerSocketChannelserverBootstrap.channel(NioServerSocketChannel.class);
serverBootstrap.option(ChannelOption.SO_BACKLOG,1024);
serverBootstrap.childOption(ChannelOption.TCP_NODELAY,true);
serverBootstrap.handler(newLoggingHandler(LogLevel.INFO));
serverBootstrap.childHandler(newHttpServerInitializer());
ChannelFuture bindFuture =null;if("0.0.0.0".equalsIgnoreCase(this.bindHost)) {
bindFuture = serverBootstrap.bind(this.bindPort).sync();
}else{
InetSocketAddress bindAddress =newInetSocketAddress(this.bindHost,this.bindPort);
bindFuture = serverBootstrap.bind(bindAddress).sync();
}
Channel parentChannel = bindFuture.channel();
bindFuture.addListener(newChannelFutureListener() {@OverridepublicvoidoperationComplete(ChannelFuture channelFuture)throwsException{if(channelFuture.isSuccess()) {
System.out.println(HttpServer.this.serverName +",绑定监听成功,"+ channelFuture.channel().localAddress());
}else{
System.err.println(HttpServer.this.serverName +",绑定监听失败!"+ channelFuture.cause());
}
}
});
ChannelFuture closeFuture = bindFuture.channel().closeFuture().sync();
closeFuture.addListener(newChannelFutureListener() {@OverridepublicvoidoperationComplete(ChannelFuture channelFuture)throwsException{if(channelFuture.isSuccess()) {
System.out.println(HttpServer.this.serverName +",停止监听成功,"+ channelFuture.channel().localAddress());
}else{
System.err.println(HttpServer.this.serverName +",停止监听失败!"+ channelFuture.cause());
}
}
});
}catch(Exception e) {
e.printStackTrace();
}finally{// 优雅退出,释放"线程池"if(bossEventGroup !=null) {
bossEventGroup.shutdownGracefully();
}if(workerEventGroup !=null) {
workerEventGroup.shutdownGracefully();
}
}
}publicstaticvoidmain(String[] args){try{newHttpServer("HTTP代理服务器","0.0.0.0",8888).start();
}catch(Exception e) {
e.printStackTrace();
}
}
}
packagecom.what21.netty03.demo01;importio.netty.channel.Channel;importio.netty.channel.ChannelInitializer;importio.netty.handler.codec.http.HttpObjectAggregator;importio.netty.handler.codec.http.HttpServerCodec;importio.netty.handler.ipfilter.IpFilterRule;importio.netty.handler.ipfilter.IpFilterRuleType;importio.netty.handler.ipfilter.IpSubnetFilterRule;importio.netty.handler.ipfilter.RuleBasedIpFilter;importjava.net.InetSocketAddress;publicclassHttpServerInitializerextendsChannelInitializer<Channel>{@OverrideprotectedvoidinitChannel(Channel ch)throwsException{
IpSubnetFilterRule rule1 =newIpSubnetFilterRule("192.168.119.1",24, IpFilterRuleType.ACCEPT);
IpSubnetFilterRule rule2 =newIpSubnetFilterRule("127.0.0.1",32, IpFilterRuleType.REJECT);
IpFilterRule rejectAll =newIpFilterRule() {@Overridepublicbooleanmatches(InetSocketAddress remoteAddress){returntrue;
}@OverridepublicIpFilterRuleTyperuleType(){returnIpFilterRuleType.REJECT;
}
};
RuleBasedIpFilter filter =newRuleBasedIpFilter(rule1, rejectAll);
ch.pipeline().addLast("ipFilter", filter);
ch.pipeline().addLast("httpServerCodec",newHttpServerCodec());
ch.pipeline().addLast("httpObjectAggregator",newHttpObjectAggregator(65536));
ch.pipeline().addLast("httpProxyServerHandler",newHttpProxyServerHandler());
}
}
packagecom.what21.netty03.demo01;importio.netty.bootstrap.Bootstrap;importio.netty.channel.*;importio.netty.handler.codec.http.*;publicclassHttpProxyServerHandlerextendsChannelInboundHandlerAdapter{privateChannelFuture cf;privateString host;privateintport;@OverridepublicvoidchannelRead(finalChannelHandlerContext ctx,finalObject msg)throwsException{if(msginstanceofFullHttpRequest) {
FullHttpRequest request = (FullHttpRequest) msg;
String host = request.headers().get("Host");
String[] temp = host.split(":");intport =80;if(temp.length >1) {
port = Integer.parseInt(temp[1]);
}else{if(request.uri().indexOf("https") ==0) {
port =443;
}
}this.host = temp[0];this.port = port;
String authorization = request.headers().get("Authorization");
System.out.println("http basic认证信息:"+ authorization);if("CONNECT".equalsIgnoreCase(request.method().name())) {// HTTPS建立代理握手HttpResponse response =newDefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
ctx.writeAndFlush(response);
ctx.pipeline().remove("httpServerCodec");
ctx.pipeline().remove("httpObjectAggregator");return;
}//连接至目标服务器Bootstrap bootstrap =newBootstrap();
bootstrap.group(ctx.channel().eventLoop())// 注册线程池.channel(ctx.channel().getClass())// 使用NioSocketChannel来作为连接用的channel类.handler(newHttpProxyInitializer(ctx.channel()));
ChannelFuture cf = bootstrap.connect(temp[0], port);
cf.addListener(newChannelFutureListener() {publicvoidoperationComplete(ChannelFuture future)throwsException{if(future.isSuccess()) {
future.channel().writeAndFlush(msg);
}else{
ctx.channel().close();
}
}
});// ChannelFuture cf = bootstrap.connect(temp[0], port).sync();// cf.channel().writeAndFlush(request);}else{// https 只转发数据,不做处理if(cf ==null) {//连接至目标服务器Bootstrap bootstrap =newBootstrap();
bootstrap.group(ctx.channel().eventLoop())// 复用客户端连接线程池.channel(ctx.channel().getClass())// 使用NioSocketChannel来作为连接用的channel类.handler(newChannelInitializer() {@OverrideprotectedvoidinitChannel(Channel ch)throwsException{
ch.pipeline().addLast(newChannelInboundHandlerAdapter() {@OverridepublicvoidchannelRead(ChannelHandlerContext ctx0, Object msg)throwsException{
ctx.channel().writeAndFlush(msg);
}
});
}
});
cf = bootstrap.connect(host, port);
cf.addListener(newChannelFutureListener() {publicvoidoperationComplete(ChannelFuture future)throwsException{if(future.isSuccess()) {
future.channel().writeAndFlush(msg);
}else{
ctx.channel().close();
}
}
});
}else{
cf.channel().writeAndFlush(msg);
}
}
}
}
packagecom.what21.netty03.demo01;importio.netty.channel.Channel;importio.netty.channel.ChannelInitializer;importio.netty.handler.codec.http.HttpClientCodec;importio.netty.handler.codec.http.HttpObjectAggregator;publicclassHttpProxyInitializerextendsChannelInitializer{privateChannel clientChannel;publicHttpProxyInitializer(Channel clientChannel){this.clientChannel = clientChannel;
}@OverrideprotectedvoidinitChannel(Channel ch)throwsException{
ch.pipeline().addLast(newHttpClientCodec());
ch.pipeline().addLast(newHttpObjectAggregator(512*1024));
ch.pipeline().addLast(newHttpProxyClientHandler(clientChannel));
}
}
packagecom.what21.netty03.demo01;importio.netty.channel.Channel;importio.netty.channel.ChannelHandlerContext;importio.netty.channel.ChannelInboundHandlerAdapter;importio.netty.handler.codec.http.FullHttpResponse;publicclassHttpProxyClientHandlerextendsChannelInboundHandlerAdapter{privateChannel clientChannel;publicHttpProxyClientHandler(Channel clientChannel){this.clientChannel = clientChannel;
}@OverridepublicvoidchannelRead(ChannelHandlerContext ctx, Object msg)throwsException{
FullHttpResponse response = (FullHttpResponse) msg;//修改http响应体返回至客户端response.headers().add("response","from proxy");
clientChannel.writeAndFlush(msg);
}
}
声明:本文部分素材转载自互联网,如有侵权立即删除 。
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
丞旭猿论坛
暂无评论内容