Send server SSH version immediately after forwarding client version to prevent timeout

parent 56dae178
......@@ -349,6 +349,9 @@ void *forward_tcp_to_ws(void *arg) {
fflush(stdout);
}
// Check if this is the SSH client version string
int is_client_version = (bytes_read >= 8 && strncmp(buffer, "SSH-2.0-", 8) == 0);
// Convert to hex with bounds checking
size_t hex_size = (size_t)bytes_read * 2 + 1;
if (hex_size > BUFFER_SIZE) {
......@@ -377,6 +380,29 @@ void *forward_tcp_to_ws(void *arg) {
}
break;
}
// If this was the client SSH version, immediately send the server version to prevent timeout
if (is_client_version) {
const char *server_version = "SSH-2.0-OpenSSH_10.0p2 Debian-8\r\n";
size_t version_len = strlen(server_version);
if (debug) {
printf("[DEBUG - TCPConnection] Sending server version immediately after client version\n");
fflush(stdout);
}
ssize_t sent = send(client_sock, server_version, version_len, 0);
if (sent > 0) {
if (debug) {
printf("[DEBUG] Sent %zd bytes of server version\n", sent);
fflush(stdout);
}
// Set flag to skip duplicate server version
pthread_mutex_lock(&tunnel_mutex);
if (active_tunnel) {
active_tunnel->server_version_sent = 1;
}
pthread_mutex_unlock(&tunnel_mutex);
}
}
}
}
......
......@@ -477,32 +477,17 @@ int main(int argc, char *argv[]) {
pthread_mutex_lock(&tunnel_mutex);
active_tunnel->local_sock = accepted_sock;
// Send server version immediately to prevent SSH client timeout
const char *server_version = "SSH-2.0-OpenSSH_10.0p2 Debian-8\r\n";
size_t version_len = strlen(server_version);
if (config.debug) {
printf("[DEBUG - Tunnel] Sending server version immediately to SSH client\n");
fflush(stdout);
}
ssize_t sent = send(accepted_sock, server_version, version_len, 0);
if (sent > 0) {
if (config.debug) {
printf("[DEBUG] Sent %zd bytes of server version to SSH client\n", sent);
fflush(stdout);
}
}
// Send any buffered data to the SSH client immediately
if (active_tunnel->incoming_buffer && active_tunnel->incoming_buffer->used > 0) {
if (config.debug) {
printf("[DEBUG - Tunnel] Sending %zu bytes of buffered server response to SSH client\n", active_tunnel->incoming_buffer->used);
fflush(stdout);
}
ssize_t sent2 = send(accepted_sock, active_tunnel->incoming_buffer->buffer, active_tunnel->incoming_buffer->used, 0);
if (sent2 > 0) {
frame_buffer_consume(active_tunnel->incoming_buffer, sent2);
ssize_t sent = send(accepted_sock, active_tunnel->incoming_buffer->buffer, active_tunnel->incoming_buffer->used, 0);
if (sent > 0) {
frame_buffer_consume(active_tunnel->incoming_buffer, sent);
if (config.debug) {
printf("[DEBUG] Sent %zd bytes of buffered server response to SSH client\n", sent2);
printf("[DEBUG] Sent %zd bytes of buffered server response to SSH client\n", sent);
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