Skip to content

Commit f1c93b8

Browse files
committed
Close connection on CancelledKeyException
1 parent 9464045 commit f1c93b8

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name := "scala-tlsproxy"
22
organization := "io.github.erikvanzijst"
33

4-
version := "0.4.1"
4+
version := "0.4.2"
55

66
scalaVersion := "2.12.14"
77

src/main/scala/io/github/erikvanzijst/scalatlsproxy/TlsProxyHandler.scala

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ package io.github.erikvanzijst.scalatlsproxy
33
import java.io.IOException
44
import java.net.InetSocketAddress
55
import java.nio.ByteBuffer
6-
import java.nio.channels.{SelectionKey, Selector, SocketChannel, UnresolvedAddressException}
6+
import java.nio.channels.{CancelledKeyException, SelectionKey, Selector, SocketChannel, UnresolvedAddressException}
77
import java.nio.charset.StandardCharsets
8-
98
import com.typesafe.scalalogging.StrictLogging
109

1110
import scala.util.Try
@@ -152,7 +151,7 @@ class TlsProxyHandler(selector: Selector, clientChannel: SocketChannel, config:
152151
}.get
153152

154153
if (phase == Response)
155-
if (clientKey.isValid && clientKey.isWritable) {
154+
if (clientKey.isWritable) {
156155
serverBuffer.flip()
157156
clientChannel.write(serverBuffer)
158157
serverBuffer.compact()
@@ -180,7 +179,7 @@ class TlsProxyHandler(selector: Selector, clientChannel: SocketChannel, config:
180179
}
181180

182181
if (phase == Error)
183-
if (clientKey.isValid && clientKey.isWritable) {
182+
if (clientKey.isWritable) {
184183
serverBuffer.flip()
185184
clientChannel.write(serverBuffer)
186185
serverBuffer.compact()
@@ -190,11 +189,10 @@ class TlsProxyHandler(selector: Selector, clientChannel: SocketChannel, config:
190189
}
191190

192191
} catch {
193-
case e: IOException =>
194-
val msg = s"$clientAddress -> $getServerAddress" +
192+
case e @ (_: IOException | _: CancelledKeyException) =>
193+
logger.warn(s"$clientAddress -> $getServerAddress" +
195194
(if (phase == Established) s" (up: ${upstreamPipe.bytes} down: ${downstreamPipe.bytes})" else "") +
196-
s" connection failed: ${e.getClass.getSimpleName}: ${e.getMessage}"
197-
logger.warn(msg)
195+
s" connection failed: ${e.getClass.getSimpleName}: ${e.getMessage}")
198196
close()
199197
}
200198

0 commit comments

Comments
 (0)