Skip to content

Commit

Permalink
fix some of the breaking changes that hitbox to smashcast did
Browse files Browse the repository at this point in the history
  • Loading branch information
Azenet committed May 13, 2017
1 parent 902176d commit 964dd92
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 45 deletions.
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

API_URL = "https://api.hitbox.tv"
API_URL = "https://api.smashcast.tv"
logLevel = logging.DEBUG
logFormat = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"

89 changes: 45 additions & 44 deletions hitbox_irc_socket.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# vim: sts=4:sw=4:et:tw=80:nosta
import asyncio, aiohttp, config, json, logging, random, websockets
from datetime import datetime
import re

class HitboxClient:

Expand Down Expand Up @@ -70,14 +71,9 @@ def get_token(self):
"""

r = yield from aiohttp.request("GET",
"http://{}/socket.io/1/".format(self._server))
d = yield from r.read()
d = d.decode("UTF-8").split(":")[0]
self._token = d
self._token = self._logintoken

r.close()
return d
return self._token

@asyncio.coroutine
def establish_connection(self):
Expand All @@ -88,9 +84,11 @@ def establish_connection(self):

yield from self.select_server()
yield from self.get_token()
self._log.debug(self._server)
self._log.debug(self._token)
self._socket = yield from websockets.connect(
"ws://{}/socket.io/1/websocket/{}".format(
self._server, self._token))
"wss://{}/socket.io/?EIO=3&transport=websocket".format(
self._server))

return self._socket

Expand Down Expand Up @@ -126,16 +124,21 @@ def recv(self):
break
self._log.debug("< {}".format(msg))

if msg == "1::" and not self._loggedIn:
if re.match("^\d+\{", msg) and not self._loggedIn:
self._log.debug("Logging into channel #{}..." \
.format(self._channel))
yield from self.joinChannel()
elif msg == "2::":
self._log.debug("PING? PONG!")
yield from self.pong()
else:
json = msg[4:]
yield from self.dispatchMessage(json)
# The only incoming message I've seen that is not an array that
# looks like ["message", "encoded json"] is the first received
# message that contains the SID, which is not used when replying
# to the server. So for now I'm dropping objects
if re.match("^\d+\[", msg):
json = "[{}".format(msg.split('[', 1)[-1])
yield from self.dispatchMessage(json)

@asyncio.coroutine
def send(self, msg):
Expand Down Expand Up @@ -164,10 +167,10 @@ def dispatchMessage(self, msg):
"""Adds a message to the message queue and signals that a message is
available, unblocking anything calling getNextMessage()"""
try:
msg["args"]["method"] # this will throw an error normally
msg["method"] # this will throw an error normally
except TypeError:
self._log.debug(msg)
args = json.loads(json.loads(msg)["args"][0])
args = json.loads(json.loads(msg)[1])
if args["method"] == "loginMsg":
asyncio.async(self.updateNickListEveryTen())
if args["method"] == "userList":
Expand Down Expand Up @@ -197,25 +200,23 @@ def signalNames(self):
@asyncio.coroutine
def joinChannel(self):
"""Joins the channel this socket is assigned to."""
prefix = "5:::"
prefix = "42"
if self._nick == None:
nick = "UnknownSoldier"
else:
nick = self._nick
j = json.dumps({
"name": "message",
"args": [
{
"method": "joinChannel",
"params": {
"channel": self._channel,
"name": nick,
"token": self._logintoken,
"isAdmin": False
}
}
]
})
j = json.dumps([
"message",
{
"method": "joinChannel",
"params": {
"channel": self._channel,
"name": nick,
"token": self._logintoken
},
"service": "chat"
}
])
yield from self.send(prefix + j)

@asyncio.coroutine
Expand Down Expand Up @@ -496,21 +497,21 @@ def sendMessage(self, text):
:text: Text to send. Limited to 300 characters
"""
prefix = "5:::"
j = json.dumps({
"name": "message",
"args": [
{
"method": "chatMsg",
"params": {
"channel": self._channel,
"name": self._nick,
"nameColor": self._namecolor,
"text": text
}
}
]
})
prefix = "42"
j = json.dumps([
"message",
{
"method": "chatMsg",
"params": {
"channel": self._channel,
"name": self._nick,
"nameColor": self._namecolor,
"text": text,
"id": "abcd"
},
"service": "chat"
}
])
yield from self.send(prefix + j)

@asyncio.coroutine
Expand Down

2 comments on commit 964dd92

@mtcjayne
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately it looks like there are still some issues. I am unable to connect to channels in an IRC client, although it looks like the proxy is attempting to/successful.

mtcjayne@mtcserver:~/Azenet_hitbox-irc-proxy$ python3 hitbox_irc_server.py
2017-07-28 01:40:32,280 - asyncio - DEBUG - Using selector: EpollSelector
2017-07-28 01:40:32,282 - main - INFO - Serving requests on ('0.0.0.0', 7778)
2017-07-28 01:40:32,521 - irc - INFO - Connection from ('10.0.0.10', 55126)
2017-07-28 01:40:32,723 - irc - INFO - << CAP LS
2017-07-28 01:40:32,723 - irc - DEBUG - Unknown command cap(['LS'])
2017-07-28 01:40:32,723 - irc - INFO - << PASS ***
2017-07-28 01:40:32,724 - irc - DEBUG - Calling on_pass (synchronously)
2017-07-28 01:40:32,724 - irc - DEBUG - Pass set to ***
2017-07-28 01:40:32,724 - irc - INFO - << NICK mtcjayne
2017-07-28 01:40:32,724 - irc - DEBUG - Calling on_nick (synchronously)
2017-07-28 01:40:32,725 - irc - DEBUG - Nick set to mtcjayne
2017-07-28 01:40:32,725 - irc - INFO - << USER mtcjayne 2 * :mtcjayne
2017-07-28 01:40:32,725 - irc - DEBUG - Calling on_user (synchronously)
2017-07-28 01:40:32,725 - irc - DEBUG - Logging in user mtcjayne
2017-07-28 01:40:32,725 - token - DEBUG - Making request to /auth/login
2017-07-28 01:40:33,141 - token - INFO - Login successful!
2017-07-28 01:40:33,141 - token - INFO - Your authentication token is: 83869c45b17495fd738c61f44c7ed976658986bf
2017-07-28 01:40:33,143 - irc - INFO - >> :hitbox_irc_proxy 001 mtcjayne :Welcome to the IRC Relay Network mtcjayne! mtcjayne@hitbox_irc_proxy
2017-07-28 01:40:33,144 - irc - INFO - >> :hitbox_irc_proxy 002 mtcjayne :Your host is hitbox_irc_proxy, running v2.0
2017-07-28 01:40:33,144 - irc - INFO - >> :hitbox_irc_proxy 003 mtcjayne :This server was created 5/6 3:40 PM
2017-07-28 01:40:33,144 - irc - INFO - >> :hitbox_irc_proxy 005 mtcjayne PREFIX=(qaohv)~&@%+ CHANMODES=fm :are supported by this server
2017-07-28 01:40:39,943 - irc - INFO - << JOIN #mtcjayne
2017-07-28 01:40:39,943 - irc - DEBUG - Calling on_join
2017-07-28 01:40:39,943 - irc - DEBUG - Joining mtcjayne
2017-07-28 01:40:39,943 - irc - DEBUG - Socket handler for mtcjayne established.
2017-07-28 01:40:40,291 - asyncio - ERROR - Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f3a899b3a58>
2017-07-28 01:40:40,292 - ws - DEBUG - ec2-54-90-129-166.ws.smashcast.tv
2017-07-28 01:40:40,292 - ws - DEBUG - 83869c45b17495fd738c61f44c7ed976658986bf
2017-07-28 01:40:40,611 - ws - DEBUG - < 0{"sid":"SknlZnpC2eOEaA9AClRJ","upgrades":[],"pingInterval":25000,"pingTimeout":60000}
2017-07-28 01:40:40,612 - ws - DEBUG - Logging into channel #mtcjayne...
2017-07-28 01:40:40,612 - ws - DEBUG - > 42["message", {"service": "chat", "method": "joinChannel", "params": {"channel": "mtcjayne", "token": "83869c45b17495fd738c61f44c7ed976658986bf", "name": "mtcjayne"}}]
2017-07-28 01:40:40,612 - ws - DEBUG - < 40
2017-07-28 01:40:40,748 - ws - DEBUG - < 42["message","{\"method\":\"loginMsg\",\"params\":{\"channel\":\"mtcjayne\",\"name\":\"mtcjayne\",\"role\":\"admin\"},\"service\":\"chat\"}"]
2017-07-28 01:40:40,748 - ws - DEBUG - ["message","{\"method\":\"loginMsg\",\"params\":{\"channel\":\"mtcjayne\",\"name\":\"mtcjayne\",\"role\":\"admin\"},\"service\":\"chat\"}"]
2017-07-28 01:40:40,749 - ws - DEBUG - Message dispatched.  Sem: <asyncio.locks.Semaphore object at 0x7f3a899a4ba8 [unlocked,value:1,waiters:1]>
2017-07-28 01:40:40,749 - ws - DEBUG - Nicklist updater called
2017-07-28 01:40:40,749 - ws - DEBUG - Updating Nick List for mtcjayne
2017-07-28 01:40:40,749 - ws - DEBUG - > 5:::{"name": "message", "args": [{"method": "getChannelUserList", "params": {"channel": "mtcjayne"}}]}
2017-07-28 01:40:40,749 - irc - DEBUG - incoming message from mtcjayne: ["message","{\"method\":\"loginMsg\",\"params\":{\"channel\":\"mtcjayne\",\"name\":\"mtcjayne\",\"role\":\"admin\"},\"service\":\"chat\"}"]
2017-07-28 01:40:40,750 - asyncio - ERROR - Task exception was never retrieved
future: <Task finished coro=<on_join() done, defined at hitbox_irc_server.py:116> exception=TypeError('list indices must be integers, not str',)>
Traceback (most recent call last):
  File "/usr/lib/python3.4/asyncio/tasks.py", line 235, in _step
    result = coro.send(value)
  File "hitbox_irc_server.py", line 135, in on_join
    yield from self.handle_socket(channel)
  File "hitbox_irc_server.py", line 209, in handle_socket
    cmd = json.loads(j["args"][0])["method"]
TypeError: list indices must be integers, not str
2017-07-28 01:40:40,752 - ws - DEBUG - < 42["message","{\"method\":\"chatLog\",\"params\":{\"text\":\"<user>mtcjayne</user> changed channel information\",\"timestamp\":1501119599,\"channel\":\"mtcjayne\",\"buffer\":true},\"service\":\"chat\"}"]
2017-07-28 01:40:40,753 - ws - DEBUG - ["message","{\"method\":\"chatLog\",\"params\":{\"text\":\"<user>mtcjayne</user> changed channel information\",\"timestamp\":1501119599,\"channel\":\"mtcjayne\",\"buffer\":true},\"service\":\"chat\"}"]
2017-07-28 01:40:40,753 - ws - DEBUG - Message dispatched.  Sem: <asyncio.locks.Semaphore object at 0x7f3a899a4ba8 [unlocked,value:1]>
2017-07-28 01:40:40,753 - ws - DEBUG - < 42["message","{\"method\":\"chatLog\",\"params\":{\"text\":\"<user>mtcjayne</user> changed channel information\",\"timestamp\":1501128760,\"channel\":\"mtcjayne\",\"buffer\":true},\"service\":\"chat\"}"]
2017-07-28 01:40:40,753 - ws - DEBUG - ["message","{\"method\":\"chatLog\",\"params\":{\"text\":\"<user>mtcjayne</user> changed channel information\",\"timestamp\":1501128760,\"channel\":\"mtcjayne\",\"buffer\":true},\"service\":\"chat\"}"]
2017-07-28 01:40:40,754 - ws - DEBUG - Message dispatched.  Sem: <asyncio.locks.Semaphore object at 0x7f3a899a4ba8 [unlocked,value:2]>
2017-07-28 01:40:40,754 - ws - DEBUG - < 42["message","{\"method\":\"chatLog\",\"params\":{\"text\":\"<user>mtcjayne</user> changed game played\",\"timestamp\":1501128767,\"channel\":\"mtcjayne\",\"buffer\":true},\"service\":\"chat\"}"]
2017-07-28 01:40:40,754 - ws - DEBUG - ["message","{\"method\":\"chatLog\",\"params\":{\"text\":\"<user>mtcjayne</user> changed game played\",\"timestamp\":1501128767,\"channel\":\"mtcjayne\",\"buffer\":true},\"service\":\"chat\"}"]
2017-07-28 01:40:40,754 - ws - DEBUG - Message dispatched.  Sem: <asyncio.locks.Semaphore object at 0x7f3a899a4ba8 [unlocked,value:3]>
2017-07-28 01:40:40,754 - ws - DEBUG - < 42["message","{\"method\":\"chatLog\",\"params\":{\"text\":\"<user>mtcjayne</user> changed channel information\",\"timestamp\":1501128767,\"channel\":\"mtcjayne\",\"buffer\":true},\"service\":\"chat\"}"]
2017-07-28 01:40:40,754 - ws - DEBUG - ["message","{\"method\":\"chatLog\",\"params\":{\"text\":\"<user>mtcjayne</user> changed channel information\",\"timestamp\":1501128767,\"channel\":\"mtcjayne\",\"buffer\":true},\"service\":\"chat\"}"]
2017-07-28 01:40:40,754 - ws - DEBUG - Message dispatched.  Sem: <asyncio.locks.Semaphore object at 0x7f3a899a4ba8 [unlocked,value:4]>
2017-07-28 01:40:40,755 - ws - DEBUG - < 42["message","{\"method\":\"chatLog\",\"params\":{\"text\":\"<user>mtcjayne</user> changed channel information\",\"timestamp\":1501153110,\"channel\":\"mtcjayne\",\"buffer\":true,\"buffersent\":true},\"service\":\"chat\"}"]
2017-07-28 01:40:40,755 - ws - DEBUG - ["message","{\"method\":\"chatLog\",\"params\":{\"text\":\"<user>mtcjayne</user> changed channel information\",\"timestamp\":1501153110,\"channel\":\"mtcjayne\",\"buffer\":true,\"buffersent\":true},\"service\":\"chat\"}"]
2017-07-28 01:40:40,755 - ws - DEBUG - Message dispatched.  Sem: <asyncio.locks.Semaphore object at 0x7f3a899a4ba8 [unlocked,value:5]>
2017-07-28 01:40:50,762 - ws - DEBUG - Updating Nick List for mtcjayne
2017-07-28 01:40:50,763 - ws - DEBUG - > 5:::{"name": "message", "args": [{"method": "getChannelUserList", "params": {"channel": "mtcjayne"}}]}
2017-07-28 01:41:00,764 - ws - DEBUG - Updating Nick List for mtcjayne
2017-07-28 01:41:00,764 - ws - DEBUG - > 5:::{"name": "message", "args": [{"method": "getChannelUserList", "params": {"channel": "mtcjayne"}}]}
2017-07-28 01:41:10,765 - ws - DEBUG - Updating Nick List for mtcjayne
2017-07-28 01:41:10,766 - ws - DEBUG - > 5:::{"name": "message", "args": [{"method": "getChannelUserList", "params": {"channel": "mtcjayne"}}]}
2017-07-28 01:41:16,184 - irc - INFO - << QUIT :Going offline, see ya! (www.adiirc.com)
2017-07-28 01:41:16,184 - irc - DEBUG - Calling on_quit
2017-07-28 01:41:16,185 - ws - DEBUG - > 5:::{"name": "message", "args": [{"method": "partChannel", "params": {"name": "mtcjayne"}}]}
2017-07-28 01:41:16,242 - ws - DEBUG - Connection closed.  No longer receiving incoming messages.
2017-07-28 01:41:16,242 - irc - DEBUG - Connection to #mtcjayne closed.
2017-07-28 01:41:20,771 - ws - DEBUG - Updating Nick List for mtcjayne
2017-07-28 01:41:20,771 - asyncio - ERROR - Task exception was never retrieved
future: <Task finished coro=<updateNickListEveryTen() done, defined at /home/mtcjayne/Azenet_hitbox-irc-proxy/hitbox_irc_socket.py:149> exception=ConnectionClosed('WebSocket connection is closed: code = 1000, no reason.',)>
Traceback (most recent call last):
  File "/usr/lib/python3.4/asyncio/tasks.py", line 237, in _step
    result = next(coro)
  File "/home/mtcjayne/Azenet_hitbox-irc-proxy/hitbox_irc_socket.py", line 155, in updateNickListEveryTen
    yield from self.userList()
  File "/home/mtcjayne/Azenet_hitbox-irc-proxy/hitbox_irc_socket.py", line 264, in userList
    yield from self.send(prefix + j)
  File "/home/mtcjayne/Azenet_hitbox-irc-proxy/hitbox_irc_socket.py", line 146, in send
    yield from self._socket.send(msg)
  File "/usr/local/lib/python3.4/dist-packages/websockets/protocol.py", line 310, in send
    yield from self.ensure_open()
  File "/usr/local/lib/python3.4/dist-packages/websockets/protocol.py", line 394, in ensure_open
    raise ConnectionClosed(self.close_code, self.close_reason)
websockets.exceptions.ConnectionClosed: WebSocket connection is closed: code = 1000, no reason.

@Azenet
Copy link
Owner Author

@Azenet Azenet commented on 964dd92 Jul 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mtcjayne I don't expect it to work, as I stated on the issue I only did half the work, as you can only send messages (and maybe that's broken again, I didn't use the code since I committed this).

Please sign in to comment.