Spaces:
Runtime error
Runtime error
File size: 12,673 Bytes
6a5b8d8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
"""
Socket Translator Module
Bridges virtual connections to real host sockets:
- Map virtual connections to host sockets/HTTP clients
- Bidirectional data streaming
- Connection lifecycle management
- Protocol translation (TCP/UDP to host sockets)
"""
import socket
import threading
import time
import asyncio
import aiohttp
import ssl
from typing import Dict, Optional, Callable, Tuple, Any
from dataclasses import dataclass
from enum import Enum
import urllib.parse
import json
from .tcp_engine import TCPConnection
class ConnectionType(Enum):
TCP_SOCKET = "TCP_SOCKET"
UDP_SOCKET = "UDP_SOCKET"
HTTP_CLIENT = "HTTP_CLIENT"
HTTPS_CLIENT = "HTTPS_CLIENT"
@dataclass
class SocketConnection:
"""Represents a socket connection"""
connection_id: str
connection_type: ConnectionType
virtual_connection: Optional[TCPConnection]
host_socket: Optional[socket.socket]
remote_host: str
remote_port: int
created_time: float
last_activity: float
bytes_sent: int = 0
bytes_received: int = 0
is_connected: bool = False
error_count: int = 0
def update_activity(self, bytes_transferred: int = 0, direction: str = 'sent'):
"""Update connection activity"""
self.last_activity = time.time()
if direction == 'sent':
self.bytes_sent += bytes_transferred
else:
self.bytes_received += bytes_transferred
def to_dict(self) -> Dict:
"""Convert to dictionary"""
return {
'connection_id': self.connection_id,
'connection_type': self.connection_type.value,
'remote_host': self.remote_host,
'remote_port': self.remote_port,
'created_time': self.created_time,
'last_activity': self.last_activity,
'bytes_sent': self.bytes_sent,
'bytes_received': self.bytes_received,
'is_connected': self.is_connected,
'error_count': self.error_count,
'duration': time.time() - self.created_time
}
class SocketTranslator:
"""Manages socket translations and connections"""
_instance = None
_lock = threading.Lock()
def __new__(cls):
with cls._lock:
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance._initialized = False
return cls._instance
def __init__(self):
if self._initialized:
return
self.connections: Dict[str, SocketConnection] = {}
self.lock = threading.Lock()
self.cleanup_thread = threading.Thread(target=self._cleanup_loop)
self.cleanup_thread.daemon = True
self.running = True
self.cleanup_thread.start()
# SSL context for HTTPS
self.ssl_context = ssl.create_default_context()
self.ssl_context.check_hostname = False
self.ssl_context.verify_mode = ssl.CERT_NONE
# Async event loop
self.loop = asyncio.new_event_loop()
self.async_thread = threading.Thread(target=self._run_async_loop)
self.async_thread.daemon = True
self.async_thread.start()
self._initialized = True
def create_connection(self, connection_type: ConnectionType,
remote_host: str, remote_port: int,
virtual_connection: Optional[TCPConnection] = None) -> str:
"""Create a new connection"""
connection = SocketConnection(
connection_id=str(time.time_ns()),
connection_type=connection_type,
virtual_connection=virtual_connection,
host_socket=None,
remote_host=remote_host,
remote_port=remote_port,
created_time=time.time(),
last_activity=time.time()
)
with self.lock:
self.connections[connection.connection_id] = connection
return connection.connection_id
async def connect(self, connection_id: str) -> bool:
"""Establish connection"""
connection = self.connections.get(connection_id)
if not connection:
return False
try:
if connection.connection_type in (ConnectionType.TCP_SOCKET, ConnectionType.UDP_SOCKET):
# Create socket
sock_type = socket.SOCK_STREAM if connection.connection_type == ConnectionType.TCP_SOCKET else socket.SOCK_DGRAM
sock = socket.socket(socket.AF_INET, sock_type)
sock.setblocking(False)
# Connect
try:
sock.connect((connection.remote_host, connection.remote_port))
except BlockingIOError:
pass # Expected for non-blocking socket
connection.host_socket = sock
connection.is_connected = True
# Start monitoring
asyncio.create_task(self._monitor_socket(connection))
elif connection.connection_type in (ConnectionType.HTTP_CLIENT, ConnectionType.HTTPS_CLIENT):
# HTTP(S) connection will be made per request
connection.is_connected = True
connection.update_activity()
return True
except Exception as e:
print(f"Connection error: {e}")
connection.error_count += 1
return False
async def send_data(self, connection_id: str, data: bytes) -> bool:
"""Send data through connection"""
connection = self.connections.get(connection_id)
if not connection or not connection.is_connected:
return False
try:
if connection.connection_type in (ConnectionType.TCP_SOCKET, ConnectionType.UDP_SOCKET):
await self.loop.sock_sendall(connection.host_socket, data)
connection.update_activity(len(data), 'sent')
elif connection.connection_type in (ConnectionType.HTTP_CLIENT, ConnectionType.HTTPS_CLIENT):
# Parse HTTP request
request = self._parse_http_request(data)
if not request:
return False
# Make HTTP request
async with aiohttp.ClientSession() as session:
url = f"{'https' if connection.connection_type == ConnectionType.HTTPS_CLIENT else 'http'}://{connection.remote_host}:{connection.remote_port}{request['path']}"
async with session.request(
method=request['method'],
url=url,
headers=request['headers'],
data=request.get('body', b''),
ssl=self.ssl_context if connection.connection_type == ConnectionType.HTTPS_CLIENT else None
) as response:
# Forward response to virtual connection
response_data = await response.read()
if connection.virtual_connection:
connection.virtual_connection.send_data(response_data)
connection.update_activity(len(data), 'sent')
connection.update_activity(len(response_data), 'received')
return True
except Exception as e:
print(f"Send error: {e}")
connection.error_count += 1
return False
def close_connection(self, connection_id: str):
"""Close a connection"""
connection = self.connections.get(connection_id)
if connection:
if connection.host_socket:
try:
connection.host_socket.close()
except:
pass
connection.is_connected = False
connection.update_activity()
def get_connection(self, connection_id: str) -> Optional[SocketConnection]:
"""Get connection by ID"""
return self.connections.get(connection_id)
def _run_async_loop(self):
"""Run async event loop"""
asyncio.set_event_loop(self.loop)
self.loop.run_forever()
async def _monitor_socket(self, connection: SocketConnection):
"""Monitor socket for incoming data"""
while connection.is_connected:
try:
data = await self.loop.sock_recv(connection.host_socket, 8192)
if not data:
break
connection.update_activity(len(data), 'received')
# Forward data to virtual connection
if connection.virtual_connection:
connection.virtual_connection.send_data(data)
except Exception as e:
print(f"Monitor error: {e}")
connection.error_count += 1
break
connection.is_connected = False
def _cleanup_loop(self):
"""Background cleanup loop"""
while self.running:
time.sleep(60) # Run every minute
try:
self._cleanup_connections()
except Exception as e:
print(f"Cleanup error: {e}")
def _cleanup_connections(self):
"""Clean up inactive connections"""
current_time = time.time()
to_remove = []
with self.lock:
for connection_id, connection in self.connections.items():
# Remove if:
# 1. Not connected and inactive for 5 minutes
# 2. Connected but inactive for 30 minutes
# 3. Too many errors
if ((not connection.is_connected and current_time - connection.last_activity > 300) or
(connection.is_connected and current_time - connection.last_activity > 1800) or
connection.error_count > 5):
self.close_connection(connection_id)
to_remove.append(connection_id)
for connection_id in to_remove:
del self.connections[connection_id]
def _parse_http_request(self, data: bytes) -> Optional[Dict]:
"""Parse HTTP request from raw data"""
try:
# Split into lines
lines = data.decode('utf-8', errors='ignore').split('\r\n')
if not lines:
return None
# Parse request line
request_line = lines[0].split(' ')
if len(request_line) < 3:
return None
method, path, version = request_line[0], request_line[1], request_line[2]
# Parse headers
headers = {}
i = 1
while i < len(lines):
line = lines[i].strip()
if not line:
break
if ':' in line:
key, value = line.split(':', 1)
headers[key.strip()] = value.strip()
i += 1
# Get body
body = '\r\n'.join(lines[i+1:]).encode('utf-8') if i+1 < len(lines) else b''
return {
'method': method,
'path': path,
'version': version,
'headers': headers,
'body': body
}
except Exception as e:
print(f"HTTP parse error: {e}")
return None
def shutdown(self):
"""Shutdown the translator"""
self.running = False
if self.cleanup_thread.is_alive():
self.cleanup_thread.join()
# Close all connections
with self.lock:
for connection_id in list(self.connections.keys()):
self.close_connection(connection_id)
self.connections.clear()
# Stop async loop
self.loop.stop()
if self.async_thread.is_alive():
self.async_thread.join()
|