Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b6df0be865 | ||
|
|
a89f84f76e | ||
|
|
5a916cc221 | ||
|
|
dcf421ff93 | ||
|
|
d655258311 |
@@ -58,7 +58,7 @@
|
|||||||
<div class="inline-buttons">
|
<div class="inline-buttons">
|
||||||
<button type="button" id="connect-relay-btn">CONNECT TO RELAY</button>
|
<button type="button" id="connect-relay-btn">CONNECT TO RELAY</button>
|
||||||
<button type="button" id="disconnect-relay-btn" disabled>DISCONNECT</button>
|
<button type="button" id="disconnect-relay-btn" disabled>DISCONNECT</button>
|
||||||
<button type="button" id="test-websocket-btn" disabled>TEST WEBSOCKET</button>
|
<button type="button" id="restart-relay-btn" disabled>RESTART RELAY</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="status disconnected" id="relay-connection-status">NOT CONNECTED</div>
|
<div class="status disconnected" id="relay-connection-status">NOT CONNECTED</div>
|
||||||
|
|||||||
118
api/index.js
118
api/index.js
@@ -41,7 +41,7 @@
|
|||||||
const relayConnectionStatus = document.getElementById('relay-connection-status');
|
const relayConnectionStatus = document.getElementById('relay-connection-status');
|
||||||
const connectRelayBtn = document.getElementById('connect-relay-btn');
|
const connectRelayBtn = document.getElementById('connect-relay-btn');
|
||||||
const disconnectRelayBtn = document.getElementById('disconnect-relay-btn');
|
const disconnectRelayBtn = document.getElementById('disconnect-relay-btn');
|
||||||
const testWebSocketBtn = document.getElementById('test-websocket-btn');
|
const restartRelayBtn = document.getElementById('restart-relay-btn');
|
||||||
const configDisplay = document.getElementById('config-display');
|
const configDisplay = document.getElementById('config-display');
|
||||||
const configTableBody = document.getElementById('config-table-body');
|
const configTableBody = document.getElementById('config-table-body');
|
||||||
|
|
||||||
@@ -369,28 +369,28 @@
|
|||||||
relayConnectionStatus.className = 'status connected';
|
relayConnectionStatus.className = 'status connected';
|
||||||
connectRelayBtn.disabled = true;
|
connectRelayBtn.disabled = true;
|
||||||
disconnectRelayBtn.disabled = true;
|
disconnectRelayBtn.disabled = true;
|
||||||
testWebSocketBtn.disabled = true;
|
restartRelayBtn.disabled = true;
|
||||||
break;
|
break;
|
||||||
case 'connected':
|
case 'connected':
|
||||||
relayConnectionStatus.textContent = 'CONNECTED';
|
relayConnectionStatus.textContent = 'CONNECTED';
|
||||||
relayConnectionStatus.className = 'status connected';
|
relayConnectionStatus.className = 'status connected';
|
||||||
connectRelayBtn.disabled = true;
|
connectRelayBtn.disabled = true;
|
||||||
disconnectRelayBtn.disabled = false;
|
disconnectRelayBtn.disabled = false;
|
||||||
testWebSocketBtn.disabled = false;
|
restartRelayBtn.disabled = false;
|
||||||
break;
|
break;
|
||||||
case 'disconnected':
|
case 'disconnected':
|
||||||
relayConnectionStatus.textContent = 'NOT CONNECTED';
|
relayConnectionStatus.textContent = 'NOT CONNECTED';
|
||||||
relayConnectionStatus.className = 'status disconnected';
|
relayConnectionStatus.className = 'status disconnected';
|
||||||
connectRelayBtn.disabled = false;
|
connectRelayBtn.disabled = false;
|
||||||
disconnectRelayBtn.disabled = true;
|
disconnectRelayBtn.disabled = true;
|
||||||
testWebSocketBtn.disabled = true;
|
restartRelayBtn.disabled = true;
|
||||||
break;
|
break;
|
||||||
case 'error':
|
case 'error':
|
||||||
relayConnectionStatus.textContent = 'CONNECTION FAILED';
|
relayConnectionStatus.textContent = 'CONNECTION FAILED';
|
||||||
relayConnectionStatus.className = 'status error';
|
relayConnectionStatus.className = 'status error';
|
||||||
connectRelayBtn.disabled = false;
|
connectRelayBtn.disabled = false;
|
||||||
disconnectRelayBtn.disabled = true;
|
disconnectRelayBtn.disabled = true;
|
||||||
testWebSocketBtn.disabled = true;
|
restartRelayBtn.disabled = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1670,21 +1670,11 @@
|
|||||||
disconnectFromRelay();
|
disconnectFromRelay();
|
||||||
});
|
});
|
||||||
|
|
||||||
testWebSocketBtn.addEventListener('click', function (e) {
|
restartRelayBtn.addEventListener('click', function (e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
const url = relayConnectionUrl.value.trim();
|
sendRestartCommand().catch(error => {
|
||||||
if (!url) {
|
log(`Restart command failed: ${error.message}`, 'ERROR');
|
||||||
log('Please enter a relay URL first', 'ERROR');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
testWebSocketConnection(url)
|
|
||||||
.then(() => {
|
|
||||||
log('WebSocket test successful', 'INFO');
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
log(`WebSocket test failed: ${error.message}`, 'ERROR');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -3078,10 +3068,14 @@
|
|||||||
messageDiv.className = 'log-entry';
|
messageDiv.className = 'log-entry';
|
||||||
|
|
||||||
const directionColor = direction === 'sent' ? '#007bff' : '#28a745';
|
const directionColor = direction === 'sent' ? '#007bff' : '#28a745';
|
||||||
|
|
||||||
|
// Convert newlines to <br> tags for proper HTML display
|
||||||
|
const formattedMessage = message.replace(/\n/g, '<br>');
|
||||||
|
|
||||||
messageDiv.innerHTML = `
|
messageDiv.innerHTML = `
|
||||||
<span class="log-timestamp">${timestamp}</span>
|
<span class="log-timestamp">${timestamp}</span>
|
||||||
<span style="color: ${directionColor}; font-weight: bold;">[${direction.toUpperCase()}]</span>
|
<span style="color: ${directionColor}; font-weight: bold;">[${direction.toUpperCase()}]</span>
|
||||||
${message}
|
<span style="white-space: pre-wrap;">${formattedMessage}</span>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
// Remove the "No messages received yet" placeholder if it exists
|
// Remove the "No messages received yet" placeholder if it exists
|
||||||
@@ -3144,6 +3138,83 @@
|
|||||||
// DATABASE STATISTICS FUNCTIONS
|
// DATABASE STATISTICS FUNCTIONS
|
||||||
// ================================
|
// ================================
|
||||||
|
|
||||||
|
// Send restart command to restart the relay using Administrator API
|
||||||
|
async function sendRestartCommand() {
|
||||||
|
if (!isLoggedIn || !userPubkey) {
|
||||||
|
log('Must be logged in to restart relay', 'ERROR');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!relayPool) {
|
||||||
|
log('SimplePool connection not available', 'ERROR');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
log('Sending restart command to relay...', 'INFO');
|
||||||
|
|
||||||
|
// Create command array for restart
|
||||||
|
const command_array = ["system_command", "restart"];
|
||||||
|
|
||||||
|
// Encrypt the command array directly using NIP-44
|
||||||
|
const encrypted_content = await encryptForRelay(JSON.stringify(command_array));
|
||||||
|
if (!encrypted_content) {
|
||||||
|
throw new Error('Failed to encrypt command array');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create single kind 23456 admin event
|
||||||
|
const restartEvent = {
|
||||||
|
kind: 23456,
|
||||||
|
pubkey: userPubkey,
|
||||||
|
created_at: Math.floor(Date.now() / 1000),
|
||||||
|
tags: [["p", getRelayPubkey()]],
|
||||||
|
content: encrypted_content
|
||||||
|
};
|
||||||
|
|
||||||
|
// Sign the event
|
||||||
|
const signedEvent = await window.nostr.signEvent(restartEvent);
|
||||||
|
if (!signedEvent || !signedEvent.sig) {
|
||||||
|
throw new Error('Event signing failed');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Publish via SimplePool
|
||||||
|
const url = relayConnectionUrl.value.trim();
|
||||||
|
const publishPromises = relayPool.publish([url], signedEvent);
|
||||||
|
|
||||||
|
// Use Promise.allSettled to capture per-relay outcomes
|
||||||
|
const results = await Promise.allSettled(publishPromises);
|
||||||
|
|
||||||
|
// Check if any relay accepted the event
|
||||||
|
let successCount = 0;
|
||||||
|
results.forEach((result, index) => {
|
||||||
|
if (result.status === 'fulfilled') {
|
||||||
|
successCount++;
|
||||||
|
log(`Restart command published successfully to relay ${index}`, 'INFO');
|
||||||
|
} else {
|
||||||
|
log(`Restart command failed on relay ${index}: ${result.reason?.message || result.reason}`, 'ERROR');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (successCount === 0) {
|
||||||
|
const errorDetails = results.map((r, i) => `Relay ${i}: ${r.reason?.message || r.reason}`).join('; ');
|
||||||
|
throw new Error(`All relays rejected restart command. Details: ${errorDetails}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
log('Restart command sent successfully - relay should restart shortly...', 'INFO');
|
||||||
|
|
||||||
|
// Update connection status to indicate restart is in progress
|
||||||
|
updateRelayConnectionStatus('connecting');
|
||||||
|
relayConnectionStatus.textContent = 'RESTARTING...';
|
||||||
|
|
||||||
|
// The relay will disconnect and need to be reconnected after restart
|
||||||
|
// This will be handled by the WebSocket disconnection event
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
log(`Failed to send restart command: ${error.message}`, 'ERROR');
|
||||||
|
updateRelayConnectionStatus('error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Send stats_query command to get database statistics using Administrator API (inner events)
|
// Send stats_query command to get database statistics using Administrator API (inner events)
|
||||||
async function sendStatsQuery() {
|
async function sendStatsQuery() {
|
||||||
if (!isLoggedIn || !userPubkey) {
|
if (!isLoggedIn || !userPubkey) {
|
||||||
@@ -3300,9 +3371,12 @@
|
|||||||
const events7d = document.getElementById('events-7d');
|
const events7d = document.getElementById('events-7d');
|
||||||
const events30d = document.getElementById('events-30d');
|
const events30d = document.getElementById('events-30d');
|
||||||
|
|
||||||
if (events24h) events24h.textContent = data.events_24h || '-';
|
// Access the nested time_stats object from backend response
|
||||||
if (events7d) events7d.textContent = data.events_7d || '-';
|
const timeStats = data.time_stats || {};
|
||||||
if (events30d) events30d.textContent = data.events_30d || '-';
|
|
||||||
|
if (events24h) events24h.textContent = timeStats.last_24h || '0';
|
||||||
|
if (events7d) events7d.textContent = timeStats.last_7d || '0';
|
||||||
|
if (events30d) events30d.textContent = timeStats.last_30d || '0';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Populate top pubkeys table
|
// Populate top pubkeys table
|
||||||
|
|||||||
@@ -319,12 +319,18 @@ create_gitea_release() {
|
|||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d "{\"tag_name\": \"$NEW_VERSION\", \"name\": \"$NEW_VERSION\", \"body\": \"$COMMIT_MESSAGE\"}")
|
-d "{\"tag_name\": \"$NEW_VERSION\", \"name\": \"$NEW_VERSION\", \"body\": \"$COMMIT_MESSAGE\"}")
|
||||||
|
|
||||||
|
local upload_result=false
|
||||||
|
|
||||||
if echo "$response" | grep -q '"id"'; then
|
if echo "$response" | grep -q '"id"'; then
|
||||||
print_success "Created release $NEW_VERSION"
|
print_success "Created release $NEW_VERSION"
|
||||||
upload_release_binaries "$api_url" "$token"
|
if upload_release_binaries "$api_url" "$token"; then
|
||||||
|
upload_result=true
|
||||||
|
fi
|
||||||
elif echo "$response" | grep -q "already exists"; then
|
elif echo "$response" | grep -q "already exists"; then
|
||||||
print_warning "Release $NEW_VERSION already exists"
|
print_warning "Release $NEW_VERSION already exists"
|
||||||
upload_release_binaries "$api_url" "$token"
|
if upload_release_binaries "$api_url" "$token"; then
|
||||||
|
upload_result=true
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
print_error "Failed to create release $NEW_VERSION"
|
print_error "Failed to create release $NEW_VERSION"
|
||||||
print_error "Response: $response"
|
print_error "Response: $response"
|
||||||
@@ -334,18 +340,29 @@ create_gitea_release() {
|
|||||||
local check_response=$(curl -s -H "Authorization: token $token" "$api_url/releases/tags/$NEW_VERSION")
|
local check_response=$(curl -s -H "Authorization: token $token" "$api_url/releases/tags/$NEW_VERSION")
|
||||||
if echo "$check_response" | grep -q '"id"'; then
|
if echo "$check_response" | grep -q '"id"'; then
|
||||||
print_warning "Release exists but creation response was unexpected"
|
print_warning "Release exists but creation response was unexpected"
|
||||||
upload_release_binaries "$api_url" "$token"
|
if upload_release_binaries "$api_url" "$token"; then
|
||||||
|
upload_result=true
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
print_error "Release does not exist and creation failed"
|
print_error "Release does not exist and creation failed"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Return based on upload success
|
||||||
|
if [[ "$upload_result" == true ]]; then
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
print_error "Binary upload failed"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to upload release binaries
|
# Function to upload release binaries
|
||||||
upload_release_binaries() {
|
upload_release_binaries() {
|
||||||
local api_url="$1"
|
local api_url="$1"
|
||||||
local token="$2"
|
local token="$2"
|
||||||
|
local upload_success=true
|
||||||
|
|
||||||
# Get release ID with more robust parsing
|
# Get release ID with more robust parsing
|
||||||
print_status "Getting release ID for $NEW_VERSION..."
|
print_status "Getting release ID for $NEW_VERSION..."
|
||||||
@@ -367,30 +384,58 @@ upload_release_binaries() {
|
|||||||
# Upload x86_64 binary
|
# Upload x86_64 binary
|
||||||
if [[ -f "c-relay-x86_64" ]]; then
|
if [[ -f "c-relay-x86_64" ]]; then
|
||||||
print_status "Uploading x86_64 binary..."
|
print_status "Uploading x86_64 binary..."
|
||||||
if curl -s -X POST "$api_url/releases/$release_id/assets" \
|
local upload_response=$(curl -s -w "\n%{http_code}" -X POST "$api_url/releases/$release_id/assets" \
|
||||||
-H "Authorization: token $token" \
|
-H "Authorization: token $token" \
|
||||||
-F "attachment=@c-relay-x86_64;filename=c-relay-${NEW_VERSION}-linux-x86_64" > /dev/null; then
|
-F "attachment=@c-relay-x86_64;filename=c-relay-${NEW_VERSION}-linux-x86_64")
|
||||||
print_success "Uploaded x86_64 binary"
|
|
||||||
|
local http_code=$(echo "$upload_response" | tail -n1)
|
||||||
|
local response_body=$(echo "$upload_response" | head -n -1)
|
||||||
|
|
||||||
|
if [[ "$http_code" == "201" ]]; then
|
||||||
|
print_success "Uploaded x86_64 binary successfully"
|
||||||
else
|
else
|
||||||
print_warning "Failed to upload x86_64 binary"
|
print_error "Failed to upload x86_64 binary (HTTP $http_code)"
|
||||||
|
print_error "Response: $response_body"
|
||||||
|
upload_success=false
|
||||||
fi
|
fi
|
||||||
|
else
|
||||||
|
print_warning "x86_64 binary not found: c-relay-x86_64"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Upload ARM64 binary
|
# Upload ARM64 binary
|
||||||
if [[ -f "c-relay-arm64" ]]; then
|
if [[ -f "c-relay-arm64" ]]; then
|
||||||
print_status "Uploading ARM64 binary..."
|
print_status "Uploading ARM64 binary..."
|
||||||
if curl -s -X POST "$api_url/releases/$release_id/assets" \
|
local upload_response=$(curl -s -w "\n%{http_code}" -X POST "$api_url/releases/$release_id/assets" \
|
||||||
-H "Authorization: token $token" \
|
-H "Authorization: token $token" \
|
||||||
-F "attachment=@c-relay-arm64;filename=c-relay-${NEW_VERSION}-linux-arm64" > /dev/null; then
|
-F "attachment=@c-relay-arm64;filename=c-relay-${NEW_VERSION}-linux-arm64")
|
||||||
print_success "Uploaded ARM64 binary"
|
|
||||||
|
local http_code=$(echo "$upload_response" | tail -n1)
|
||||||
|
local response_body=$(echo "$upload_response" | head -n -1)
|
||||||
|
|
||||||
|
if [[ "$http_code" == "201" ]]; then
|
||||||
|
print_success "Uploaded ARM64 binary successfully"
|
||||||
else
|
else
|
||||||
print_warning "Failed to upload ARM64 binary"
|
print_error "Failed to upload ARM64 binary (HTTP $http_code)"
|
||||||
|
print_error "Response: $response_body"
|
||||||
|
upload_success=false
|
||||||
fi
|
fi
|
||||||
|
else
|
||||||
|
print_warning "ARM64 binary not found: c-relay-arm64"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Return success/failure status
|
||||||
|
if [[ "$upload_success" == true ]]; then
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to clean up release binaries
|
# Function to clean up release binaries
|
||||||
cleanup_release_binaries() {
|
cleanup_release_binaries() {
|
||||||
|
local force_cleanup="$1" # Optional parameter to force cleanup even on failure
|
||||||
|
|
||||||
|
if [[ "$force_cleanup" == "force" ]] || [[ "$upload_success" == true ]]; then
|
||||||
if [[ -f "c-relay-x86_64" ]]; then
|
if [[ -f "c-relay-x86_64" ]]; then
|
||||||
rm -f c-relay-x86_64
|
rm -f c-relay-x86_64
|
||||||
print_status "Cleaned up x86_64 binary"
|
print_status "Cleaned up x86_64 binary"
|
||||||
@@ -399,6 +444,16 @@ cleanup_release_binaries() {
|
|||||||
rm -f c-relay-arm64
|
rm -f c-relay-arm64
|
||||||
print_status "Cleaned up ARM64 binary"
|
print_status "Cleaned up ARM64 binary"
|
||||||
fi
|
fi
|
||||||
|
else
|
||||||
|
print_warning "Keeping binary files due to upload failures"
|
||||||
|
print_status "Files available for manual upload:"
|
||||||
|
if [[ -f "c-relay-x86_64" ]]; then
|
||||||
|
print_status " - c-relay-x86_64"
|
||||||
|
fi
|
||||||
|
if [[ -f "c-relay-arm64" ]]; then
|
||||||
|
print_status " - c-relay-arm64"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Main execution
|
# Main execution
|
||||||
@@ -433,13 +488,17 @@ main() {
|
|||||||
git_commit_and_push_no_tag
|
git_commit_and_push_no_tag
|
||||||
|
|
||||||
# Create Gitea release with binaries
|
# Create Gitea release with binaries
|
||||||
create_gitea_release
|
if create_gitea_release; then
|
||||||
|
|
||||||
# Cleanup
|
|
||||||
cleanup_release_binaries
|
|
||||||
|
|
||||||
print_success "Release $NEW_VERSION completed successfully!"
|
print_success "Release $NEW_VERSION completed successfully!"
|
||||||
print_status "Binaries uploaded to Gitea release"
|
print_status "Binaries uploaded to Gitea release"
|
||||||
|
upload_success=true
|
||||||
|
else
|
||||||
|
print_error "Release creation or binary upload failed"
|
||||||
|
upload_success=false
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Cleanup (only if upload was successful)
|
||||||
|
cleanup_release_binaries
|
||||||
|
|
||||||
else
|
else
|
||||||
print_status "=== DEFAULT MODE ==="
|
print_status "=== DEFAULT MODE ==="
|
||||||
|
|||||||
BIN
c-relay-x86_64
Executable file
BIN
c-relay-x86_64
Executable file
Binary file not shown.
41
src/config.c
41
src/config.c
@@ -16,6 +16,9 @@
|
|||||||
// External database connection (from main.c)
|
// External database connection (from main.c)
|
||||||
extern sqlite3* g_db;
|
extern sqlite3* g_db;
|
||||||
|
|
||||||
|
// External shutdown flag (from main.c)
|
||||||
|
extern volatile sig_atomic_t g_shutdown_flag;
|
||||||
|
|
||||||
// Global unified configuration cache instance
|
// Global unified configuration cache instance
|
||||||
unified_config_cache_t g_unified_cache = {
|
unified_config_cache_t g_unified_cache = {
|
||||||
.cache_lock = PTHREAD_MUTEX_INITIALIZER,
|
.cache_lock = PTHREAD_MUTEX_INITIALIZER,
|
||||||
@@ -3730,6 +3733,44 @@ int handle_system_command_unified(cJSON* event, const char* command, char* error
|
|||||||
snprintf(error_message, error_size, "failed to send system status response");
|
snprintf(error_message, error_size, "failed to send system status response");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
else if (strcmp(command, "restart") == 0) {
|
||||||
|
// Build restart acknowledgment response
|
||||||
|
cJSON* response = cJSON_CreateObject();
|
||||||
|
cJSON_AddStringToObject(response, "command", "restart");
|
||||||
|
cJSON_AddStringToObject(response, "status", "initiating_restart");
|
||||||
|
cJSON_AddStringToObject(response, "message", "Relay restart initiated - shutting down gracefully");
|
||||||
|
cJSON_AddNumberToObject(response, "timestamp", (double)time(NULL));
|
||||||
|
|
||||||
|
printf("=== Relay Restart Initiated ===\n");
|
||||||
|
printf("Admin requested system restart\n");
|
||||||
|
printf("Sending acknowledgment and initiating shutdown...\n");
|
||||||
|
|
||||||
|
// Get admin pubkey from event for response
|
||||||
|
cJSON* pubkey_obj = cJSON_GetObjectItem(event, "pubkey");
|
||||||
|
const char* admin_pubkey = pubkey_obj ? cJSON_GetStringValue(pubkey_obj) : NULL;
|
||||||
|
|
||||||
|
if (!admin_pubkey) {
|
||||||
|
cJSON_Delete(response);
|
||||||
|
snprintf(error_message, error_size, "missing admin pubkey for response");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send acknowledgment response as signed kind 23457 event
|
||||||
|
if (send_admin_response_event(response, admin_pubkey, wsi) == 0) {
|
||||||
|
log_success("Restart acknowledgment sent successfully - initiating shutdown");
|
||||||
|
|
||||||
|
// Trigger graceful shutdown by setting the global shutdown flag
|
||||||
|
g_shutdown_flag = 1;
|
||||||
|
log_info("Shutdown flag set - relay will restart gracefully");
|
||||||
|
|
||||||
|
cJSON_Delete(response);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON_Delete(response);
|
||||||
|
snprintf(error_message, error_size, "failed to send restart acknowledgment");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
snprintf(error_message, error_size, "invalid: unknown system command '%s'", command);
|
snprintf(error_message, error_size, "invalid: unknown system command '%s'", command);
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
1497
src/dm_admin.c
1497
src/dm_admin.c
File diff suppressed because it is too large
Load Diff
@@ -18,4 +18,10 @@ int process_nip17_admin_command(cJSON* dm_event, char* error_message, size_t err
|
|||||||
int is_nip17_gift_wrap_for_relay(cJSON* gift_wrap_event);
|
int is_nip17_gift_wrap_for_relay(cJSON* gift_wrap_event);
|
||||||
char* generate_stats_json(void);
|
char* generate_stats_json(void);
|
||||||
|
|
||||||
|
// Unified NIP-17 response functions
|
||||||
|
int send_nip17_response(const char* sender_pubkey, const char* response_content,
|
||||||
|
char* error_message, size_t error_size);
|
||||||
|
char* generate_config_text(void);
|
||||||
|
char* generate_stats_text(void);
|
||||||
|
|
||||||
#endif // DM_ADMIN_H
|
#endif // DM_ADMIN_H
|
||||||
File diff suppressed because one or more lines are too long
@@ -45,6 +45,8 @@ int nostr_nip42_verify_auth_event(cJSON *event, const char *challenge_id,
|
|||||||
// Global state
|
// Global state
|
||||||
sqlite3* g_db = NULL; // Non-static so config.c can access it
|
sqlite3* g_db = NULL; // Non-static so config.c can access it
|
||||||
int g_server_running = 1; // Non-static so websockets.c can access it
|
int g_server_running = 1; // Non-static so websockets.c can access it
|
||||||
|
volatile sig_atomic_t g_shutdown_flag = 0; // Non-static so config.c can access it for restart functionality
|
||||||
|
int g_restart_requested = 0; // Non-static so config.c can access it for restart functionality
|
||||||
struct lws_context *ws_context = NULL; // Non-static so websockets.c can access it
|
struct lws_context *ws_context = NULL; // Non-static so websockets.c can access it
|
||||||
|
|
||||||
// NIP-11 relay information structure
|
// NIP-11 relay information structure
|
||||||
|
|||||||
@@ -95,6 +95,8 @@ extern unified_config_cache_t g_unified_cache;
|
|||||||
// Forward declarations for global state
|
// Forward declarations for global state
|
||||||
extern sqlite3* g_db;
|
extern sqlite3* g_db;
|
||||||
extern int g_server_running;
|
extern int g_server_running;
|
||||||
|
extern volatile sig_atomic_t g_shutdown_flag;
|
||||||
|
extern int g_restart_requested;
|
||||||
extern struct lws_context *ws_context;
|
extern struct lws_context *ws_context;
|
||||||
|
|
||||||
// Global subscription manager
|
// Global subscription manager
|
||||||
@@ -644,6 +646,9 @@ static int nostr_relay_callback(struct lws *wsi, enum lws_callback_reasons reaso
|
|||||||
cJSON* response_event = process_nip17_admin_message(event, nip17_error, sizeof(nip17_error), wsi);
|
cJSON* response_event = process_nip17_admin_message(event, nip17_error, sizeof(nip17_error), wsi);
|
||||||
|
|
||||||
if (!response_event) {
|
if (!response_event) {
|
||||||
|
// Check if this is an error or if the command was already handled
|
||||||
|
if (strlen(nip17_error) > 0) {
|
||||||
|
// There was an actual error
|
||||||
log_error("DEBUG NIP17: NIP-17 admin message processing failed");
|
log_error("DEBUG NIP17: NIP-17 admin message processing failed");
|
||||||
result = -1;
|
result = -1;
|
||||||
size_t error_len = strlen(nip17_error);
|
size_t error_len = strlen(nip17_error);
|
||||||
@@ -655,6 +660,18 @@ static int nostr_relay_callback(struct lws *wsi, enum lws_callback_reasons reaso
|
|||||||
snprintf(debug_nip17_error_msg, sizeof(debug_nip17_error_msg),
|
snprintf(debug_nip17_error_msg, sizeof(debug_nip17_error_msg),
|
||||||
"DEBUG NIP17 ERROR: %.400s", nip17_error);
|
"DEBUG NIP17 ERROR: %.400s", nip17_error);
|
||||||
log_error(debug_nip17_error_msg);
|
log_error(debug_nip17_error_msg);
|
||||||
|
} else {
|
||||||
|
// No error message means the command was already handled (plain text commands)
|
||||||
|
log_success("DEBUG NIP17: NIP-17 admin message processed successfully (already handled)");
|
||||||
|
// Store the original gift wrap event in database
|
||||||
|
if (store_event(event) != 0) {
|
||||||
|
log_error("DEBUG NIP17: Failed to store gift wrap event in database");
|
||||||
|
result = -1;
|
||||||
|
strncpy(error_message, "error: failed to store gift wrap event", sizeof(error_message) - 1);
|
||||||
|
} else {
|
||||||
|
log_info("DEBUG NIP17: Gift wrap event stored successfully in database");
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
log_success("DEBUG NIP17: NIP-17 admin message processed successfully");
|
log_success("DEBUG NIP17: NIP-17 admin message processed successfully");
|
||||||
// Store the original gift wrap event in database (unlike kind 23456)
|
// Store the original gift wrap event in database (unlike kind 23456)
|
||||||
@@ -1138,7 +1155,7 @@ int start_websocket_relay(int port_override, int strict_port) {
|
|||||||
log_success(startup_msg);
|
log_success(startup_msg);
|
||||||
|
|
||||||
// Main event loop with proper signal handling
|
// Main event loop with proper signal handling
|
||||||
while (g_server_running) {
|
while (g_server_running && !g_shutdown_flag) {
|
||||||
int result = lws_service(ws_context, 1000);
|
int result = lws_service(ws_context, 1000);
|
||||||
|
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user