lazydns 0.3.20

A light and fast DNS server/forwarder implementation in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! WebUI server module
//!
//! Provides a real-time dashboard and API for monitoring DNS server operations.
//!
//! # Features
//!
//! - **REST API**: Query metrics, alerts, and configuration
//! - **SSE Streaming**: Real-time query logs and security events
//! - **WebSocket**: Live metrics updates
//! - **Alert Engine**: Configurable alerting with webhook support
//! - **Static File Serving**: Development mode for WebUI frontend
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────┐
//! │                    WebUI Server                      │
//! ├─────────────────────────────────────────────────────┤
//! │  Routes:                                             │
//! │  ├── /api/dashboard/overview      GET               │
//! │  ├── /api/metrics/top-domains     GET               │
//! │  ├── /api/metrics/top-clients     GET               │
//! │  ├── /api/metrics/upstream-health GET               │
//! │  ├── /api/alerts/recent           GET               │
//! │  ├── /api/audit/query-logs/stream GET (SSE)         │
//! │  ├── /api/audit/security/stream   GET (SSE)         │
//! │  └── /ws/metrics                  WebSocket         │
//! └─────────────────────────────────────────────────────┘
//! ```

pub mod alerts;
pub mod config;
pub mod metrics;
pub mod routes;
pub mod state;
pub mod upstream_registry;
pub mod websocket;

#[cfg(feature = "web-embed")]
pub mod embedded;

use crate::Result;
use crate::config::Config;
use crate::plugin::Registry;
use crate::plugins::audit::init_event_bus;
#[cfg(feature = "admin")]
use axum::routing::post;
use axum::{Json, Router, routing::get};
use config::WebConfig;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::sync::RwLock;
use tower_http::cors::{Any, CorsLayer};
use tower_http::trace::TraceLayer;
use tracing::{error, info};

// Re-export upstream registry for easy access
pub use upstream_registry::{
    UpstreamHealthData, UpstreamHealthSnapshot, UpstreamRegistry, get_all_upstream_health,
    register_upstream, unregister_upstream, upstream_registry,
};

pub use state::WebState;

/// WebUI Server
pub struct WebServer {
    config: WebConfig,
    state: Arc<WebState>,
}

impl WebServer {
    /// Create a new WebUI server
    pub async fn new(config: WebConfig) -> Result<Self> {
        // Validate configuration
        config.validate().map_err(crate::Error::Config)?;

        // Initialize event bus if not already initialized (normally done in main.rs)
        // This is a fallback for cases where WebServer is created directly
        if crate::plugins::audit::event_bus().is_none() {
            init_event_bus(config.event_bus_capacity);
        }

        // Create shared state
        let state = Arc::new(WebState::new(&config).await?);

        Ok(Self { config, state })
    }

    /// Create a new WebUI server with admin capabilities
    pub async fn with_admin(
        config: WebConfig,
        registry: Arc<Registry>,
        global_config: Arc<RwLock<Config>>,
    ) -> Result<Self> {
        // Validate configuration
        config.validate().map_err(crate::Error::Config)?;

        // Initialize event bus if not already initialized
        if crate::plugins::audit::event_bus().is_none() {
            init_event_bus(config.event_bus_capacity);
        }

        // Create shared state with admin capabilities
        let mut state = WebState::new(&config).await?;
        state.set_registry(registry);
        state.set_config_arc(global_config);

        Ok(Self {
            config,
            state: Arc::new(state),
        })
    }

    /// Build the router with all routes
    fn build_router(&self) -> Router {
        let state = Arc::clone(&self.state);

        // Build CORS layer
        let cors = if self.config.cors.allowed_origins.is_empty() {
            CorsLayer::new()
                .allow_origin(Any)
                .allow_methods(Any)
                .allow_headers(Any)
        } else {
            let origins: Vec<_> = self
                .config
                .cors
                .allowed_origins
                .iter()
                .filter_map(|s| s.parse().ok())
                .collect();
            CorsLayer::new()
                .allow_origin(origins)
                .allow_methods(Any)
                .allow_headers(Any)
        };

        // Request tracing/logging layer for debugging
        use tower_http::LatencyUnit;
        use tower_http::trace::{DefaultOnRequest, DefaultOnResponse};

        let trace_layer = TraceLayer::new_for_http()
            .make_span_with(|req: &axum::http::Request<axum::body::Body>| {
                tracing::span!(tracing::Level::TRACE, "http_request",
                    method = %req.method(),
                    uri = %req.uri()
                )
            })
            .on_request(DefaultOnRequest::new().level(tracing::Level::TRACE))
            .on_response(
                DefaultOnResponse::new()
                    .level(tracing::Level::TRACE)
                    .latency_unit(LatencyUnit::Millis),
            );

        // API routes
        #[cfg(feature = "admin")]
        let mut api_router = Router::new()
            // Health check endpoint
            .route("/health", get(routes::dashboard::overview))
            // Server features detection
            .route("/features", get(server_features))
            // Dashboard
            .route("/dashboard/overview", get(routes::dashboard::overview))
            .route(
                "/dashboard/cache/stats",
                get(routes::dashboard::cache_stats),
            )
            .route(
                "/dashboard/server/info",
                get(routes::dashboard::server_info),
            )
            .route("/alerts/recent", get(routes::dashboard::recent_alerts))
            // Metrics
            .route("/metrics/top-domains", get(routes::metrics::top_domains))
            .route("/metrics/top-clients", get(routes::metrics::top_clients))
            .route(
                "/metrics/upstream-health",
                get(routes::metrics::upstream_health),
            )
            .route(
                "/metrics/latency",
                get(routes::metrics::latency_distribution),
            )
            .route("/metrics/qps", get(routes::metrics::qps_history))
            // Config dump (read-only)
            .route("/config/dump", get(routes::config_dump::dump))
            // SSE streams
            .route(
                "/audit/query-logs/stream",
                get(routes::audit::query_logs_stream),
            )
            .route(
                "/audit/security-events/stream",
                get(routes::audit::security_events_stream),
            );

        #[cfg(not(feature = "admin"))]
        let api_router = Router::new()
            // Health check endpoint
            .route("/health", get(routes::dashboard::overview))
            // Server features detection
            .route("/features", get(server_features))
            // Dashboard
            .route("/dashboard/overview", get(routes::dashboard::overview))
            .route(
                "/dashboard/cache/stats",
                get(routes::dashboard::cache_stats),
            )
            .route(
                "/dashboard/server/info",
                get(routes::dashboard::server_info),
            )
            .route("/alerts/recent", get(routes::dashboard::recent_alerts))
            // Metrics
            .route("/metrics/top-domains", get(routes::metrics::top_domains))
            .route("/metrics/top-clients", get(routes::metrics::top_clients))
            .route(
                "/metrics/upstream-health",
                get(routes::metrics::upstream_health),
            )
            .route(
                "/metrics/latency",
                get(routes::metrics::latency_distribution),
            )
            .route("/metrics/qps", get(routes::metrics::qps_history))
            // Config dump (read-only)
            .route("/config/dump", get(routes::config_dump::dump))
            // SSE streams
            .route(
                "/audit/query-logs/stream",
                get(routes::audit::query_logs_stream),
            )
            .route(
                "/audit/security-events/stream",
                get(routes::audit::security_events_stream),
            );

        // Admin routes (only when admin feature is enabled)
        #[cfg(feature = "admin")]
        {
            api_router = api_router
                .route("/admin/cache/clear", post(routes::admin::clear_cache))
                .route("/admin/config/reload", post(routes::admin::reload_config))
                // Alert management
                .route(
                    "/admin/alerts/acknowledge-all",
                    post(routes::admin::acknowledge_all_alerts),
                )
                .route(
                    "/admin/alerts/acknowledge/{id}",
                    post(routes::admin::acknowledge_alert),
                )
                .route("/admin/alerts/clear", post(routes::admin::clear_alerts))
                // Log export
                .route("/admin/logs/export", post(routes::admin::export_logs));
        }

        let api_router = api_router
            .layer(trace_layer.clone())
            .with_state(state.clone());

        // WebSocket routes
        let ws_router = Router::new()
            .route("/metrics", get(websocket::handler::metrics_ws))
            .layer(trace_layer.clone())
            .with_state(state.clone());

        // Main router
        let mut router = Router::new()
            .nest("/api", api_router)
            .nest("/ws", ws_router)
            .layer(cors)
            .layer(trace_layer);

        // Static file serving
        // Priority: static_dir config > embedded assets (web-embed feature) > root handler
        if let Some(ref static_dir) = self.config.static_dir {
            info!(path = %static_dir, "Serving static files from directory");
            router = router.fallback_service(
                tower_http::services::ServeDir::new(static_dir)
                    .append_index_html_on_directories(true),
            );
        } else {
            #[cfg(feature = "web-embed")]
            {
                if embedded::has_embedded_assets() {
                    info!("Serving embedded WebUI assets");
                    router = router.merge(embedded::embedded_assets_router());
                } else {
                    info!("No embedded assets found, WebUI will not be served");
                    // Add root handler only if no embedded assets
                    router = router.route("/", get(root_handler));
                }
            }

            #[cfg(not(feature = "web-embed"))]
            {
                info!("No static_dir configured and web-embed feature not enabled");
                // Add root handler when web-embed is not available
                router = router.route("/", get(root_handler));
            }
        }

        router
    }

    /// Run the WebUI server
    pub async fn run(self) -> Result<()> {
        let addr: SocketAddr = self
            .config
            .socket_addr()
            .map_err(|e| crate::Error::Config(format!("Invalid address: {}", e)))?;

        let router = self.build_router();

        info!(address = %addr, "Starting WebUI server");

        // Start metrics collector
        let state = Arc::clone(&self.state);
        tokio::spawn(async move {
            if let Err(e) = state.metrics_collector().run().await {
                error!(error = %e, "Metrics collector failed");
            }
        });

        // Start alert engine if enabled
        if self.config.alerts.enabled {
            let state = Arc::clone(&self.state);
            tokio::spawn(async move {
                if let Err(e) = state.alert_engine().run().await {
                    error!(error = %e, "Alert engine failed");
                }
            });
        }

        // Run the server
        let listener = tokio::net::TcpListener::bind(addr)
            .await
            .map_err(crate::Error::Io)?;

        axum::serve(listener, router)
            .await
            .map_err(crate::Error::Io)?;

        Ok(())
    }

    /// Get a reference to the shared state
    pub fn state(&self) -> Arc<WebState> {
        Arc::clone(&self.state)
    }
}

/// Server features detection endpoint
///
/// GET /api/features
async fn server_features() -> Json<serde_json::Value> {
    Json(serde_json::json!({
        "admin": cfg!(feature = "admin"),
        "metrics": cfg!(feature = "metrics"),
        "audit": cfg!(feature = "web"),
    }))
}

/// Root handler - returns a simple info page or redirects to dashboard
async fn root_handler() -> axum::response::Html<&'static str> {
    axum::response::Html(
        r#"<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>LazyDNS WebUI</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            margin: 0 0 10px 0;
        }
        .info {
            color: #666;
            margin: 20px 0;
        }
        .links {
            margin: 20px 0;
        }
        a {
            display: inline-block;
            padding: 10px 15px;
            margin: 5px 0;
            background: #007bff;
            color: white;
            text-decoration: none;
            border-radius: 4px;
        }
        a:hover {
            background: #0056b3;
        }
        .divider {
            border-top: 1px solid #eee;
            margin: 20px 0;
        }
        .status {
            background: #d4edda;
            color: #155724;
            padding: 10px;
            border-radius: 4px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🚀 LazyDNS WebUI</h1>
        <div class="status">✓ Server is running</div>
        <p class="info">A light and fast DNS server implementation in Rust</p>
        
        <div class="divider"></div>
        
        <h3>API Endpoints:</h3>
        <div class="links">
            <a href="/api/dashboard/overview">Dashboard Overview</a>
            <a href="/api/dashboard/cache/stats">Cache Stats</a>
            <a href="/api/dashboard/server/info">Server Info</a>
        </div>
        
        <div class="divider"></div>
        
        <p style="color: #999; font-size: 12px;">
            For detailed API documentation, see the REST API at /api/...
        </p>
    </div>
</body>
</html>"#,
    )
}