v0.7.11 - Got api back working after switching to static build

This commit is contained in:
Your Name
2025-10-12 14:54:02 -04:00
parent 34bb1c34a2
commit 49ffc3d99e
7 changed files with 106 additions and 189 deletions

View File

@@ -227,8 +227,7 @@ static int refresh_unified_cache_from_table(void) {
const char* count_sql = "SELECT COUNT(*) FROM config";
if (sqlite3_prepare_v2(g_db, count_sql, -1, &count_stmt, NULL) == SQLITE_OK) {
if (sqlite3_step(count_stmt) == SQLITE_ROW) {
int row_count = sqlite3_column_int(count_stmt, 0);
printf("[DEBUG] refresh_unified_cache_from_table: Config table row count at start: %d\n", row_count);
// Row count check completed
}
sqlite3_finalize(count_stmt);
}
@@ -372,8 +371,7 @@ static int refresh_unified_cache_from_table(void) {
// Log config table row count at end of refresh_unified_cache_from_table
if (sqlite3_prepare_v2(g_db, count_sql, -1, &count_stmt, NULL) == SQLITE_OK) {
if (sqlite3_step(count_stmt) == SQLITE_ROW) {
int row_count = sqlite3_column_int(count_stmt, 0);
printf("[DEBUG] refresh_unified_cache_from_table: Config table row count at end: %d\n", row_count);
// Row count check completed
}
sqlite3_finalize(count_stmt);
}
@@ -1141,8 +1139,8 @@ cJSON* create_default_config_event(const unsigned char* admin_privkey_bytes,
cJSON* pubkey_obj = cJSON_GetObjectItem(event, "pubkey");
if (id_obj && pubkey_obj) {
printf(" Event ID: %s\n", cJSON_GetStringValue(id_obj));
printf(" Admin Public Key: %s\n", cJSON_GetStringValue(pubkey_obj));
// printf(" Event ID: %s\n", cJSON_GetStringValue(id_obj));
// printf(" Admin Public Key: %s\n", cJSON_GetStringValue(pubkey_obj));
}
return event;
@@ -1308,8 +1306,7 @@ int startup_existing_relay(const char* relay_pubkey) {
const char* count_sql = "SELECT COUNT(*) FROM config";
if (sqlite3_prepare_v2(g_db, count_sql, -1, &count_stmt, NULL) == SQLITE_OK) {
if (sqlite3_step(count_stmt) == SQLITE_ROW) {
int row_count = sqlite3_column_int(count_stmt, 0);
printf("[DEBUG] startup_existing_relay: Config table row count at start: %d\n", row_count);
// Row count check completed
}
sqlite3_finalize(count_stmt);
}
@@ -1347,8 +1344,7 @@ int startup_existing_relay(const char* relay_pubkey) {
const char* count_sql = "SELECT COUNT(*) FROM config";
if (sqlite3_prepare_v2(g_db, count_sql, -1, &count_stmt, NULL) == SQLITE_OK) {
if (sqlite3_step(count_stmt) == SQLITE_ROW) {
int row_count = sqlite3_column_int(count_stmt, 0);
printf("[DEBUG] startup_existing_relay: Config table row count at end: %d\n", row_count);
// Row count check completed
}
sqlite3_finalize(count_stmt);
}
@@ -2168,8 +2164,7 @@ int populate_default_config_values(void) {
const char* count_sql = "SELECT COUNT(*) FROM config";
if (sqlite3_prepare_v2(g_db, count_sql, -1, &count_stmt, NULL) == SQLITE_OK) {
if (sqlite3_step(count_stmt) == SQLITE_ROW) {
int row_count = sqlite3_column_int(count_stmt, 0);
printf("[DEBUG] populate_default_config_values: Config table row count at start: %d\n", row_count);
// Row count check completed
}
sqlite3_finalize(count_stmt);
}
@@ -2309,8 +2304,7 @@ int populate_default_config_values(void) {
// Log config table row count at end of populate_default_config_values
if (sqlite3_prepare_v2(g_db, count_sql, -1, &count_stmt, NULL) == SQLITE_OK) {
if (sqlite3_step(count_stmt) == SQLITE_ROW) {
int row_count = sqlite3_column_int(count_stmt, 0);
printf("[DEBUG] populate_default_config_values: Config table row count at end: %d\n", row_count);
// Row count check completed
}
sqlite3_finalize(count_stmt);
}
@@ -2330,8 +2324,7 @@ int add_pubkeys_to_config_table(void) {
const char* count_sql = "SELECT COUNT(*) FROM config";
if (sqlite3_prepare_v2(g_db, count_sql, -1, &count_stmt, NULL) == SQLITE_OK) {
if (sqlite3_step(count_stmt) == SQLITE_ROW) {
int row_count = sqlite3_column_int(count_stmt, 0);
printf("[DEBUG] add_pubkeys_to_config_table: Config table row count at start: %d\n", row_count);
// Row count check completed
}
sqlite3_finalize(count_stmt);
}
@@ -2428,8 +2421,7 @@ int add_pubkeys_to_config_table(void) {
// Log config table row count at end of add_pubkeys_to_config_table
if (sqlite3_prepare_v2(g_db, count_sql, -1, &count_stmt, NULL) == SQLITE_OK) {
if (sqlite3_step(count_stmt) == SQLITE_ROW) {
int row_count = sqlite3_column_int(count_stmt, 0);
printf("[DEBUG] add_pubkeys_to_config_table: Config table row count at end: %d\n", row_count);
// Row count check completed
}
sqlite3_finalize(count_stmt);
}
@@ -2455,11 +2447,6 @@ int process_admin_event_in_config(cJSON* event, char* error_message, size_t erro
int kind = (int)cJSON_GetNumberValue(kind_obj);
// Get event pubkey for authorization logging
cJSON* pubkey_obj = cJSON_GetObjectItem(event, "pubkey");
const char* event_pubkey = pubkey_obj ? cJSON_GetStringValue(pubkey_obj) : NULL;
// Route to appropriate handler based on kind
switch (kind) {
case 23456: // New ephemeral auth rules management
@@ -2571,16 +2558,12 @@ int process_admin_auth_event(cJSON* event, char* error_message, size_t error_siz
int kind = kind_obj ? (int)cJSON_GetNumberValue(kind_obj) : 0;
// Extract and log additional event details for debugging
cJSON* content_obj = cJSON_GetObjectItem(event, "content");
cJSON* tags_obj = cJSON_GetObjectItem(event, "tags");
const char* event_content = content_obj ? cJSON_GetStringValue(content_obj) : "unknown";
printf(" Content length: %zu\n", event_content ? strlen(event_content) : 0);
printf(" Has tags: %s\n", tags_obj ? "yes" : "no");
if (tags_obj && cJSON_IsArray(tags_obj)) {
printf(" Tags count: %d\n", cJSON_GetArraySize(tags_obj));
}
// printf(" Content length: %zu\n", event_content ? strlen(event_content) : 0);
// printf(" Has tags: %s\n", tags_obj ? "yes" : "no");
// if (tags_obj && cJSON_IsArray(tags_obj)) {
// printf(" Tags count: %d\n", cJSON_GetArraySize(tags_obj));
// }
// Route all Kind 23456 events through the unified handler
if (kind == 23456) {
@@ -2731,8 +2714,8 @@ cJSON* create_admin_response_event(const char* encrypted_content, const char* re
return NULL;
}
printf(" Recipient pubkey: %.16s...\n", recipient_pubkey);
printf(" Encrypted content length: %zu\n", strlen(encrypted_content));
// printf(" Recipient pubkey: %.16s...\n", recipient_pubkey);
// printf(" Encrypted content length: %zu\n", strlen(encrypted_content));
// Get relay private key for signing
char* relay_privkey = get_relay_private_key();
@@ -2789,8 +2772,8 @@ cJSON* create_admin_response_event(const char* encrypted_content, const char* re
cJSON* pubkey_obj = cJSON_GetObjectItem(response_event, "pubkey");
if (id_obj && pubkey_obj) {
printf(" Event ID: %s\n", cJSON_GetStringValue(id_obj));
printf(" Relay pubkey: %.16s...\n", cJSON_GetStringValue(pubkey_obj));
// printf(" Event ID: %s\n", cJSON_GetStringValue(id_obj));
// printf(" Relay pubkey: %.16s...\n", cJSON_GetStringValue(pubkey_obj));
}
return response_event;
@@ -2803,7 +2786,6 @@ char* encrypt_admin_response_content(const cJSON* response_data, const char* rec
return NULL;
}
printf(" Recipient pubkey: %.16s...\n", recipient_pubkey);
// Convert response data to JSON string
char* response_json = cJSON_Print(response_data);
@@ -2812,9 +2794,9 @@ char* encrypt_admin_response_content(const cJSON* response_data, const char* rec
return NULL;
}
printf(" JSON length: %zu\n", strlen(response_json));
printf(" JSON preview: %.100s%s\n", response_json,
strlen(response_json) > 100 ? "..." : "");
// printf(" JSON length: %zu\n", strlen(response_json));
// printf(" JSON preview: %.100s%s\n", response_json,
// strlen(response_json) > 100 ? "..." : "");
// Get relay private key for encryption
char* relay_privkey = get_relay_private_key();
@@ -2867,8 +2849,8 @@ char* encrypt_admin_response_content(const cJSON* response_data, const char* rec
return NULL;
}
printf(" Encrypted content length: %zu\n", strlen(encrypted_content));
printf(" Encrypted preview: %.50s...\n", encrypted_content);
// printf(" Encrypted content length: %zu\n", strlen(encrypted_content));
// printf(" Encrypted preview: %.50s...\n", encrypted_content);
// Return encrypted content as allocated string
return strdup(encrypted_content);
@@ -2883,7 +2865,6 @@ int send_admin_response_event(const cJSON* response_data, const char* recipient_
return -1;
}
printf(" Recipient pubkey: %.16s...\n", recipient_pubkey);
// Step 1: Encrypt response data using NIP-44
char* encrypted_content = encrypt_admin_response_content(response_data, recipient_pubkey);
@@ -2903,7 +2884,7 @@ int send_admin_response_event(const cJSON* response_data, const char* recipient_
cJSON* id_obj = cJSON_GetObjectItem(response_event, "id");
if (id_obj) {
printf(" Event ID: %s\n", cJSON_GetStringValue(id_obj));
// printf(" Event ID: %s\n", cJSON_GetStringValue(id_obj));
}
// Step 3: Store event in database for persistence
@@ -2917,8 +2898,6 @@ int send_admin_response_event(const cJSON* response_data, const char* recipient_
int broadcast_count = broadcast_event_to_subscriptions(response_event);
if (broadcast_count >= 0) {
printf(" Event kind: 23457 (admin response)\n");
printf(" Subscriptions notified: %d\n", broadcast_count);
// Clean up and return success - event creation succeeded regardless of broadcast count
cJSON_Delete(response_event);
@@ -2995,7 +2974,32 @@ int handle_kind_23456_unified(cJSON* event, char* error_message, size_t error_si
snprintf(error_message, error_size, "invalid: null event");
return -1;
}
// Verify the event sender is the authorized admin
cJSON* pubkey_obj = cJSON_GetObjectItem(event, "pubkey");
if (!pubkey_obj || !cJSON_IsString(pubkey_obj)) {
log_error("invalid: missing sender pubkey in event");
snprintf(error_message, error_size, "invalid: missing sender pubkey in event");
return -1;
}
const char* sender_pubkey = cJSON_GetStringValue(pubkey_obj);
const char* admin_pubkey = get_admin_pubkey_cached();
if (!admin_pubkey) {
log_error("error: admin pubkey not available for authorization check");
snprintf(error_message, error_size, "error: admin pubkey not available for authorization check");
return -1;
}
if (strcmp(sender_pubkey, admin_pubkey) != 0) {
log_error("invalid: unauthorized admin event - sender pubkey does not match admin pubkey");
printf(" Sender pubkey: %.16s...\n", sender_pubkey);
printf(" Admin pubkey: %.16s...\n", admin_pubkey);
snprintf(error_message, error_size, "invalid: unauthorized admin event");
return -1;
}
// Check if content is encrypted (NIP-44)
cJSON* content_obj = cJSON_GetObjectItem(event, "content");
if (!content_obj || !cJSON_IsString(content_obj)) {
@@ -3280,7 +3284,7 @@ int handle_auth_query_unified(cJSON* event, const char* query_type, char* error_
}
int rule_count = 0;
printf("=== Auth Query Results (%s) ===\n", query_type);
// printf("=== Auth Query Results (%s) ===\n", query_type);
while (sqlite3_step(stmt) == SQLITE_ROW) {
const char* rule_type = (const char*)sqlite3_column_text(stmt, 0);
@@ -3288,11 +3292,11 @@ int handle_auth_query_unified(cJSON* event, const char* query_type, char* error_
const char* pattern_value_result = (const char*)sqlite3_column_text(stmt, 2);
const char* action = (const char*)sqlite3_column_text(stmt, 3);
printf(" %s %s:%s -> %s\n",
rule_type ? rule_type : "",
pattern_type ? pattern_type : "",
pattern_value_result ? pattern_value_result : "",
action ? action : "allow");
// printf(" %s %s:%s -> %s\n",
// rule_type ? rule_type : "",
// pattern_type ? pattern_type : "",
// pattern_value_result ? pattern_value_result : "",
// action ? action : "allow");
// Add rule to results array
cJSON* rule_obj = cJSON_CreateObject();
@@ -3324,8 +3328,6 @@ int handle_auth_query_unified(cJSON* event, const char* query_type, char* error_
// Send response as signed kind 23457 event
if (send_admin_response_event(response, admin_pubkey, wsi) == 0) {
printf("Total results: %d\n", rule_count);
printf(" Response query_type: %s (mapped from %s)\n", mapped_query_type, query_type);
cJSON_Delete(response);
cJSON_Delete(results_array);
return 0;
@@ -3401,7 +3403,6 @@ int handle_config_query_unified(cJSON* event, const char* query_type, char* erro
}
int config_count = 0;
printf("=== Config Query Results (%s) ===\n", query_type);
while (sqlite3_step(stmt) == SQLITE_ROW) {
const char* key = (const char*)sqlite3_column_text(stmt, 0);
@@ -3410,11 +3411,6 @@ int handle_config_query_unified(cJSON* event, const char* query_type, char* erro
const char* category = (const char*)sqlite3_column_text(stmt, 3);
const char* description = (const char*)sqlite3_column_text(stmt, 4);
printf(" %s = %s [%s] (%s)\n",
key ? key : "",
value ? value : "",
data_type ? data_type : "string",
category ? category : "general");
// Add config item to results array
cJSON* config_obj = cJSON_CreateObject();
@@ -3447,8 +3443,6 @@ int handle_config_query_unified(cJSON* event, const char* query_type, char* erro
// Send response as signed kind 23457 event
if (send_admin_response_event(response, admin_pubkey, wsi) == 0) {
printf("Total results: %d\n", config_count);
printf(" Response query_type: %s (mapped from %s)\n", mapped_query_type, query_type);
cJSON_Delete(response);
cJSON_Delete(results_array);
return 0;
@@ -3519,7 +3513,6 @@ int handle_config_set_unified(cJSON* event, const char* config_key, const char*
cJSON_AddStringToObject(response, "status", "success");
cJSON_AddNumberToObject(response, "timestamp", (double)time(NULL));
printf("Updated config: %s = %s\n", config_key, config_value);
// Get admin pubkey from event for response
cJSON* pubkey_obj = cJSON_GetObjectItem(event, "pubkey");
@@ -3584,7 +3577,6 @@ int handle_system_command_unified(cJSON* event, const char* command, char* error
cJSON_AddStringToObject(response, "status", "success");
cJSON_AddNumberToObject(response, "timestamp", (double)time(NULL));
printf("Cleared %d auth rules from database\n", rule_count);
// Get admin pubkey from event for response
cJSON* pubkey_obj = cJSON_GetObjectItem(event, "pubkey");
@@ -3662,7 +3654,6 @@ int handle_system_command_unified(cJSON* event, const char* command, char* error
cJSON_AddStringToObject(response, "status", "success");
cJSON_AddNumberToObject(response, "timestamp", (double)time(NULL));
printf("Deleted auth rule: %s %s:%s\n", rule_type, pattern_type, pattern_value);
// Get admin pubkey from event for response
cJSON* pubkey_obj = cJSON_GetObjectItem(event, "pubkey");
@@ -4001,10 +3992,6 @@ int handle_stats_query_unified(cJSON* event, char* error_message, size_t error_s
sqlite3_finalize(stmt);
}
printf("=== Database Statistics ===\n");
printf("Database size: %lld bytes\n", db_size);
printf("Event kinds: %d\n", cJSON_GetArraySize(event_kinds));
printf("Top pubkeys: %d\n", cJSON_GetArraySize(top_pubkeys));
// Get admin pubkey from event for response
cJSON* pubkey_obj = cJSON_GetObjectItem(event, "pubkey");
@@ -4381,7 +4368,6 @@ int handle_config_update_unified(cJSON* event, char* error_message, size_t error
cJSON_AddNumberToObject(response, "timestamp", (double)time(NULL));
cJSON_AddItemToObject(response, "processed_configs", processed_configs);
printf("Config update completed: %d/%d configs updated successfully\n", updates_applied, config_count);
// Get admin pubkey from event for response
cJSON* pubkey_obj = cJSON_GetObjectItem(event, "pubkey");