mirror of
https://github.com/nexy7574/LCC-bot.git
synced 2024-09-19 18:16:34 +01:00
Migrate server.py to logging
This commit is contained in:
parent
e7601fd1d3
commit
1bf3a324cb
1 changed files with 16 additions and 16 deletions
|
@ -1,9 +1,8 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import ipaddress
|
import ipaddress
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
import textwrap
|
import textwrap
|
||||||
from rich import print
|
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from hashlib import sha512
|
from hashlib import sha512
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
|
@ -37,7 +36,9 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
WEB_ROOT_PATH = ""
|
WEB_ROOT_PATH = ""
|
||||||
|
|
||||||
GENERAL = "https://ptb.discord.com/channels/994710566612500550/1018915342317277215/"
|
log = logging.getLogger("jimmy.api")
|
||||||
|
|
||||||
|
GENERAL = "https://discord.com/channels/994710566612500550/"
|
||||||
|
|
||||||
OAUTH_ENABLED = OAUTH_ID and OAUTH_SECRET and OAUTH_REDIRECT_URI
|
OAUTH_ENABLED = OAUTH_ID and OAUTH_SECRET and OAUTH_REDIRECT_URI
|
||||||
|
|
||||||
|
@ -87,7 +88,7 @@ async def authenticate(req: Request, code: str = None, state: str = None):
|
||||||
if not (code and state) or state not in app.state.states:
|
if not (code and state) or state not in app.state.states:
|
||||||
value = os.urandom(4).hex()
|
value = os.urandom(4).hex()
|
||||||
if value in app.state.states:
|
if value in app.state.states:
|
||||||
print("Generated a state that already exists. Cleaning up", file=sys.stderr)
|
log.warning("Generated a state that already exists. Cleaning up")
|
||||||
# remove any states older than 5 minutes
|
# remove any states older than 5 minutes
|
||||||
removed = 0
|
removed = 0
|
||||||
for _value in list(app.state.states):
|
for _value in list(app.state.states):
|
||||||
|
@ -95,24 +96,23 @@ async def authenticate(req: Request, code: str = None, state: str = None):
|
||||||
del app.state.states[_value]
|
del app.state.states[_value]
|
||||||
removed += 1
|
removed += 1
|
||||||
value = os.urandom(4).hex()
|
value = os.urandom(4).hex()
|
||||||
print(f"Removed {removed} states.", file=sys.stderr)
|
log.warning(f"Removed {removed} old states.")
|
||||||
|
|
||||||
if value in app.state.states:
|
if value in app.state.states:
|
||||||
print("Critical: Generated a state that already exists and could not free any slots.", file=sys.stderr)
|
log.critical("Generated a state that already exists and could not free any slots.")
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
HTTPStatus.SERVICE_UNAVAILABLE,
|
HTTPStatus.SERVICE_UNAVAILABLE,
|
||||||
"Could not generate a state token (state container full, potential (D)DOS attack?). "
|
"Could not generate a state token (state container full, potential (D)DOS attack?). "
|
||||||
"Please try again later.",
|
"Please try again later.",
|
||||||
# Saying a suspected DDOS makes sense, there are 4,294,967,296 possible states, the likelyhood of a
|
# Saying a suspected DDOS makes sense, there are 4,294,967,296 possible states, the likelyhood of a
|
||||||
# collision is 1 in 4,294,967,296.
|
# collision is 1 in 4,294,967,296.
|
||||||
headers={"Retry-After": "300"},
|
headers={"Retry-After": "60"},
|
||||||
)
|
)
|
||||||
app.state.states[value] = datetime.now()
|
app.state.states[value] = datetime.now()
|
||||||
return RedirectResponse(
|
return RedirectResponse(
|
||||||
discord.utils.oauth_url(
|
discord.utils.oauth_url(
|
||||||
OAUTH_ID, redirect_uri=OAUTH_REDIRECT_URI, scopes=("identify", "connections", "guilds", "email")
|
OAUTH_ID, redirect_uri=OAUTH_REDIRECT_URI, scopes=("identify", "connections", "guilds", "email")
|
||||||
)
|
) + f"&state={value}&prompt=none",
|
||||||
+ f"&state={value}&prompt=none",
|
|
||||||
status_code=HTTPStatus.TEMPORARY_REDIRECT,
|
status_code=HTTPStatus.TEMPORARY_REDIRECT,
|
||||||
headers={"Cache-Control": "no-store, no-cache"},
|
headers={"Cache-Control": "no-store, no-cache"},
|
||||||
)
|
)
|
||||||
|
@ -239,7 +239,7 @@ async def verify(code: str):
|
||||||
# And delete the code
|
# And delete the code
|
||||||
await verify_code.delete()
|
await verify_code.delete()
|
||||||
|
|
||||||
console.log(f"[green]{verify_code.bind} verified ({verify_code.bind}/{verify_code.student_id})")
|
log.info(f"[green]{verify_code.bind} verified ({verify_code.bind}/{verify_code.student_id})")
|
||||||
|
|
||||||
return RedirectResponse(GENERAL, status_code=308)
|
return RedirectResponse(GENERAL, status_code=308)
|
||||||
|
|
||||||
|
@ -293,12 +293,12 @@ async def bridge(req: Request):
|
||||||
@app.websocket("/bridge/recv")
|
@app.websocket("/bridge/recv")
|
||||||
async def bridge_recv(ws: WebSocket, secret: str = Header(None)):
|
async def bridge_recv(ws: WebSocket, secret: str = Header(None)):
|
||||||
await ws.accept()
|
await ws.accept()
|
||||||
print("Websocket %r accepted." % ws)
|
log.info("Websocket %s:%s accepted.", ws.client.host, ws.client.port)
|
||||||
if secret != app.state.bot.http.token:
|
if secret != app.state.bot.http.token:
|
||||||
print("Closing websocket %r, invalid secret." % ws)
|
log.warning("Closing websocket %r, invalid secret.", ws.client.host)
|
||||||
raise _WSException(code=1008, reason="Invalid Secret")
|
raise _WSException(code=1008, reason="Invalid Secret")
|
||||||
if app.state.ws_connected.locked():
|
if app.state.ws_connected.locked():
|
||||||
print("Closing websocket %r, already connected." % ws)
|
log.warning("Closing websocket %r, already connected." % ws)
|
||||||
raise _WSException(code=1008, reason="Already connected.")
|
raise _WSException(code=1008, reason="Already connected.")
|
||||||
queue: asyncio.Queue = app.state.bot.bridge_queue
|
queue: asyncio.Queue = app.state.bot.bridge_queue
|
||||||
|
|
||||||
|
@ -307,7 +307,7 @@ async def bridge_recv(ws: WebSocket, secret: str = Header(None)):
|
||||||
try:
|
try:
|
||||||
await ws.send_json({"status": "ping"})
|
await ws.send_json({"status": "ping"})
|
||||||
except (WebSocketDisconnect, WebSocketException):
|
except (WebSocketDisconnect, WebSocketException):
|
||||||
print("Websocket %r disconnected." % ws)
|
log.info("Websocket %r disconnected.", ws)
|
||||||
break
|
break
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -316,10 +316,10 @@ async def bridge_recv(ws: WebSocket, secret: str = Header(None)):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print("Sent data %r to websocket %r." % (data, ws))
|
|
||||||
await ws.send_json(data)
|
await ws.send_json(data)
|
||||||
|
log.debug("Sent data %r to websocket %r.", data, ws)
|
||||||
except (WebSocketDisconnect, WebSocketException):
|
except (WebSocketDisconnect, WebSocketException):
|
||||||
print("Websocket %r disconnected." % ws)
|
log.info("Websocket %r disconnected." % ws)
|
||||||
break
|
break
|
||||||
finally:
|
finally:
|
||||||
queue.task_done()
|
queue.task_done()
|
||||||
|
|
Loading…
Reference in a new issue