Spaces:
Runtime error
Runtime error
Lewis Kimaru
feat: add WebSocket and HTTP tunnels for ISUP protocol to bypass HF port limitations
5003a03 Download test/test_connection_logging.py from Atomio/hikvision-isup: direct link, hf CLI and curl.
- Browser
- Download file 6.43 kB
-
https://huggingface.co/spaces/Atomio/hikvision-isup/resolve/main/test/test_connection_logging.py
- Command line
-
hf download hf://spaces/Atomio/hikvision-isup/test/test_connection_logging.py
-
curl -L -o test_connection_logging.py https://huggingface.co/spaces/Atomio/hikvision-isup/resolve/main/test/test_connection_logging.py
6.43 kB
| #!/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()) | |