kohlschutter / junixsocket

Unix Domain Sockets in Java 7 and newer (AF_UNIX), AF_TIPC, AF_VSOCK, and more
Apache License 2.0
433 stars 114 forks source link

OkHTTP + junixsocket #79

Closed slinkydeveloper closed 3 years ago

slinkydeveloper commented 4 years ago

Hi, I'm trying to use junixsocket in conjunction with okhttp3 (3.14.6) to use HTTP over UDS.

I'm trying something like:

      OkHttpClient specificClient =
          sharedClient
              .newBuilder()
              .socketFactory(new AFUNIXSocketFactory.FactoryArg(sockFile))
              .callTimeout(spec.maxRequestDuration())
              .build();

And then i use this address to perform the request:

HttpUrl.get("http://127.0.0.1/")

But as soon as i try to perform a request i get this:

Caused by: java.net.SocketException: Not open
    at org.newsclub.net.unix.AFUNIXSocketImpl.validFdOrException(AFUNIXSocketImpl.java:424)
    at org.newsclub.net.unix.AFUNIXSocketImpl.setOption(AFUNIXSocketImpl.java:504)
    at org.newsclub.net.unix.AFUNIXSocketImpl$Lenient.setOption(AFUNIXSocketImpl.java:568)
    at java.base/java.net.Socket.setSoTimeout(Socket.java:1239)
    at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:261)
    at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:183)
    at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.java:224)
    at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.java:108)
    at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.java:88)
    at okhttp3.internal.connection.Transmitter.newExchange(Transmitter.java:169)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:41)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:88)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:229)
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:172)

Trying to debug it, I've found that the unix socket factory AFUNIXSocketFactory.FactoryArg is never invoked, because okhttp uses always the SocketFactory method Socket createSocket() which doesn't set the sockFile I've provided as parameter of AFUNIXSocketFactory.FactoryArg. I've tried to search a workaround implementing manually a SocketFactory, but i didn't managed to make it working.

Any hints?

gesellix commented 4 years ago

I use the AFUNIXSocket in a custom Socket like this: https://github.com/docker-client/docker-filesocket/blob/a03c2238fa40f20be86fac72e6edf668a314b8c4/src/main/java/de/gesellix/docker/client/filesocket/UnixSocket.java#L36 Maybe that code gives you some inspiration.

raymank26 commented 3 years ago

Also have had a similar issue (jdk 11.0.8):

java.net.SocketException: already connected
    at java.base/java.net.Socket.connect(Socket.java:589)
    at my.package.UnixSocketWrapper.connect(TunnelingUnixSocket.kt:<line_number>)
    at okhttp3.internal.platform.Platform.connectSocket(Platform.kt:117)
    at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:266)
    at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:180)

but at the same time curl --unix-socket ... worked as expected. The solution pointed by @gesellix resolved the problem for me.

kohlschuetter commented 3 years ago

Thanks for reporting! This should be fixed in 2.4.0 (just released)