Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c0051b22be | ||
|
|
4cc2d2376e |
174
Real-Time Traffic Monitoring Commands.md
Normal file
174
Real-Time Traffic Monitoring Commands.md
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
# Real-Time Traffic Monitoring Commands (Direct Server Use)
|
||||||
|
|
||||||
|
Copy and paste these commands directly on your server.
|
||||||
|
|
||||||
|
## Quick Status Checks
|
||||||
|
|
||||||
|
### See IPs visiting in the last few minutes:
|
||||||
|
```bash
|
||||||
|
sudo tail -500 /var/log/nginx/access.log | awk '{print $1}' | sort | uniq -c | sort -rn | head -20
|
||||||
|
```
|
||||||
|
|
||||||
|
### See what status codes they're getting:
|
||||||
|
```bash
|
||||||
|
sudo tail -500 /var/log/nginx/access.log | awk '{print $1, $9}' | grep '216.73.216.38'
|
||||||
|
```
|
||||||
|
|
||||||
|
### Count status codes (200 vs 403):
|
||||||
|
```bash
|
||||||
|
sudo tail -500 /var/log/nginx/access.log | awk '{print $9}' | sort | uniq -c
|
||||||
|
```
|
||||||
|
|
||||||
|
## Real-Time Monitoring
|
||||||
|
|
||||||
|
### Watch live traffic (updates every 2 seconds):
|
||||||
|
```bash
|
||||||
|
watch -n 2 'sudo tail -200 /var/log/nginx/access.log | awk "{print \$1}" | sort | uniq -c | sort -rn | head -15'
|
||||||
|
```
|
||||||
|
|
||||||
|
### See live log entries as they happen:
|
||||||
|
```bash
|
||||||
|
sudo tail -f /var/log/nginx/access.log
|
||||||
|
```
|
||||||
|
|
||||||
|
### Live GoAccess dashboard:
|
||||||
|
```bash
|
||||||
|
sudo tail -f /var/log/nginx/access.log | goaccess -
|
||||||
|
```
|
||||||
|
|
||||||
|
## Active Connections
|
||||||
|
|
||||||
|
### See who's connected RIGHT NOW:
|
||||||
|
```bash
|
||||||
|
sudo netstat -tn | grep ':443' | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn
|
||||||
|
```
|
||||||
|
|
||||||
|
### Alternative (using ss command):
|
||||||
|
```bash
|
||||||
|
sudo ss -tn | grep ':443' | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn
|
||||||
|
```
|
||||||
|
|
||||||
|
## Detailed Analysis
|
||||||
|
|
||||||
|
### Last 100 requests with timestamps:
|
||||||
|
```bash
|
||||||
|
sudo tail -100 /var/log/nginx/access.log | awk '{print $4, $1}' | sed 's/\[//'
|
||||||
|
```
|
||||||
|
|
||||||
|
### See what blocked IPs are trying to access:
|
||||||
|
```bash
|
||||||
|
sudo tail -500 /var/log/nginx/access.log | grep '216.73.216.38' | awk '{print $7}' | head -10
|
||||||
|
```
|
||||||
|
|
||||||
|
### Show all 403 (blocked) requests:
|
||||||
|
```bash
|
||||||
|
sudo tail -500 /var/log/nginx/access.log | awk '$9==403 {print $1}' | sort | uniq -c | sort -rn
|
||||||
|
```
|
||||||
|
|
||||||
|
### Show all successful (200) requests:
|
||||||
|
```bash
|
||||||
|
sudo tail -500 /var/log/nginx/access.log | awk '$9==200 {print $1}' | sort | uniq -c | sort -rn | head -10
|
||||||
|
```
|
||||||
|
|
||||||
|
## Comprehensive Monitoring Script
|
||||||
|
|
||||||
|
### Create a monitoring script:
|
||||||
|
```bash
|
||||||
|
cat > /tmp/monitor-traffic.sh << 'EOF'
|
||||||
|
#!/bin/bash
|
||||||
|
echo "=== Traffic in last 5 minutes ==="
|
||||||
|
echo "Time: $(date)"
|
||||||
|
echo ""
|
||||||
|
echo "Top IPs:"
|
||||||
|
sudo tail -1000 /var/log/nginx/access.log | awk '{print $1}' | sort | uniq -c | sort -rn | head -10
|
||||||
|
echo ""
|
||||||
|
echo "Blocked IPs (403 errors):"
|
||||||
|
sudo tail -1000 /var/log/nginx/access.log | awk '$9==403 {print $1}' | sort | uniq -c | sort -rn
|
||||||
|
echo ""
|
||||||
|
echo "Successful requests (200):"
|
||||||
|
sudo tail -1000 /var/log/nginx/access.log | awk '$9==200 {print $1}' | sort | uniq -c | sort -rn | head -5
|
||||||
|
echo ""
|
||||||
|
echo "Status Code Summary:"
|
||||||
|
sudo tail -1000 /var/log/nginx/access.log | awk '{print $9}' | sort | uniq -c | sort -rn
|
||||||
|
EOF
|
||||||
|
chmod +x /tmp/monitor-traffic.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
### Run the monitoring script:
|
||||||
|
```bash
|
||||||
|
/tmp/monitor-traffic.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
## Auto-Refreshing Dashboard
|
||||||
|
|
||||||
|
### Live dashboard (refreshes every 5 seconds):
|
||||||
|
```bash
|
||||||
|
watch -n 5 'echo "=== Last 5 minutes ==="
|
||||||
|
date
|
||||||
|
echo ""
|
||||||
|
echo "Top IPs:"
|
||||||
|
sudo tail -1000 /var/log/nginx/access.log | awk "{print \$1}" | sort | uniq -c | sort -rn | head -10
|
||||||
|
echo ""
|
||||||
|
echo "Status Codes:"
|
||||||
|
sudo tail -1000 /var/log/nginx/access.log | awk "{print \$9}" | sort | uniq -c | sort -rn'
|
||||||
|
```
|
||||||
|
|
||||||
|
Press `Ctrl+C` to exit.
|
||||||
|
|
||||||
|
## GoAccess HTML Report (Live Updating)
|
||||||
|
|
||||||
|
### Generate live HTML report:
|
||||||
|
```bash
|
||||||
|
sudo goaccess /var/log/nginx/access.log -o /var/www/html/live-stats.html --real-time-html --daemonize
|
||||||
|
```
|
||||||
|
|
||||||
|
Then visit: https://git.laantungir.net/live-stats.html
|
||||||
|
|
||||||
|
### Stop the live report:
|
||||||
|
```bash
|
||||||
|
sudo pkill -f "goaccess.*live-stats"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Filter by Time
|
||||||
|
|
||||||
|
### Get timestamp from 5 minutes ago:
|
||||||
|
```bash
|
||||||
|
date -d '5 minutes ago' '+%d/%b/%Y:%H:%M'
|
||||||
|
```
|
||||||
|
|
||||||
|
### Analyze only recent logs (replace timestamp):
|
||||||
|
```bash
|
||||||
|
sudo awk '/01\/Feb\/2026:19:09/,0' /var/log/nginx/access.log | goaccess -
|
||||||
|
```
|
||||||
|
|
||||||
|
## Check Gitea CPU
|
||||||
|
|
||||||
|
### Current CPU usage:
|
||||||
|
```bash
|
||||||
|
ps aux | grep gitea | grep -v grep
|
||||||
|
```
|
||||||
|
|
||||||
|
### Watch CPU in real-time:
|
||||||
|
```bash
|
||||||
|
watch -n 2 'ps aux | grep gitea | grep -v grep'
|
||||||
|
```
|
||||||
|
|
||||||
|
## Most Useful Command for Quick Check
|
||||||
|
|
||||||
|
This one-liner shows everything you need:
|
||||||
|
```bash
|
||||||
|
|
||||||
|
echo "=== Quick Status ===" && \
|
||||||
|
echo "Time: $(date)" && \
|
||||||
|
echo "" && \
|
||||||
|
echo "Top 10 IPs (last 1000 requests):" && \
|
||||||
|
sudo tail -1000 /var/log/nginx/access.log | awk '{print $1}' | sort | uniq -c | sort -rn | head -10 && \
|
||||||
|
echo "" && \
|
||||||
|
echo "Status Codes:" && \
|
||||||
|
sudo tail -1000 /var/log/nginx/access.log | awk '{print $9}' | sort | uniq -c && \
|
||||||
|
echo "" && \
|
||||||
|
echo "Gitea CPU:" && \
|
||||||
|
ps aux | grep gitea | grep -v grep
|
||||||
|
```
|
||||||
|
|
||||||
|
Copy any of these commands and run them directly on your server!
|
||||||
17
api/index.js
17
api/index.js
@@ -4324,6 +4324,12 @@ function populateSubscriptionDetailsTable(subscriptionsData) {
|
|||||||
const oldestDuration = Math.max(...subscriptions.map(s => now - s.created_at));
|
const oldestDuration = Math.max(...subscriptions.map(s => now - s.created_at));
|
||||||
const oldestDurationStr = formatDuration(oldestDuration);
|
const oldestDurationStr = formatDuration(oldestDuration);
|
||||||
|
|
||||||
|
// Calculate total query stats for this connection
|
||||||
|
const totalQueries = subscriptions.reduce((sum, s) => sum + (s.db_queries_executed || 0), 0);
|
||||||
|
const totalRows = subscriptions.reduce((sum, s) => sum + (s.db_rows_returned || 0), 0);
|
||||||
|
const avgQueryRate = subscriptions.length > 0 ? (subscriptions[0].query_rate_per_min || 0) : 0;
|
||||||
|
const clientIp = subscriptions.length > 0 ? (subscriptions[0].client_ip || 'unknown') : 'unknown';
|
||||||
|
|
||||||
// Create header row (summary)
|
// Create header row (summary)
|
||||||
const headerRow = document.createElement('tr');
|
const headerRow = document.createElement('tr');
|
||||||
headerRow.className = 'subscription-group-header';
|
headerRow.className = 'subscription-group-header';
|
||||||
@@ -4334,9 +4340,14 @@ function populateSubscriptionDetailsTable(subscriptionsData) {
|
|||||||
headerRow.innerHTML = `
|
headerRow.innerHTML = `
|
||||||
<td colspan="4" style="padding: 8px;">
|
<td colspan="4" style="padding: 8px;">
|
||||||
<span class="expand-icon" style="display: inline-block; width: 20px; transition: transform 0.2s;">▶</span>
|
<span class="expand-icon" style="display: inline-block; width: 20px; transition: transform 0.2s;">▶</span>
|
||||||
<strong style="font-family: 'Courier New', monospace; font-size: 12px;">Websocket: ${wsiPointer}</strong>
|
<strong style="font-family: 'Courier New', monospace; font-size: 12px;">IP: ${clientIp}</strong>
|
||||||
<span style="color: #666; margin-left: 15px;">
|
<span style="color: #666; margin-left: 10px; font-size: 11px;">
|
||||||
Subscriptions: ${subCount} | Oldest: ${oldestDurationStr}
|
WS: ${wsiPointer} |
|
||||||
|
Subs: ${subCount} |
|
||||||
|
Queries: ${totalQueries.toLocaleString()} |
|
||||||
|
Rows: ${totalRows.toLocaleString()} |
|
||||||
|
Rate: ${avgQueryRate.toFixed(1)} q/min |
|
||||||
|
Duration: ${oldestDurationStr}
|
||||||
</span>
|
</span>
|
||||||
</td>
|
</td>
|
||||||
`;
|
`;
|
||||||
|
|||||||
40
src/api.c
40
src/api.c
@@ -287,6 +287,46 @@ cJSON* query_subscription_details(void) {
|
|||||||
cJSON_AddBoolToObject(sub_obj, "active", 1); // All from this view are active
|
cJSON_AddBoolToObject(sub_obj, "active", 1); // All from this view are active
|
||||||
cJSON_AddStringToObject(sub_obj, "wsi_pointer", wsi_pointer ? wsi_pointer : "N/A");
|
cJSON_AddStringToObject(sub_obj, "wsi_pointer", wsi_pointer ? wsi_pointer : "N/A");
|
||||||
|
|
||||||
|
// Extract query stats from per_session_data if wsi is still valid
|
||||||
|
int db_queries = 0;
|
||||||
|
int db_rows = 0;
|
||||||
|
double query_rate = 0.0;
|
||||||
|
double row_rate = 0.0;
|
||||||
|
double avg_rows_per_query = 0.0;
|
||||||
|
|
||||||
|
if (wsi_pointer && strlen(wsi_pointer) > 2) { // Check for valid pointer string
|
||||||
|
// Parse wsi pointer from hex string
|
||||||
|
struct lws* wsi = NULL;
|
||||||
|
if (sscanf(wsi_pointer, "%p", (void**)&wsi) == 1 && wsi != NULL) {
|
||||||
|
// Get per_session_data from wsi
|
||||||
|
struct per_session_data* pss = (struct per_session_data*)lws_wsi_user(wsi);
|
||||||
|
if (pss) {
|
||||||
|
db_queries = pss->db_queries_executed;
|
||||||
|
db_rows = pss->db_rows_returned;
|
||||||
|
|
||||||
|
// Calculate rates (per minute)
|
||||||
|
time_t connection_duration = current_time - pss->query_tracking_start;
|
||||||
|
if (connection_duration > 0) {
|
||||||
|
double minutes = connection_duration / 60.0;
|
||||||
|
query_rate = db_queries / minutes;
|
||||||
|
row_rate = db_rows / minutes;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate average rows per query
|
||||||
|
if (db_queries > 0) {
|
||||||
|
avg_rows_per_query = (double)db_rows / (double)db_queries;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add query stats to subscription object
|
||||||
|
cJSON_AddNumberToObject(sub_obj, "db_queries_executed", db_queries);
|
||||||
|
cJSON_AddNumberToObject(sub_obj, "db_rows_returned", db_rows);
|
||||||
|
cJSON_AddNumberToObject(sub_obj, "query_rate_per_min", query_rate);
|
||||||
|
cJSON_AddNumberToObject(sub_obj, "row_rate_per_min", row_rate);
|
||||||
|
cJSON_AddNumberToObject(sub_obj, "avg_rows_per_query", avg_rows_per_query);
|
||||||
|
|
||||||
// Parse and add filter JSON if available
|
// Parse and add filter JSON if available
|
||||||
if (filter_json) {
|
if (filter_json) {
|
||||||
cJSON* filters = cJSON_Parse(filter_json);
|
cJSON* filters = cJSON_Parse(filter_json);
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
10
src/main.c
10
src/main.c
@@ -1202,6 +1202,11 @@ int handle_req_message(const char* sub_id, cJSON* filters, struct lws *wsi, stru
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Track query execution for abuse detection
|
||||||
|
if (pss) {
|
||||||
|
pss->db_queries_executed++;
|
||||||
|
}
|
||||||
|
|
||||||
// Bind parameters
|
// Bind parameters
|
||||||
for (int i = 0; i < bind_param_count; i++) {
|
for (int i = 0; i < bind_param_count; i++) {
|
||||||
sqlite3_bind_text(stmt, i + 1, bind_params[i], -1, SQLITE_TRANSIENT);
|
sqlite3_bind_text(stmt, i + 1, bind_params[i], -1, SQLITE_TRANSIENT);
|
||||||
@@ -1211,6 +1216,11 @@ int handle_req_message(const char* sub_id, cJSON* filters, struct lws *wsi, stru
|
|||||||
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||||
row_count++;
|
row_count++;
|
||||||
|
|
||||||
|
// Track rows returned for abuse detection
|
||||||
|
if (pss) {
|
||||||
|
pss->db_rows_returned++;
|
||||||
|
}
|
||||||
|
|
||||||
// Build event JSON
|
// Build event JSON
|
||||||
cJSON* event = cJSON_CreateObject();
|
cJSON* event = cJSON_CreateObject();
|
||||||
cJSON_AddStringToObject(event, "id", (char*)sqlite3_column_text(stmt, 0));
|
cJSON_AddStringToObject(event, "id", (char*)sqlite3_column_text(stmt, 0));
|
||||||
|
|||||||
@@ -13,8 +13,8 @@
|
|||||||
// Using CRELAY_ prefix to avoid conflicts with nostr_core_lib VERSION macros
|
// Using CRELAY_ prefix to avoid conflicts with nostr_core_lib VERSION macros
|
||||||
#define CRELAY_VERSION_MAJOR 1
|
#define CRELAY_VERSION_MAJOR 1
|
||||||
#define CRELAY_VERSION_MINOR 1
|
#define CRELAY_VERSION_MINOR 1
|
||||||
#define CRELAY_VERSION_PATCH 5
|
#define CRELAY_VERSION_PATCH 7
|
||||||
#define CRELAY_VERSION "v1.1.5"
|
#define CRELAY_VERSION "v1.1.7"
|
||||||
|
|
||||||
// Relay metadata (authoritative source for NIP-11 information)
|
// Relay metadata (authoritative source for NIP-11 information)
|
||||||
#define RELAY_NAME "C-Relay"
|
#define RELAY_NAME "C-Relay"
|
||||||
|
|||||||
@@ -63,6 +63,10 @@ void add_subscription_to_kind_index(subscription_t* sub) {
|
|||||||
|
|
||||||
int has_kind_filter = 0;
|
int has_kind_filter = 0;
|
||||||
|
|
||||||
|
// Track which kinds we've already added to avoid duplicates
|
||||||
|
// Use a bitmap for memory efficiency: 65536 bits = 8192 bytes
|
||||||
|
unsigned char added_kinds[8192] = {0}; // 65536 / 8 = 8192 bytes
|
||||||
|
|
||||||
// Iterate through all filters in this subscription
|
// Iterate through all filters in this subscription
|
||||||
subscription_filter_t* filter = sub->filters;
|
subscription_filter_t* filter = sub->filters;
|
||||||
while (filter) {
|
while (filter) {
|
||||||
@@ -82,6 +86,17 @@ void add_subscription_to_kind_index(subscription_t* sub) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if we've already added this kind (deduplication)
|
||||||
|
int byte_index = kind / 8;
|
||||||
|
int bit_index = kind % 8;
|
||||||
|
if (added_kinds[byte_index] & (1 << bit_index)) {
|
||||||
|
DEBUG_TRACE("KIND_INDEX: Skipping duplicate kind %d for subscription '%s'", kind, sub->id);
|
||||||
|
continue; // Already added this kind
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark this kind as added
|
||||||
|
added_kinds[byte_index] |= (1 << bit_index);
|
||||||
|
|
||||||
// Create new index node
|
// Create new index node
|
||||||
kind_subscription_node_t* node = malloc(sizeof(kind_subscription_node_t));
|
kind_subscription_node_t* node = malloc(sizeof(kind_subscription_node_t));
|
||||||
if (!node) {
|
if (!node) {
|
||||||
|
|||||||
@@ -391,6 +391,11 @@ static int nostr_relay_callback(struct lws *wsi, enum lws_callback_reasons reaso
|
|||||||
memset(pss, 0, sizeof(*pss));
|
memset(pss, 0, sizeof(*pss));
|
||||||
pthread_mutex_init(&pss->session_lock, NULL);
|
pthread_mutex_init(&pss->session_lock, NULL);
|
||||||
|
|
||||||
|
// Initialize database query tracking
|
||||||
|
pss->db_queries_executed = 0;
|
||||||
|
pss->db_rows_returned = 0;
|
||||||
|
pss->query_tracking_start = time(NULL);
|
||||||
|
|
||||||
// Get real client IP address
|
// Get real client IP address
|
||||||
char client_ip[CLIENT_IP_MAX_LENGTH];
|
char client_ip[CLIENT_IP_MAX_LENGTH];
|
||||||
memset(client_ip, 0, sizeof(client_ip));
|
memset(client_ip, 0, sizeof(client_ip));
|
||||||
@@ -2429,7 +2434,7 @@ int process_dm_stats_command(cJSON* dm_event, char* error_message, size_t error_
|
|||||||
|
|
||||||
// Handle NIP-45 COUNT message
|
// Handle NIP-45 COUNT message
|
||||||
int handle_count_message(const char* sub_id, cJSON* filters, struct lws *wsi, struct per_session_data *pss) {
|
int handle_count_message(const char* sub_id, cJSON* filters, struct lws *wsi, struct per_session_data *pss) {
|
||||||
(void)pss; // Suppress unused parameter warning
|
// pss is now used for query tracking, so remove unused warning suppression
|
||||||
|
|
||||||
if (!cJSON_IsArray(filters)) {
|
if (!cJSON_IsArray(filters)) {
|
||||||
DEBUG_ERROR("COUNT filters is not an array");
|
DEBUG_ERROR("COUNT filters is not an array");
|
||||||
|
|||||||
@@ -79,6 +79,11 @@ struct per_session_data {
|
|||||||
size_t reassembly_size; // Current size of accumulated data
|
size_t reassembly_size; // Current size of accumulated data
|
||||||
size_t reassembly_capacity; // Allocated capacity of reassembly buffer
|
size_t reassembly_capacity; // Allocated capacity of reassembly buffer
|
||||||
int reassembly_active; // Flag: 1 if currently reassembling a message
|
int reassembly_active; // Flag: 1 if currently reassembling a message
|
||||||
|
|
||||||
|
// Database query tracking for abuse detection and monitoring
|
||||||
|
int db_queries_executed; // Total SELECT queries executed by this connection
|
||||||
|
int db_rows_returned; // Total rows returned across all queries
|
||||||
|
time_t query_tracking_start; // When connection was established (for rate calculation)
|
||||||
};
|
};
|
||||||
|
|
||||||
// NIP-11 HTTP session data structure for managing buffer lifetime
|
// NIP-11 HTTP session data structure for managing buffer lifetime
|
||||||
|
|||||||
Reference in New Issue
Block a user