在Netty反向代理中启用SSL支持

时间:2014-11-26 14:05:41

标签: java ssl https proxy netty

我使用netty编写了一个代理。代理接受传入的客户端请求并将其发送到后端服务。我在[1]中使用了Netty Proxy。这适用于HTTP后端服务。现在我需要扩展它,以便代理也可以使用HTTPS /安全后端。

客户---------------->代理服务-------------------->后端服务

我在你给出的HexdumpProxy类中创建了一个SSLContext。

    SslContext sslCtx = null;
    if (isSsl) {
        try {
            SelfSignedCertificate ssc = new SelfSignedCertificate();
            sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (SSLException e) {
            e.printStackTrace();
        }
    }

首先,我使客户端启用了代理服务SSL。我在HexDumpProxyInitializer中做了如下。以下使用了上面创建的SSL上下文。

protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    if (sslContext != null) {
        pipeline.addLast(sslContext.newHandler(ch.alloc()))
    }


    pipeline.addLast(new LoggingHandler(LogLevel.INFO),
                     new HexDumpProxyFrontendHandler(remoteHost, remotePort));

}

这很好用,客户端可以向代理服务发送HTTPS请求。然后,代理将HTTP请求发送到HTTP后端服务。

然后我想保护代理和后端服务之间的通道。这是我遇到问题的地方。我通过以下几种方式做到了。

首先我尝试在创建出站通道后,在HexDumpProxyFrontendHandler类的channelActive mathod中添加它,如下所示。

    SslContext sslCtx = null;
    try {
        sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
    } catch (SSLException e) {
        e.printStackTrace();
    }
    outboundChannel.pipeline().addLast(sslCtx.newHandler(outboundChannel.alloc(), remoteHost,
                                                         remotePort));

它给出了一些错误。然后我将它添加到HexDumpProxyInitializer类的initChannel方法中。它也没有用。有人可以告诉我如何将SSL支持添加到此代理。我也试过安全聊天样本。您可以在这里找到我的项目[2],但我已经注释了为SSL通信编写的一些代码,所以请不要误导。

[1] http://netty.io/4.1/xref/io/netty/example/proxy/package-summary.html

[2] https://github.com/ravindraranwala/NettyReverseProxy

0 个答案:

没有答案
相关问题