From 21374c145c0f8de8639f4b5a64c646fc650af249 Mon Sep 17 00:00:00 2001 From: Erik van Zijst Date: Fri, 3 Sep 2021 17:47:20 +0200 Subject: [PATCH] Don't NPE on ServerChannel.getRemoteAddress --- .../erikvanzijst/scalatlsproxy/TlsProxyHandler.scala | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main/scala/io/github/erikvanzijst/scalatlsproxy/TlsProxyHandler.scala b/src/main/scala/io/github/erikvanzijst/scalatlsproxy/TlsProxyHandler.scala index ffeb2f4..4e97c2e 100644 --- a/src/main/scala/io/github/erikvanzijst/scalatlsproxy/TlsProxyHandler.scala +++ b/src/main/scala/io/github/erikvanzijst/scalatlsproxy/TlsProxyHandler.scala @@ -45,13 +45,11 @@ class TlsProxyHandler(selector: Selector, clientChannel: SocketChannel, config: private var phase = Destination - def getServerAddress: String = Option(serverChannel) - .filter(_.isOpen) - .map(_.getRemoteAddress.toString) - .orElse(Option(destination) - .map(d => d._1 + ":" + d._2) - .orElse(Some("unconnected")) ) - .get + def getServerAddress: String = + Try(serverChannel.getRemoteAddress.toString) + .recover { case _ => destination._1 + ":" + destination._2 } + .recover { case _ => "unconnected" } + .get private def readClient(): Unit = { if (clientKey.isValid && clientKey.isReadable && clientChannel.read(clientBuffer) == -1)