我正在使用KTor https服务器为我的Android应用程序,我注意到机器人试图连接到我的服务器失败,因为TLS握手没有发生。这是恼人的,因为我的日志文件充满了这些巨大的转储由netty为WARN标记与SL 4J日志。有没有办法处理这样的客户端,而不会导致错误/警告?我尝试在Ktor中使用请求验证插件,但它不起作用。这是我的logback.xml:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{dd-MM-YYYY HH:mm:ss.SSS} [%thread] [%-5level] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="trace">
<appender-ref ref="STDOUT"/>
</root>
<logger name="org.eclipse.jetty" level="WARN"/>
<logger name="io.netty" level="WARN"/>
<logger name="io.ktor" level="WARN"/>
</configuration>
字符串
这是我在/plugins文件夹中Validation.kt中的请求验证函数:
fun Application.configureValidation() {
println("Inside application validation")//this is printed at app startup
install(RequestValidation) {
validate<String> {bodyText ->
if (bodyText.contains("mySite")) {
println("Received request validation text: $bodyText")//this is never printed
ValidationResult.Valid
}
else if (bodyText.contains("myApp")) {
println("Received request validation text: $bodyText")//this is never printed
ValidationResult.Valid
}
else {
println("Received request validation text: $bodyText")//this is never printed
ValidationResult.Invalid("Request Validation failed for bot")
}
}
validate {
filter { body ->
body is Any
}
validation { body ->
println("Received request validation 2 text: $body")//this is never printed
if(body.toString().isNullOrEmpty())
ValidationResult.Invalid("Request Validation failed because its empty")
else
ValidationResult.Valid
}
}
}
println("After application validation")//this is printed at app startup
}
型
日志文件中不会打印任何接收到的请求验证println。
在我的Application.kt中:
fun Application.module() {
printMyStr("Inside Application")
configureValidation()
printMyStr("After validation")
install(StatusPages) {
exception<RequestValidationException> { call, cause ->
call.respond(HttpStatusCode.BadRequest, cause.reasons.joinToString())
}
}
install(ContentNegotiation) {
json()
}
printMyStr("Before socket")
configureSockets()
printMyStr("Before routing")
configureRouting()
printMyStr("After routing")
}
型
我的日志文件中的转储消息示例:
29-09-2023 18:39:13.240 [eventLoopGroupProxy-3-1] [WARN ] i.n.h.s.ApplicationProtocolNegotiationHandler - [id: 0x54c76869, L:0.0.0.0/0.0.0.0:1234 ! R:/34.756.619.638:9375] TLS handshake failed:
io.netty.handler.ssl.NotSslRecordException: not an SSL/TLS record: 0300002f2ae00000000000436f6f6b69653a206d737473686173683d41646d696e697374720d0a0100080003000000
at io.netty.handler.ssl.SslHandler.decodeJdkCompatible(SslHandler.java:1225)
at io.netty.handler.ssl.SslHandler.decode(SslHandler.java:1295)
at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:529)
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:468)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:290)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:440)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:788)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.ktor.server.netty.EventLoopGroupProxy$Companion.create$lambda$1$lambda$0(NettyApplicationEngine.kt:296)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:750)
30-09-2023 04:06:41.006 [eventLoopGroupProxy-3-1] [WARN ] i.n.h.s.ApplicationProtocolNegotiationHandler - [id: 0xe8b12e42, L:0.0.0.0/0.0.0.0:1234 ! R:/432.690.528.47:9264] TLS handshake failed:
io.netty.handler.ssl.NotSslRecordException: not an SSL/TLS record: 0100000000cd00010000414e592d5343502020202020202020204543484f534355202020202020202020000000000000000000000000000000000000000000000000000000000000000010000015312e322e3834302e31303030382e332e312e312e312000002e0100ff0030000011312e322e3834302e31303030382e312e3140000011312e322e3834302e31303030382e312e325000003a51000004000040005200001b312e322e3237362e302e373233303031302e332e302e332e362e325500000f4f464649535f44434d544b5f333632
at io.netty.handler.ssl.SslHandler.decodeJdkCompatible(SslHandler.java:1225)
at io.netty.handler.ssl.SslHandler.decode(SslHandler.java:1295)
at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:529)
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:468)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:290)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:440)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:788)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.ktor.server.netty.EventLoopGroupProxy$Companion.create$lambda$1$lambda$0(NettyApplicationEngine.kt:296)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:750)
型
1条答案
按热度按时间dojqjjoe1#
使用Janino过滤异常是不起作用的,正如Aleksei在上面的评论中所推荐的那样。下面是我的logback.xml:
字符串
下面是打印在我的日志文件现在:
型
由于这没有工作,并打印follwing仍然:
型
所以唯一的解决办法似乎是将logger更改为只打印ERROR级别的消息,而不是logback.xml中的WARN级别:
型