diff --git a/components/ts_api/CMakeLists.txt b/components/ts_api/CMakeLists.txt index c3bffc5c..095ea45d 100644 --- a/components/ts_api/CMakeLists.txt +++ b/components/ts_api/CMakeLists.txt @@ -36,6 +36,7 @@ set(TS_API_SRCS set(TS_API_EMBED_FILES "assets/lpmu-agx-network-setup.tar.gz" + "assets/lpmu-agx-network-setup.sha256" ) idf_component_register( diff --git a/components/ts_api/assets/lpmu-agx-network-setup.manifest b/components/ts_api/assets/lpmu-agx-network-setup.manifest index 770e78a3..590d3db2 100644 --- a/components/ts_api/assets/lpmu-agx-network-setup.manifest +++ b/components/ts_api/assets/lpmu-agx-network-setup.manifest @@ -1,6 +1,6 @@ repo: RMinte/lpmu-agx-network-setup -ref: main -commit: 9ee3c51de5c9612e6e2c68d271ae329bd67b66ce +ref: fix/detect-lpmu-internal-interface +commit: 3faa1ef4bc144843f5c8b136c095dc2dc79b6f2b archive: lpmu-agx-network-setup.tar.gz -sha256: 77ac9410a62eea50b2a5079d3a7adb1012deee1188c03e0a5cfb4609e4e5f636 -size: 12874 +sha256: b53c20ffd4651a58246a6f06e1ce3f275d0768c1627e80acdcdae7bdc823faf5 +size: 7454 diff --git a/components/ts_api/assets/lpmu-agx-network-setup.sha256 b/components/ts_api/assets/lpmu-agx-network-setup.sha256 new file mode 100644 index 00000000..b19aefd2 --- /dev/null +++ b/components/ts_api/assets/lpmu-agx-network-setup.sha256 @@ -0,0 +1,8 @@ +98370ee1bfadbb6a706b23b9f6699b58b26bb2283da48a2904c5eedd8f3c48e6 lpmu-agx-network-setup/lpmu/install-nat-service.sh +3216b67162c673d4360b42f4a6be1a3b785b1301366f3373936da8f865f00047 lpmu-agx-network-setup/lpmu/install-persistent.sh +9e193cf1cfa2e9b4d0ae9308c847cc2b22a0b865b2fe95113bc600bff6a0980b lpmu-agx-network-setup/lpmu/install-smart-route-service.sh +09a43407e22050f40d9ba797078d5080d3c70c3997d78a096ae6c1332e978895 lpmu-agx-network-setup/lpmu/lpmu-network.sh +a6f6f122011aa40ba564cc760983f66dfc16c93f8ca2253c4a3ca69859aae56f lpmu-agx-network-setup/lpmu/monitor-smart-route.sh +5dc4418b80438a74113bd0dc284e9d40d37ce57c094b216e0f18815e9d594ff8 lpmu-agx-network-setup/lpmu/setup-nat.sh +e8914194864ec1a810877604c3015ccf95ea9578725adac5431ec05e1499be25 lpmu-agx-network-setup/lpmu/setup-smart-route.sh +f57b051fab2b172c42323d96a9f9e175e800f0ccc99e1da28ec066c20790c968 lpmu-agx-network-setup/lpmu/test-nat.sh diff --git a/components/ts_api/assets/lpmu-agx-network-setup.tar.gz b/components/ts_api/assets/lpmu-agx-network-setup.tar.gz index 7106ec03..fcca83a4 100644 Binary files a/components/ts_api/assets/lpmu-agx-network-setup.tar.gz and b/components/ts_api/assets/lpmu-agx-network-setup.tar.gz differ diff --git a/components/ts_api/src/ts_api_lpmu_access.c b/components/ts_api/src/ts_api_lpmu_access.c index 33847e3c..cc225f01 100644 --- a/components/ts_api/src/ts_api_lpmu_access.c +++ b/components/ts_api/src/ts_api_lpmu_access.c @@ -32,6 +32,7 @@ #define LPMU_HOST "10.10.99.99" #define LPMU_PORT 22 #define LPMU_REMOTE_TARBALL "lpmu-agx-network-setup.tar.gz" +#define LPMU_REMOTE_MISMATCH 1 #define LPMU_STACK_SIZE 8192 #define LPMU_OUTPUT_TAIL_MAX 1024 #define LPMU_LOG_PATH "/sdcard/logs/lpmu-access.log" @@ -39,6 +40,8 @@ extern const uint8_t lpmu_archive_start[] asm("_binary_lpmu_agx_network_setup_tar_gz_start"); extern const uint8_t lpmu_archive_end[] asm("_binary_lpmu_agx_network_setup_tar_gz_end"); +extern const char lpmu_checksums_start[] asm("_binary_lpmu_agx_network_setup_sha256_start"); +extern const char lpmu_checksums_end[] asm("_binary_lpmu_agx_network_setup_sha256_end"); typedef enum { LPMU_STAGE_IDLE = 0, @@ -50,7 +53,6 @@ typedef enum { LPMU_STAGE_CHECKING_REMOTE, LPMU_STAGE_UPLOADING, LPMU_STAGE_EXTRACTING, - LPMU_STAGE_CHMOD, LPMU_STAGE_RUNNING_SCRIPT, LPMU_STAGE_SUCCESS, LPMU_STAGE_FAILED, @@ -122,7 +124,6 @@ static const char *lpmu_stage_str(lpmu_stage_t stage) case LPMU_STAGE_CHECKING_REMOTE: return "checking_remote"; case LPMU_STAGE_UPLOADING: return "uploading"; case LPMU_STAGE_EXTRACTING: return "extracting"; - case LPMU_STAGE_CHMOD: return "chmod"; case LPMU_STAGE_RUNNING_SCRIPT: return "running_script"; case LPMU_STAGE_SUCCESS: return "success"; case LPMU_STAGE_FAILED: return "failed"; @@ -244,6 +245,21 @@ static void lpmu_append_tail_locked(const char *data, size_t len) s_status.output_tail[s_status.output_len] = '\0'; } +static bool lpmu_utf8_continuation(char byte) +{ + return ((unsigned char)byte & 0xc0) == 0x80; +} + +static size_t lpmu_complete_utf8_prefix(const char *text, size_t len) +{ + size_t start = len; + while (start && lpmu_utf8_continuation(text[start - 1])) start--; + if (!start) return 0; + unsigned char lead = (unsigned char)text[start - 1]; + size_t width = lead >= 0xf0 ? 4 : lead >= 0xe0 ? 3 : lead >= 0xc0 ? 2 : 1; + return len - (start - 1) < width ? start - 1 : len; +} + static void lpmu_append_log(const char *data, size_t len) { // Only the running task owns this descriptor; another start is excluded. @@ -312,6 +328,18 @@ static esp_err_t lpmu_run_command(ts_ssh_session_t session, const char *command, return ret; } +static esp_err_t lpmu_check_remote(ts_ssh_session_t session, int *exit_code) +{ + // Compare the actual files, then check that the entry point and its NAT dependency can run. + const char *command = "cd \"$HOME\" || exit 2; sha256sum --check --status - && " + "test -x lpmu-agx-network-setup/lpmu/setup-smart-route.sh && " + "test -x lpmu-agx-network-setup/lpmu/setup-nat.sh"; + lpmu_record_command(command); + return ts_ssh_exec_stream_input(session, command, + lpmu_checksums_start, lpmu_checksums_end - lpmu_checksums_start, + lpmu_output_cb, NULL, exit_code); +} + static const char *lpmu_verify_known_host(ts_ssh_session_t session, char *err, size_t err_size) { ts_host_verify_result_t verify_result = TS_HOST_VERIFY_ERROR; @@ -415,15 +443,17 @@ static void lpmu_task(void *arg) } lpmu_set_stage(LPMU_STAGE_CHECKING_REMOTE); - ret = lpmu_run_command(session, - "test -f \"$HOME/lpmu-agx-network-setup/lpmu/setup-smart-route.sh\"", - &exit_code); + ret = lpmu_check_remote(session, &exit_code); if (ret != ESP_OK) { lpmu_fail(ret, exit_code, "access_remote_check_failed", "remote check failed: %s", ts_ssh_get_error(session)); goto cleanup; } + if (exit_code != 0 && exit_code != LPMU_REMOTE_MISMATCH) { + lpmu_fail(ESP_FAIL, exit_code, "access_remote_check_failed", "remote checksum command failed (exit=%d)", exit_code); + goto cleanup; + } - if (exit_code != 0) { + if (exit_code == LPMU_REMOTE_MISMATCH) { lpmu_set_stage(LPMU_STAGE_UPLOADING); lpmu_lock(); s_status.bytes_transferred = 0; @@ -444,7 +474,7 @@ static void lpmu_task(void *arg) lpmu_set_stage(LPMU_STAGE_EXTRACTING); ret = lpmu_run_command(session, "mkdir -p \"$HOME/lpmu-agx-network-setup\" && " - "tar -xzf \"$HOME/lpmu-agx-network-setup.tar.gz\" " + "tar -xpzf \"$HOME/lpmu-agx-network-setup.tar.gz\" " "-C \"$HOME/lpmu-agx-network-setup\" --strip-components=1", &exit_code); if (ret != ESP_OK || exit_code != 0) { @@ -453,17 +483,14 @@ static void lpmu_task(void *arg) ret != ESP_OK ? ts_ssh_get_error(session) : "tar command failed"); goto cleanup; } - } - lpmu_set_stage(LPMU_STAGE_CHMOD); - ret = lpmu_run_command(session, - "chmod +x \"$HOME/lpmu-agx-network-setup/lpmu/\"*.sh", - &exit_code); - if (ret != ESP_OK || exit_code != 0) { - lpmu_fail(ret != ESP_OK ? ret : ESP_FAIL, exit_code, - "access_chmod_failed", "remote chmod failed (exit=%d): %s", exit_code, - ret != ESP_OK ? ts_ssh_get_error(session) : "chmod command failed"); - goto cleanup; + ret = lpmu_check_remote(session, &exit_code); + if (ret != ESP_OK || exit_code != 0) { + lpmu_fail(ret != ESP_OK ? ret : ESP_FAIL, exit_code, + "access_unpack_failed", "remote checksum verification after extract failed (exit=%d): %s", + exit_code, ret != ESP_OK ? ts_ssh_get_error(session) : "installed files did not pass verification"); + goto cleanup; + } } lpmu_set_stage(LPMU_STAGE_RUNNING_SCRIPT); @@ -608,6 +635,13 @@ static esp_err_t api_lpmu_access_status(const cJSON *params, ts_api_result_t *re lpmu_status_t snap = s_status; lpmu_unlock(); + // The byte cap can split the first character, and a live SSH chunk + // can end before the last character is complete. + size_t start = 0; + while (start < snap.output_len && lpmu_utf8_continuation(snap.output_tail[start])) start++; + char *output_tail = snap.output_tail + start; + output_tail[lpmu_complete_utf8_prefix(output_tail, snap.output_len - start)] = '\0'; + cJSON *data = cJSON_CreateObject(); cJSON_AddNumberToObject(data, "run_id", snap.run_id); cJSON_AddBoolToObject(data, "running", snap.running); @@ -623,7 +657,7 @@ static esp_err_t api_lpmu_access_status(const cJSON *params, ts_api_result_t *re cJSON_AddStringToObject(data, "reason", snap.reason ? snap.reason : ""); cJSON_AddStringToObject(data, "failure_stage", snap.reason ? lpmu_stage_str(snap.failure_stage) : ""); cJSON_AddStringToObject(data, "last_error", snap.last_error); - cJSON_AddStringToObject(data, "output_tail", snap.output_tail); + cJSON_AddStringToObject(data, "output_tail", output_tail); cJSON_AddStringToObject(data, "host", LPMU_HOST); cJSON_AddNumberToObject(data, "port", LPMU_PORT); @@ -705,16 +739,9 @@ static esp_err_t api_lpmu_access_log(const cJSON *params, ts_api_result_t *resul size_t count = received > 0 ? (size_t)received : 0; // Keep a UTF-8 character intact across JSON pages. if (!read_failed && count) { - size_t start = count; - while (start && ((unsigned char)chunk[start - 1] & 0xc0) == 0x80) start--; - if (start) { - unsigned char lead = (unsigned char)chunk[start - 1]; - size_t width = lead >= 0xf0 ? 4 : lead >= 0xe0 ? 3 : lead >= 0xc0 ? 2 : 1; - if (count - (start - 1) < width) { - if (position + count == log_len) read_failed = true; - else count = start - 1; - } - } else read_failed = true; + size_t complete = lpmu_complete_utf8_prefix(chunk, count); + if (complete != count && position + count == log_len) read_failed = true; + count = complete; } if (!count && position < log_len) read_failed = true; diff --git a/components/ts_api/src/ts_api_ota.c b/components/ts_api/src/ts_api_ota.c index 46f14571..7812b054 100644 --- a/components/ts_api/src/ts_api_ota.c +++ b/components/ts_api/src/ts_api_ota.c @@ -19,6 +19,7 @@ #include "ts_security.h" #include "nvs_flash.h" #include "nvs.h" +#include "sdkconfig.h" static const char *TAG = "ts_api_ota"; static atomic_flag s_ota_probe_busy = ATOMIC_FLAG_INIT; @@ -26,10 +27,25 @@ static atomic_flag s_ota_probe_busy = ATOMIC_FLAG_INIT; // NVS 存储键 #define NVS_NAMESPACE_OTA "ota_config" #define NVS_KEY_SERVER_URL "server_url" +#define LEGACY_OTA_SERVER_URL "http://a6000nvlink.aoseo.com:15207" -// 服务器 URL 缓存 +// 用户配置的服务器 URL,空字符串表示使用固件默认地址。 static char s_ota_server_url[256] = {0}; +static const char *ota_server_url(void) +{ +#ifdef CONFIG_TS_OTA_DEFAULT_URL + if (!s_ota_server_url[0]) return CONFIG_TS_OTA_DEFAULT_URL; +#endif + return s_ota_server_url; +} + +static bool ota_server_is_legacy(const char *url) +{ + return strcmp(url, LEGACY_OTA_SERVER_URL) == 0 || + strcmp(url, LEGACY_OTA_SERVER_URL "/") == 0; +} + // ============================================================================ // OTA Server Config // ============================================================================ @@ -65,7 +81,7 @@ static esp_err_t load_ota_server_config(void) /** * @brief 保存 OTA 服务器配置到 NVS */ -static esp_err_t save_ota_server_config(void) +static esp_err_t save_ota_server_config(const char *url) { nvs_handle_t handle; esp_err_t ret = nvs_open(NVS_NAMESPACE_OTA, NVS_READWRITE, &handle); @@ -74,8 +90,8 @@ static esp_err_t save_ota_server_config(void) return ret; } - if (s_ota_server_url[0]) { - ret = nvs_set_str(handle, NVS_KEY_SERVER_URL, s_ota_server_url); + if (url[0]) { + ret = nvs_set_str(handle, NVS_KEY_SERVER_URL, url); } else { ret = nvs_erase_key(handle, NVS_KEY_SERVER_URL); if (ret == ESP_ERR_NVS_NOT_FOUND) { @@ -91,6 +107,17 @@ static esp_err_t save_ota_server_config(void) return ret; } +static void init_ota_server_config(void) +{ + if (load_ota_server_config() == ESP_OK && ota_server_is_legacy(s_ota_server_url)) { + s_ota_server_url[0] = '\0'; + if (save_ota_server_config("") != ESP_OK) { + ESP_LOGW(TAG, "Could not clear legacy OTA server URL from NVS"); + } + } + ESP_LOGI(TAG, "OTA server URL: %s", ota_server_url()); +} + /** * @brief API: ota.server.get - 获取 OTA 服务器地址 */ @@ -98,9 +125,10 @@ static esp_err_t api_ota_server_get(const cJSON *params, ts_api_result_t *result { (void)params; + const char *url = ota_server_url(); cJSON *json = cJSON_CreateObject(); - if (!json || !cJSON_AddStringToObject(json, "url", s_ota_server_url) || - !cJSON_AddBoolToObject(json, "configured", s_ota_server_url[0] != '\0')) { + if (!json || !cJSON_AddStringToObject(json, "url", url) || + !cJSON_AddBoolToObject(json, "configured", url[0] != '\0')) { cJSON_Delete(json); ts_api_result_error(result, TS_API_ERR_NO_MEM, "内存不足"); return ESP_ERR_NO_MEM; @@ -130,6 +158,7 @@ static esp_err_t api_ota_server_set(const cJSON *params, ts_api_result_t *result // URL 可以是空字符串(表示清除) const char *url = cJSON_IsString(url_item) ? url_item->valuestring : ""; + if (ota_server_is_legacy(url)) url = ""; // 验证 URL 长度 if (strlen(url) >= sizeof(s_ota_server_url)) { @@ -137,18 +166,12 @@ static esp_err_t api_ota_server_set(const cJSON *params, ts_api_result_t *result return ESP_ERR_INVALID_ARG; } - // 更新缓存 - strncpy(s_ota_server_url, url, sizeof(s_ota_server_url) - 1); - s_ota_server_url[sizeof(s_ota_server_url) - 1] = '\0'; - - ESP_LOGI(TAG, "OTA server configuration updated (configured=%d)", s_ota_server_url[0] != 0); - // 检查是否需要持久化 cJSON *save_item = cJSON_GetObjectItem(params, "save"); bool do_save = save_item && cJSON_IsTrue(save_item); if (do_save) { - esp_err_t ret = save_ota_server_config(); + esp_err_t ret = save_ota_server_config(url); if (ret != ESP_OK) { ts_api_result_error(result, TS_API_ERR_INTERNAL, "保存到 NVS 失败"); return ret; @@ -156,8 +179,11 @@ static esp_err_t api_ota_server_set(const cJSON *params, ts_api_result_t *result ESP_LOGI(TAG, "OTA server URL saved to NVS"); } + strcpy(s_ota_server_url, url); + ESP_LOGI(TAG, "OTA server configuration updated (configured=%d)", s_ota_server_url[0] != 0); + cJSON *json = cJSON_CreateObject(); - cJSON_AddStringToObject(json, "url", s_ota_server_url); + cJSON_AddStringToObject(json, "url", ota_server_url()); cJSON_AddBoolToObject(json, "saved", do_save); ts_api_result_ok(result, json); @@ -688,6 +714,7 @@ static esp_err_t api_ota_www_start(const cJSON *params, ts_api_result_t *result) // Get URL - either from params or use server URL with www.bin suffix cJSON *url_item = cJSON_GetObjectItem(params, "url"); char url[300] = {0}; + const char *server = ota_server_url(); if (url_item && cJSON_IsString(url_item) && strlen(url_item->valuestring) >= sizeof(url)) { ts_api_result_error(result, TS_API_ERR_INVALID_ARG, "URL too long"); @@ -695,9 +722,9 @@ static esp_err_t api_ota_www_start(const cJSON *params, ts_api_result_t *result) } if (url_item && cJSON_IsString(url_item) && url_item->valuestring[0]) { strncpy(url, url_item->valuestring, sizeof(url) - 1); - } else if (s_ota_server_url[0]) { + } else if (server[0]) { // Use server URL and replace firmware.bin with www.bin - strncpy(url, s_ota_server_url, sizeof(url) - 1); + strncpy(url, server, sizeof(url) - 1); // Try to replace common firmware names char *bin_pos = strstr(url, "firmware.bin"); if (bin_pos) { @@ -1131,8 +1158,7 @@ esp_err_t ts_api_ota_register(void) size_t num_endpoints = sizeof(endpoints) / sizeof(endpoints[0]); // 加载保存的配置 - load_ota_server_config(); - ESP_LOGI(TAG, "Loaded OTA server URL: %s", s_ota_server_url[0] ? s_ota_server_url : "(not set)"); + init_ota_server_config(); for (size_t i = 0; i < num_endpoints; i++) { esp_err_t ret = ts_api_register(&endpoints[i]); diff --git a/components/ts_ota/Kconfig b/components/ts_ota/Kconfig index f2d47713..2d7a456e 100644 --- a/components/ts_ota/Kconfig +++ b/components/ts_ota/Kconfig @@ -72,7 +72,7 @@ menu "TianShanOS OTA Configuration" config TS_OTA_DEFAULT_URL string "Default OTA server URL" - default "https://ota.example.com/tianshanos" + default "https://ota.rminte.com" depends on TS_OTA_HTTPS_ENABLED help Default URL for checking and downloading OTA updates. diff --git a/components/ts_security/src/ts_scp.c b/components/ts_security/src/ts_scp.c index 44ce540c..9ac36a10 100644 --- a/components/ts_security/src/ts_scp.c +++ b/components/ts_security/src/ts_scp.c @@ -10,7 +10,7 @@ #include "ts_storage.h" #include "ts_scp.h" -#include "ts_ssh_client.h" +#include "ts_ssh_transport.h" #include "ts_core.h" /* TS_MALLOC_PSRAM */ #include "esp_log.h" #include "freertos/FreeRTOS.h" @@ -347,6 +347,25 @@ esp_err_t ts_scp_recv(ts_ssh_session_t ssh_session, const char *remote_path, return ret; } +static bool scp_write_buffer(ts_ssh_session_t ssh_session, LIBSSH2_CHANNEL *channel, + const char *data, size_t size) +{ + while (size > 0) { + if (ts_ssh_operation_expired(ssh_session)) return false; + ssize_t written = libssh2_channel_write(channel, data, size); + if (written == LIBSSH2_ERROR_EAGAIN || written == 0) { + if (ts_ssh_wait_socket(ssh_session) < 0) return false; + } else if (written < 0) { + ESP_LOGE(TAG, "SCP write error: %zd", written); + return false; + } else { + data += written; + size -= written; + } + } + return true; +} + esp_err_t ts_scp_send_buffer(ts_ssh_session_t ssh_session, const uint8_t *buffer, size_t size, const char *remote_path, int mode) { @@ -372,9 +391,10 @@ esp_err_t ts_scp_send_buffer(ts_ssh_session_t ssh_session, const uint8_t *buffer } ESP_LOGI(TAG, "SCP send buffer: %zu bytes -> %s", size, remote_path); + ts_ssh_operation_begin(ssh_session); /* 启动 SCP 发送 */ - while (1) { + while (!ts_ssh_operation_expired(ssh_session)) { channel = libssh2_scp_send(session, remote_path, mode, size); if (channel) { break; @@ -382,52 +402,57 @@ esp_err_t ts_scp_send_buffer(ts_ssh_session_t ssh_session, const uint8_t *buffer int err = libssh2_session_last_errno(session); if (err == LIBSSH2_ERROR_EAGAIN) { - scp_wait_socket(ssh_session, 100); + if (ts_ssh_wait_socket(ssh_session) < 0) goto failed; continue; } char *errmsg; libssh2_session_last_error(session, &errmsg, NULL, 0); ESP_LOGE(TAG, "Failed to initiate SCP send: %s", errmsg); - return ESP_FAIL; + goto failed; } + if (!channel) goto failed; - /* 写入数据 */ - const char *ptr = (const char *)buffer; - size_t remaining = size; - - while (remaining > 0) { - ssize_t rc = libssh2_channel_write(channel, ptr, remaining); - - if (rc > 0) { - ptr += rc; - remaining -= rc; - } else if (rc == LIBSSH2_ERROR_EAGAIN) { - scp_wait_socket(ssh_session, 100); - continue; - } else { - ESP_LOGE(TAG, "SCP write error: %zd", rc); - goto cleanup; - } + // The SCP protocol uses a zero byte after the file and a zero reply from + // the receiver before either side closes the SSH channel. + if (!scp_write_buffer(ssh_session, channel, (const char *)buffer, size) || + !scp_write_buffer(ssh_session, channel, "", 1)) goto failed; + char ack; + ssize_t received; + for (;;) { + if (ts_ssh_operation_expired(ssh_session)) goto failed; + received = libssh2_channel_read(channel, &ack, 1); + if (received != LIBSSH2_ERROR_EAGAIN && + (received != 0 || libssh2_channel_eof(channel))) break; + if (ts_ssh_wait_socket(ssh_session) < 0) goto failed; } + if (received != 1 || ack != '\0') goto failed; /* 发送 EOF */ - while (libssh2_channel_send_eof(channel) == LIBSSH2_ERROR_EAGAIN) { - scp_wait_socket(ssh_session, 100); - } - - while (libssh2_channel_wait_eof(channel) == LIBSSH2_ERROR_EAGAIN) { - scp_wait_socket(ssh_session, 100); + int (*const finish[])(LIBSSH2_CHANNEL *) = { + libssh2_channel_send_eof, + libssh2_channel_wait_eof, + libssh2_channel_close, + }; + for (size_t i = 0; i < sizeof(finish) / sizeof(finish[0]); i++) { + int rc; + do { + if (ts_ssh_operation_expired(ssh_session)) goto failed; + rc = finish[i](channel); + if (rc == LIBSSH2_ERROR_EAGAIN && ts_ssh_wait_socket(ssh_session) < 0) goto failed; + } while (rc == LIBSSH2_ERROR_EAGAIN); + if (rc != 0) goto failed; } - + + int exit_status = libssh2_channel_get_exit_status(channel); + if (libssh2_channel_free(channel) != 0 || exit_status != 0) goto failed; ESP_LOGI(TAG, "SCP send buffer complete"); - ret = ESP_OK; - -cleanup: - if (channel) { - libssh2_channel_free(channel); - } - + return ESP_OK; + +failed: + if (ts_ssh_operation_expired(ssh_session)) ret = ESP_ERR_TIMEOUT; + // Close the transport as a unit when channel teardown cannot be trusted. + ts_ssh_disconnect(ssh_session); return ret; } diff --git a/components/ts_security/src/ts_ssh_client.c b/components/ts_security/src/ts_ssh_client.c index a8326a2b..16a439a4 100644 --- a/components/ts_security/src/ts_ssh_client.c +++ b/components/ts_security/src/ts_ssh_client.c @@ -8,7 +8,7 @@ * 会话结构和缓冲区优先分配到 PSRAM */ -#include "ts_ssh_client.h" +#include "ts_ssh_transport.h" #include "esp_log.h" #include "esp_timer.h" #include "freertos/FreeRTOS.h" @@ -64,14 +64,18 @@ static bool s_initialized = false; /** * @brief 等待 socket 可读/可写 */ -static bool operation_expired(ts_ssh_session_t s) { +void ts_ssh_operation_begin(ts_ssh_session_t s) { + s->deadline_us = esp_timer_get_time() + (int64_t)s->config.timeout_ms * 1000; +} + +bool ts_ssh_operation_expired(ts_ssh_session_t s) { return atomic_load(&s->abort_flag) || (s->config.cancelled && s->config.cancelled(s->config.cancel_context)) || esp_timer_get_time() >= s->deadline_us; } -static int wait_socket(ts_ssh_session_t s) { - if (operation_expired(s)) +int ts_ssh_wait_socket(ts_ssh_session_t s) { + if (ts_ssh_operation_expired(s)) return -1; int64_t left = s->deadline_us - esp_timer_get_time(); if (left > 100000) @@ -91,7 +95,7 @@ static int wait_socket(ts_ssh_session_t s) { if (dir & LIBSSH2_SESSION_BLOCK_OUTBOUND) FD_SET(s->sock, &wr); int rc = select(s->sock + 1, &rd, &wr, NULL, &timeout); - return (rc < 0 && errno != EINTR) || operation_expired(s) ? -1 : rc; + return (rc < 0 && errno != EINTR) || ts_ssh_operation_expired(s) ? -1 : rc; } /* Teardown cannot wait for a remote peer. These callbacks make all remaining @@ -263,7 +267,7 @@ esp_err_t ts_ssh_connect_with_verifier(ts_ssh_session_t s, ts_ssh_verify_cb_t ve return ESP_OK; if (s->session || s->sock >= 0) ts_ssh_disconnect(s); - s->deadline_us = esp_timer_get_time() + (int64_t)s->config.timeout_ms * 1000; + ts_ssh_operation_begin(s); s->state = TS_SSH_STATE_CONNECTING; s->fail = TS_SSH_FAIL_NONE; esp_err_t ret = ESP_FAIL; @@ -278,7 +282,7 @@ esp_err_t ts_ssh_connect_with_verifier(ts_ssh_session_t s, ts_ssh_verify_cb_t ve } memcpy(&addr.sin_addr, host->h_addr, sizeof(addr.sin_addr)); } - if (operation_expired(s)) + if (ts_ssh_operation_expired(s)) goto timeout; s->sock = socket(AF_INET, SOCK_STREAM, 0); if (s->sock < 0 || fcntl(s->sock, F_SETFL, O_NONBLOCK) < 0) { @@ -291,7 +295,7 @@ esp_err_t ts_ssh_connect_with_verifier(ts_ssh_session_t s, ts_ssh_verify_cb_t ve goto failed; } while (rc) { - int ready = wait_socket(s); + int ready = ts_ssh_wait_socket(s); if (ready < 0) goto timeout; if (!ready) @@ -311,7 +315,7 @@ esp_err_t ts_ssh_connect_with_verifier(ts_ssh_session_t s, ts_ssh_verify_cb_t ve } libssh2_session_set_blocking(s->session, 0); while ((rc = libssh2_session_handshake(s->session, s->sock)) == LIBSSH2_ERROR_EAGAIN) - if (wait_socket(s) < 0) + if (ts_ssh_wait_socket(s) < 0) goto timeout; if (rc) { s->fail = TS_SSH_FAIL_HANDSHAKE; @@ -331,7 +335,7 @@ esp_err_t ts_ssh_connect_with_verifier(ts_ssh_session_t s, ts_ssh_verify_cb_t ve s->fail = TS_SSH_FAIL_HOST_KEY; goto failed; } - if (operation_expired(s)) + if (ts_ssh_operation_expired(s)) goto timeout; s->state = TS_SSH_STATE_AUTHENTICATING; do { @@ -354,7 +358,7 @@ esp_err_t ts_ssh_connect_with_verifier(ts_ssh_session_t s, ts_ssh_verify_cb_t ve ret = ESP_ERR_INVALID_ARG; goto failed; } - if (rc == LIBSSH2_ERROR_EAGAIN && wait_socket(s) < 0) + if (rc == LIBSSH2_ERROR_EAGAIN && ts_ssh_wait_socket(s) < 0) goto timeout; } while (rc == LIBSSH2_ERROR_EAGAIN); if (rc) { @@ -362,7 +366,7 @@ esp_err_t ts_ssh_connect_with_verifier(ts_ssh_session_t s, ts_ssh_verify_cb_t ve s->fail = TS_SSH_FAIL_AUTH; goto failed; } - if (operation_expired(s)) + if (ts_ssh_operation_expired(s)) goto timeout; s->state = TS_SSH_STATE_CONNECTED; return ESP_OK; @@ -433,34 +437,34 @@ static esp_err_t exec_command(ts_ssh_session_t s, const char *command, ts_ssh_ou } if (exit_code) *exit_code = -1; - s->deadline_us = esp_timer_get_time() + (int64_t)s->config.timeout_ms * 1000; + ts_ssh_operation_begin(s); esp_err_t ret = ESP_FAIL; LIBSSH2_CHANNEL *channel = NULL; int rc; - while (!operation_expired(s)) { + while (!ts_ssh_operation_expired(s)) { channel = libssh2_channel_open_session(s->session); if (channel) break; if (libssh2_session_last_errno(s->session) != LIBSSH2_ERROR_EAGAIN) goto failed; - if (wait_socket(s) < 0) + if (ts_ssh_wait_socket(s) < 0) goto timeout; } - if (!channel || operation_expired(s)) + if (!channel || ts_ssh_operation_expired(s)) goto timeout; while ((rc = libssh2_channel_exec(channel, command)) == LIBSSH2_ERROR_EAGAIN) - if (wait_socket(s) < 0) + if (ts_ssh_wait_socket(s) < 0) goto timeout; if (rc) goto failed; if (input) { size_t sent = 0; while (sent < input_len) { - if (operation_expired(s)) + if (ts_ssh_operation_expired(s)) goto timeout; ssize_t written = libssh2_channel_write(channel, input + sent, input_len - sent); if (written == LIBSSH2_ERROR_EAGAIN || written == 0) { - if (wait_socket(s) < 0) + if (ts_ssh_wait_socket(s) < 0) goto timeout; } else if (written < 0) { set_error(s, "Failed to send command input"); @@ -470,7 +474,7 @@ static esp_err_t exec_command(ts_ssh_session_t s, const char *command, ts_ssh_ou } } while ((rc = libssh2_channel_send_eof(channel)) == LIBSSH2_ERROR_EAGAIN) - if (wait_socket(s) < 0) + if (ts_ssh_wait_socket(s) < 0) goto timeout; if (rc) { set_error(s, "Failed to close command input"); @@ -483,7 +487,7 @@ static esp_err_t exec_command(ts_ssh_session_t s, const char *command, ts_ssh_ou bool got_data = false; for (int stream = 0; stream < 2; stream++) { /* One bounded chunk per stream prevents stdout starving stderr/cancel. */ - if (operation_expired(s)) + if (ts_ssh_operation_expired(s)) goto timeout; char buffer[512]; rc = libssh2_channel_read_ex(channel, stream ? SSH_EXTENDED_DATA_STDERR : 0, buffer, @@ -522,11 +526,11 @@ static esp_err_t exec_command(ts_ssh_session_t s, const char *command, ts_ssh_ou /* Drain buffered bytes even when EOF arrived with the last packet. */ if (!got_data && libssh2_channel_eof(channel)) break; - if (!got_data && wait_socket(s) < 0) + if (!got_data && ts_ssh_wait_socket(s) < 0) goto timeout; } while ((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN) - if (wait_socket(s) < 0) + if (ts_ssh_wait_socket(s) < 0) goto timeout; if (rc) goto failed; diff --git a/components/ts_security/src/ts_ssh_transport.h b/components/ts_security/src/ts_ssh_transport.h new file mode 100644 index 00000000..e1d8dd7a --- /dev/null +++ b/components/ts_security/src/ts_ssh_transport.h @@ -0,0 +1,7 @@ +#pragma once + +#include "ts_ssh_client.h" + +void ts_ssh_operation_begin(ts_ssh_session_t session); +bool ts_ssh_operation_expired(ts_ssh_session_t session); +int ts_ssh_wait_socket(ts_ssh_session_t session); diff --git a/components/ts_webui/web/css/style.css b/components/ts_webui/web/css/style.css index 1cc0e80d..31a8d4dc 100644 --- a/components/ts_webui/web/css/style.css +++ b/components/ts_webui/web/css/style.css @@ -367,13 +367,16 @@ input[type=number]::-webkit-inner-spin-button,input[type=number]::-webkit-outer- .tile-i{display:inline-flex;align-items:center;justify-content:center;color:var(--ink-2)} .tile-m{flex:1} .tile-m>div:last-child{margin-top:2px} -.net-grid{display:grid;grid-template-columns:1fr 1fr;gap:16px;flex:1} +.net-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));align-content:start;gap:16px;flex:1} +.net-grid>div{display:flex;flex-direction:column} +.net-grid:has(#net-tab-eth:not(.hidden))>div>.card{flex:1} .sec-t{font-size:17px} +.net-interface>.seg{margin-bottom:12px} +.net-interface .panel-tab.on:focus-visible{outline:2px solid var(--accent-text);outline-offset:2px} .net-wifi{display:grid;gap:12px} -.svc{display:grid;gap:0} +.svc{display:grid;grid-template-columns:minmax(0,1fr);align-content:start;gap:0} .nat-row{display:flex;align-items:center;gap:16px;margin-top:12px} -.net-lpmu{display:flex;align-items:center;justify-content:space-between;padding:16px 20px} -#page-content .net-lpmu .lpmu-access-btn{white-space:nowrap;flex:none} +.net-lpmu{display:flex;flex-wrap:wrap;align-items:center;justify-content:space-between;gap:8px 16px;padding:16px 20px} .dhcp-iface-row{display:flex;align-items:center;gap:8px} select.field{-webkit-appearance:none;appearance:none;padding-right:30px;background-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M.7 1.35 6 6.65l5.3-5.3' fill='none' stroke='%235f6670' stroke-width='1.5'/%3E%3C/svg%3E");background-repeat:no-repeat;background-position:right 9.3px top calc(50% + 1.05px)} .sel{position:relative;padding-right:30px} @@ -639,7 +642,8 @@ input[type=color].swi::-webkit-color-swatch{border:0;border-radius:50%} .card-h{flex-wrap:wrap} .stat{grid-template-columns:repeat(2,1fr);row-gap:12px} .stat>div:nth-child(odd){border-left:0;padding-left:0} -.net-grid{grid-template-columns:1fr} +.net-grid{grid-template-columns:minmax(0,1fr)} +.nat-row{flex-wrap:wrap;gap:8px} .led-f{flex-wrap:wrap} .seg.full:has(>button:nth-child(4)){display:grid;grid-template-columns:repeat(2,minmax(0,1fr));grid-auto-rows:1fr} .seg.full:has(>button:nth-child(4)) button{min-height:34px;padding:5px 8px} diff --git a/components/ts_webui/web/js/app.js b/components/ts_webui/web/js/app.js index 9e938663..75875c6b 100644 --- a/components/ts_webui/web/js/app.js +++ b/components/ts_webui/web/js/app.js @@ -6535,16 +6535,16 @@ async function loadNetworkPage() {
- ${t('network.interfaceConfig')} -
- - -
+ ${t('network.interfaceConfig')}
- - -
-
+
+
+ + +
+ + +
${t('network.linkStatus')}-
${kvRow(t('network.ipAddress'), '-', 'net-eth-ip')} ${kvRow(t('network.subnetMask'), '-', 'net-eth-netmask')} @@ -6552,11 +6552,9 @@ async function loadNetworkPage() { ${kvRow(t('network.dns'), '-', 'net-eth-dns')}
${t('network.mac')}-
-
- - -