http代理数据传播路径:
- 客户端将请求发送到代理,代理解析出消息目的地再去请求服务器
- 服务器将完整结果返回给代理,代理再将结果返回给客户端
- 代理就在两者之间进行中转数据
https消息传播模式:
- 客户端将请求的目的地端口明文发送到代理,
- 代理解析出服务器host 端口,并连接成功,返回客户端连接成功的标识
- 客户端知道代理已经连接成功了,开始将ssl握手之类的加密数据发送给代理
- 代理就在服务器客户端之间进行转发数据,他并不知道传输的数据到底是什么,因为是加密的
实现方式:
编程语言:java
框架选择:netty
首先创建一个标准的netty启动
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 6000)
.childHandler(new ProxyServiceInit());
ChannelFuture f = b.bind(PropertiesUtil.getIntProp("start.port")).sync();
f.channel().closeFuture().sync();
} finally {
workGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
向其中添加这么两个handler,其中HttpServerCodec是netty自带的,HttpService是自己实现的
@Override
protected void initChannel(Channel channel) throws Exception {
ChannelPipeline p = channel.pipeline();
p.addLast("httpcode", new HttpServerCodec());
p.addLast("httpservice", new HttpService());
}
HttpServerCodec会将客户端传进来的消息转成httpobject对象,并且是已经被聚合了的http消息,我们在自己写的HttpService中使用
自定义httpservice 集成simpleinbondhandler
public class HttpService extends SimpleChannelInboundHandler<HttpObject&