TCP socket with SSL on Scala with Akka

2019-02-18 03:11发布

问题:

Using Scala 2.10 and Akka 2.3.4, I've put together a simple proxy server that accepts incoming TCP connections and then proxies those messages to a remote server. Things are working with plain text, but I'm stuck with SSL.

Briefly, this is how I launch my non-secure server for incoming connections:

val server = system.actorOf(Props(new LegacyTCPServer), name = "my-tcp-server")

implicit val bindingTimeout = Timeout(1.second)
import system.dispatcher // execution context for the future

val boundFuture = IO(Tcp) ? Tcp.Bind(server, endpoint)

boundFuture.onSuccess { case Tcp.Bound(address) =>
  println("Fantastic! We have a connection: " + address)
}

I can connect to this server via telnet, but now I'd like to move no to opensl. I guess there must be some configuration options for that, but I can't seem to parse it from the documentation: http://doc.akka.io/docs/akka/2.3.4/scala.html

I have seen some (non-functional) examples using akka 2.2.x that use a TCPPipelineHandler, e.g.,

https://groups.google.com/forum/#!topic/akka-user/auErrrk9wS0

https://github.com/betehess/ping-pong-bot/blob/master/app/ircbot/IrcClient.scala#L183

but TCPPipelineHandler doesn't seem to exist in akka 2.3.x, so that feels like a dead end.

I would love it if someone could provide an example of how to set up a tcp socket over ssl using current versions of Scala & Akka.

Please let me know if you'd like more information. Thanks!

回答1:

spray is HTTP server/client built on top of Akka IO.

There is a pipelining infrastructure and SslTlsSupport in "io.spray" % "spray-io" % "1.3.1" package. I use it in a project, I'm currently working on. For more details, please see how it is configured for HttpServerConnection in spray.

You would need to refactor your code to use pipelines from spray, from my experience code becomes much easier if you split your code into multiple stages each responsible for a small piece.