使 Okhttp3 绑定到特定的本地地址

Make Okhttp3 bind to a specific local address

我想将通过 okhttp3 发起的 HTTP 客户端连接绑定到特定的网络接口。 VM 上将配置许多 vNIC,并且防火墙规则是特定的。所以,我想控制为此使用哪个 vNIC。我也计算出了正确的 InetAddress。

还有。我已经尝试了 中建议的解决方案,但仍然出现 ConnectionException。有人可以建议我缺少什么来让它工作吗?

      OkHttpClient okclient = new OkHttpClient.Builder()
              .retryOnConnectionFailure(false)
              .socketFactory(new CustomSocketFactory())
              .build();
      Request req = new Request.Builder()
              .url("http://www.google.com")
              .get()
              .build();
      Response rsp = okclient.newCall(req).execute();

下面是 CustomSocketFactory,它与我上面提到的上述 Whosebug 问题几乎相同。

class CustomSocketFactory extends SocketFactory
{
    private static final String ERROR_MSG = "Only unconnected sockets are supported";
    final SocketFactory defaultSocketFactory;
    final InetAddress localAddress;
    final int localPort = 0;

    public CustomSocketFactory() throws Exception
    {
        System.out.println("[creating a custom socket factory]");
        localAddress = InetAddress.getLocalHost();
        defaultSocketFactory = SocketFactory.getDefault();
    }

    @Override
    public Socket createSocket() throws IOException
    {
        System.out.println("I am here");
        Socket s = defaultSocketFactory.createSocket();
        s.bind(new InetSocketAddress(localAddress, localPort));
        return s;
    }

    @Override
    public Socket createSocket(final String remoteAddress, final int remotePort)
    {
        throw new UnsupportedOperationException(ERROR_MSG);
    }

    @Override
    public Socket createSocket(final InetAddress remoteAddress, final int remotePort)
    {
        throw new UnsupportedOperationException(ERROR_MSG);
    }

    @Override
    public Socket createSocket(final String remoteAddress,
                               final int remotePort,
                               final InetAddress localAddress,
                               final int localPort)
    {
        throw new UnsupportedOperationException(ERROR_MSG);
    }

    @Override
    public Socket createSocket(final InetAddress remoteAddress,
                               final int remotePort,
                               final InetAddress localAddress,
                               final int localPort)
    {
        throw new UnsupportedOperationException(ERROR_MSG);
    }

}


Error received: 

java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:244)
    at javax.net.DefaultSocketFactory.createSocket(SocketFactory.java:277)
    at com.okhttp3.CustomSocketFactory.createSocket(TestOkClient.java:74)
    at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:257)
    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:221)
    at okhttp3.RealCall.execute(RealCall.java:81)
    at com.okhttp3.TestOkClient.main(TestOkClient.java:40)

看起来大部分是对的,为什么要使用环回设备InetAddress.getLocalHost()

这是一个类似的解决方案https://github.com/yschimke/okurl/blob/master/src/main/kotlin/com/baulsupp/okurl/network/InterfaceSocketFactory.kt