Spaces:
Paused
Paused
File size: 23,839 Bytes
aaaaa79 |
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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 |
"""
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 HTTPRequest:
"""Represents an HTTP request"""
def __init__(self, method: str = 'GET', path: str = '/', headers: Dict[str, str] = None, body: bytes = b''):
self.method = method.upper()
self.path = path
self.headers = headers or {}
self.body = body
self.version = 'HTTP/1.1'
@classmethod
def parse(cls, data: bytes) -> Optional['HTTPRequest']:
"""Parse HTTP request from raw data"""
try:
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 = {}
body_start = 1
for i, line in enumerate(lines[1:], 1):
if line == '':
body_start = i + 1
break
if ':' in line:
key, value = line.split(':', 1)
headers[key.strip().lower()] = value.strip()
# Parse body
body_lines = lines[body_start:]
body = '\r\n'.join(body_lines).encode('utf-8')
return cls(method, path, headers, body)
except Exception:
return None
def to_bytes(self) -> bytes:
"""Convert to raw HTTP request"""
request_line = f"{self.method} {self.path} {self.version}\r\n"
# Add default headers
if 'host' not in self.headers:
self.headers['host'] = 'localhost'
if 'user-agent' not in self.headers:
self.headers['user-agent'] = 'VirtualISP/1.0'
if self.body and 'content-length' not in self.headers:
self.headers['content-length'] = str(len(self.body))
# Build headers
header_lines = []
for key, value in self.headers.items():
header_lines.append(f"{key}: {value}\r\n")
# Combine all parts
request_data = request_line + ''.join(header_lines) + '\r\n'
return request_data.encode('utf-8') + self.body
class HTTPResponse:
"""Represents an HTTP response"""
def __init__(self, status_code: int = 200, reason: str = 'OK', headers: Dict[str, str] = None, body: bytes = b''):
self.status_code = status_code
self.reason = reason
self.headers = headers or {}
self.body = body
self.version = 'HTTP/1.1'
@classmethod
def parse(cls, data: bytes) -> Optional['HTTPResponse']:
"""Parse HTTP response from raw data"""
try:
lines = data.decode('utf-8', errors='ignore').split('\r\n')
if not lines:
return None
# Parse status line
status_line = lines[0].split(' ', 2)
if len(status_line) < 3:
return None
version, status_code, reason = status_line[0], int(status_line[1]), status_line[2]
# Parse headers
headers = {}
body_start = 1
for i, line in enumerate(lines[1:], 1):
if line == '':
body_start = i + 1
break
if ':' in line:
key, value = line.split(':', 1)
headers[key.strip().lower()] = value.strip()
# Parse body
body_lines = lines[body_start:]
body = '\r\n'.join(body_lines).encode('utf-8')
return cls(status_code, reason, headers, body)
except Exception:
return None
def to_bytes(self) -> bytes:
"""Convert to raw HTTP response"""
status_line = f"{self.version} {self.status_code} {self.reason}\r\n"
# Add default headers
if 'content-length' not in self.headers and self.body:
self.headers['content-length'] = str(len(self.body))
if 'server' not in self.headers:
self.headers['server'] = 'VirtualISP/1.0'
# Build headers
header_lines = []
for key, value in self.headers.items():
header_lines.append(f"{key}: {value}\r\n")
# Combine all parts
response_data = status_line + ''.join(header_lines) + '\r\n'
return response_data.encode('utf-8') + self.body
class SocketTranslator:
"""Socket translator implementation"""
def __init__(self, config: Dict):
self.config = config
self.connections: Dict[str, SocketConnection] = {}
self.lock = threading.Lock()
# Configuration
self.connect_timeout = config.get('connect_timeout', 10)
self.read_timeout = config.get('read_timeout', 30)
self.max_connections = config.get('max_connections', 1000)
self.buffer_size = config.get('buffer_size', 8192)
# HTTP client session
self.http_session = None
self.loop = None
# Statistics
self.stats = {
'total_connections': 0,
'active_connections': 0,
'failed_connections': 0,
'bytes_transferred': 0,
'http_requests': 0,
'tcp_connections': 0,
'udp_connections': 0
}
# Background tasks
self.running = False
self.cleanup_thread = None
async def _init_http_session(self):
"""Initialize HTTP client session"""
connector = aiohttp.TCPConnector(
limit=100,
limit_per_host=10,
ttl_dns_cache=300,
use_dns_cache=True,
)
timeout = aiohttp.ClientTimeout(
total=self.read_timeout,
connect=self.connect_timeout
)
self.http_session = aiohttp.ClientSession(
connector=connector,
timeout=timeout,
headers={'User-Agent': 'VirtualISP/1.0'}
)
def _is_http_request(self, data: bytes) -> bool:
"""Check if data looks like an HTTP request"""
try:
first_line = data.split(b'\r\n')[0].decode('utf-8', errors='ignore')
methods = ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS', 'PATCH', 'TRACE']
return any(first_line.startswith(method + ' ') for method in methods)
except:
return False
def _determine_connection_type(self, remote_host: str, remote_port: int, data: bytes = b'') -> ConnectionType:
"""Determine the appropriate connection type"""
# Check for HTTP/HTTPS based on port and data
if remote_port == 80 or (data and self._is_http_request(data)):
return ConnectionType.HTTP_CLIENT
elif remote_port == 443:
return ConnectionType.HTTPS_CLIENT
else:
return ConnectionType.TCP_SOCKET
def create_connection(self, virtual_conn: TCPConnection, remote_host: str, remote_port: int,
initial_data: bytes = b'') -> Optional[SocketConnection]:
"""Create a new socket connection"""
connection_id = f"{virtual_conn.connection_id}->{remote_host}:{remote_port}"
# Check connection limit
with self.lock:
if len(self.connections) >= self.max_connections:
return None
# Determine connection type
conn_type = self._determine_connection_type(remote_host, remote_port, initial_data)
# Create socket connection
socket_conn = SocketConnection(
connection_id=connection_id,
connection_type=conn_type,
virtual_connection=virtual_conn,
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_id] = socket_conn
# Establish connection based on type
if conn_type in [ConnectionType.HTTP_CLIENT, ConnectionType.HTTPS_CLIENT]:
success = self._create_http_connection(socket_conn, initial_data)
else:
success = self._create_tcp_connection(socket_conn, initial_data)
if success:
self.stats['total_connections'] += 1
self.stats['active_connections'] = len(self.connections)
if conn_type in [ConnectionType.HTTP_CLIENT, ConnectionType.HTTPS_CLIENT]:
self.stats['http_requests'] += 1
else:
self.stats['tcp_connections'] += 1
else:
self.stats['failed_connections'] += 1
with self.lock:
if connection_id in self.connections:
del self.connections[connection_id]
return None
return socket_conn
def _create_tcp_connection(self, socket_conn: SocketConnection, initial_data: bytes) -> bool:
"""Create TCP socket connection"""
try:
# Create socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(self.connect_timeout)
# Connect
sock.connect((socket_conn.remote_host, socket_conn.remote_port))
sock.settimeout(self.read_timeout)
socket_conn.host_socket = sock
socket_conn.is_connected = True
# Send initial data if any
if initial_data:
sock.send(initial_data)
socket_conn.update_activity(len(initial_data), 'sent')
# Start background thread for receiving data
thread = threading.Thread(
target=self._tcp_receive_loop,
args=(socket_conn,),
daemon=True
)
thread.start()
return True
except Exception as e:
print(f"Failed to create TCP connection to {socket_conn.remote_host}:{socket_conn.remote_port}: {e}")
socket_conn.error_count += 1
return False
def _create_http_connection(self, socket_conn: SocketConnection, initial_data: bytes) -> bool:
"""Create HTTP connection"""
try:
# Parse HTTP request
http_request = HTTPRequest.parse(initial_data)
if not http_request:
return False
# Set host header
http_request.headers['host'] = socket_conn.remote_host
# Start async HTTP request
if self.loop and not self.loop.is_closed():
asyncio.run_coroutine_threadsafe(
self._handle_http_request(socket_conn, http_request),
self.loop
)
else:
# Fallback to sync HTTP handling
return self._handle_http_request_sync(socket_conn, http_request)
return True
except Exception as e:
print(f"Failed to create HTTP connection to {socket_conn.remote_host}:{socket_conn.remote_port}: {e}")
socket_conn.error_count += 1
return False
async def _handle_http_request(self, socket_conn: SocketConnection, http_request: HTTPRequest):
"""Handle HTTP request asynchronously"""
try:
if not self.http_session:
await self._init_http_session()
# Build URL
scheme = 'https' if socket_conn.connection_type == ConnectionType.HTTPS_CLIENT else 'http'
url = f"{scheme}://{socket_conn.remote_host}:{socket_conn.remote_port}{http_request.path}"
# Make request
async with self.http_session.request(
method=http_request.method,
url=url,
headers=http_request.headers,
data=http_request.body
) as response:
# Read response
response_body = await response.read()
# Create HTTP response
http_response = HTTPResponse(
status_code=response.status,
reason=response.reason or 'OK',
headers=dict(response.headers),
body=response_body
)
# Send response back to virtual connection
response_data = http_response.to_bytes()
if socket_conn.virtual_connection and socket_conn.virtual_connection.on_data_received:
socket_conn.virtual_connection.on_data_received(response_data)
socket_conn.update_activity(len(response_data), 'received')
self.stats['bytes_transferred'] += len(response_data)
except Exception as e:
print(f"HTTP request failed: {e}")
socket_conn.error_count += 1
# Send error response
error_response = HTTPResponse(
status_code=500,
reason='Internal Server Error',
body=f"Error: {str(e)}".encode('utf-8')
)
response_data = error_response.to_bytes()
if socket_conn.virtual_connection and socket_conn.virtual_connection.on_data_received:
socket_conn.virtual_connection.on_data_received(response_data)
def _handle_http_request_sync(self, socket_conn: SocketConnection, http_request: HTTPRequest) -> bool:
"""Handle HTTP request synchronously (fallback)"""
try:
# Use urllib for sync HTTP requests
scheme = 'https' if socket_conn.connection_type == ConnectionType.HTTPS_CLIENT else 'http'
url = f"{scheme}://{socket_conn.remote_host}:{socket_conn.remote_port}{http_request.path}"
import urllib.request
import urllib.error
# Create request
req = urllib.request.Request(
url,
data=http_request.body if http_request.body else None,
headers=http_request.headers,
method=http_request.method
)
# Make request
with urllib.request.urlopen(req, timeout=self.read_timeout) as response:
response_body = response.read()
# Create HTTP response
http_response = HTTPResponse(
status_code=response.getcode(),
reason='OK',
headers=dict(response.headers),
body=response_body
)
# Send response back to virtual connection
response_data = http_response.to_bytes()
if socket_conn.virtual_connection and socket_conn.virtual_connection.on_data_received:
socket_conn.virtual_connection.on_data_received(response_data)
socket_conn.update_activity(len(response_data), 'received')
self.stats['bytes_transferred'] += len(response_data)
return True
except Exception as e:
print(f"Sync HTTP request failed: {e}")
socket_conn.error_count += 1
return False
def _tcp_receive_loop(self, socket_conn: SocketConnection):
"""Background loop for receiving TCP data"""
sock = socket_conn.host_socket
if not sock:
return
try:
while socket_conn.is_connected:
try:
data = sock.recv(self.buffer_size)
if not data:
break
# Forward data to virtual connection
if socket_conn.virtual_connection and socket_conn.virtual_connection.on_data_received:
socket_conn.virtual_connection.on_data_received(data)
socket_conn.update_activity(len(data), 'received')
self.stats['bytes_transferred'] += len(data)
except socket.timeout:
continue
except Exception as e:
print(f"TCP receive error: {e}")
break
finally:
self._close_connection(socket_conn.connection_id)
def send_data(self, connection_id: str, data: bytes) -> bool:
"""Send data through socket connection"""
with self.lock:
socket_conn = self.connections.get(connection_id)
if not socket_conn or not socket_conn.is_connected:
return False
try:
if socket_conn.connection_type in [ConnectionType.HTTP_CLIENT, ConnectionType.HTTPS_CLIENT]:
# For HTTP connections, treat as new request
return self._create_http_connection(socket_conn, data)
else:
# TCP connection
if socket_conn.host_socket:
socket_conn.host_socket.send(data)
socket_conn.update_activity(len(data), 'sent')
self.stats['bytes_transferred'] += len(data)
return True
except Exception as e:
print(f"Failed to send data: {e}")
socket_conn.error_count += 1
self._close_connection(connection_id)
return False
def _close_connection(self, connection_id: str):
"""Close socket connection"""
with self.lock:
socket_conn = self.connections.get(connection_id)
if not socket_conn:
return
# Close socket
if socket_conn.host_socket:
try:
socket_conn.host_socket.close()
except:
pass
socket_conn.is_connected = False
# Remove from connections
del self.connections[connection_id]
self.stats['active_connections'] = len(self.connections)
def close_connection(self, connection_id: str) -> bool:
"""Manually close connection"""
self._close_connection(connection_id)
return True
def get_connection(self, connection_id: str) -> Optional[SocketConnection]:
"""Get socket connection"""
with self.lock:
return self.connections.get(connection_id)
def get_connections(self) -> Dict[str, Dict]:
"""Get all socket connections"""
with self.lock:
return {
conn_id: conn.to_dict()
for conn_id, conn in self.connections.items()
}
def get_stats(self) -> Dict:
"""Get socket translator statistics"""
with self.lock:
stats = self.stats.copy()
stats['active_connections'] = len(self.connections)
return stats
def _cleanup_loop(self):
"""Background cleanup loop"""
while self.running:
try:
current_time = time.time()
expired_connections = []
with self.lock:
for conn_id, conn in self.connections.items():
# Close connections that have been inactive too long
if current_time - conn.last_activity > self.read_timeout * 2:
expired_connections.append(conn_id)
for conn_id in expired_connections:
self._close_connection(conn_id)
time.sleep(30) # Cleanup every 30 seconds
except Exception as e:
print(f"Socket translator cleanup error: {e}")
time.sleep(5)
def start(self):
"""Start socket translator"""
self.running = True
# Start event loop for async HTTP
try:
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
# Start cleanup thread
self.cleanup_thread = threading.Thread(target=self._cleanup_loop, daemon=True)
self.cleanup_thread.start()
print("Socket translator started")
except Exception as e:
print(f"Failed to start socket translator: {e}")
def stop(self):
"""Stop socket translator"""
self.running = False
# Close all connections
with self.lock:
connection_ids = list(self.connections.keys())
for conn_id in connection_ids:
self._close_connection(conn_id)
# Close HTTP session
if self.http_session:
asyncio.run_coroutine_threadsafe(self.http_session.close(), self.loop)
# Close event loop
if self.loop and not self.loop.is_closed():
self.loop.call_soon_threadsafe(self.loop.stop)
# Wait for cleanup thread
if self.cleanup_thread:
self.cleanup_thread.join()
print("Socket translator stopped")
|