Add --enc option support to wsssh and wsscp, and fix wssshd tunnel forwarding

- Added --enc option to wsssh and wsscp for data encoding control
- wsssh and wsscp now accept optional --enc hex|base64|bin argument
- Encoding option is passed to wsssht ProxyCommand when specified
- Updated help text and usage messages for both tools

- Fixed wssshd tunnel request forwarding to include enc, service, and version parameters
- wssshd now extracts enc, service, and version from incoming tunnel_request messages
- Forwarded tunnel_request to wssshc includes all required parameters
- Added proper memory management for extracted parameters

- Updated documentation to reflect --enc option availability for all tools
- Maintained backward compatibility - --enc option is completely optional
parent fd4a3385
......@@ -37,7 +37,7 @@
// Pre-computed JSON message templates
static const char *REGISTERED_MSG = "{\"type\":\"registered\",\"client_id\":\"%s\"}";
static const char *REGISTRATION_ERROR_MSG = "{\"type\":\"registration_error\",\"error\":\"%s\"}";
static const char *TUNNEL_REQUEST_MSG = "{\"type\":\"tunnel_request\",\"request_id\":\"%s\"}";
static const char *TUNNEL_REQUEST_MSG = "{\"type\":\"tunnel_request\",\"request_id\":\"%s\",\"enc\":\"%s\",\"service\":\"%s\",\"version\":\"%s\"}";
static const char *TUNNEL_ACK_MSG = "{\"type\":\"tunnel_ack\",\"request_id\":\"%s\"}";
static const char *TUNNEL_ERROR_MSG = "{\"type\":\"tunnel_error\",\"request_id\":\"%s\",\"error\":\"%s\"}";
......@@ -373,6 +373,9 @@ int websocket_handle_message(wssshd_state_t *state, ws_connection_t *conn __attr
// Handle tunnel request (simplified)
char *client_id = NULL;
char *request_id = NULL;
char *enc = NULL;
char *service = NULL;
char *version = NULL;
// Extract client_id (make a copy to avoid modifying the original string)
char *tunnel_client_id_start = strstr(msg_copy, "\"client_id\":\"");
......@@ -414,6 +417,57 @@ int websocket_handle_message(wssshd_state_t *state, ws_connection_t *conn __attr
}
}
// Extract enc
char *enc_start = strstr(msg_copy, "\"enc\":\"");
if (enc_start) {
enc_start += strlen("\"enc\":\"");
char *enc_end = strchr(enc_start, '"');
if (enc_end) {
size_t enc_len = enc_end - enc_start;
char *enc_copy = malloc(enc_len + 1);
if (enc_copy) {
memcpy(enc_copy, enc_start, enc_len);
enc_copy[enc_len] = '\0';
enc = enc_copy;
if (state->debug) printf("[DEBUG - %s -> wssshd] Extracted enc: '%s'\n", direction, enc);
}
}
}
// Extract service
char *service_start = strstr(msg_copy, "\"service\":\"");
if (service_start) {
service_start += strlen("\"service\":\"");
char *service_end = strchr(service_start, '"');
if (service_end) {
size_t service_len = service_end - service_start;
char *service_copy = malloc(service_len + 1);
if (service_copy) {
memcpy(service_copy, service_start, service_len);
service_copy[service_len] = '\0';
service = service_copy;
if (state->debug) printf("[DEBUG - %s -> wssshd] Extracted service: '%s'\n", direction, service);
}
}
}
// Extract version
char *version_start = strstr(msg_copy, "\"version\":\"");
if (version_start) {
version_start += strlen("\"version\":\"");
char *version_end = strchr(version_start, '"');
if (version_end) {
size_t version_len = version_end - version_start;
char *version_copy = malloc(version_len + 1);
if (version_copy) {
memcpy(version_copy, version_start, version_len);
version_copy[version_len] = '\0';
version = version_copy;
if (state->debug) printf("[DEBUG - %s -> wssshd] Extracted version: '%s'\n", direction, version);
}
}
}
if (client_id && request_id) {
client_t *client = websocket_find_client(state, client_id);
......@@ -429,7 +483,11 @@ int websocket_handle_message(wssshd_state_t *state, ws_connection_t *conn __attr
// Send tunnel request to client (wssshc)
char request_msg[512];
snprintf(request_msg, sizeof(request_msg), TUNNEL_REQUEST_MSG, request_id);
// Use defaults if parameters not provided
const char *enc_val = enc ? enc : "hex";
const char *service_val = service ? service : "ssh";
const char *version_val = version ? version : "1.6.5";
snprintf(request_msg, sizeof(request_msg), TUNNEL_REQUEST_MSG, request_id, enc_val, service_val, version_val);
if (state->debug) printf("[DEBUG - wssshd -> wssshc] Sending tunnel request: %s\n", request_msg);
ws_send_frame(client->websocket, WS_OPCODE_TEXT, request_msg, strlen(request_msg));
......@@ -458,10 +516,13 @@ int websocket_handle_message(wssshd_state_t *state, ws_connection_t *conn __attr
// Free allocated strings
if (state->debug) {
printf("[DEBUG - %s -> wssshd] Freeing tunnel request strings: client_id=%p, request_id=%p\n", direction, client_id, request_id);
printf("[DEBUG - %s -> wssshd] Freeing tunnel request strings: client_id=%p, request_id=%p, enc=%p, service=%p, version=%p\n", direction, client_id, request_id, enc, service, version);
}
if (client_id) free(client_id);
if (request_id) free(request_id);
if (enc) free(enc);
if (service) free(service);
if (version) free(version);
} else if (strstr(msg_copy, "\"type\":\"tunnel_ack\"") || strstr(msg_copy, "\"type\": \"tunnel_ack\"")) {
if (state->debug) {
printf("[DEBUG - %s -> wssshd] Processing tunnel acknowledgment\n", direction);
......
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