Browse Source

Add locks for g_connections

master
mid-kid 3 weeks ago
parent
commit
310e0e27d7
  1. 28
      auth/server.py

28
auth/server.py

@ -1,6 +1,7 @@
#!/usr/bin/env python3
from http import HTTPStatus
from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler
from threading import Lock
from urllib.parse import urlparse, parse_qs
from configparser import ConfigParser
from base64 import b64encode
@ -8,7 +9,8 @@ from os.path import getmtime
import json
userdata = {}
g_connections = {}
_g_connections = {}
_g_connections_lock = Lock()
class HTTPRequestHandler(BaseHTTPRequestHandler):
protocol_version = "HTTP/1.1"
@ -134,7 +136,8 @@ class HTTPRequestHandler(BaseHTTPRequestHandler):
token, profileId = params["sessionId"][0].split(":")[1:]
serverId = params["serverId"][0]
g_connections[serverId] = profileId
with _g_connections_lock:
_g_connections[serverId] = profileId
if True:
self.send_ok(b"OK")
@ -148,7 +151,8 @@ class HTTPRequestHandler(BaseHTTPRequestHandler):
profileId = data["selectedProfile"]
serverId = data["serverId"]
g_connections[serverId] = profileId
with _g_connections_lock:
_g_connections[serverId] = profileId
if True:
self.send_response(HTTPStatus.NO_CONTENT)
@ -164,8 +168,11 @@ class HTTPRequestHandler(BaseHTTPRequestHandler):
user = params["user"][0]
serverId = params["serverId"][0]
if not serverId in g_connections:
self.send_ok(b"NO")
with _g_connections_lock:
if not serverId in _g_connections:
self.send_ok(b"NO")
return
del _g_connections[serverId]
self.send_ok(b"YES")
@ -173,11 +180,12 @@ class HTTPRequestHandler(BaseHTTPRequestHandler):
serverId = params["serverId"][0]
user = params["username"][0]
if not serverId in g_connections:
self.send_response(HTTPStatus.NO_CONTENT)
self.end_headers()
profileId = g_connections[serverId]
with _g_connections_lock:
if not serverId in _g_connections:
self.send_response(HTTPStatus.NO_CONTENT)
self.end_headers()
del _g_connections[serverId]
profileId = _g_connections[serverId]
data = self.get_profile(profileId)
if data is None:

Loading…
Cancel
Save