netty代理转发_Netty实现代理模式的通信

该demo是从netty提供的demo修改并添加注释

按我的理解简单的说一下netty中代理通信的编程模型。

代理就是通过一个中间服务器去目标服务器请求所要的信息,然后通过代理服务器返回信息给客户端。

在netty中,netty客户端请求代理服务器,连接到代理服务器时->inbound,代理服务器马上去连接目标服务器->outbound,当连接目标服务器成功时,这是开始读取客户端给代理服务器的数据,然后使用outbound写入目标服务器,目标服务器返回数据,代理服务器读取,并使用inbound把数据返回客户端。

客户端程序

package proxy4;

import io.netty.bootstrap.Bootstrap;

import io.netty.channel.ChannelFuture;

import io.netty.channel.ChannelInitializer;

import io.netty.channel.ChannelOption;

import io.netty.channel.EventLoopGroup;

import io.netty.channel.nio.NioEventLoopGroup;

import io.netty.channel.socket.SocketChannel;

import io.netty.channel.socket.nio.NioSocketChannel;

import io.netty.handler.codec.serialization.ClassResolvers;

import io.netty.handler.codec.serialization.ObjectDecoder;

import io.netty.handler.codec.serialization.ObjectEncoder;

/**

* Created with IntelliJ IDEA.

* User: ASUS

* Date: 14-6-24

* Time: 下午3:32

* To change this template use File | Settings | File Templates.

*/

public class Client {

public void connect(String host, int port) throws Exception {

EventLoopGroup workerGroup = new NioEventLoopGroup();

try {

Bootstrap b = new Bootstrap();

b.group(workerGroup);

b.channel(NioSocketChannel.class);

b.option(ChannelOption.SO_KEEPALIVE, true);

b.handler(new ChannelInitializer() {

@Override

public void initChannel(SocketChannel ch) throws Exception {

ch.pipeline().addLast(

new ObjectEncoder(),

new ObjectDecoder(ClassResolvers.cacheDisabled(null)),

new ClientHandler()

);

}

});

// Start the client.

ChannelFuture f = b.connect(host, port).sync();

// Wait until the connection is closed.

f.channel().closeFuture().sync();

} finally {

workerGroup.shutdownGracefully();

}

}

public static void main(String[] args) throws Exception {

new Client().connect("127.0.0.1", 12359);

}

}

package proxy4;

import io.netty.channel.ChannelFuture;

import io.netty.channel.ChannelFutureListener;

import io.netty.channel.ChannelHandlerContext;

import io.netty.channel.ChannelInboundHandlerAdapter;

/**

* Created with IntelliJ IDEA.

* User: ASUS

* Date: 14-6-24

* Time: 下午3:33

* To change this template use File | Settings | File Templates.

*/

public class ClientHandler extends ChannelInboundHandlerAdapter {

@Override

public void channelActive(ChannelHandlerContext ctx) throws Exception {

System.out.println("Client to the proxy server, said: I was a client, give my regards to the target server!");

ChannelFuture f = ctx.writeAndFlush("I'm client, give my regards to the target server!");

f.addListener(new ChannelFutureListener() {

@Override

public void operationComplete(ChannelFuture future) throws Exception {

System.out.println("===========write message success==========");

}

});

}

@Override

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

String mess = (String) msg;

System.out.println("Receiving a message from the proxy server to:" + mess);

ctx.close();

}

}

目标服务器端程序

package proxy4;

import io.netty.bootstrap.ServerBootstrap;

import io.netty.channel.ChannelInitializer;

import io.netty.channel.EventLoopGroup;

import io.netty.channel.nio.NioEventLoopGroup;

import io.netty.channel.socket.SocketChannel;

import io.netty.channel.socket.nio.NioServerSocketChannel;

import io.netty.handler.codec.serialization.ClassResolvers;

import io.netty.handler.codec.serialization.ObjectDecoder;

import io.netty.handler.codec.serialization.ObjectEncoder;

/**

* Created with IntelliJ IDEA.

* User: ASUS

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Netty是一个基于Java的网络应用框架,它提供了异步、事件驱动的网络编程模型,可以用来开发高性能的网络应用程序。 GmSSL是一个开源的加密库,支持国密算法。在Netty中使用GmSSL可以实现国密算法的加密和解密,保证网络通信的安全性。 实现步骤如下: 1. 引入GmSSL依赖 在Maven项目中,需要在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcpkix-jdk15on</artifactId> <version>1.68</version> </dependency> <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.68</version> </dependency> ``` 2. 创建SSLContext 使用GmSSL进行加密和解密需要创建一个SSLContext对象,可以通过以下代码实现: ```java Security.addProvider(new BouncyCastleProvider()); SSLContext sslContext = SSLContext.getInstance("GMSSL", "BC"); ``` 其中,BouncyCastleProvider是一个开源的加密库提供商,可以支持各种加密算法。 3. 配置SslHandler 在Netty中,可以通过SslHandler实现SSL加密和解密。可以通过以下代码创建一个SslHandler对象: ```java SslHandler sslHandler = new SslHandler(sslContext.createSSLEngine()); ``` 4. 配置ChannelPipeline 在Netty中,可以通过ChannelPipeline实现消息的编解码和处理。可以通过以下代码将SslHandler添加到ChannelPipeline中: ```java ChannelPipeline pipeline = channel.pipeline(); pipeline.addLast("ssl", sslHandler); ``` 5. 完成GMSSL加密和解密 完成上述步骤后,就可以使用GMSSL进行加密和解密了。在Netty中,可以通过SslHandler的write和read方法实现加密和解密: ```java ByteBuf buf = Unpooled.copiedBuffer("Hello, world!".getBytes()); sslHandler.write(ctx, buf, ctx.newPromise()); ``` ```java public void channelRead(ChannelHandlerContext ctx, Object msg) { ByteBuf buf = (ByteBuf) msg; System.out.println(buf.toString(CharsetUtil.UTF_8)); } ``` 以上就是在Netty中使用GmSSL进行加密和解密的基本步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值