(20241231) CORS on SocketIO.
This commit is contained in:
@@ -25,7 +25,8 @@
|
|||||||
N/A
|
N/A
|
||||||
|
|
||||||
"""
|
"""
|
||||||
import datetime
|
|
||||||
|
|
||||||
# *****************************************************************************************************************
|
# *****************************************************************************************************************
|
||||||
# ***** ****
|
# ***** ****
|
||||||
# *** IMPORT ***
|
# *** IMPORT ***
|
||||||
@@ -69,6 +70,9 @@ from models.api.finstitutions.trading.symbols.list import (
|
|||||||
TradingSymbolListBrokerResponse
|
TradingSymbolListBrokerResponse
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# To work with dat and time:
|
||||||
|
import datetime
|
||||||
|
|
||||||
# For asynchronous activities:
|
# For asynchronous activities:
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ import os
|
|||||||
# My utils:
|
# My utils:
|
||||||
from utils_v2.string import json
|
from utils_v2.string import json
|
||||||
from utils_v2.system import files
|
from utils_v2.system import files
|
||||||
from utils_v2.queue.kafka import ProducerKafka, ConsumerKafka
|
from utils_v2.queue.kafka.controllers.kafka import ProducerKafka, ConsumerKafka
|
||||||
|
|
||||||
# To make HTTP calls:
|
# To make HTTP calls:
|
||||||
import httpx
|
import httpx
|
||||||
@@ -134,6 +134,9 @@ def flush_kafka():
|
|||||||
kafka_producer.flush()
|
kafka_producer.flush()
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
def to_kafka(tick: TradingTick) -> bool:
|
def to_kafka(tick: TradingTick) -> bool:
|
||||||
|
|
||||||
global tick_count
|
global tick_count
|
||||||
|
|||||||
@@ -305,7 +305,7 @@ class ConsumerKafka:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def create_config(
|
def create_config(
|
||||||
bootstrap_servers: str | List[str],
|
bootstrap_servers: str | List[str],
|
||||||
group_id: str,
|
group_id: str = "default",
|
||||||
auto_offset_reset: Literal["latest", "earliest"] = "latest",
|
auto_offset_reset: Literal["latest", "earliest"] = "latest",
|
||||||
security_protocol: Literal["PLAINTEXT", "SSL"] = "PLAINTEXT",
|
security_protocol: Literal["PLAINTEXT", "SSL"] = "PLAINTEXT",
|
||||||
ca_file: str | None = None,
|
ca_file: str | None = None,
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ import os
|
|||||||
|
|
||||||
# My utils:
|
# My utils:
|
||||||
from utils_v2.string import json
|
from utils_v2.string import json
|
||||||
|
from utils_v2.string import regex
|
||||||
from utils_v2.system import files
|
from utils_v2.system import files
|
||||||
from utils_v2.database.async_mongo_v2 import AsyncMongo
|
from utils_v2.database.async_mongo_v2 import AsyncMongo
|
||||||
from utils_v2.queue.kafka.controllers.async_kafka import ConsumerKafka, get_ssl_context
|
from utils_v2.queue.kafka.controllers.async_kafka import ConsumerKafka, get_ssl_context
|
||||||
@@ -86,11 +87,33 @@ printer.disable()
|
|||||||
SERVER_HOSTNAME = str(socket.gethostname())
|
SERVER_HOSTNAME = str(socket.gethostname())
|
||||||
|
|
||||||
# For SocketIO:
|
# 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)
|
app = socketio.ASGIApp(sio)
|
||||||
|
|
||||||
# SocketIO Namespaces:
|
# SocketIO Namespaces:
|
||||||
NAMESPACE_MODULE = "/finstitutions/trading"
|
# NAMESPACE_MODULE = "/finstitutions/trading"
|
||||||
|
NAMESPACE_MODULE = None
|
||||||
NAMESPACE_PASSTHROUGH = "/passthrough"
|
NAMESPACE_PASSTHROUGH = "/passthrough"
|
||||||
|
|
||||||
# SocketIO Events:
|
# SocketIO Events:
|
||||||
@@ -147,7 +170,7 @@ async def handle_connect(sid, environ) -> bool:
|
|||||||
|
|
||||||
# Allow/reject requests:
|
# Allow/reject requests:
|
||||||
printer(sid)
|
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)
|
while redis_cache is None: await asyncio.sleep(0.5)
|
||||||
result = await redis_cache.set(
|
result = await redis_cache.set(
|
||||||
key = connected_clients[sid]["redisKey"],
|
key = connected_clients[sid]["redisKey"],
|
||||||
|
|||||||
Reference in New Issue
Block a user