Ktor安全套接字(SSL / TLS)Windows示例?

时间:2018-12-13 06:35:16

标签: sockets ssl kotlin ktor

我试图遵循原始套接字的ktor文档,特别是与安全套接字(https://ktor.io/servers/raw-sockets.html)相关的部分:

runBlocking {
    val socket = aSocket(ActorSelectorManager(ioCoroutineDispatcher)).tcp().connect(InetSocketAddress("google.com", 443)).tls()
    val w = socket.openWriteChannel(autoFlush = false)
    w.write("GET / HTTP/1.1\r\n")
    w.write("Host: google.com\r\n")
    w.write("\r\n")
    w.flush()
    val r = socket.openReadChannel()
    println(r.readUTF8Line())
}

You can adjust a few optional parameters for the TLS connection:

suspend fun Socket.tls(
        trustManager: X509TrustManager? = null,
        randomAlgorithm: String = "NativePRNGNonBlocking",
        serverName: String? = null,
        coroutineContext: CoroutineContext = ioCoroutineDispatcher
): Socket

但是 NativePRNGNonBlocking SecureRandom算法在Windows上不可用,所以我唯一的选择是使用 SHA1PRNG https://docs.oracle.com/javase/8/docs/technotes/guides/security/SunProviders.html#SecureRandomImp

这是我正在运行的用于连接到侦听套接字的代码:

socket = aSocket(ActorSelectorManager(Dispatchers.IO)).tcp().connect(InetSocketAddress(host, port))
    .tls(Dispatchers.IO, randomAlgorithm = "SHA1PRNG")

不幸的是,我总是收到相同的错误:“ 频道已关闭

如果我删除tls,则仅保留原始套接字:

socket = aSocket(ActorSelectorManager(Dispatchers.IO)).tcp().connect(InetSocketAddress(host, port))

一切正常。

有人在Windows中使用过Ktor安全套接字吗? (不幸的是,Ktor的文档还有很长的路要走。)

谢谢, J

0 个答案:

没有答案