package com.proxy.proxy.server.handler;
import com.proxy.proxy.proxy.ProxyServer;
import com.proxy.proxy.server.Server;
import com.proxy.proxy.util.ProtoUtil;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.handler.codec.http.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.proxy.proxy.util.ProtoUtil.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
/**
* @ClassName: ServerHandler
* @Auther: CemB
* @Description:
* @Email: [email protected]
* @Company: 东方瑞云
* @Date: 2018/12/20 15:20
*/
//线程间共享,但必须要保证此类线程安全
@ChannelHandler.Sharable
public class ServerHandler extends ChannelInboundHandlerAdapter {
private final static Log LOG = LogFactory.getLog(ServerHandler.class);
//保证线程安全
private ThreadLocal<ChannelFuture> futureThreadLocal = new ThreadLocal<>();
private final AtomicInteger PORT = new AtomicInteger(0);
private final AtomicReference<String> HOST = new AtomicReference<String>("0.0.0.0");
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
LOG.info("服务器连接成功......");
}
@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
//http
if (msg instanceof FullHttpRequest) {
FullHttpRequest request = (FullHttpRequest) msg;
String name = request.method().name();
RequestProto protoUtil = ProtoUtil.getRequestProto(request);
String host = protoUtil.getHost();
int port = protoUtil.getPort();
PORT.set(port);
HOST.set(host);
request.headers().set("11", "222");
if ("CONNECT".equalsIgnoreCase(name)) {//HTTPS建立代理握手
HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, Server.SUCCESS);
ctx.writeAndFlush(response);
ctx.pipeline().remove("httpCodec");
ctx.pipeline().remove("httpObject");
return;
}
//开启代理服务器
new ProxyServer(host, port, msg, ctx.channel()).start();
} else { //https,只转发数据,不对数据做处理,所以不需要解密密文
ChannelFuture future = futureThreadLocal.get();
//代理连接还未建立
if (future == null) {
//连接至目标服务器
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(ctx.channel().eventLoop()) // 复用客户端连接线程池
.channel(ctx.channel().getClass()) // 使用NioSocketChannel来作为连接用的channel类
.handler(new ChannelInitializer() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx0, Object msg) throws Exception {
ctx.channel().writeAndFlush(msg);
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("https 代理服务器连接成功...");
}
});
}
});
future = bootstrap.connect(HOST.get(), PORT.get());
futureThreadLocal.set(future);
future.addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
future.channel().writeAndFlush(msg);
} else {
ctx.channel().close();
}
}
});
} else {
//代理建立连接之后,直接刷回数据
future.channel().writeAndFlush(msg);
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
springboot集成netty实现代理服务器

共87个文件
xml:54个
class:16个
java:9个

需积分: 50 86 下载量 109 浏览量
2018-12-21
17:39:37
上传
评论 2
收藏 109KB ZIP 举报
温馨提示
springboot集成netty实现代理服务器,实现http和https请求的代理功能
资源推荐
资源详情
资源评论















格式:zip 资源大小:135.2KB















收起资源包目录
































































































































共 87 条
- 1
资源评论


与衫
- 粉丝: 274
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 继电器在电气工程及自动化低压电器中的应用.docx
- 典型网络工程的案例分析.doc
- 全国计算机等考试二C笔试试卷.doc
- 大学计算机实验报告记录样本.doc
- 科大讯飞人工智能定义城市1.0版本发布.docx
- 软件学院软件工程硕士版培养方案终稿单证.doc
- 基于单片机的数字万用表研究设计.doc
- 集团公司大数据平台建设方案.docx
- 南京大学关于机器学习的 PPT 教学课件
- 热电厂建设项目管理控制研究.docx
- 项目管理的难点与对策.doc
- Oracle程序设计.docx
- 不依赖 sk-learn 库的纯 Python 机器学习算法实现
- 基于单片机的抢答器的方案设计书.doc
- 试论大数据环境下的企业财务管理改革路径.docx
- 初中英语教师基于网络平台的自主发展.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
