Skip to content

Latest commit

 

History

History
99 lines (71 loc) · 3.29 KB

INIT_FORMAT_README.md

File metadata and controls

99 lines (71 loc) · 3.29 KB

INIT Response Format for Delphi Client Compatibility

This document explains the issue with the INIT command response format and how it was fixed to ensure compatibility with Delphi clients.

Problem

The Delphi clients (Windows-based) were unable to initialize communication with the Linux server, resulting in the error:

ERRL [Error]Unable to initizlize communication! Terminate connection

Root Cause

The Delphi client uses a specific method to parse the server's response:

// In CloudReportClient.pas (TReportClientThread.FTCPGetCryptoKey):
if not TryStrToInt(FTCPClient.LastCmdResult.Text.Values['LEN'], Len) then 
  raise EHndError.Create(C_ErrCode_TcpCmd, 'Invalid answer! Missing "LEN" parameter');

CryptoKey_ := FTCPClient.LastCmdResult.Text.Values['KEY'] +
               Copy(C_CryptoDictionary[Key], 1, Len) +
               Copy(FHostName, 1, 2) + Copy(FHostName, Length(FHostName), 1);

This code has these requirements:

  1. The response must contain LEN=value and KEY=value on separate lines
  2. These lines must use CRLF line endings (\r\n) for proper parsing by Delphi's TStringList.Values property
  3. The final line should also end with CRLF for consistency

Solution

We've modified the server's response format to match exactly what the Delphi client expects:

LEN=8\r\nKEY=ABCDEFGH\r\n

Key changes:

  1. Updated _format_init_response in tcp_server.py to use format type 14
  2. Set INIT_RESPONSE_FORMAT=14 in the .env file and docker-compose.yml
  3. Added validation tests to verify the format works correctly

Correct Response Format

The correct Delphi-compatible response format (INIT_RESPONSE_FORMAT=14) is:

LEN=8\r\nKEY=ABCDEFGH\r\n

Where:

  • LEN comes before KEY (the order matters for some clients)
  • Each key-value pair is on its own line
  • Lines end with CRLF (\r\n)
  • The response ends with a trailing CRLF
  • No additional whitespace around the = sign

Key Generation Process

The crypto key is generated as follows:

  1. Server sends LEN and KEY in response to INIT
  2. Client constructs the crypto key as:
    crypto_key = server_key + crypto_dictionary[key_id-1][:key_length] + hostname[:2] + hostname[-1:]
    
  3. This key is then used for encrypting/decrypting all subsequent commands

Testing the Fix

  1. Run the validation test:

    ./run_format_test.sh
  2. This will:

    • Set the environment variable INIT_RESPONSE_FORMAT=14
    • Restart the server
    • Run test_format_validation.py which simulates a Delphi client connection
    • Verify the response format is correct
    • Test that the crypto key can be properly generated and encryption works

Troubleshooting

If clients still have connection issues:

  1. Check the server logs for detailed error messages about the INIT response format
  2. Verify INIT_RESPONSE_FORMAT=14 is set in both .env and environment variables
  3. Use test_format_validation.py to test if the server is responding correctly
  4. Check for any network issues that might be preventing proper communication
  5. Verify the crypto dictionary matches between client and server

References