Fix bridge mode to use SSL for WebSocket communication

- Bridge mode was incorrectly using raw socket operations over SSL connection
- Updated to use SSL_read/SSL_write for all WebSocket communication
- Fixed send_websocket_message to use send_websocket_frame with SSL
- Fixed pong frame sending to use SSL
- Fixed select() and FD_ISSET to monitor correct socket

This resolves the 'send_error' and connection closure issues in bridge mode tests.
parent 84781de4
This diff is collapsed.
......@@ -106,20 +106,77 @@ test_bridge_mode() {
fi
done
# Test 1: Send a status command
log_info "Test 1: Sending status command"
send_command '{"command":"status","timestamp":'$(date +%s)'}' >&${wsssht_in}
# Generate a unique request ID for the tunnel
REQUEST_ID="test_$(date +%s)_$$"
# Test 1: Send tunnel request
log_info "Test 1: Sending tunnel request"
send_command '{"type":"tunnel_request","client_id":"'$CLIENT_ID'","request_id":"'$REQUEST_ID'","tunnel":"any","tunnel_control":"any","service":"ssh","timestamp":'$(date +%s)'}' >&${wsssht_in}
# Wait for tunnel acknowledgment
log_info "Waiting for tunnel acknowledgment..."
local tunnel_ack_received=false
local timeout_counter=0
while [ $timeout_counter -lt 10 ]; do # 10 second timeout
if read -t 1 -u ${wsssht_out} line 2>/dev/null; then
if [ -n "$line" ]; then
log_info "Response: $line"
# Check if we received tunnel_ack
if echo "$line" | grep -q '"message".*"tunnel_ack"'; then
log_success "Tunnel acknowledgment received!"
tunnel_ack_received=true
break
fi
fi
fi
timeout_counter=$((timeout_counter + 1))
done
sleep 2
read_response
if [ "$tunnel_ack_received" = false ]; then
log_error "Timeout waiting for tunnel acknowledgment"
# Send quit to clean up
send_command '{"command":"quit","timestamp":'$(date +%s)'}' >&${wsssht_in}
return 1
fi
# Test 2: Monitor for tunnel control messages
log_info "Test 2: Monitoring for tunnel control messages (5 seconds)"
log_info "Note: In a real scenario, you would see tunnel_data, tunnel_response, or tunnel_close messages here"
sleep 5
# Test 2: Send data message with text string
log_info "Test 2: Sending data message with text string"
TEST_MESSAGE="Hello from bridge mode test!"
# Convert to hex for tunnel_data format
HEX_MESSAGE=$(echo -n "$TEST_MESSAGE" | xxd -p | tr -d '\n')
send_command '{"type":"tunnel_data","request_id":"'$REQUEST_ID'","data":"'$HEX_MESSAGE'","timestamp":'$(date +%s)'}' >&${wsssht_in}
# Wait for tunnel response
log_info "Waiting for tunnel response..."
local tunnel_response_received=false
timeout_counter=0
while [ $timeout_counter -lt 10 ]; do # 10 second timeout
if read -t 1 -u ${wsssht_out} line 2>/dev/null; then
if [ -n "$line" ]; then
log_info "Response: $line"
# Check if we received tunnel_response
if echo "$line" | grep -q '"message".*"tunnel_response"'; then
log_success "Tunnel response received!"
tunnel_response_received=true
break
fi
fi
fi
timeout_counter=$((timeout_counter + 1))
done
if [ "$tunnel_response_received" = false ]; then
log_warning "No tunnel response received within timeout"
fi
# Test 3: Send tunnel close
log_info "Test 3: Sending tunnel close"
send_command '{"type":"tunnel_close","request_id":"'$REQUEST_ID'","timestamp":'$(date +%s)'}' >&${wsssht_in}
sleep 2
# Test 3: Send a quit command
log_info "Test 3: Sending quit command"
# Test 4: Send quit command to end bridge mode
log_info "Test 4: Sending quit command to end bridge mode"
send_command '{"command":"quit","timestamp":'$(date +%s)'}' >&${wsssht_in}
sleep 2
......
......@@ -297,6 +297,16 @@ int run_bridge_mode(wsssh_config_t *config, const char *client_id, const char *w
return 1;
}
// Get the SSL connection for sending messages
SSL *ws_ssl = active_tunnel ? active_tunnel->ssl : NULL;
if (!ws_ssl) {
printf("{\"type\":\"error\",\"message\":\"Failed to get SSL connection\",\"timestamp\":%ld}\n", time(NULL));
fflush(stdout);
close(ws_sock);
if (ws_ctx) SSL_CTX_free(ws_ctx);
return 1;
}
// Send connection established message
printf("{\"type\":\"websocket_connected\",\"socket\":%d,\"timestamp\":%ld}\n", ws_sock, time(NULL));
fflush(stdout);
......@@ -349,8 +359,8 @@ int run_bridge_mode(wsssh_config_t *config, const char *client_id, const char *w
channel = "data";
}
// Send message to appropriate WebSocket channel
if (send_websocket_message(ws_sock, buffer, len, channel, config->debug) == 0) {
// Send message to appropriate WebSocket channel using SSL
if (send_websocket_frame(ws_ssl, buffer)) {
printf("{\"type\":\"message_sent\",\"channel\":\"%s\",\"message\":\"%s\",\"timestamp\":%ld}\n",
channel, buffer, time(NULL));
fflush(stdout);
......@@ -364,8 +374,9 @@ int run_bridge_mode(wsssh_config_t *config, const char *client_id, const char *w
// Handle WebSocket data (messages from server)
if (FD_ISSET(ws_sock, &readfds)) {
if ((size_t)frame_buffer_used < sizeof(frame_buffer)) {
int bytes_read = recv(ws_sock, frame_buffer + frame_buffer_used,
sizeof(frame_buffer) - frame_buffer_used, 0);
// Use SSL to read WebSocket data
int bytes_read = SSL_read(ws_ssl, frame_buffer + frame_buffer_used,
sizeof(frame_buffer) - frame_buffer_used);
if (bytes_read > 0) {
frame_buffer_used += bytes_read;
......@@ -406,7 +417,10 @@ int run_bridge_mode(wsssh_config_t *config, const char *client_id, const char *w
printf("{\"type\":\"ping_received\",\"timestamp\":%ld}\n", time(NULL));
fflush(stdout);
// Send pong
send_pong_frame_ws(ws_sock, payload, payload_len);
if (!send_pong_frame(ws_ssl, payload, payload_len)) {
printf("{\"type\":\"pong_send_error\",\"timestamp\":%ld}\n", time(NULL));
fflush(stdout);
}
} else if (frame_type == 0x8A) { // Pong frame
printf("{\"type\":\"pong_received\",\"timestamp\":%ld}\n", time(NULL));
fflush(stdout);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment