Netty代理服务器引发的NotSslRecordException异常

时间:2019-04-23 16:47:40

标签: ssl netty

我正在将本地Netty服务器连接到远程https服务器以代理请求。

public class CustomInitializer extends ChannelInitializer<SocketChannel> {

    @Autowired(required = false)
    private SslContext sslContext;


    @Override
    public void initChannel(SocketChannel ch) {
        ChannelPipeline p = ch.pipeline();
        if (sslContext != null) {
            p.addLast(sslContext.newHandler(ch.alloc()));
        }
        p.addLast(new LoggingHandler(LogLevel.INFO));
        p.addLast(new HttpClientCodec());
        p.addLast(new HttpObjectAggregator(8*1024, true));
        p.addLast(new MyCustomHandler());
    }
}

这是我创建ssLcontext bean的方式

@Bean
    public SslContext sslContext(@Value("${tcp.ssl}") boolean ssl) throws SSLException, CertificateException {
        SslContext sslContext = null;
        if (ssl) {
            SelfSignedCertificate ssc = new SelfSignedCertificate();
            sslContext = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
        }
        return sslContext;
    }

当我访问本地主机时,它应该代理到后端。我得到以下异常。但是,如果关闭SSL并连接到远程服务器(该本地服务器在其他端口上运行),则可以正常工作

io.netty.handler.codec.DecoderException: io.netty.handler.ssl.NotSslRecordException: not an SSL/TLS record: dsffddfs
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:459)
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:265)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
    at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1434)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:965)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:647)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysPlain(NioEventLoop.java:547)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:501)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:461)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:834)
io.netty.handler.ssl.NotSslRecordException: not an SSL/TLS record: thjhjhhj     at io.netty.handler.ssl.SslHandler.decodeJdkCompatible(SslHandler.java:1178)
    at io.netty.handler.ssl.SslHandler.decode(SslHandler.java:1243)
    at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:489)
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:428)
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:265)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
    at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1434)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:965)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:647)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysPlain(NioEventLoop.java:547)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:501)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:461)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:834)
io.netty.handler.codec.DecoderException: io.netty.handler.ssl.NotSslRecordException: not an SSL/TLS record: tyuuiiio
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:459)
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:265)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
    at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1434)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:965)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:647)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysPlain(NioEventLoop.java:547)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:501)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:461)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: io.netty.handler.ssl.NotSslRecordException: not an SSL/TLS record: 678900
    at io.netty.handler.ssl.SslHandler.decodeJdkCompatible(SslHandler.java:1178)
    at io.netty.handler.ssl.SslHandler.decode(SslHandler.java:1243)
    at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:489)
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:428)
    ... 16 more

编辑

已添加日志

X`20190423173352.427``40`````io.netty.handler.logging.LoggingHandler```````eE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0xc839c47e, L:/0:0:0:0:0:0:0:0:8080] READ: [id: 0xca2cb8c7, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56170]
X`20190423173352.427``40`````io.netty.handler.logging.LoggingHandler```````fE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0xc839c47e, L:/0:0:0:0:0:0:0:0:8080] READ COMPLETE
X`20190423173352.428``40`````io.netty.handler.logging.LoggingHandler```````gE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0xc839c47e, L:/0:0:0:0:0:0:0:0:8080] READ: [id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171]
X`20190423173352.428``76`````io.netty.handler.logging.LoggingHandler```````hE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0xca2cb8c7, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56170] REGISTERED
X`20190423173352.428``40`````io.netty.handler.logging.LoggingHandler```````iE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0xc839c47e, L:/0:0:0:0:0:0:0:0:8080] READ COMPLETE
X`20190423173352.428``76`````io.netty.handler.logging.LoggingHandler```````jE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0xca2cb8c7, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56170] ACTIVE
X`20190423173352.429``77`````io.netty.handler.logging.LoggingHandler```````kE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] REGISTERED
X`20190423173352.429``77`````io.netty.handler.logging.LoggingHandler```````lE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] ACTIVE
X`20190423173352.442``76`````io.netty.handler.logging.LoggingHandler```````mE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0xca2cb8c7, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56170] READ COMPLETE
X`20190423173352.442``77`````io.netty.handler.logging.LoggingHandler```````nE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] READ: 294B

X`20190423173352.442``76`````io.netty.handler.logging.LoggingHandler```````oE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0xca2cb8c7, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56170] WRITE: 0B
X`20190423173352.442``76`````io.netty.handler.logging.LoggingHandler```````pE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0xca2cb8c7, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56170] FLUSH
X`20190423173352.443``76`````io.netty.handler.logging.LoggingHandler```````qE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0xca2cb8c7, L:/0:0:0:0:0:0:0:1:8080 ! R:/0:0:0:0:0:0:0:1:56170] INACTIVE
X`20190423173352.443``76`````io.netty.handler.logging.LoggingHandler```````rE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0xca2cb8c7, L:/0:0:0:0:0:0:0:1:8080 ! R:/0:0:0:0:0:0:0:1:56170] FLUSH
X`20190423173352.443``76`````io.netty.handler.logging.LoggingHandler```````sE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0xca2cb8c7, L:/0:0:0:0:0:0:0:1:8080 ! R:/0:0:0:0:0:0:0:1:56170] CLOSE
X`20190423173352.443``76`````io.netty.handler.logging.LoggingHandler```````tE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0xca2cb8c7, L:/0:0:0:0:0:0:0:1:8080 ! R:/0:0:0:0:0:0:0:1:56170] UNREGISTERED
X`20190423173352.450``77`````io.netty.handler.logging.LoggingHandler```````uE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] WRITE: 734B
X`20190423173352.452``77`````io.netty.handler.logging.LoggingHandler```````vE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] READ COMPLETE
X`20190423173352.452``77`````io.netty.handler.logging.LoggingHandler```````wE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] FLUSH
X`20190423173352.452``77`````io.netty.handler.logging.LoggingHandler```````xE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] WRITE: 0B
X`20190423173352.452``77`````io.netty.handler.logging.LoggingHandler```````yE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] FLUSH
X`20190423173352.453``77`````io.netty.handler.logging.LoggingHandler```````zE````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] READ: 126B
X`20190423173352.456``77`````io.netty.handler.logging.LoggingHandler```````0E````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] WRITE: 6B
X`20190423173352.457``77`````io.netty.handler.logging.LoggingHandler```````1E````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] WRITE: 45B
X`20190423173352.457``77`````io.netty.handler.logging.LoggingHandler```````2E````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] WRITE: 0B
X`20190423173352.457``77`````io.netty.handler.logging.LoggingHandler```````3E````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] READ COMPLETE
X`20190423173352.457``77`````io.netty.handler.logging.LoggingHandler```````4E````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] FLUSH
X`20190423173352.457``77`````io.netty.handler.logging.LoggingHandler```````5E````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] WRITE: 0B
X`20190423173352.457``77`````io.netty.handler.logging.LoggingHandler```````6E````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] WRITE: 0B
X`20190423173352.457``77`````io.netty.handler.logging.LoggingHandler```````7E````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] FLUSH
X`20190423173352.458``77`````io.netty.handler.logging.LoggingHandler```````8E````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] READ: 512B
X`20190423173352.458``77`````io.netty.handler.logging.LoggingHandler```````9E````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] READ: 87B
X`20190423173352.460``77`````io.netty.handler.logging.LoggingHandler```````-E````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x8c3b87f8] REGISTERED
X`20190423173352.460``77`````io.netty.handler.logging.LoggingHandler```````_E````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] READ COMPLETE
X`20190423173352.460``77`````io.netty.handler.logging.LoggingHandler```````AF````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] WRITE: 0B
X`20190423173352.461``77`````io.netty.handler.logging.LoggingHandler```````BF````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] WRITE: 0B
X`20190423173352.461``77`````io.netty.handler.logging.LoggingHandler```````CF````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] FLUSH
X`20190423173352.461``77`````io.netty.handler.logging.LoggingHandler```````DF````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x8c3b87f8] CONNECT: dom.fbde.lol.hepd.com/122.126.80.1:443
X`20190423173352.483``77`````io.netty.handler.logging.LoggingHandler```````EF````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x8c3b87f8, L:/10.3.49.27:56172 - R:dom.fbde.lol.hepd.com/122.126.80.1:443] WRITE: 0B
X`20190423173352.484``77`````io.netty.handler.logging.LoggingHandler```````FF````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x8c3b87f8, L:/10.3.49.27:56172 - R:dom.fbde.lol.hepd.com/122.126.80.1:443] FLUSH
X`20190423173352.484``77`````io.netty.handler.logging.LoggingHandler```````GF````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x8c3b87f8, L:/10.3.49.27:56172 - R:dom.fbde.lol.hepd.com/122.126.80.1:443] ACTIVE
X`20190423173402.488``77`````io.netty.handler.logging.LoggingHandler```````HF````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x8c3b87f8, L:/10.3.49.27:56172 - R:dom.fbde.lol.hepd.com/122.126.80.1:443] FLUSH
X`20190423173402.488``77`````io.netty.handler.logging.LoggingHandler```````IF````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x8c3b87f8, L:/10.3.49.27:56172 - R:dom.fbde.lol.hepd.com/122.126.80.1:443] CLOSE
X`20190423173402.489``77`````io.netty.handler.logging.LoggingHandler```````JF````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] WRITE: 31B
X`20190423173402.489``77`````io.netty.handler.logging.LoggingHandler```````KF````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] FLUSH
X`20190423173402.490``77`````io.netty.handler.logging.LoggingHandler```````LF````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:56171] CLOSE
X`20190423173402.490``77`````io.netty.handler.logging.LoggingHandler```````MF````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x8c3b87f8, L:/10.3.49.27:56172 ! R:dom.fbde.lol.hepd.com/122.126.80.1:443] CLOSE
X`20190423173402.490``77`````io.netty.handler.logging.LoggingHandler```````NF````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x8c3b87f8, L:/10.3.49.27:56172 ! R:dom.fbde.lol.hepd.com/122.126.80.1:443] INACTIVE
X`20190423173402.490``77`````io.netty.handler.logging.LoggingHandler```````OF````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x8c3b87f8, L:/10.3.49.27:56172 ! R:dom.fbde.lol.hepd.com/122.126.80.1:443] UNREGISTERED
X`20190423173402.490``77`````io.netty.handler.logging.LoggingHandler```````PF````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 ! R:/0:0:0:0:0:0:0:1:56171] INACTIVE
X`20190423173402.491``77`````io.netty.handler.logging.LoggingHandler```````QF````INFO`io.netty.handler.logging.LoggingHandler`[id: 0x08537ac8, L:/0:0:0:0:0:0:0:1:8080 ! R:/0:0:0:0:0:0:0:1:56171] UNREGISTERED

1 个答案:

答案 0 :(得分:1)

当您收到NotSslRecordException异常时,最好的起点是错误之后的字节随机blob。

您的“ blob”以以下字节开头:504f5354...

converting十六进制数据返回到字符串之后,您将得到50 = P4f = O53 = { {1}},S = 54,最终结果:T

由于我们现在将二进制Blob的开头转换为字符串,因此现在我们需要弄清楚哪些协议使用了这些“魔术字符串”。我们不必花很多时间,它是POST,您不能使用HTTP Request来接收它。

解决方案很简单,使用HTTPS而不是https连接到服务器:

https://localhost:8080/

相关问题