2017-02-18 12:45:48 +13:00
|
|
|
|
2017-03-06 19:13:06 +13:00
|
|
|
from __future__ import unicode_literals
|
2017-02-18 12:45:48 +13:00
|
|
|
from datetime import datetime
|
|
|
|
|
from tornado.escape import json_encode, json_decode
|
2017-03-07 18:30:51 +13:00
|
|
|
import tornado.ioloop, tornado.web, tornado.websocket, tornado.template
|
2017-10-13 10:02:06 +13:00
|
|
|
import random, string, logging, uuid, subprocess, pykka, ast, logging, json, urllib, urllib2, mem, requests
|
2017-02-18 12:45:48 +13:00
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
class WebsocketHandler(tornado.websocket.WebSocketHandler):
|
|
|
|
|
|
|
|
|
|
# initiate (not the actual object __init__, but run shortly after)
|
|
|
|
|
def initialize(self, core, config):
|
|
|
|
|
self.core = core
|
|
|
|
|
self.config = config
|
|
|
|
|
|
|
|
|
|
def check_origin(self, origin):
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def select_subprotocol(self, subprotocols):
|
|
|
|
|
|
|
|
|
|
# select one of our subprotocol elements and return it. This confirms the connection has been accepted.
|
|
|
|
|
protocols = mem.iris.digest_protocol( subprotocols )
|
|
|
|
|
|
|
|
|
|
# if we've auto-generated some ids, the provided subprotocols was a string, so just return it right back
|
|
|
|
|
# this allows a connection to be completed
|
|
|
|
|
if protocols['generated']:
|
|
|
|
|
return subprotocols[0]
|
|
|
|
|
|
|
|
|
|
# otherwise, just return one of the supplied subprotocols
|
|
|
|
|
else:
|
|
|
|
|
return protocols['clientid']
|
|
|
|
|
|
|
|
|
|
def open(self):
|
|
|
|
|
|
|
|
|
|
# decode our connection protocol value (which is a payload of id/name from javascript)
|
|
|
|
|
protocolElements = mem.iris.digest_protocol(self.request.headers.get('Sec-Websocket-Protocol', []))
|
|
|
|
|
|
|
|
|
|
connection_id = protocolElements['connection_id']
|
|
|
|
|
clientid = protocolElements['clientid']
|
|
|
|
|
self.connection_id = connection_id
|
|
|
|
|
username = protocolElements['username']
|
|
|
|
|
created = datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')
|
|
|
|
|
|
2017-03-28 21:36:14 +13:00
|
|
|
# get our IP
|
2017-04-07 19:40:33 +12:00
|
|
|
# if it's local, then check for proxy origin
|
2017-03-28 21:36:14 +13:00
|
|
|
ip = self.request.remote_ip
|
2017-04-07 19:40:33 +12:00
|
|
|
if (ip == '127.0.0.1' and hasattr(self.request.headers,'X-Forwarded-For')):
|
2017-03-28 21:36:14 +13:00
|
|
|
ip = self.request.headers['X-Forwarded-For']
|
|
|
|
|
|
2017-02-18 12:45:48 +13:00
|
|
|
# construct our client object, and add to our list of connections
|
|
|
|
|
client = {
|
|
|
|
|
'clientid': clientid,
|
|
|
|
|
'connection_id': connection_id,
|
|
|
|
|
'username': username,
|
2017-03-28 21:36:14 +13:00
|
|
|
'ip': ip,
|
2017-02-18 12:45:48 +13:00
|
|
|
'created': created
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# add to connections
|
2017-10-04 16:47:15 +13:00
|
|
|
mem.iris.add_connection(connection_id=connection_id, connection=self, client=client)
|
2017-02-18 12:45:48 +13:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def on_message(self, message):
|
2017-02-21 05:46:28 +13:00
|
|
|
|
2017-02-18 12:45:48 +13:00
|
|
|
message = json_decode(message)
|
|
|
|
|
|
|
|
|
|
if 'data' in message:
|
|
|
|
|
data = message['data']
|
|
|
|
|
else:
|
|
|
|
|
data = {}
|
|
|
|
|
|
2017-06-13 11:56:20 +12:00
|
|
|
# Handle hard-coded connection_id in messages
|
|
|
|
|
# Otherwise include the origin connection of this message
|
|
|
|
|
if 'connection_id' not in data:
|
|
|
|
|
data['connection_id'] = self.connection_id
|
2017-02-18 22:04:05 +13:00
|
|
|
|
2017-02-18 12:45:48 +13:00
|
|
|
if 'request_id' in message:
|
2017-10-13 20:48:35 +13:00
|
|
|
request_id = message['request_id']
|
2017-02-18 12:45:48 +13:00
|
|
|
else:
|
2017-10-13 20:48:35 +13:00
|
|
|
request_id = False
|
2017-02-18 12:45:48 +13:00
|
|
|
|
|
|
|
|
# call the method, as specified in payload
|
|
|
|
|
if 'method' in message:
|
|
|
|
|
|
|
|
|
|
# make sure the method exists
|
|
|
|
|
if hasattr(mem.iris, message['method']):
|
2017-10-20 21:49:41 +13:00
|
|
|
getattr(mem.iris, message['method'])(data=data, callback=lambda response, error: self.handle_response(response=response, error=error, request_id=request_id))
|
2017-02-18 12:45:48 +13:00
|
|
|
|
|
|
|
|
else:
|
2017-10-20 21:49:41 +13:00
|
|
|
self.handle_response(error={'message': 'Method "'+message['method']+'" does not exist'})
|
|
|
|
|
return
|
2017-02-18 12:45:48 +13:00
|
|
|
else:
|
2017-10-20 21:49:41 +13:00
|
|
|
self.handle_response(error={'message': 'Method key missing from request'})
|
|
|
|
|
return
|
2017-02-18 12:45:48 +13:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def on_close(self):
|
2017-10-04 16:47:15 +13:00
|
|
|
mem.iris.remove_connection(connection_id=self.connection_id)
|
2017-02-18 12:45:48 +13:00
|
|
|
|
2017-10-13 10:02:06 +13:00
|
|
|
##
|
|
|
|
|
# Handle a response from our core
|
|
|
|
|
# This is just our callback from an Async request
|
|
|
|
|
##
|
2017-10-13 20:48:35 +13:00
|
|
|
def handle_response(self, *args, **kwargs):
|
|
|
|
|
response = kwargs.get('response', None)
|
2017-10-20 21:49:41 +13:00
|
|
|
error = kwargs.get('error', None)
|
2017-10-13 20:48:35 +13:00
|
|
|
request_id = kwargs.get('request_id', False)
|
|
|
|
|
|
2017-10-20 21:49:41 +13:00
|
|
|
# We've been given an error
|
|
|
|
|
if error:
|
|
|
|
|
data = error
|
|
|
|
|
data['status'] = 0
|
|
|
|
|
data['request_id'] = request_id
|
|
|
|
|
|
|
|
|
|
# Log error with Sentry
|
|
|
|
|
mem.iris.raven_client.captureMessage(error.message)
|
|
|
|
|
|
2017-10-13 21:19:37 +13:00
|
|
|
# We've been handed an AsyncHTTPClient callback. This is the case
|
|
|
|
|
# when our request calls subsequent external requests (eg Spotify, Genius)
|
2017-10-20 21:49:41 +13:00
|
|
|
elif isinstance(response, tornado.httpclient.HTTPResponse):
|
|
|
|
|
data = {
|
|
|
|
|
'status': 1,
|
2017-10-13 21:19:37 +13:00
|
|
|
'response_code': response.code,
|
|
|
|
|
'response_reason': response.reason,
|
|
|
|
|
'response': response.body,
|
2017-10-13 10:02:06 +13:00
|
|
|
'request_id': request_id
|
|
|
|
|
}
|
2017-10-13 21:19:37 +13:00
|
|
|
|
|
|
|
|
# Just a regular json object, so not an external request
|
2017-10-13 10:02:06 +13:00
|
|
|
else:
|
2017-10-20 21:49:41 +13:00
|
|
|
data = response
|
|
|
|
|
data['status'] = 1
|
|
|
|
|
data['request_id'] = request_id
|
2017-10-13 10:02:06 +13:00
|
|
|
|
2017-10-20 21:49:41 +13:00
|
|
|
# Respond to the original request
|
|
|
|
|
mem.iris.send_message(connection_id=self.connection_id, data=data)
|
2017-10-13 10:02:06 +13:00
|
|
|
|
2017-02-18 12:45:48 +13:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HttpHandler(tornado.web.RequestHandler):
|
|
|
|
|
|
|
|
|
|
def set_default_headers(self):
|
|
|
|
|
self.set_header("Access-Control-Allow-Origin", "*")
|
2017-10-06 09:07:28 +13:00
|
|
|
self.set_header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, Client-Security-Token, Accept-Encoding")
|
2017-02-18 12:45:48 +13:00
|
|
|
|
|
|
|
|
def initialize(self, core, config):
|
|
|
|
|
self.core = core
|
|
|
|
|
self.config = config
|
2017-10-06 09:07:28 +13:00
|
|
|
|
|
|
|
|
# Options request
|
|
|
|
|
# This is a preflight request for CORS requests
|
|
|
|
|
def options(self, slug=None):
|
|
|
|
|
self.set_status(204)
|
|
|
|
|
self.finish()
|
2017-02-18 12:45:48 +13:00
|
|
|
|
2017-10-13 10:02:06 +13:00
|
|
|
@tornado.web.asynchronous
|
2017-02-18 12:45:48 +13:00
|
|
|
def get(self, slug=None):
|
|
|
|
|
|
2017-02-18 22:04:05 +13:00
|
|
|
# make sure the method exists
|
|
|
|
|
if hasattr(mem.iris, slug):
|
2017-10-20 21:49:41 +13:00
|
|
|
getattr(mem.iris, slug)(request=self.request, callback=lambda response, error: self.handle_response(response=response, error=error))
|
2017-02-18 12:45:48 +13:00
|
|
|
|
2017-10-04 16:47:15 +13:00
|
|
|
else:
|
2017-10-20 21:49:41 +13:00
|
|
|
self.handle_response(error={'message': "Method "+slug+" does not exist"})
|
|
|
|
|
return
|
2017-10-13 10:02:06 +13:00
|
|
|
|
|
|
|
|
@tornado.web.asynchronous
|
2017-10-04 16:47:15 +13:00
|
|
|
def post(self, slug=None):
|
|
|
|
|
|
|
|
|
|
# make sure the method exists
|
|
|
|
|
if hasattr(mem.iris, slug):
|
|
|
|
|
try:
|
|
|
|
|
data = json.loads(self.request.body.decode('utf-8'))
|
2017-10-20 21:49:41 +13:00
|
|
|
getattr(mem.iris, slug)(data=data, request=self.request, callback=lambda response, error: self.handle_response(response=response, error=error))
|
2017-10-04 16:47:15 +13:00
|
|
|
|
|
|
|
|
except urllib2.HTTPError as e:
|
2017-10-20 21:49:41 +13:00
|
|
|
self.handle_response(error={'message': "Invalid JSON payload"})
|
|
|
|
|
return
|
2017-10-04 16:47:15 +13:00
|
|
|
|
2017-02-18 12:45:48 +13:00
|
|
|
else:
|
2017-10-20 21:49:41 +13:00
|
|
|
self.handle_response(error={'message': "Method "+slug+" does not exist"})
|
|
|
|
|
return
|
2017-10-13 10:02:06 +13:00
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Handle a response from our core
|
|
|
|
|
# This is just our callback from an Async request
|
|
|
|
|
##
|
2017-10-20 21:49:41 +13:00
|
|
|
def handle_response(self, *args, **kwargs):
|
|
|
|
|
response = kwargs.get('response', None)
|
|
|
|
|
error = kwargs.get('error', None)
|
|
|
|
|
data = {}
|
|
|
|
|
|
|
|
|
|
print response
|
|
|
|
|
print error
|
|
|
|
|
print "------------"
|
|
|
|
|
|
|
|
|
|
if error:
|
|
|
|
|
data = error
|
|
|
|
|
data['status'] = 0
|
|
|
|
|
|
|
|
|
|
# Log error with Sentry
|
|
|
|
|
mem.iris.raven_client.captureMessage(data.message)
|
|
|
|
|
|
2017-10-13 21:19:37 +13:00
|
|
|
|
|
|
|
|
# We've been handed an AsyncHTTPClient callback. This is the case
|
|
|
|
|
# when our request calls subsequent external requests (eg Spotify, Genius).
|
|
|
|
|
# We don't need to wrap non-HTTPResponse responses as these are dicts
|
2017-10-20 21:49:41 +13:00
|
|
|
elif isinstance(response, tornado.httpclient.HTTPResponse):
|
2017-10-17 18:54:57 +13:00
|
|
|
|
2017-10-20 21:49:41 +13:00
|
|
|
# Digest JSON responses into JSON
|
2017-10-17 18:54:57 +13:00
|
|
|
content_type = response.headers.get('Content-Type')
|
|
|
|
|
if content_type.startswith('application/json') or content_type.startswith('text/json'):
|
|
|
|
|
body = json.loads(response.body)
|
2017-10-20 21:49:41 +13:00
|
|
|
|
|
|
|
|
# Non-JSON so just copy as-is
|
2017-10-17 18:54:57 +13:00
|
|
|
else:
|
|
|
|
|
body = response.body
|
|
|
|
|
|
2017-10-20 21:49:41 +13:00
|
|
|
data = {
|
|
|
|
|
'status': response.code,
|
|
|
|
|
'message': response.reason,
|
2017-10-17 18:54:57 +13:00
|
|
|
'response': body
|
2017-10-13 21:19:37 +13:00
|
|
|
}
|
|
|
|
|
|
2017-10-20 21:49:41 +13:00
|
|
|
# Regular ol successful response
|
|
|
|
|
else:
|
|
|
|
|
data = response
|
|
|
|
|
data['status'] = 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Write our response
|
|
|
|
|
self.write(data)
|
2017-10-13 10:02:06 +13:00
|
|
|
self.finish()
|
|
|
|
|
|
|
|
|
|
|
2017-02-18 12:45:48 +13:00
|
|
|
|