Pu Zhibing
7 天以前 22c308a8c1317473c7279f7bf866814c64ef36e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.sinata.push.util.applets;
 
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.ssl.ClientAuth;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import io.netty.handler.stream.ChunkedWriteHandler;
 
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import java.io.File;
 
public class ChildChannelHandler extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
//        String path = "C:\\app\\cert\\tomcat\\www.gzjwzc.com.pfx";
//        SSLContext sslContext = createSSLContext.createSSLContext("PKCS12"
//                , path, "79uc9bsd");
//        SSLEngine engine = sslContext.createSSLEngine();
    
        
        File path = new File("/root/server/app/cert/qytzt.cn.key");
        File path1 = new File("/root/server/app/cert/qytzt.cn.pem");
        SslContext sslContext = SslContextBuilder.forServer(path, path1, null).clientAuth(ClientAuth.NONE).build();
        
        //SSLEngine 此类允许使用ssl安全套接层协议进行安全通信
        SSLEngine engine = sslContext.newEngine(socketChannel.alloc());
        engine.setUseClientMode(false);
        socketChannel.pipeline().addLast("ssl", new SslHandler(engine));
 
        // 设置30秒没有读到数据,则触发一个READER_IDLE事件。
        // pipeline.addLast(new IdleStateHandler(30, 0, 0));
        // HttpServerCodec:将请求和应答消息解码为HTTP消息
        socketChannel.pipeline().addLast("http-codec", new HttpServerCodec());
        // HttpObjectAggregator:将HTTP消息的多个部分合成一条完整的HTTP消息
        socketChannel.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
        // ChunkedWriteHandler:向客户端发送HTML5文件
        socketChannel.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
        // 在管道中添加我们自己的接收数据实现方法
        socketChannel.pipeline().addLast("handler", new WebSocketHandler());
    }
}