(20241231) CORS on SocketIO.
This commit is contained in:
@@ -41,6 +41,7 @@ import os
|
||||
|
||||
# My utils:
|
||||
from utils_v2.string import json
|
||||
from utils_v2.string import regex
|
||||
from utils_v2.system import files
|
||||
from utils_v2.database.async_mongo_v2 import AsyncMongo
|
||||
from utils_v2.queue.kafka.controllers.async_kafka import ConsumerKafka, get_ssl_context
|
||||
@@ -86,11 +87,33 @@ printer.disable()
|
||||
SERVER_HOSTNAME = str(socket.gethostname())
|
||||
|
||||
# For SocketIO:
|
||||
sio = socketio.AsyncServer(async_mode = "asgi")
|
||||
# Custom CORS function to allow local IPs
|
||||
def allow_origins(origin):
|
||||
|
||||
# Allow specific domains
|
||||
allowed_origins = [
|
||||
r".*\.thecaoffice\.com.*",
|
||||
r".*\.ditscentre\.in.*",
|
||||
r"http[s]?://127\.0\.0\.1.*",
|
||||
r"http[s]?://192\.168\.[\d]{1,3}\.[\d]{1,3}.*",
|
||||
]
|
||||
|
||||
# Check if the origin is in the allowed list
|
||||
for allowed_origin in allowed_origins:
|
||||
if regex.match(origin, allowed_origin): return True
|
||||
|
||||
# Reject other origins
|
||||
return False
|
||||
|
||||
sio = socketio.AsyncServer(
|
||||
cors_allowed_origins = allow_origins,
|
||||
async_mode = "asgi"
|
||||
)
|
||||
app = socketio.ASGIApp(sio)
|
||||
|
||||
# SocketIO Namespaces:
|
||||
NAMESPACE_MODULE = "/finstitutions/trading"
|
||||
# NAMESPACE_MODULE = "/finstitutions/trading"
|
||||
NAMESPACE_MODULE = None
|
||||
NAMESPACE_PASSTHROUGH = "/passthrough"
|
||||
|
||||
# SocketIO Events:
|
||||
@@ -147,7 +170,7 @@ async def handle_connect(sid, environ) -> bool:
|
||||
|
||||
# Allow/reject requests:
|
||||
printer(sid)
|
||||
print("ENVIRON:", json.to_string(environ, default = str))
|
||||
# print("ENVIRON:", json.to_string(environ, default = str))
|
||||
while redis_cache is None: await asyncio.sleep(0.5)
|
||||
result = await redis_cache.set(
|
||||
key = connected_clients[sid]["redisKey"],
|
||||
|
||||
Reference in New Issue
Block a user