Switch to using libssh2 from someone who knows how to properly configure it

This commit is contained in:
Kelvin Ly 2025-03-18 22:21:45 -04:00
parent 363ce80f14
commit f0e7bbe358
9 changed files with 133 additions and 25 deletions

3
.gitmodules vendored
View File

@ -1,3 +0,0 @@
[submodule "shroom-controller/components/libssh2/libssh2"]
path = shroom-controller/components/libssh2/libssh2
url = https://github.com/libssh2/libssh2

View File

@ -1 +1,3 @@
.cache/
build/
managed_components/

View File

@ -1,10 +0,0 @@
idf_component_register(
SRC_DIRS "libssh2/src" # "libssh2/src/threads" "libssh2/src/external"
INCLUDE_DIRS "libssh2/include"
PRIV_INCLUDE_DIRS "libssh2/src"
REQUIRES "mbedtls"
)
# _3DS is used to have libssh2 provide its own iovec; it doesn't seem to trigger any other behavior for now
target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-error=format -Wno-error=char-subscripts -DLIBSSH2_MBEDTLS -DHAVE_SYS_SOCKET_H)

@ -1 +0,0 @@
Subproject commit a312b43325e3383c865a87bb1d26cb52e3292641

View File

@ -0,0 +1,18 @@
dependencies:
idf:
source:
type: idf
version: 5.4.0
skuodi/libssh2_esp:
component_hash: a65c753c7388cf47299765dc8c8a22dccdc53d1e72a2fb0cf361c1f800067a63
dependencies: []
source:
registry_url: https://components.espressif.com/
type: service
version: 1.0.0
direct_dependencies:
- idf
- skuodi/libssh2_esp
manifest_hash: d8b4209272b4e81a14bbef159a9fa99da51e39fb3a9a5dd92295f22e4eb5cf79
target: esp32
version: 2.0.0

View File

@ -5,7 +5,8 @@ idf_component_register(
esp_driver_gpio
esp_wifi
nvs_flash
libssh2
libssh2_esp
)
# TODO maybe add the commands to generate these?

View File

@ -0,0 +1,17 @@
## IDF Component Manager Manifest File
dependencies:
## Required IDF version
idf:
version: '>=4.1.0'
# # Put list of dependencies here
# # For components maintained by Espressif:
# component: "~1.0.0"
# # For 3rd party components:
# username/component: ">=1.0.0,<2.0.0"
# username2/component2:
# version: "~1.0.0"
# # For transient dependencies `public` flag can be set.
# # `public` flag doesn't have an effect dependencies of the `main` component.
# # All dependencies of `main` are public by default.
# public: true
skuodi/libssh2_esp: ^1.0.0

View File

@ -38,6 +38,8 @@ static void event_handler(void* arg, esp_event_base_t event_base,
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
// TODO is this correct?
xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
if (s_retry_num < 5) {
esp_wifi_connect();
s_retry_num++;
@ -90,6 +92,9 @@ void main_task(void* args) {
bool connect_success = false;
libssh2_socket_t sock = LIBSSH2_INVALID_SOCKET;
LIBSSH2_SESSION* session = 0;
LIBSSH2_CHANNEL* channel = 0;
for (struct addrinfo* rp = results; rp; rp = rp->ai_next) {
ESP_LOGI(TAG, "creating socket fam %d type %d proto %d", rp->ai_family, rp->ai_socktype, rp->ai_protocol);
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
@ -117,35 +122,107 @@ void main_task(void* args) {
ESP_LOGI(TAG, "connection established, creating SSH session...");
LIBSSH2_SESSION* session = libssh2_session_init();
session = libssh2_session_init();
if (!session) {
ESP_LOGE(TAG, "could not create libssh2 session");
// TODO error out
break;
}
libssh2_trace(session, -0);
//libssh2_session_set_read_timeout(session, 20);
libssh2_trace(session, -1);
libssh2_session_handshake(session, sock);
const char* fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
const char username[] = "shrooms";
extern char* _binary_shrooms_key_start;
extern size_t _binary_shrooms_key_size;
extern char* _binary_shrooms_key_pub_start;
extern size_t _binary_shrooms_key_pub_size;
extern char _binary_shrooms_key_start[];
extern char _binary_shrooms_key_end[];
size_t shrooms_key_size = _binary_shrooms_key_end - _binary_shrooms_key_start;
extern char _binary_shrooms_key_pub_start[];
extern char _binary_shrooms_key_pub_end[];
size_t shrooms_key_pub_size = _binary_shrooms_key_pub_end - _binary_shrooms_key_pub_start;
ESP_LOGI(TAG, "pub key %p %p %d key %p %p %d",
_binary_shrooms_key_pub_start,
_binary_shrooms_key_pub_end,
shrooms_key_pub_size,
_binary_shrooms_key_start,
_binary_shrooms_key_end,
shrooms_key_size
);
rc = libssh2_userauth_publickey_frommemory(
session,
username,
sizeof(username) - 1,
_binary_shrooms_key_pub_start,
_binary_shrooms_key_pub_size,
shrooms_key_pub_size,
_binary_shrooms_key_start,
_binary_shrooms_key_size,
shrooms_key_size,
NULL);
// TODO the rest of it
if (rc) {
ESP_LOGE(TAG, "could not authenticate, err %d", rc);
break;
}
ESP_LOGE(TAG, "authenticated, opening channel...");
channel = libssh2_channel_open_session(session);
if (!channel) {
ESP_LOGE(TAG, "unable to open channel");
break;
}
rc = libssh2_channel_request_pty(channel, "vanilla");
if (rc) {
ESP_LOGE(TAG, "unable to request pty %d", rc);
break;
}
ESP_LOGE(TAG, "executing command...");
rc = libssh2_channel_exec(channel, "./runtest.sh");
if (rc) {
ESP_LOGE(TAG, "could not open channel");
}
//libssh2_session_set_timeout(session, 20);
ESP_LOGE(TAG, "waiting for results...");
while (!libssh2_channel_eof(channel)) {
ESP_LOGE(TAG, "waiting for read loop");
char tmp[256];
ssize_t rlen = libssh2_channel_read(channel, tmp, sizeof(tmp) - 1);
if (rlen < 0) {
ESP_LOGE(TAG, "received err %d", rlen);
continue;
}
tmp[rlen] = 0;
ESP_LOGI(TAG, "received %s", tmp);
}
ESP_LOGI(TAG, "ssh exiting");
//rc = libssh2_channel_get_get_exit_status(channel);
//cleanup:
if (libssh2_channel_close(channel)) {
ESP_LOGE(TAG, "could not close channel");
}
if (channel) {
libssh2_channel_free(channel);
}
channel = 0;
if (session) {
libssh2_session_disconnect(session, "normal shutdown");
libssh2_session_free(session);
}
if (sock != LIBSSH2_INVALID_SOCKET) {
shutdown(sock, 2);
LIBSSH2_SOCKET_CLOSE(sock);
}
break;
}

View File

@ -1866,6 +1866,13 @@ CONFIG_WIFI_PROV_AUTOSTOP_TIMEOUT=30
CONFIG_WIFI_PROV_STA_ALL_CHANNEL_SCAN=y
# CONFIG_WIFI_PROV_STA_FAST_SCAN is not set
# end of Wi-Fi Provisioning Manager
#
# libssh2
#
CONFIG_LIBSSH2_CRYPTO_ENGINE_MBEDTLS=y
CONFIG_LIBSSH2_DEBUG_ENABLE=y
# end of libssh2
# end of Component config
# CONFIG_IDF_EXPERIMENTAL_FEATURES is not set