Skip to content

Commit

Permalink
Close connection on CancelledKeyException
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvanzijst committed Sep 1, 2021
1 parent 9464045 commit f1c93b8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name := "scala-tlsproxy"
organization := "io.github.erikvanzijst"

version := "0.4.1"
version := "0.4.2"

scalaVersion := "2.12.14"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package io.github.erikvanzijst.scalatlsproxy
import java.io.IOException
import java.net.InetSocketAddress
import java.nio.ByteBuffer
import java.nio.channels.{SelectionKey, Selector, SocketChannel, UnresolvedAddressException}
import java.nio.channels.{CancelledKeyException, SelectionKey, Selector, SocketChannel, UnresolvedAddressException}
import java.nio.charset.StandardCharsets

import com.typesafe.scalalogging.StrictLogging

import scala.util.Try
Expand Down Expand Up @@ -152,7 +151,7 @@ class TlsProxyHandler(selector: Selector, clientChannel: SocketChannel, config:
}.get

if (phase == Response)
if (clientKey.isValid && clientKey.isWritable) {
if (clientKey.isWritable) {
serverBuffer.flip()
clientChannel.write(serverBuffer)
serverBuffer.compact()
Expand Down Expand Up @@ -180,7 +179,7 @@ class TlsProxyHandler(selector: Selector, clientChannel: SocketChannel, config:
}

if (phase == Error)
if (clientKey.isValid && clientKey.isWritable) {
if (clientKey.isWritable) {
serverBuffer.flip()
clientChannel.write(serverBuffer)
serverBuffer.compact()
Expand All @@ -190,11 +189,10 @@ class TlsProxyHandler(selector: Selector, clientChannel: SocketChannel, config:
}

} catch {
case e: IOException =>
val msg = s"$clientAddress -> $getServerAddress" +
case e @ (_: IOException | _: CancelledKeyException) =>
logger.warn(s"$clientAddress -> $getServerAddress" +
(if (phase == Established) s" (up: ${upstreamPipe.bytes} down: ${downstreamPipe.bytes})" else "") +
s" connection failed: ${e.getClass.getSimpleName}: ${e.getMessage}"
logger.warn(msg)
s" connection failed: ${e.getClass.getSimpleName}: ${e.getMessage}")
close()
}

Expand Down

0 comments on commit f1c93b8

Please sign in to comment.