add basis of websockets

This commit is contained in:
Luna Mendes 2018-06-17 17:07:52 -03:00
parent 7e7a6a1aeb
commit 39d85d1a1a
7 changed files with 59 additions and 6 deletions

View File

@ -7,6 +7,7 @@ name = "pypi"
bcrypt = "==3.1.4" bcrypt = "==3.1.4"
itsdangerous = "==0.24" itsdangerous = "==0.24"
asyncpg = "==0.16.0" asyncpg = "==0.16.0"
websockets = "==5.0.1"
Quart = "==0.6.0" Quart = "==0.6.0"
[dev-packages] [dev-packages]

6
Pipfile.lock generated
View File

@ -1,7 +1,7 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "e8807281b43032a3fde4598f48c98436b8ef50fe332a413e71c37062a16b61d9" "sha256": "e37a82fc53dadfc4b8ebbded3bb043d686fa5c2cde07b11589430586e236386b"
}, },
"host-environment-markers": { "host-environment-markers": {
"implementation_name": "cpython", "implementation_name": "cpython",
@ -113,6 +113,10 @@
"hashes": [], "hashes": [],
"version": "==3.6.5" "version": "==3.6.5"
}, },
"websockets": {
"hashes": [],
"version": "==5.0.1"
},
"wsproto": { "wsproto": {
"hashes": [], "hashes": [],
"version": "==0.11.0" "version": "==0.11.0"

View File

@ -29,9 +29,6 @@ async def check_password(pwd_hash, given_password) -> bool:
pwd_hash = pwd_hash.encode('utf-8') pwd_hash = pwd_hash.encode('utf-8')
given_password = given_password.encode('utf-8') given_password = given_password.encode('utf-8')
print(repr(pwd_hash))
print(repr(given_password))
future = app.loop.run_in_executor( future = app.loop.run_in_executor(
None, bcrypt.checkpw, given_password, pwd_hash) None, bcrypt.checkpw, given_password, pwd_hash)
@ -73,8 +70,6 @@ async def register():
@bp.route('/login', methods=['POST']) @bp.route('/login', methods=['POST'])
async def login(): async def login():
"""Login one user into Litecord.""" """Login one user into Litecord."""
print(request.headers)
j = await request.get_json() j = await request.get_json()
email, password = j['email'], j['password'] email, password = j['email'], j['password']

View File

@ -8,3 +8,13 @@ class LitecordError(Exception):
class AuthError(LitecordError): class AuthError(LitecordError):
status_code = 403 status_code = 403
class WebsocketClose(Exception):
@property
def code(self):
return self.args[0]
@property
def reason(self):
return self.args[1]

View File

@ -0,0 +1 @@
from .gateway import websocket_handler

View File

@ -0,0 +1,32 @@
import urllib.parse
async def websocket_handler(ws, url):
qs = urllib.parse.parse_qs(
urllib.parse.urlparse(url).query
)
print(qs)
try:
gw_version = qs['v'][0]
gw_encoding = qs['encoding'][0]
except (KeyError, IndexError):
return await ws.close(1000, 'Invalid query args')
if gw_version not in ('6',):
return await ws.close(1000, 'Invalid gateway version')
if gw_encoding not in ('json', 'etf'):
return await ws.close(1000, 'Invalid gateway encoding')
try:
gw_compress = qs['compress'][0]
except (KeyError, IndexError):
gw_compress = None
if gw_compress and gw_compress not in ('zlib-stream',):
return await ws.close(1000, 'Invalid gateway compress')
await ws.close(code=1000, reason='ass')
return

10
run.py
View File

@ -1,12 +1,14 @@
import logging import logging
import asyncio import asyncio
import websockets
import asyncpg import asyncpg
from quart import Quart, g, jsonify from quart import Quart, g, jsonify
import config import config
from litecord.blueprints import gateway, auth from litecord.blueprints import gateway, auth
from litecord.gateway import websocket_handler
from litecord.errors import LitecordError from litecord.errors import LitecordError
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
@ -37,6 +39,14 @@ async def app_before_serving():
app.loop = asyncio.get_event_loop() app.loop = asyncio.get_event_loop()
g.loop = asyncio.get_event_loop() g.loop = asyncio.get_event_loop()
# start the websocket, etc
host, port = app.config['WS_HOST'], app.config['WS_PORT']
log.info(f'starting websocket at {host} {port}')
ws_future = websockets.serve(
websocket_handler, host, port)
await ws_future
@app.after_serving @app.after_serving
async def app_after_serving(): async def app_after_serving():