1

我试图遵循原始套接字的 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

4

0 回答 0