diff --git a/main/service/control.c b/main/service/control.c index f0fbf8d..859ae98 100644 --- a/main/service/control.c +++ b/main/service/control.c @@ -1,3 +1,4 @@ +#include "auth.h" #include "cJSON.h" #include "driver/gpio.h" #include "esp_http_server.h" @@ -5,12 +6,12 @@ #include "freertos/FreeRTOS.h" #include "sw.h" #include "webserver.h" -#include "auth.h" static esp_err_t control_get_handler(httpd_req_t* req) { esp_err_t err = api_auth_check(req); - if (err != ESP_OK) { + if (err != ESP_OK) + { return err; } @@ -32,7 +33,8 @@ static esp_err_t control_get_handler(httpd_req_t* req) static esp_err_t control_post_handler(httpd_req_t* req) { esp_err_t err = api_auth_check(req); - if (err != ESP_OK) { + if (err != ESP_OK) + { return err; } diff --git a/main/service/monitor.c b/main/service/monitor.c index 0655ef3..7469dd0 100644 --- a/main/service/monitor.c +++ b/main/service/monitor.c @@ -280,13 +280,14 @@ void init_status_monitor() .name = "sensor_reading_timer"}; const esp_timer_create_args_t wifi_timer_args = {.callback = &status_wifi_callback, .name = "wifi_status_timer"}; const esp_timer_create_args_t long_press_timer_args = {.callback = &long_press_timer_callback, - .name = "long_press_timer"}; + .name = "long_press_timer"}; ESP_ERROR_CHECK(esp_timer_create(&sensor_timer_args, &sensor_timer)); ESP_ERROR_CHECK(esp_timer_create(&wifi_timer_args, &wifi_status_timer)); ESP_ERROR_CHECK(esp_timer_create(&long_press_timer_args, &long_press_timer)); - xTaskCreate(shutdown_load_sw_task, "shutdown_sw_task", configMINIMAL_STACK_SIZE * 3, NULL, 15, &shutdown_task_handle); + xTaskCreate(shutdown_load_sw_task, "shutdown_sw_task", configMINIMAL_STACK_SIZE * 3, NULL, 15, + &shutdown_task_handle); ESP_ERROR_CHECK(esp_timer_start_periodic(sensor_timer, 1000000)); ESP_ERROR_CHECK(esp_timer_start_periodic(wifi_status_timer, 1000000 * 5)); diff --git a/main/service/setting.c b/main/service/setting.c index c47a88f..7e50aff 100644 --- a/main/service/setting.c +++ b/main/service/setting.c @@ -1,4 +1,5 @@ #include +#include "auth.h" #include "cJSON.h" #include "climit.h" #include "esp_http_server.h" @@ -8,14 +9,14 @@ #include "nconfig.h" #include "webserver.h" #include "wifi.h" -#include "auth.h" static const char* TAG = "webserver"; static esp_err_t setting_get_handler(httpd_req_t* req) { esp_err_t err = api_auth_check(req); - if (err != ESP_OK) { + if (err != ESP_OK) + { return err; } @@ -110,7 +111,8 @@ static esp_err_t setting_get_handler(httpd_req_t* req) static esp_err_t wifi_scan(httpd_req_t* req) { esp_err_t err = api_auth_check(req); - if (err != ESP_OK) { + if (err != ESP_OK) + { return err; } @@ -145,7 +147,8 @@ static esp_err_t wifi_scan(httpd_req_t* req) static esp_err_t setting_post_handler(httpd_req_t* req) { esp_err_t err = api_auth_check(req); - if (err != ESP_OK) { + if (err != ESP_OK) + { return err; } diff --git a/main/service/system.c b/main/service/system.c index b55f6a2..d5ca6ff 100644 --- a/main/service/system.c +++ b/main/service/system.c @@ -7,9 +7,9 @@ #include #include #include +#include "auth.h" #include "esp_http_server.h" #include "esp_system.h" -#include "auth.h" static const char* TAG = "odroid"; @@ -52,7 +52,8 @@ void start_reboot_timer(int sec) static esp_err_t reboot_post_handler(httpd_req_t* req) { esp_err_t err = api_auth_check(req); - if (err != ESP_OK) { + if (err != ESP_OK) + { return err; } @@ -87,7 +88,8 @@ void register_reboot_endpoint(httpd_handle_t server) static esp_err_t version_get_handler(httpd_req_t* req) { esp_err_t err = api_auth_check(req); - if (err != ESP_OK) { + if (err != ESP_OK) + { return err; } @@ -103,4 +105,4 @@ void register_version_endpoint(httpd_handle_t server) httpd_uri_t post_uri = { .uri = "/api/version", .method = HTTP_GET, .handler = version_get_handler, .user_ctx = NULL}; httpd_register_uri_handler(server, &post_uri); -} \ No newline at end of file +} diff --git a/main/service/webserver.c b/main/service/webserver.c index e5e5edf..9dbfd23 100644 --- a/main/service/webserver.c +++ b/main/service/webserver.c @@ -1,6 +1,8 @@ #include "webserver.h" #include #include +#include "auth.h" +#include "cJSON.h" #include "esp_http_server.h" #include "esp_log.h" #include "esp_wifi.h" @@ -11,8 +13,6 @@ #include "monitor.h" #include "nconfig.h" #include "system.h" -#include "cJSON.h" -#include "auth.h" static const char* TAG = "WEBSERVER"; @@ -28,9 +28,11 @@ static esp_err_t index_handler(httpd_req_t* req) size_t remaining = index_html_size; const char* ptr = (const char*)index_html_start; - while (remaining > 0) { + while (remaining > 0) + { size_t to_send = remaining < 2048 ? remaining : 2048; - if (httpd_resp_send_chunk(req, ptr, to_send) != ESP_OK) { + if (httpd_resp_send_chunk(req, ptr, to_send) != ESP_OK) + { ESP_LOGE(TAG, "File sending failed!"); httpd_resp_send_chunk(req, NULL, 0); httpd_resp_send_500(req); @@ -48,8 +50,10 @@ static esp_err_t login_handler(httpd_req_t* req) { char content[100]; // Adjust size as needed for username/password int ret = httpd_req_recv(req, content, sizeof(content) - 1); // -1 for null terminator - if (ret <= 0) { // 0 means connection closed, < 0 means error - if (ret == HTTPD_SOCK_ERR_TIMEOUT) { + if (ret <= 0) + { // 0 means connection closed, < 0 means error + if (ret == HTTPD_SOCK_ERR_TIMEOUT) + { httpd_resp_send_408(req); } return ESP_FAIL; @@ -58,33 +62,37 @@ static esp_err_t login_handler(httpd_req_t* req) ESP_LOGI(TAG, "Received login request: %s", content); - cJSON *root = cJSON_Parse(content); - if (root == NULL) { + cJSON* root = cJSON_Parse(content); + if (root == NULL) + { httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid JSON"); return ESP_FAIL; } - cJSON *username_json = cJSON_GetObjectItemCaseSensitive(root, "username"); - cJSON *password_json = cJSON_GetObjectItemCaseSensitive(root, "password"); + cJSON* username_json = cJSON_GetObjectItemCaseSensitive(root, "username"); + cJSON* password_json = cJSON_GetObjectItemCaseSensitive(root, "password"); - if (!cJSON_IsString(username_json) || (username_json->valuestring == NULL) || - !cJSON_IsString(password_json) || (password_json->valuestring == NULL)) { + if (!cJSON_IsString(username_json) || (username_json->valuestring == NULL) || !cJSON_IsString(password_json) || + (password_json->valuestring == NULL)) + { cJSON_Delete(root); httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Missing username or password"); return ESP_FAIL; } - const char *username = username_json->valuestring; - const char *password = password_json->valuestring; + const char* username = username_json->valuestring; + const char* password = password_json->valuestring; // TODO: Implement actual credential validation // For now, a simple hardcoded check - if (strcmp(username, "admin") == 0 && strcmp(password, "password") == 0) { - char *token = auth_generate_token(); - if (token) { - cJSON *response_root = cJSON_CreateObject(); + if (strcmp(username, "admin") == 0 && strcmp(password, "password") == 0) + { + char* token = auth_generate_token(); + if (token) + { + cJSON* response_root = cJSON_CreateObject(); cJSON_AddStringToObject(response_root, "token", token); - char *json_response = cJSON_Print(response_root); + char* json_response = cJSON_Print(response_root); httpd_resp_set_type(req, "application/json"); httpd_resp_sendstr(req, json_response); @@ -92,10 +100,14 @@ static esp_err_t login_handler(httpd_req_t* req) free(token); // Free the token generated by auth_generate_token free(json_response); cJSON_Delete(response_root); - } else { + } + else + { httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to generate token"); } - } else { + } + else + { httpd_resp_send_err(req, HTTPD_401_UNAUTHORIZED, "Invalid credentials"); } diff --git a/main/service/webserver.h b/main/service/webserver.h index eb1e34f..3c32896 100644 --- a/main/service/webserver.h +++ b/main/service/webserver.h @@ -5,9 +5,9 @@ #ifndef ODROID_REMOTE_HTTP_WEBSERVER_H #define ODROID_REMOTE_HTTP_WEBSERVER_H -#include "esp_http_server.h" #include #include +#include "esp_http_server.h" void register_wifi_endpoint(httpd_handle_t server); void register_ws_endpoint(httpd_handle_t server); diff --git a/main/service/ws.c b/main/service/ws.c index 5b57f6f..443f007 100644 --- a/main/service/ws.c +++ b/main/service/ws.c @@ -2,6 +2,7 @@ // Created by shinys on 25. 8. 18.. // +#include "auth.h" #include "driver/uart.h" #include "esp_err.h" #include "esp_http_server.h" @@ -13,7 +14,6 @@ #include "pb_encode.h" #include "status.pb.h" #include "webserver.h" -#include "auth.h" #define UART_NUM UART_NUM_1 #define BUF_SIZE (2048) @@ -210,14 +210,17 @@ static esp_err_t ws_handler(httpd_req_t* req) char* query_str = NULL; size_t query_len = httpd_req_get_url_query_len(req) + 1; - if (query_len > 1) { + if (query_len > 1) + { query_str = malloc(query_len); - if (query_str == NULL) { + if (query_str == NULL) + { ESP_LOGE(TAG, "Failed to allocate memory for query string"); httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Internal Server Error"); return ESP_FAIL; } - if (httpd_req_get_url_query_str(req, query_str, query_len) != ESP_OK) { + if (httpd_req_get_url_query_str(req, query_str, query_len) != ESP_OK) + { ESP_LOGE(TAG, "Failed to get query string from URI: %s", req->uri); free(query_str); httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Internal Server Error"); @@ -229,21 +232,27 @@ static esp_err_t ws_handler(httpd_req_t* req) char token_str[TOKEN_LENGTH]; esp_err_t err = ESP_FAIL; // Default to fail - if (query_str) { + if (query_str) + { err = httpd_query_key_value(query_str, "token", token_str, sizeof(token_str)); free(query_str); // Free allocated query string } - if (err == ESP_OK) { + if (err == ESP_OK) + { ESP_LOGI(TAG, "Token extracted from query string, value: %s", token_str); - if (!auth_validate_token(token_str)) { + if (!auth_validate_token(token_str)) + { ESP_LOGW(TAG, "WebSocket connection attempt with invalid token for URI: %s", req->uri); httpd_resp_send_err(req, HTTPD_401_UNAUTHORIZED, "Invalid or expired token"); return ESP_FAIL; } ESP_LOGD(TAG, "WebSocket token validated for URI: %s", req->uri); - } else { - ESP_LOGW(TAG, "Failed to extract token from query string or query string not found, error: %s", esp_err_to_name(err)); + } + else + { + ESP_LOGW(TAG, "Failed to extract token from query string or query string not found, error: %s", + esp_err_to_name(err)); httpd_resp_send_err(req, HTTPD_401_UNAUTHORIZED, "Authorization token required"); return ESP_FAIL; }