v0.4.1 - Fixed startup bug
This commit is contained in:
113
src/main.c
113
src/main.c
@@ -348,18 +348,18 @@ int init_database(const char* database_path_override) {
|
||||
}
|
||||
|
||||
if (!has_auth_rules) {
|
||||
// Add auth_rules table
|
||||
// Add auth_rules table matching sql_schema.h
|
||||
const char* create_auth_rules_sql =
|
||||
"CREATE TABLE IF NOT EXISTS auth_rules ("
|
||||
" id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||
" rule_type TEXT NOT NULL," // 'pubkey_whitelist', 'pubkey_blacklist', 'hash_blacklist'
|
||||
" operation TEXT NOT NULL," // 'event', 'event_kind_1', etc.
|
||||
" rule_target TEXT NOT NULL," // pubkey, hash, or other identifier
|
||||
" enabled INTEGER DEFAULT 1," // 0 = disabled, 1 = enabled
|
||||
" priority INTEGER DEFAULT 1000," // Lower numbers = higher priority
|
||||
" description TEXT," // Optional description
|
||||
" created_at INTEGER DEFAULT (strftime('%s', 'now')),"
|
||||
" UNIQUE(rule_type, operation, rule_target)"
|
||||
" rule_type TEXT NOT NULL CHECK (rule_type IN ('whitelist', 'blacklist', 'rate_limit', 'auth_required')),"
|
||||
" pattern_type TEXT NOT NULL CHECK (pattern_type IN ('pubkey', 'kind', 'ip', 'global')),"
|
||||
" pattern_value TEXT,"
|
||||
" action TEXT NOT NULL CHECK (action IN ('allow', 'deny', 'require_auth', 'rate_limit')),"
|
||||
" parameters TEXT,"
|
||||
" active INTEGER NOT NULL DEFAULT 1,"
|
||||
" created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now')),"
|
||||
" updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now'))"
|
||||
");";
|
||||
|
||||
char* error_msg = NULL;
|
||||
@@ -373,6 +373,24 @@ int init_database(const char* database_path_override) {
|
||||
return -1;
|
||||
}
|
||||
log_success("Created auth_rules table");
|
||||
|
||||
// Add indexes for auth_rules table
|
||||
const char* create_auth_rules_indexes_sql =
|
||||
"CREATE INDEX IF NOT EXISTS idx_auth_rules_pattern ON auth_rules(pattern_type, pattern_value);"
|
||||
"CREATE INDEX IF NOT EXISTS idx_auth_rules_type ON auth_rules(rule_type);"
|
||||
"CREATE INDEX IF NOT EXISTS idx_auth_rules_active ON auth_rules(active);";
|
||||
|
||||
char* index_error_msg = NULL;
|
||||
int index_rc = sqlite3_exec(g_db, create_auth_rules_indexes_sql, NULL, NULL, &index_error_msg);
|
||||
if (index_rc != SQLITE_OK) {
|
||||
char index_error_log[512];
|
||||
snprintf(index_error_log, sizeof(index_error_log), "Failed to create auth_rules indexes: %s",
|
||||
index_error_msg ? index_error_msg : "unknown error");
|
||||
log_error(index_error_log);
|
||||
if (index_error_msg) sqlite3_free(index_error_msg);
|
||||
return -1;
|
||||
}
|
||||
log_success("Created auth_rules indexes");
|
||||
} else {
|
||||
log_info("auth_rules table already exists, skipping creation");
|
||||
}
|
||||
@@ -1408,35 +1426,43 @@ int main(int argc, char* argv[]) {
|
||||
}
|
||||
|
||||
// Handle configuration setup after database is initialized
|
||||
if (cli_options.admin_pubkey_override && strlen(cli_options.admin_pubkey_override) == 64) {
|
||||
// Admin pubkey provided - populate config table directly
|
||||
log_info("Populating config table for admin pubkey override after database initialization");
|
||||
// Always populate defaults directly in config table (abandoning legacy event signing)
|
||||
log_info("Populating config table with defaults after database initialization");
|
||||
|
||||
// Populate default config values in table
|
||||
if (populate_default_config_values() != 0) {
|
||||
log_error("Failed to populate default config values");
|
||||
cleanup_configuration_system();
|
||||
nostr_cleanup();
|
||||
close_database();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Add pubkeys to config table
|
||||
if (add_pubkeys_to_config_table() != 0) {
|
||||
log_error("Failed to add pubkeys to config table");
|
||||
cleanup_configuration_system();
|
||||
nostr_cleanup();
|
||||
close_database();
|
||||
return 1;
|
||||
}
|
||||
|
||||
log_success("Configuration populated directly in config table after database initialization");
|
||||
} else {
|
||||
// Admin private key available - retry storing initial config event
|
||||
if (retry_store_initial_config_event() != 0) {
|
||||
log_warning("Failed to store initial config event - will retry later");
|
||||
}
|
||||
// Populate default config values in table
|
||||
if (populate_default_config_values() != 0) {
|
||||
log_error("Failed to populate default config values");
|
||||
cleanup_configuration_system();
|
||||
nostr_cleanup();
|
||||
close_database();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Apply CLI overrides now that database is available
|
||||
if (cli_options.port_override > 0) {
|
||||
char port_str[16];
|
||||
snprintf(port_str, sizeof(port_str), "%d", cli_options.port_override);
|
||||
if (update_config_in_table("relay_port", port_str) != 0) {
|
||||
log_error("Failed to update relay port override in config table");
|
||||
cleanup_configuration_system();
|
||||
nostr_cleanup();
|
||||
close_database();
|
||||
return 1;
|
||||
}
|
||||
log_info("Applied port override from command line");
|
||||
printf(" Port: %d (overriding default)\n", cli_options.port_override);
|
||||
}
|
||||
|
||||
// Add pubkeys to config table
|
||||
if (add_pubkeys_to_config_table() != 0) {
|
||||
log_error("Failed to add pubkeys to config table");
|
||||
cleanup_configuration_system();
|
||||
nostr_cleanup();
|
||||
close_database();
|
||||
return 1;
|
||||
}
|
||||
|
||||
log_success("Configuration populated directly in config table after database initialization");
|
||||
|
||||
// Now store the pubkeys in config table since database is available
|
||||
const char* admin_pubkey = get_admin_pubkey_cached();
|
||||
@@ -1539,6 +1565,21 @@ int main(int argc, char* argv[]) {
|
||||
log_warning("No configuration event found in existing database");
|
||||
}
|
||||
|
||||
// Apply CLI overrides for existing relay (port override should work even for existing relays)
|
||||
if (cli_options.port_override > 0) {
|
||||
char port_str[16];
|
||||
snprintf(port_str, sizeof(port_str), "%d", cli_options.port_override);
|
||||
if (update_config_in_table("relay_port", port_str) != 0) {
|
||||
log_error("Failed to update relay port override in config table for existing relay");
|
||||
cleanup_configuration_system();
|
||||
nostr_cleanup();
|
||||
close_database();
|
||||
return 1;
|
||||
}
|
||||
log_info("Applied port override from command line for existing relay");
|
||||
printf(" Port: %d (overriding configured port)\n", cli_options.port_override);
|
||||
}
|
||||
|
||||
// Free memory
|
||||
free(relay_pubkey);
|
||||
for (int i = 0; existing_files[i]; i++) {
|
||||
|
||||
Reference in New Issue
Block a user