Browse Source

Add locks for g_connections

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

22
auth/server.py

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

Loading…
Cancel
Save