Kotlin 在调用期间使用OkHttp访问底层TCP流

Kotlin 在调用期间使用OkHttp访问底层TCP流,kotlin,okhttp,Kotlin,Okhttp,我已经为Docker API实现了一个客户端 附加到容器输出的端点有点不寻常,因为您需要劫持底层TCP流并使用它从容器读取输出 API端点文档:(不幸的是,此端点的web套接字版本是,所以我不能使用它。) 我一直在使用OkHttp v3.13.1(包括OkHttp v3.13.1)访问流: class ConnectionHijacker : Interceptor { var source: BufferedSource? = null var sink: BufferedSi

我已经为Docker API实现了一个客户端

附加到容器输出的端点有点不寻常,因为您需要劫持底层TCP流并使用它从容器读取输出

API端点文档:(不幸的是,此端点的web套接字版本是,所以我不能使用它。)

我一直在使用OkHttp v3.13.1(包括OkHttp v3.13.1)访问流:

class ConnectionHijacker : Interceptor {
    var source: BufferedSource? = null
    var sink: BufferedSink? = null

    override fun intercept(chain: Interceptor.Chain): Response {
        val connection = chain.connection() as RealConnection
        val streams = connection.newWebSocketStreams(connection.allocations.single().get())

        sink = streams.sink
        source = streams.source

        return chain.proceed(chain.request())
    }
}
这很好用

然而,在较新的版本中,OkHttp API发生了显著的变化。类似的内容适用于v1.13.1,并可在更高版本中编译,但不会在流中提供任何输出:

class ConnectionHijacker : Interceptor {
    var source: BufferedSource? = null
    var sink: BufferedSink? = null

    override fun intercept(chain: Interceptor.Chain): Response {
        val connection = chain.connection() as RealConnection

        sink = connection.sink
        source = connection.source

        return chain.proceed(chain.request())
    }

    private val RealConnection.sink: BufferedSink
        get() {
            val property = RealConnection::class.declaredMemberProperties.single { it.name == "sink" }
            property.isAccessible = true
            return property.get(this) as BufferedSink
        }

    private val RealConnection.source: BufferedSource
        get() {
            val property = RealConnection::class.declaredMemberProperties.single { it.name == "source" }
            property.isAccessible = true
            return property.get(this) as BufferedSource
        }
}

我意识到这是一个在另一个黑客之上的黑客攻击,完全不受支持,但有人对我如何实现这一点有任何想法吗?

如果您可以使用HTTP/2,请查看。它将允许您访问请求输出和响应输入,以执行您喜欢的操作。

我希望能够这样做,但遗憾的是Docker API不支持HTTP/2。