pusher / pusher-websocket-java

Pusher Channels client library for Java targeting general Java and Android
Other
309 stars 143 forks source link

Proxy settings not working #356

Closed apouche closed 1 year ago

apouche commented 1 year ago

What is the issue?

The issue I'm about to describe is probably related to #99 but given that the issue is still open and slightly different than what I'm seeing I preferred to open a new one.

I'm trying to use Proxyman (macOS) to intercept pusher traffic. However this just simply doesn't work on the Android emulator. I've tried many different configuration options (with or without TLS), with DIRECT (NO_PROXY), HTTP and SOCKS and it always fails with different errors.

Here are my findings:

DIRECT

With a DIRECT (NO_PROXY) proxy the connection never establishes (connection state goes from DISCONNECTED to CONNECTING) and eventually fails with:

java.net.ConnectException: failed to connect to ws-eu.pusher.com/34.255.229.199 (port 443) from /:: (port 58482): connect failed: ECONNREFUSED (Connection refused)
at libcore.io.IoBridge.connect(IoBridge.java:188)
at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:142)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)

HTTP

With HTTP Proxy if I enable TLS I see the same thing it looks like proxy settings are simply bypassed when using TLS. If I disable TLS I get

java.lang.IllegalArgumentException: Invalid Proxy
at java.net.Socket.<init>(Socket.java:155)
at org.java_websocket.client.WebSocketClient.run(WebSocketClient.java:384)
at java.lang.Thread.run(Thread.java:920)

This seems like an issue with old version of Java. However I tried upgrading the Android JDK to 11 or even 17 but it's still the same issue.

SOCKS

With SOCKS and TLS enabled I still get the same error:

java.net.ConnectException: failed to connect to ws-eu.pusher.com/34.255.229.199 (port 443) from /:: (port 58482): connect failed: ECONNREFUSED (Connection refused)
at libcore.io.IoBridge.connect(IoBridge.java:188)
at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:142)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)

and If disable TLS I just never get any error but the connection never establishes either.

Here's how the proxy is setup:

class CorePusher {
  private lateinit var pusher: Pusher

  init {
    val options = PusherOptions()
    options.setCluster("eu")
    options.isUseTLS = false

    thread {
      options.proxy = Proxy(Proxy.Type.SOCKS, InetSocketAddress("192.168.1.16",9090))
//
      android.os.Handler(Looper.getMainLooper()).post {
        pusher = Pusher(API_KEY, options)
        pusher.connect(object : ConnectionEventListener {
          override fun onConnectionStateChange(change: ConnectionStateChange?) {
            Log.i(tagName, "pusher connection state changed from ${change?.previousState?.name} to ${change?.currentState?.name}")
          }

          override fun onError(message: String?, code: String?, e: Exception?) {
            Log.e(tagName, "failed to connect to pusher: $message")
            e?.let {
              Log.e(tagName, it.stackTraceToString())
            }
          }
        })
      }
    }
  }
}

What baffles me though is that (if and only if) I disable TLS. I do see the websocket traffic on Proxyman but the connection doesn't establishes on the client SDK. (stays on CONNECTING state)

CleanShot 2023-02-09 at 00 53 35@2x

Any help would be greatly appreciated.

CC @pusher/mobile

benw-pusher commented 1 year ago

Are you able to confirm you have completed the steps at https://docs.proxyman.io/debug-devices/android-device?

apouche commented 1 year ago

Yes I can confirm I did, the proxy is working and I do see the traffic between app & server. Just not over websockets.

benw-pusher commented 1 year ago

I have not been able to configure apps to work with Proxyman - however I have verified that the proxy settings are respected and work by using a different proxy application. When using Charles Proxy the app works as expected, connecting to the Channels service without error and the traffic shows in the Charles UI proxy logs as expected. This indicates the issue is with Proxyman and not with our library.