Skip to content

Commit 760843d

Browse files
committed
Syslog and timezones
1 parent 33fdc9f commit 760843d

21 files changed

Lines changed: 774 additions & 52 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# in this exact order for cmake to work correctly
33
cmake_minimum_required(VERSION 3.16)
44

5-
set(PROJECT_VER "2.3.1")
5+
set(PROJECT_VER "2.4.0")
66
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
77
project(esp32_nat_router)
88

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
idf_component_register(SRCS "cmd_router.c"
22
INCLUDE_DIRS . "${CMAKE_SOURCE_DIR}/include"
3-
REQUIRES console nvs_flash esp_wifi driver spi_flash pcap_capture acl remote_console oled_display mbedtls app_update)
3+
REQUIRES console nvs_flash esp_wifi driver spi_flash pcap_capture acl remote_console syslog oled_display mbedtls app_update)

components/cmd_router/cmd_router.c

Lines changed: 149 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
*/
77

88
#include <stdio.h>
9+
#include <stdlib.h>
910
#include <string.h>
1011
#include <ctype.h>
12+
#include <time.h>
1113
#include <inttypes.h>
1214
#include <stdint.h>
1315
#include "esp_log.h"
@@ -39,6 +41,7 @@
3941
#include "pcap_capture.h"
4042
#include "acl.h"
4143
#include "remote_console.h"
44+
#include "syslog_client.h"
4245
#include "oled_display.h"
4346
#include "esp_ota_ops.h"
4447
#include "esp_app_desc.h"
@@ -81,6 +84,7 @@ static void register_set_rf_switch(void);
8184
#endif
8285
static void register_acl(void);
8386
static void register_remote_console_cmd(void);
87+
static void register_syslog_cmd(void);
8488
#ifdef CONFIG_IDF_TARGET_ESP32C3
8589
static void register_set_oled(void);
8690
static void register_set_oled_gpio(void);
@@ -89,6 +93,7 @@ static void register_set_oled_gpio(void);
8993
static void register_scan(void);
9094
#endif
9195
static void register_set_vpn(void);
96+
static void register_set_tz(void);
9297

9398
/* ACL helper functions (forward declarations) */
9499
static char* acl_format_ip_with_name(uint32_t ip, uint32_t mask, char* buf, size_t buf_len);
@@ -297,6 +302,8 @@ void register_router(void)
297302
register_set_rf_switch();
298303
#endif
299304
register_remote_console_cmd();
305+
register_syslog_cmd();
306+
register_set_tz();
300307
register_set_vpn();
301308
#ifdef CONFIG_IDF_TARGET_ESP32C3
302309
register_set_oled();
@@ -865,13 +872,13 @@ static int web_ui_cmd(int argc, char **argv)
865872
if (strcmp(action, "enable") == 0) {
866873
err = set_config_param_str("web_disabled", "0");
867874
if (err == ESP_OK) {
868-
ESP_LOGI(TAG, "Web interface enabled.");
875+
ESP_LOGW(TAG, "Web interface enabled via CLI.");
869876
printf("Web interface will be enabled after reboot.\n");
870877
}
871878
} else if (strcmp(action, "disable") == 0) {
872879
err = set_config_param_str("web_disabled", "1");
873880
if (err == ESP_OK) {
874-
ESP_LOGI(TAG, "Web interface disabled.");
881+
ESP_LOGW(TAG, "Web interface disabled via CLI.");
875882
printf("Web interface will be disabled after reboot.\n");
876883
printf("Use 'web_ui enable' to re-enable it.\n");
877884
}
@@ -1042,10 +1049,10 @@ static int set_router_password_cmd(int argc, char **argv)
10421049
esp_err_t err = set_web_password_hashed(argv[1]);
10431050
if (err == ESP_OK) {
10441051
if (argv[1][0] == '\0') {
1045-
ESP_LOGI(TAG, "Web password protection disabled.");
1052+
ESP_LOGW(TAG, "Web password protection disabled via CLI.");
10461053
printf("Password protection disabled.\n");
10471054
} else {
1048-
ESP_LOGI(TAG, "Web password updated.");
1055+
ESP_LOGW(TAG, "Web password changed via CLI.");
10491056
printf("Password updated successfully.\n");
10501057
}
10511058
} else {
@@ -2417,6 +2424,7 @@ static int acl_cmd(int argc, char **argv)
24172424
if (strcmp(argv[2], "clear") == 0) {
24182425
acl_clear(list_no);
24192426
save_acl_rules();
2427+
ESP_LOGW(TAG, "ACL list %s cleared via CLI.", acl_get_name(list_no));
24202428
printf("ACL list %s cleared.\n", acl_get_name(list_no));
24212429
return 0;
24222430
}
@@ -2441,6 +2449,7 @@ static int acl_cmd(int argc, char **argv)
24412449
}
24422450
if (acl_delete(list_no, idx)) {
24432451
save_acl_rules();
2452+
ESP_LOGW(TAG, "ACL rule %d deleted from %s via CLI.", idx, acl_get_name(list_no));
24442453
printf("Deleted rule %d from %s\n", idx, acl_get_name(list_no));
24452454
} else {
24462455
printf("No rule at index %d\n", idx);
@@ -2543,6 +2552,7 @@ static int acl_cmd(int argc, char **argv)
25432552
/* Add the rule */
25442553
if (acl_add(list_no, src_ip, src_mask, dst_ip, dst_mask, proto, s_port, d_port, allow)) {
25452554
save_acl_rules();
2555+
ESP_LOGW(TAG, "ACL rule added to %s via CLI.", acl_get_name(list_no));
25462556
printf("Rule added to %s\n", acl_get_name(list_no));
25472557
} else {
25482558
printf("Failed to add rule (list may be full)\n");
@@ -2621,13 +2631,15 @@ static int remote_console_cmd(int argc, char **argv)
26212631
} else if (strcmp(action, "enable") == 0) {
26222632
esp_err_t err = remote_console_enable();
26232633
if (err == ESP_OK) {
2634+
ESP_LOGW(TAG, "Remote console enabled via CLI.");
26242635
printf("Remote console enabled.\n");
26252636
} else {
26262637
printf("Error: %s\n", esp_err_to_name(err));
26272638
}
26282639

26292640
} else if (strcmp(action, "disable") == 0) {
26302641
remote_console_disable();
2642+
ESP_LOGW(TAG, "Remote console disabled via CLI.");
26312643
printf("Remote console disabled.\n");
26322644

26332645
} else if (strcmp(action, "port") == 0) {
@@ -2641,6 +2653,7 @@ static int remote_console_cmd(int argc, char **argv)
26412653
return 1;
26422654
}
26432655
remote_console_set_port((uint16_t)port);
2656+
ESP_LOGW(TAG, "Remote console port changed to %d via CLI.", port);
26442657
printf("Port set to %d. Restart or disable/enable to apply.\n", port);
26452658

26462659
} else if (strcmp(action, "bind") == 0) {
@@ -2669,6 +2682,7 @@ static int remote_console_cmd(int argc, char **argv)
26692682
return 1;
26702683
}
26712684
remote_console_set_bind(bind);
2685+
ESP_LOGW(TAG, "Remote console bind changed to %s via CLI.", argv[2]);
26722686
printf("Bind set. Restart or disable/enable to apply.\n");
26732687

26742688
} else if (strcmp(action, "timeout") == 0) {
@@ -2678,11 +2692,13 @@ static int remote_console_cmd(int argc, char **argv)
26782692
}
26792693
uint32_t timeout = (uint32_t)atoi(argv[2]);
26802694
remote_console_set_timeout(timeout);
2695+
ESP_LOGI(TAG, "Remote console timeout changed to %lu sec via CLI.", (unsigned long)timeout);
26812696
printf("Timeout set to %lu seconds.\n", (unsigned long)timeout);
26822697

26832698
} else if (strcmp(action, "kick") == 0) {
26842699
esp_err_t err = remote_console_kick();
26852700
if (err == ESP_OK) {
2701+
ESP_LOGW(TAG, "Remote console session kicked via CLI.");
26862702
printf("Kick request sent.\n");
26872703
} else {
26882704
printf("No active session to kick.\n");
@@ -2714,6 +2730,135 @@ static void register_remote_console_cmd(void)
27142730
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
27152731
}
27162732

2733+
/* 'syslog' command - configure remote syslog forwarding */
2734+
static int syslog_cmd(int argc, char **argv)
2735+
{
2736+
if (argc < 2) {
2737+
printf("Usage: syslog <action> [args]\n");
2738+
printf(" status - Show syslog configuration\n");
2739+
printf(" enable <server> [<port>] - Enable syslog (default port 514)\n");
2740+
printf(" disable - Disable syslog forwarding\n");
2741+
return 0;
2742+
}
2743+
2744+
const char *action = argv[1];
2745+
2746+
if (strcmp(action, "status") == 0) {
2747+
bool enabled;
2748+
char server[SYSLOG_MAX_SERVER_LEN];
2749+
uint16_t port;
2750+
syslog_get_config(&enabled, server, sizeof(server), &port);
2751+
2752+
printf("Syslog Status:\n");
2753+
printf("==============\n");
2754+
printf("Enabled: %s\n", enabled ? "yes" : "no");
2755+
printf("Server: %s\n", server[0] ? server : "(not set)");
2756+
printf("Port: %u\n", port);
2757+
2758+
} else if (strcmp(action, "enable") == 0) {
2759+
if (argc < 3) {
2760+
printf("Usage: syslog enable <server> [<port>]\n");
2761+
return 1;
2762+
}
2763+
const char *server = argv[2];
2764+
uint16_t port = SYSLOG_DEFAULT_PORT;
2765+
if (argc >= 4) {
2766+
int p = atoi(argv[3]);
2767+
if (p < 1 || p > 65535) {
2768+
printf("Invalid port number (1-65535)\n");
2769+
return 1;
2770+
}
2771+
port = (uint16_t)p;
2772+
}
2773+
esp_err_t err = syslog_enable(server, port);
2774+
if (err == ESP_OK) {
2775+
ESP_LOGW(TAG, "Syslog enabled: %s:%u via CLI.", server, port);
2776+
printf("Syslog enabled: %s:%u\n", server, port);
2777+
} else {
2778+
printf("Error: %s\n", esp_err_to_name(err));
2779+
}
2780+
2781+
} else if (strcmp(action, "disable") == 0) {
2782+
syslog_disable();
2783+
ESP_LOGW(TAG, "Syslog disabled via CLI.");
2784+
printf("Syslog disabled.\n");
2785+
2786+
} else {
2787+
printf("Unknown action: %s\n", action);
2788+
return 1;
2789+
}
2790+
2791+
return 0;
2792+
}
2793+
2794+
static void register_syslog_cmd(void)
2795+
{
2796+
const esp_console_cmd_t cmd = {
2797+
.command = "syslog",
2798+
.help = "Manage remote syslog forwarding\n"
2799+
" syslog status - Show syslog configuration\n"
2800+
" syslog enable <server> [<port>] - Enable syslog (default port 514)\n"
2801+
" syslog disable - Disable syslog forwarding",
2802+
.hint = " <action> [<args>]",
2803+
.func = &syslog_cmd,
2804+
};
2805+
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
2806+
}
2807+
2808+
/* 'set_tz' command - set timezone */
2809+
static int set_tz_cmd(int argc, char **argv)
2810+
{
2811+
if (argc < 2) {
2812+
const char *tz = getenv("TZ");
2813+
printf("Timezone: %s\n", tz ? tz : "(not set, using UTC)");
2814+
printf("\nUsage: set_tz <POSIX TZ string>\n");
2815+
printf("Examples:\n");
2816+
printf(" set_tz UTC - UTC\n");
2817+
printf(" set_tz CET-1CEST,M3.5.0,M10.5.0/3 - Central Europe\n");
2818+
printf(" set_tz EST5EDT,M3.2.0,M11.1.0 - US Eastern\n");
2819+
printf(" set_tz PST8PDT,M3.2.0,M11.1.0 - US Pacific\n");
2820+
printf(" set_tz clear - Clear timezone (revert to UTC)\n");
2821+
return 0;
2822+
}
2823+
2824+
if (strcmp(argv[1], "clear") == 0) {
2825+
unsetenv("TZ");
2826+
tzset();
2827+
set_config_param_str("tz", "");
2828+
printf("Timezone cleared (using UTC).\n");
2829+
return 0;
2830+
}
2831+
2832+
setenv("TZ", argv[1], 1);
2833+
tzset();
2834+
set_config_param_str("tz", argv[1]);
2835+
2836+
/* Show current time to confirm */
2837+
time_t now = time(NULL);
2838+
struct tm tm_info;
2839+
localtime_r(&now, &tm_info);
2840+
char buf[32];
2841+
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S %Z", &tm_info);
2842+
printf("Timezone set to: %s\n", argv[1]);
2843+
printf("Current time: %s\n", buf);
2844+
2845+
return 0;
2846+
}
2847+
2848+
static void register_set_tz(void)
2849+
{
2850+
const esp_console_cmd_t cmd = {
2851+
.command = "set_tz",
2852+
.help = "Set timezone (POSIX TZ string)\n"
2853+
" set_tz - Show current timezone\n"
2854+
" set_tz <TZ string> - Set timezone\n"
2855+
" set_tz clear - Clear timezone (revert to UTC)",
2856+
.hint = " <TZ string>",
2857+
.func = &set_tz_cmd,
2858+
};
2859+
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
2860+
}
2861+
27172862
#ifdef CONFIG_IDF_TARGET_ESP32C3
27182863

27192864
/* 'set_oled' command - enable/disable OLED display */

components/cmd_system/cmd_system.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ static int factory_reset(int argc, char **argv)
163163
ESP_LOGE(TAG, "Failed to erase NVS namespace: %s", esp_err_to_name(err));
164164
return 1;
165165
}
166-
ESP_LOGI(TAG, "NVS namespace '%s' erased, restarting...", PARAM_NAMESPACE);
166+
ESP_LOGW(TAG, "Factory reset: NVS namespace '%s' erased, restarting...", PARAM_NAMESPACE);
167167
esp_restart();
168168
return 0; // Never reached
169169
}

0 commit comments

Comments
 (0)