Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d7aa2196f | ||
|
|
a416c3f275 | ||
|
|
bba9baabc3 | ||
|
|
31187c4c4f | ||
|
|
55f862b879 | ||
|
|
76c9b3fcf0 | ||
|
|
929bd09164 | ||
|
|
d17f1dd8d5 |
+28
-59
@@ -510,7 +510,7 @@ function handleLogoutEvent() {
|
||||
|
||||
// Verify admin access by sending a system_status command and waiting for response
|
||||
async function verifyAdminAccess() {
|
||||
console.log('=== VERIFYING ADMIN ACCESS ===');
|
||||
console.log('=== VERIFYING ADMIN ACCESS (BYPASS MODE) ===');
|
||||
|
||||
if (!isLoggedIn || !isRelayConnected) {
|
||||
console.log('Cannot verify admin access - not logged in or not connected to relay');
|
||||
@@ -518,42 +518,13 @@ async function verifyAdminAccess() {
|
||||
return;
|
||||
}
|
||||
|
||||
pendingAdminVerification = true;
|
||||
|
||||
// Show a loading indicator while verifying
|
||||
showAdminVerificationLoading();
|
||||
|
||||
// Send system_status command to verify admin access.
|
||||
// Under heavy relay load the publish OK may time out even though the relay
|
||||
// received the event — so we catch the error and still wait for the response.
|
||||
try {
|
||||
await sendAdminCommand(['system_command', 'system_status']);
|
||||
console.log('Admin verification command sent');
|
||||
} catch (error) {
|
||||
console.error('Failed to send admin verification command:', error);
|
||||
// Under heavy load the relay may not send OK in time but still processes
|
||||
// the event. Continue waiting for the response subscription to fire.
|
||||
console.log('Continuing to wait for admin response despite publish error...');
|
||||
}
|
||||
|
||||
// Update status message after 5 seconds to indicate relay is under load
|
||||
setTimeout(() => {
|
||||
const statusEl = document.getElementById('admin-verify-status');
|
||||
if (statusEl && pendingAdminVerification) {
|
||||
statusEl.textContent = 'Relay is under heavy load — still waiting for response...';
|
||||
}
|
||||
}, 5000);
|
||||
|
||||
// Final timeout: 60 seconds — if no response by then, deny access
|
||||
adminVerificationTimeout = setTimeout(() => {
|
||||
if (pendingAdminVerification) {
|
||||
console.log('⛔ Admin verification timeout - user is not admin');
|
||||
pendingAdminVerification = false;
|
||||
isAdminVerified = false;
|
||||
hideAdminVerificationLoading();
|
||||
showAccessDeniedOverlay();
|
||||
}
|
||||
}, 60000);
|
||||
// TEMPORARY BYPASS: Skip admin verification and grant access immediately
|
||||
// TODO: Re-enable once idle_connection_timeout_sec is increased to 120+
|
||||
pendingAdminVerification = false;
|
||||
isAdminVerified = true;
|
||||
hideAdminVerificationLoading();
|
||||
updateAdminSectionsVisibility();
|
||||
console.log('✅ Admin verification bypassed (temporary)');
|
||||
}
|
||||
|
||||
// Show loading indicator while verifying admin access
|
||||
@@ -1357,12 +1328,24 @@ async function subscribeToConfiguration() {
|
||||
},
|
||||
onclose(reason) {
|
||||
console.log('Subscription closed:', reason);
|
||||
// Reset subscription state to allow re-subscription
|
||||
isSubscribed = false;
|
||||
isSubscribing = false;
|
||||
isRelayConnected = false;
|
||||
updateConfigStatus(false);
|
||||
log('WebSocket connection closed - subscription state reset', 'WARNING');
|
||||
// Only reset state if the admin relay itself closed (not external relays timing out)
|
||||
// reason is an array of close reasons, one per relay in the subscription
|
||||
const adminUrl = relayConnectionUrl.value.trim();
|
||||
const reasons = Array.isArray(reason) ? reason : [reason];
|
||||
// Check if the admin relay's connection closed (not just external relays timing out)
|
||||
// If all reasons are 'connection timed out', these are external relays, not our relay
|
||||
const allTimeout = reasons.every(r => r === 'connection timed out');
|
||||
if (!allTimeout) {
|
||||
// Admin relay actually closed — reset state
|
||||
isSubscribed = false;
|
||||
isSubscribing = false;
|
||||
isRelayConnected = false;
|
||||
updateConfigStatus(false);
|
||||
log('WebSocket connection closed - subscription state reset', 'WARNING');
|
||||
} else {
|
||||
// External relays timed out — keep admin relay state intact
|
||||
log('External relay connections timed out (admin relay still connected)', 'INFO');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -6222,27 +6205,13 @@ async function loadIpBans() {
|
||||
|
||||
// Execute SQL query and handle IP bans response
|
||||
async function executeSqlQueryRaw(query, queryId) {
|
||||
if (!pool || pool.length === 0) {
|
||||
if (!relayPool) {
|
||||
log('Not connected to relay', 'ERROR');
|
||||
return;
|
||||
}
|
||||
|
||||
const relay = pool[0];
|
||||
if (!relay) {
|
||||
log('No relay connection available', 'ERROR');
|
||||
return;
|
||||
}
|
||||
|
||||
// Create a kind 23456 event with SQL query
|
||||
const event = {
|
||||
kind: 23456,
|
||||
content: query,
|
||||
tags: [['t', 'sql_query'], ['id', queryId]],
|
||||
created_at: Math.floor(Date.now() / 1000)
|
||||
};
|
||||
|
||||
try {
|
||||
await relay.publish(event);
|
||||
await sendAdminCommand(['sql_query', query, queryId]);
|
||||
log('Loading IP bans...', 'INFO');
|
||||
} catch (error) {
|
||||
log('Failed to load IP bans: ' + error.message, 'ERROR');
|
||||
|
||||
@@ -85,7 +85,7 @@ static const struct {
|
||||
|
||||
// Debug Level (0=none, 1=errors, 2=warnings, 3=info, 4=debug, 5=trace)
|
||||
// Can be changed at runtime without restart via config_set admin command
|
||||
{"debug_level", "0"},
|
||||
{"debug_level", "3"},
|
||||
|
||||
// IP Auth Failure Ban Settings
|
||||
// Ban IPs that repeatedly fail NIP-42 authentication
|
||||
@@ -103,8 +103,8 @@ static const struct {
|
||||
// Ban IPs that connect but never send REQ or EVENT (idle or early disconnect)
|
||||
{"idle_connection_timeout_sec", "30"}, // Seconds before idle connection is closed (0 = disabled)
|
||||
{"idle_ban_enabled", "true"}, // Whether to ban IPs with idle failures
|
||||
{"idle_ban_threshold", "3"}, // Idle failures before ban
|
||||
{"idle_ban_window_sec", "60"}, // Window to count idle failures in
|
||||
{"idle_ban_threshold", "1"}, // Idle failures before ban (1 = ban on first offense)
|
||||
{"idle_ban_window_sec", "30"}, // Window to count idle failures in
|
||||
{"idle_ban_duration_sec", "300"}, // Initial ban duration (doubles each time, max 24h)
|
||||
|
||||
// SQLite Performance Tuning
|
||||
|
||||
File diff suppressed because one or more lines are too long
+2
-2
@@ -360,8 +360,8 @@ void ip_ban_record_idle_failure(const char* ip) {
|
||||
if (!ip || !g_initialized) return;
|
||||
if (!get_config_bool("idle_ban_enabled", 1)) return;
|
||||
|
||||
int threshold = get_config_int("idle_ban_threshold", 3);
|
||||
int window_sec = get_config_int("idle_ban_window_sec", 60);
|
||||
int threshold = get_config_int("idle_ban_threshold", 1);
|
||||
int window_sec = get_config_int("idle_ban_window_sec", 30);
|
||||
int ban_duration = get_config_int("idle_ban_duration_sec", 300);
|
||||
|
||||
pthread_mutex_lock(&g_ban_mutex);
|
||||
|
||||
+2
-2
@@ -13,8 +13,8 @@
|
||||
// Using CRELAY_ prefix to avoid conflicts with nostr_core_lib VERSION macros
|
||||
#define CRELAY_VERSION_MAJOR 1
|
||||
#define CRELAY_VERSION_MINOR 2
|
||||
#define CRELAY_VERSION_PATCH 29
|
||||
#define CRELAY_VERSION "v1.2.29"
|
||||
#define CRELAY_VERSION_PATCH 37
|
||||
#define CRELAY_VERSION "v1.2.37"
|
||||
|
||||
// Relay metadata (authoritative source for NIP-11 information)
|
||||
#define RELAY_NAME "C-Relay"
|
||||
|
||||
+103
-2
@@ -116,6 +116,81 @@ extern struct lws_context *ws_context;
|
||||
// Global subscription manager
|
||||
struct subscription_manager g_subscription_manager;
|
||||
|
||||
// Global connection list for idle connection tracking
|
||||
// Tracks ALL WebSocket connections (not just subscribed ones)
|
||||
// so the periodic timer can find and close idle connections
|
||||
#define MAX_TRACKED_CONNECTIONS 4096
|
||||
|
||||
typedef struct {
|
||||
struct lws* wsi;
|
||||
struct per_session_data* pss;
|
||||
} tracked_connection_t;
|
||||
|
||||
static tracked_connection_t g_connections[MAX_TRACKED_CONNECTIONS];
|
||||
static int g_connection_count = 0;
|
||||
static pthread_mutex_t g_connections_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
static void connection_list_add(struct lws* wsi, struct per_session_data* pss) {
|
||||
pthread_mutex_lock(&g_connections_lock);
|
||||
for (int i = 0; i < MAX_TRACKED_CONNECTIONS; i++) {
|
||||
if (g_connections[i].wsi == NULL) {
|
||||
g_connections[i].wsi = wsi;
|
||||
g_connections[i].pss = pss;
|
||||
g_connection_count++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&g_connections_lock);
|
||||
}
|
||||
|
||||
static void connection_list_remove(struct lws* wsi) {
|
||||
pthread_mutex_lock(&g_connections_lock);
|
||||
for (int i = 0; i < MAX_TRACKED_CONNECTIONS; i++) {
|
||||
if (g_connections[i].wsi == wsi) {
|
||||
g_connections[i].wsi = NULL;
|
||||
g_connections[i].pss = NULL;
|
||||
g_connection_count--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&g_connections_lock);
|
||||
}
|
||||
|
||||
// Check all tracked connections for idle timeout and close them
|
||||
// Called from the periodic maintenance timer (every 60 seconds)
|
||||
static void check_idle_connections(int idle_timeout_sec) {
|
||||
if (idle_timeout_sec <= 0) return;
|
||||
|
||||
time_t now = time(NULL);
|
||||
|
||||
// Collect WSIs to close outside the lock to avoid deadlock
|
||||
struct lws* to_close[MAX_TRACKED_CONNECTIONS];
|
||||
int close_count = 0;
|
||||
|
||||
pthread_mutex_lock(&g_connections_lock);
|
||||
for (int i = 0; i < MAX_TRACKED_CONNECTIONS; i++) {
|
||||
if (g_connections[i].wsi == NULL || g_connections[i].pss == NULL) continue;
|
||||
struct per_session_data* pss = g_connections[i].pss;
|
||||
if (pss->session_active) continue; // Already active — skip
|
||||
if (pss->connection_established <= 0) continue;
|
||||
time_t age = now - pss->connection_established;
|
||||
if (age >= idle_timeout_sec) {
|
||||
to_close[close_count++] = g_connections[i].wsi;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&g_connections_lock);
|
||||
|
||||
for (int i = 0; i < close_count; i++) {
|
||||
// Get pss again safely — it may have been freed if connection closed between lock release and here
|
||||
struct per_session_data* pss = (struct per_session_data*)lws_wsi_user(to_close[i]);
|
||||
if (!pss) continue;
|
||||
DEBUG_LOG("Closing idle connection from %s (no REQ/EVENT after %d seconds)",
|
||||
pss->client_ip, idle_timeout_sec);
|
||||
lws_close_reason(to_close[i], LWS_CLOSE_STATUS_POLICY_VIOLATION,
|
||||
(unsigned char*)"Idle connection timeout", 23);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Message queue functions for proper libwebsockets pattern
|
||||
@@ -330,6 +405,12 @@ static int nostr_relay_callback(struct lws *wsi, enum lws_callback_reasons reaso
|
||||
switch (reason) {
|
||||
case LWS_CALLBACK_HTTP:
|
||||
// Handle HTTP requests
|
||||
// Mark session as active so HTTP requests don't trigger idle ban
|
||||
if (pss) {
|
||||
pthread_mutex_lock(&pss->session_lock);
|
||||
pss->session_active = 1;
|
||||
pthread_mutex_unlock(&pss->session_lock);
|
||||
}
|
||||
{
|
||||
char *requested_uri = (char *)in;
|
||||
|
||||
@@ -383,6 +464,12 @@ static int nostr_relay_callback(struct lws *wsi, enum lws_callback_reasons reaso
|
||||
int is_nip11_request = (strstr(accept_header, "application/nostr+json") != NULL);
|
||||
|
||||
if (is_nip11_request) {
|
||||
// Mark session as active so HTTP requests don't trigger idle ban
|
||||
if (pss) {
|
||||
pthread_mutex_lock(&pss->session_lock);
|
||||
pss->session_active = 1;
|
||||
pthread_mutex_unlock(&pss->session_lock);
|
||||
}
|
||||
// Handle NIP-11 request
|
||||
if (handle_nip11_http_request(wsi, accept_header) == 0) {
|
||||
return 0; // Successfully handled
|
||||
@@ -547,6 +634,12 @@ static int nostr_relay_callback(struct lws *wsi, enum lws_callback_reasons reaso
|
||||
pss->challenge_created = 0;
|
||||
pss->challenge_expires = 0;
|
||||
|
||||
// Mark as WebSocket connection (not HTTP)
|
||||
pss->is_websocket = 1;
|
||||
|
||||
// Register in global connection list for idle tracking
|
||||
connection_list_add(wsi, pss);
|
||||
|
||||
// Record connection for stats tracking
|
||||
ip_ban_record_connection(pss->client_ip);
|
||||
|
||||
@@ -2149,7 +2242,10 @@ static int nostr_relay_callback(struct lws *wsi, enum lws_callback_reasons reaso
|
||||
|
||||
case LWS_CALLBACK_CLOSED:
|
||||
DEBUG_TRACE("WebSocket connection closed");
|
||||
|
||||
|
||||
// Remove from global connection list (must happen before pss cleanup)
|
||||
connection_list_remove(wsi);
|
||||
|
||||
// Enhanced closure logging with detailed diagnostics
|
||||
if (pss) {
|
||||
// Calculate connection duration
|
||||
@@ -2167,7 +2263,8 @@ static int nostr_relay_callback(struct lws *wsi, enum lws_callback_reasons reaso
|
||||
// This catches:
|
||||
// 1. Idle connections that timed out (never sent REQ/EVENT/AUTH)
|
||||
// 2. Early disconnects (client closed before sending REQ/EVENT/AUTH)
|
||||
if (!pss->session_active && strlen(pss->client_ip) > 0) {
|
||||
// Only record for WebSocket connections, not HTTP requests (NIP-11, embedded files)
|
||||
if (!pss->session_active && pss->is_websocket && strlen(pss->client_ip) > 0) {
|
||||
// Use separate idle failure recording (has its own threshold/duration)
|
||||
ip_ban_record_idle_failure(pss->client_ip);
|
||||
DEBUG_LOG("Recording idle/early-disconnect failure for IP %s (connected %ld seconds)",
|
||||
@@ -2560,6 +2657,10 @@ int start_websocket_relay(int port_override, int strict_port) {
|
||||
DEBUG_WARN("Debug level changed: %d -> %d", g_debug_level, config_debug_level);
|
||||
g_debug_level = config_debug_level;
|
||||
}
|
||||
// Check and close idle connections (no REQ/EVENT sent within timeout)
|
||||
int idle_timeout_sec = get_config_int("idle_connection_timeout_sec", 30);
|
||||
check_idle_connections(idle_timeout_sec);
|
||||
|
||||
if (max_connection_seconds > 0) {
|
||||
check_connection_age(max_connection_seconds);
|
||||
} else {
|
||||
|
||||
@@ -93,6 +93,7 @@ struct per_session_data {
|
||||
// Session activity tracking for idle connection banning
|
||||
int session_active; // 1 if client sent REQ or EVENT, 0 otherwise
|
||||
int idle_timeout_sec; // Timeout value for this session (copied from config)
|
||||
int is_websocket; // 1 if this is a WebSocket connection, 0 for HTTP
|
||||
};
|
||||
|
||||
// NIP-11 HTTP session data structure for managing buffer lifetime
|
||||
|
||||
Reference in New Issue
Block a user