通过susher lib

时间:2017-03-17 13:49:56

标签: java websocket proxy squid pusher

我必须连接到pusher上托管的websocket,它们提供lib

我设法通过cntlm代理使其在我的开发环境中工作,我有这样的日志来自我的connectionEventListener

New state for pusher connection: CONNECTING -> CONNECTED

private final ConnectionEventListener connectionEventListener = new ConnectionEventListener() {
    @Override
    public void onConnectionStateChange(ConnectionStateChange connectionStateChange) {
        log.info("New state for pusher connection: {} -> {}",
                connectionStateChange.getPreviousState(),
                connectionStateChange.getCurrentState());
    }

    @Override
    public void onError(String s, String s1, Exception e) {
        log.error("Some error {} - {} - {}", s, s1, e);
    }
};

问题在于我们的预生产环境,它位于squid代理(v.3.5.12)后面,我在我的日志中得到了这个:

New state for pusher connection: DISCONNECTED -> CONNECTING
Exception in thread "Thread-8" java.lang.InternalError: Should not reach here
    at java.net.HttpConnectSocketImpl.doTunneling(HttpConnectSocketImpl.java:181)
    at java.net.HttpConnectSocketImpl.doTunnel(HttpConnectSocketImpl.java:168)
    at java.net.HttpConnectSocketImpl.access$200(HttpConnectSocketImpl.java:44)
    at java.net.HttpConnectSocketImpl$2.run(HttpConnectSocketImpl.java:151)
    at java.net.HttpConnectSocketImpl$2.run(HttpConnectSocketImpl.java:149)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.HttpConnectSocketImpl.privilegedDoTunnel(HttpConnectSocketImpl.java:148)
    at java.net.HttpConnectSocketImpl.connect(HttpConnectSocketImpl.java:111)
    at java.net.Socket.connect(Socket.java:589)
    at com.pusher.java_websocket.client.WebSocketClient.run(WebSocketClient.java:165)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at java.net.HttpConnectSocketImpl.doTunneling(HttpConnectSocketImpl.java:179)
    ... 10 more
Caused by: java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 403 Forbidden"
    at sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:2084)
    ... 15 more

我的问题是,有人设法在squid代理后面的java中使用pusher消费websockets。

更新: 问题解决了,我们的squid代理缺少一些允许连接的规则。

1 个答案:

答案 0 :(得分:0)

由于我最近遇到了同样的问题,因此我看不出您如何解决该问题。

URL在内部squid代理中被列入白名单,但是当前Pusher Java客户端库实现的问题是(Pusher的java-websocket客户端的1.4.1版本),构造函数使用主机地址(ws.pusherapp.com),导致将URL解析为IP地址。

然后,库尝试使用解析的IP地址打开套接字(使用代理),而不是使用URL地址打开套接字连接。这对于代理来说是有问题的,因为Pusher使用AWS,并且URL被动态解析为随机的AWS IP地址。

我可以通过如下修补WebSocketClient.java包中的com.pusher.java_websocket.client来更改此行为

public void run()方法中,原始行为:

this.socket.connect(new InetSocketAddress(this.uri.getHost(), this.getPort()), this.connectTimeout);

我替换为 this.socket.connect(InetSocketAddress.createUnresolved(this.uri.getHost(), this.uri.getPort()), this.connectTimeout);

InetScoketAddress的第二个构造函数不使用DNS来解析URL,并且库使用URL地址向Pusher服务器发出请求。