LPgenerator / mattermost_bot

MatterBot - A chat bot for MatterMost (http://www.mattermost.org).
MIT License
211 stars 60 forks source link

WebSocketBadStatusException: Handshake status 401 #13

Closed liorbp closed 8 years ago

liorbp commented 8 years ago

Seems like mattermost_bot is unable to maintain the websocket session. After successfully connecting the bot to my team channel an exception is being raised:

[04/20/2016 15:31:10] Starting new HTTP connection (1): localhost
[04/20/2016 15:31:10] Starting new HTTP connection (1): localhost
[04/20/2016 15:31:10] connected to mattermost
[04/20/2016 15:31:10] loading plugin "plugins"
[04/20/2016 15:31:10] registered respond_to plugin "hello_reply" to "hello$"
[04/20/2016 15:31:10] registered listen_to plugin "hello_reply_formatting" to "hello_formatting$"
[04/20/2016 15:31:10] registered respond_to plugin "hello_reply_formatting" to "hello_formatting"
[04/20/2016 15:31:10] registered listen_to plugin "hello_send" to "hello$"
[04/20/2016 15:31:10] registered respond_to plugin "hello_decorators" to "hello_decorators"
[04/20/2016 15:31:10] registered listen_to plugin "hello_decorators" to "hello_decorators"
[04/20/2016 15:31:10] registered respond_to plugin "web_api_reply" to "hello_web_api"
[04/20/2016 15:31:10] registered listen_to plugin "hello_comment" to "hello_comment"
[04/20/2016 15:31:10] registered listen_to plugin "hello_react" to "hello_react"
[04/20/2016 15:31:10] registered respond_to plugin "busy_reply" to "busy"
[04/20/2016 15:31:10] registered respond_to plugin "ping_reply" to "ping"
[04/20/2016 15:31:10] keep active thread started
Traceback (most recent call last):
  File "cli.py", line 23, in <module>
    main()
  File "cli.py", line 18, in main
    b.run()
  File "/home/beepi/Desktop/mattermost_bot-master/mattermost_bot/bot.py", line 36, in run
    self._dispatcher.loop()
  File "/home/beepi/Desktop/mattermost_bot-master/mattermost_bot/dispatcher.py", line 103, in loop
    for self.event in self._client.messages(True, 'posted'):
  File "/home/beepi/Desktop/mattermost_bot-master/mattermost_bot/mattermost.py", line 135, in messages
    if not self.connect_websocket():
  File "/home/beepi/Desktop/mattermost_bot-master/mattermost_bot/mattermost.py", line 130, in connect_websocket
    "Cookie: MMTOKEN=%s" % self.api.token,
  File "/usr/local/lib/python2.7/dist-packages/websocket/_core.py", line 104, in create_connection
    websock.connect(url, **options)
  File "/usr/local/lib/python2.7/dist-packages/websocket/_core.py", line 269, in connect
    self.handshake_response = handshake(self.sock, *addrs, **options)
  File "/usr/local/lib/python2.7/dist-packages/websocket/_handshake.py", line 67, in handshake
    status, resp = _get_resp_headers(sock)
  File "/usr/local/lib/python2.7/dist-packages/websocket/_handshake.py", line 123, in _get_resp_headers
    raise WebSocketBadStatusException("Handshake status %d", status)
websocket._exceptions.WebSocketBadStatusException: Handshake status 401

Using the internal IP instead of localhost didn't had any affect. Intercepting the network with Wireshark brings up this error from the server:

GET /api/v1/websocket?session_token_index=0&1 HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:8065
Origin: http://localhost:8065
Sec-WebSocket-Key: Q1cIz/zUgKKxIbQEhJmyxw==
Sec-WebSocket-Version: 13
Cookie: MMTOKEN=jewonarnkfy3jkfq8qk3apnj5h

HTTP/1.1 401 Unauthorized
Content-Type: application/json
Expires: 0
X-Ratelimit-Limit: 10
X-Ratelimit-Remaining: 9
X-Ratelimit-Reset: 1
X-Request-Id: sdb77przqjgep8u94ujm8tme5o
X-Version-Id: 2.1.0.1456234852
Date: Wed, 20 Apr 2016 12:25:19 GMT
Content-Length: 215

{"id":"api.context.session_expired.app_error","message":"Invalid or expired session, please login again.","detailed_error":"UserRequired","request_id":"sdb77przqjgep8u94ujm8tme5o","status_code":401,"is_oauth":false}

I'm using mattermost_bot latest release on Linux Mint. websocket-client-0.35.0 (I also tried to use 0.34.0) requests 2.9.1 six 1.5.2

bl-robinson commented 8 years ago

I had this problem... quick fix update the connect_websocket method with "MMAUTHTOKEN" rather than "MMTOKEN"

Could be grossly misunderstanding as I have not dug through any more of the project but might help anyone who comes to go through this ticket properly.

The above update appears to work though.

liorbp commented 8 years ago

Damn you're right! I just compared this to how a regular request looks like from my web-browser. I almost missed that :)

Thank you so much!

gotlium commented 8 years ago

Hi. Thanks for your report. I see this changes on MM master https://github.com/mattermost/platform/blob/master/model/session.go#L12 Bug was fixed and compatible with old versions.

bl-robinson commented 8 years ago

I cannot comment for @liorbp however I installed via pip into a virtual environment and it did pull mattermost-bot==1.0.13 a following pip install -U mattermost-bot

did pull the latest version though (without the problem) so does not seem to be reproducible.

Can only say at some point this morning I managed to pull 1.0.13 rather than 1.0.14 without making the specification for a previous version.