0

I'm proxying my test websocket using nginx from 9000 to 80 port and all my tests are ok until the client is behind a web proxy, then the handshake process fails, where can be the problem? Thanks in advance for your time.

I'm getting these errors:
Client:

WebSocket connection to 'ws://myserver/ws/' failed: Error during WebSocket handshake: Unexpected response code: 502 autobahn.min.js:62

Server:

2014-01-10 19:51:22-0300 [PubSubServer1,19,127.0.0.1] Unhandled Error
    Traceback (most recent call last):
      File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 84, in callWithLogger
        return callWithContext({"system": lp}, func, *args, **kw)
      File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 69, in callWithContext
        return context.call({ILogContext: newCtx}, func, *args, **kw)
      File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 118, in callWithContext
        return self.currentContext().callWithContext(ctx, func, *args, **kw)
      File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 81, in callWithContext
        return func(*args,**kw)
    --- <exception caught here> ---
      File "/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.py", line 586, in _doReadOrWrite
        why = selectable.doRead()
      File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 199, in doRead
        rval = self.protocol.dataReceived(data)
      File "/usr/local/lib/python2.7/dist-packages/autobahn-0.7.3-py2.7.egg/autobahn/twisted/websocket.py", line 77, in dataReceived
        self._dataReceived(data)
      File "/usr/local/lib/python2.7/dist-packages/autobahn-0.7.3-py2.7.egg/autobahn/websocket/protocol.py", line 1270, in _dataReceived
        self.consumeData()
      File "/usr/local/lib/python2.7/dist-packages/autobahn-0.7.3-py2.7.egg/autobahn/websocket/protocol.py", line 1303, in consumeData
        self.processHandshake()
      File "/usr/local/lib/python2.7/dist-packages/autobahn-0.7.3-py2.7.egg/autobahn/websocket/protocol.py", line 2819, in processHandshake
        self.sendServerStatus()
      File "/usr/local/lib/python2.7/dist-packages/autobahn-0.7.3-py2.7.egg/autobahn/websocket/protocol.py", line 3276, in sendServerStatus
        self.sendHtml(html)
      File "/usr/local/lib/python2.7/dist-packages/autobahn-0.7.3-py2.7.egg/autobahn/websocket/protocol.py", line 3219, in sendHtml
        response += "Content-Length: %d\x0d\x0a" % len(raw)
    exceptions.NameError: global name 'raw' is not defined

My Test server:

import sys
from twisted.python import log
from twisted.internet import reactor
from autobahn.twisted.websocket import listenWS
from autobahn.wamp import WampServerFactory, \
                          WampServerProtocol

class PubSubServer1(WampServerProtocol):

   def onSessionOpen(self):

      self.registerForPubSub("http://test.com/test")

if __name__ == '__main__':

   log.startLogging(sys.stdout)

   factory = WampServerFactory("ws://localhost:9000", debugWamp = 'debug',externalPort=80)
   factory.protocol = PubSubServer1
   factory.setProtocolOptions(allowHixie76 = True)
   listenWS(factory)

   #reactor.listenTCP(9000, factory)
   reactor.run()

Command:

~$ python /home/my/wsbroker.py 
/usr/local/lib/python2.7/dist-packages/zope.interface-4.0.5-py2.7-linux-x86_64.egg/zope/__init__.py:3: UserWarning: Module twisted was already imported from /usr/lib/python2.7/dist-packages/twisted/__init__.pyc, but /usr/local/lib/python2.7/dist-packages/autobahn-0.7.3-py2.7.egg is being added to sys.path

Edited:
Debug info for a failed handshake (Client using a web proxy):http://pastebin.com/aN4ppA2e
Debug info for a done handshake (Client without proxy):http://pastebin.com/5rXREY2q

chespinoza
  • 2,638
  • 1
  • 23
  • 46

2 Answers2

1

The traceback you see above is due to a bug introduced with Autobahn 0.7.0 and fixed in 0.7.4.

However, the bug is likely not the cause of your actual problem: the traceback shows that Autobahn was trying to render a plain HTML server status page - and it does so, when the HTTP request it receives does not contain Upgrade to Websocket headers. You proxy might not (yet) be configured to properly forward WebSocket.

Another thing you might run into: WebSocketServerFactory of Autobahn provides an option to set the externalPort. This should be set to the TCP/IP port of your proxy under which it accepts WebSocket connections. This is needed since Autobahn will (in compliance with the WebSocket specification) check if the WebSocket opening HTTP request host and port matches the one it runs on (and if it's different, bail out). The mentioned option allows you to override that behavior.

oberstet
  • 21,353
  • 10
  • 64
  • 97
  • Thanks for your answer Tobias, I upgraded to 0.7.4 and added the externalPort=80 option, the traceback is gone, but now with this option I can't connect even without a proxy. – chespinoza Jan 13 '14 at 17:49
  • Sure, you should only use the option if you connect via a proxy that runs on a different port than Autobahn. – oberstet Jan 13 '14 at 18:13
  • Well, I'm trying again with the option externalPort, with a client without proxy now the connection is done, but with a client with proxy the connection fails, I have the debug info for the two connections, where can I share it with you? – chespinoza Jan 13 '14 at 18:25
  • Added paste/bin links for debug info, thanks in advance Tobias. – chespinoza Jan 13 '14 at 19:10
  • It seems, the proxy does not forward the `Upgrade: websocket` header. This header is required per WebSocket specification (RFC6455). Autobahn fails the connection consequently. – oberstet Jan 13 '14 at 21:25
  • Yeah, maybe I need try using wss...seems Squid is the problem at the client, don't work with websockets at 80 port: http://websocketstest.com/result/285954 – chespinoza Jan 13 '14 at 21:50
  • You might try Nginx, which supports reverse-proxy mode for WebSocket: http://nginx.org/en/docs/http/websocket.html. As this (no longer) seems to be related to Autobahn, would you mind accepting the answer so this Q gets "closed"? – oberstet Jan 14 '14 at 08:58
  • I'm using Nginx to forward the autobahn-twisted websocket service, the problem is with the client proxy, the only way to put it to work seems to using wss, thanks for your support Tobias and thanks for autobahn. – chespinoza Jan 14 '14 at 11:33
1

Try using secure websockets (wss), as non-secure websockets are generally not supported by proxies.

Kurt Pattyn
  • 2,758
  • 2
  • 30
  • 42