次のコードを使用していて、共有したいと思っています。
基本的に、私はakkaコミュニティから得たTcpTlsEcho.javaを見て始めました。
私はakka-streamsの文書に従った。示し、アッカ・ストリームの使用法を説明するもう一つの非常に良い例は、以下のblog post
接続設定で見つけることができ、流れは次のようになります。
/**
+---------------------------+ +---------------------------+
| Flow | | tlsConnectionFlow |
| | | |
| +------+ +------+ | | +------+ +------+ |
| | SRC | ~Out~> | | ~~> O2 -- I1 ~~> | | ~O1~> | | |
| | | | LOGG | | | | TLS | | CONN | |
| | SINK | <~In~ | | <~~ I2 -- O2 <~~ | | <~I2~ | | |
| +------+ +------+ | | +------+ +------+ |
+---------------------------+ +---------------------------+
**/
// the tcp connection to the server
val connection = Tcp().outgoingConnection(address, port)
// ignore the received data for now. There are different actions to implement the Sink.
val sink = Sink.ignore
// create a source as an actor reference
val source = Source.actorRef(1000, OverflowStrategy.fail)
// join the TLS BidiFlow (see below) with the connection
val tlsConnectionFlow = tlsStage(TLSRole.client).join(connection)
// run the source with the TLS conection flow that is joined with a logging step that prints the bytes that are sent and or received from the connection.
val sourceActor = tlsConnectionFlow.join(logging).to(sink).runWith(source)
// send a message to the sourceActor that will be send to the Source of the stream
sourceActor ! ByteString("<message>")
TLS接続の流れがBidiFlowです。私の最初の簡単な例では、すべての証明書を無視し、信頼とキーストアの管理を避けています。例がどのように行われたかは、上記の.javaの例を参照してください。
def tlsStage(role: TLSRole)(implicit system: ActorSystem) = {
val sslConfig = AkkaSSLConfig.get(system)
val config = sslConfig.config
// create a ssl-context that ignores self-signed certificates
implicit val sslContext: SSLContext = {
object WideOpenX509TrustManager extends X509TrustManager {
override def checkClientTrusted(chain: Array[X509Certificate], authType: String) =()
override def checkServerTrusted(chain: Array[X509Certificate], authType: String) =()
override def getAcceptedIssuers = Array[X509Certificate]()
}
val context = SSLContext.getInstance("TLS")
context.init(Array[KeyManager](), Array(WideOpenX509TrustManager), null)
context
}
// protocols
val defaultParams = sslContext.getDefaultSSLParameters()
val defaultProtocols = defaultParams.getProtocols()
val protocols = sslConfig.configureProtocols(defaultProtocols, config)
defaultParams.setProtocols(protocols)
// ciphers
val defaultCiphers = defaultParams.getCipherSuites()
val cipherSuites = sslConfig.configureCipherSuites(defaultCiphers, config)
defaultParams.setCipherSuites(cipherSuites)
val firstSession = new TLSProtocol.NegotiateNewSession(None, None, None, None)
.withCipherSuites(cipherSuites: _*)
.withProtocols(protocols: _*)
.withParameters(defaultParams)
val clientAuth = getClientAuth(config.sslParametersConfig.clientAuth)
clientAuth map { firstSession.withClientAuth(_) }
val tls = TLS.apply(sslContext, firstSession, role)
val pf: PartialFunction[TLSProtocol.SslTlsInbound, ByteString] = {
case TLSProtocol.SessionBytes(_, sb) => ByteString.fromByteBuffer(sb.asByteBuffer)
}
val tlsSupport = BidiFlow.fromFlows(
Flow[ByteString].map(TLSProtocol.SendBytes),
Flow[TLSProtocol.SslTlsInbound].collect(pf));
tlsSupport.atop(tls);
}
def getClientAuth(auth: ClientAuth) = {
if (auth.equals(ClientAuth.want)) {
Some(TLSClientAuth.want)
} else if (auth.equals(ClientAuth.need)) {
Some(TLSClientAuth.need)
} else if (auth.equals(ClientAuth.none)) {
Some(TLSClientAuth.none)
} else {
None
}
}
そして、完了のために、BidiFlowとして実装されたロギングステージがあります。
def logging: BidiFlow[ByteString, ByteString, ByteString, ByteString, NotUsed] = {
// function that takes a string, prints it with some fixed prefix in front and returns the string again
def logger(prefix: String) = (chunk: ByteString) => {
println(prefix + chunk.utf8String)
chunk
}
val inputLogger = logger("> ")
val outputLogger = logger("< ")
// create BidiFlow with a separate logger function for each of both streams
BidiFlow.fromFunctions(outputLogger, inputLogger)
}
私はさらに回答を改善し、更新しようとします。希望が役立ちます。
TLSサポートの使用例は、単体テストで確認できます。 https://github.com/akka/akka/blob/master/akka-stream-tests/src/test/scala/akka/stream/io/TlsSpec.scala。それが役に立てば幸い! –