#!/usr/bin/env python3 """ Test Connection Logging Simple test to verify that connection attempts and requests are being logged properly """ import asyncio import socket import struct import sys import os # Add parent directory to path sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) async def test_basic_connection(): """Test basic TCP connection to see if it gets logged""" print("๐Ÿงช Testing basic TCP connection logging...") try: # Try to connect to the ISUP server with timeout print("๐Ÿ”— Attempting connection to localhost:7660...") reader, writer = await asyncio.wait_for( asyncio.open_connection('localhost', 7660), timeout=10.0 ) print("โœ… Connected to ISUP server") # Send some invalid data to test logging writer.write(b"HELLO_WORLD_INVALID_ISUP_DATA") await writer.drain() print("๐Ÿ“ค Sent invalid data") # Wait a moment await asyncio.sleep(1) # Close connection writer.close() await writer.wait_closed() print("๐Ÿ“ด Connection closed") except Exception as e: print(f"โŒ Connection test failed: {e}") async def test_isup_header(): """Test sending a malformed ISUP header""" print("\n๐Ÿงช Testing ISUP header logging...") try: reader, writer = await asyncio.wait_for( asyncio.open_connection('localhost', 7660), timeout=10.0 ) print("โœ… Connected to ISUP server") # Create a malformed ISUP header (wrong size) fake_header = struct.pack('>BBHB32sHH', 0x50, # ISUP version 5.0 0x01, # REGISTER_REQ 0x0001, # sequence 10, # device_id_len b"TEST_DEVICE".ljust(32, b'\x00'), # device_id 0, # data_length 0 # reserved ) writer.write(fake_header) await writer.drain() print("๐Ÿ“ค Sent malformed ISUP header") # Wait a moment await asyncio.sleep(1) writer.close() await writer.wait_closed() print("๐Ÿ“ด Connection closed") except Exception as e: print(f"โŒ ISUP header test failed: {e}") async def test_registration_attempt(): """Test sending a registration request""" print("\n๐Ÿงช Testing registration attempt logging...") try: reader, writer = await asyncio.wait_for( asyncio.open_connection('localhost', 7660), timeout=10.0 ) print("โœ… Connected to ISUP server") # Create a proper ISUP registration request device_id = "TEST_DEVICE_123" registration_data = b"FAKE_REGISTRATION_DATA_FOR_TESTING" + b"\x00" * 50 header = struct.pack('>BBHB32sHH', 0x50, # ISUP version 5.0 0x01, # REGISTER_REQ 0x0001, # sequence len(device_id), # device_id_len device_id.encode('utf-8').ljust(32, b'\x00'), # device_id len(registration_data), # data_length 0 # reserved ) # Send header + data writer.write(header + registration_data) await writer.drain() print("๐Ÿ“ค Sent registration request") # Try to read response try: response_header = await asyncio.wait_for(reader.read(20), timeout=2.0) if response_header: print(f"๐Ÿ“ฅ Received response: {len(response_header)} bytes") else: print("๐Ÿ“ฅ No response received") except asyncio.TimeoutError: print("โฐ Response timeout") writer.close() await writer.wait_closed() print("๐Ÿ“ด Connection closed") except Exception as e: print(f"โŒ Registration test failed: {e}") async def test_multiple_connections(): """Test multiple simultaneous connections""" print("\n๐Ÿงช Testing multiple connection logging...") tasks = [] for i in range(3): tasks.append(test_single_connection(i)) await asyncio.gather(*tasks, return_exceptions=True) print("โœ… Multiple connection test completed") async def test_single_connection(connection_id): """Single connection for multiple connection test""" try: reader, writer = await asyncio.wait_for( asyncio.open_connection('localhost', 7660), timeout=10.0 ) print(f"โœ… Connection {connection_id} established") # Send some data writer.write(f"CONNECTION_{connection_id}_TEST_DATA".encode()) await writer.drain() await asyncio.sleep(0.5) writer.close() await writer.wait_closed() print(f"๐Ÿ“ด Connection {connection_id} closed") except Exception as e: print(f"โŒ Connection {connection_id} failed: {e}") def test_raw_socket(): """Test raw socket connection""" print("\n๐Ÿงช Testing raw socket connection...") try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(5.0) print("๐Ÿ”— Attempting raw socket connection...") sock.connect(('localhost', 7660)) print("โœ… Raw socket connected") # Send some raw data sock.send(b"RAW_SOCKET_TEST_DATA_123") print("๐Ÿ“ค Sent raw data") sock.close() print("๐Ÿ“ด Raw socket closed") except Exception as e: print(f"โŒ Raw socket test failed: {e}") async def main(): """Run all connection tests""" print("๐Ÿš€ Starting Connection Logging Tests") print("=" * 50) # Test basic connection await test_basic_connection() # Test ISUP header await test_isup_header() # Test registration attempt await test_registration_attempt() # Test multiple connections await test_multiple_connections() # Test raw socket test_raw_socket() print("\n" + "=" * 50) print("๐Ÿ All connection tests completed!") print("\n๐Ÿ’ก Check the Debug tab in the dashboard to see logged connections:") print(" https://atomio-hikvision-isup.hf.space/#debug") if __name__ == "__main__": asyncio.run(main())