lazydns 0.2.63

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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
# Admin API Usage Guide

The Admin API provides runtime management and monitoring capabilities for the Lazy DNS server without requiring a restart. This document covers all available endpoints, configuration, and usage examples.

## Table of Contents

- [Overview]#overview
- [Configuration]#configuration
- [Security Considerations]#security-considerations
- [API Reference]#api-reference
  - [Server Status]#server-status
  - [Cache Statistics]#cache-statistics
  - [Cache Control]#cache-control
  - [Configuration Reload]#configuration-reload
- [Examples]#examples
  - [curl Examples]#curl-examples
  - [HTTP Status Codes]#http-status-codes
- [Monitoring Integration]#monitoring-integration
- [Troubleshooting]#troubleshooting

## Overview

The Admin API is a separate HTTP server that runs alongside your main DNS servers. It provides endpoints for:

- **Monitoring**: Check server status and cache performance metrics
- **Cache Management**: View cache statistics and clear the cache
- **Configuration**: Reload configuration without restarting the entire server

### Features

- Real-time cache statistics (size, hit rate, evictions)
- Cache control operations (clear)
- Configuration hot-reload
- Server status and version information
- Separate HTTP server (non-blocking)

### Limitations

- No built-in authentication (see [Security Considerations]#security-considerations)
- Configuration reload validates but doesn't automatically apply to running plugins
- Cache clear affects all domains immediately

## Configuration

### Basic Setup

Enable the Admin API in your `config.yaml`:

```yaml
admin:
  enabled: true
  addr: "127.0.0.1:8080"
```

### Configuration Options

| Option    | Type    | Default          | Description                  |
| --------- | ------- | ---------------- | ---------------------------- |
| `enabled` | boolean | `false`          | Enable/disable the admin API |
| `addr`    | string  | `127.0.0.1:8080` | Listen address and port      |

### Environment Variables

You can override the admin configuration using environment variables:

```bash
# Enable the admin API
export ADMIN_ENABLED=true

# Set the listen address
export ADMIN_ADDR=0.0.0.0:8080

# Start the server
lazydns
```

Supported boolean values for `ADMIN_ENABLED`: `true`, `1`, `yes` (case-insensitive)

### Examples

#### Localhost Only (Default - Recommended for Production)

```yaml
admin:
  enabled: true
  addr: "127.0.0.1:8080"
```

#### Internal Network

```yaml
admin:
  enabled: true
  addr: "192.168.1.100:8080"
```

#### All Interfaces (Use with Caution)

```yaml
admin:
  enabled: true
  addr: "0.0.0.0:8080"
```

## Security Considerations

**Important**: The Admin API has **no built-in authentication**. Anyone who can connect to the configured address can:

- Clear the cache (affecting performance)
- Reload configuration (potentially with malicious config)
- Read server status and metrics

### Security Recommendations

1. **Bind to Localhost**: Use `127.0.0.1:8080` (default) for single-machine setups
2. **Firewall Rules**: Restrict access via network-level firewall
3. **Reverse Proxy with Auth**: Use a reverse proxy (nginx, HAProxy) with authentication:
   ```nginx
   location /api/ {
       auth_basic "Admin API";
       auth_basic_user_file /etc/nginx/.htpasswd;
       proxy_pass http://127.0.0.1:8080;
   }
   ```
4. **VPN/Private Network**: Run the server on a private network, access via VPN
5. **Application-Level Auth**: Implement auth in a reverse proxy layer

### Example: nginx Reverse Proxy with Basic Auth

```nginx
server {
    listen 8443 ssl http2;
    server_name admin.example.com;

    # SSL configuration
    ssl_certificate /etc/ssl/certs/cert.pem;
    ssl_certificate_key /etc/ssl/private/key.pem;

    location / {
        auth_basic "Admin API";
        auth_basic_user_file /etc/nginx/.htpasswd;

        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
```

## API Reference

### Server Status

**Endpoint**: `GET /api/server/stats`

Returns basic information about the server's operational status.

#### Request

```bash
curl http://127.0.0.1:8080/api/server/stats
```

#### Response

```json
{
  "status": "running",
  "version": "0.2.8",
  "uptime": "3d 04:12:32"
}
```

#### HTTP Status Codes

- `200 OK` - Server is operational

#### Use Cases

- Health checks
- Monitoring dashboards
- Uptime verification (field `uptime` is included in the response and is formatted as `Nd HH:MM:SS` when >= 1 day, otherwise `HH:MM:SS`, e.g., `21d 15:05:37`)


---

### Cache Statistics

**Endpoint**: `GET /api/cache/stats`

Retrieves detailed statistics about cache performance and utilization.

#### Request

```bash
curl http://127.0.0.1:8080/api/cache/stats
```

#### Response

```json
{
  "size": 245,
  "hits": 5800,
  "misses": 1200,
  "evictions": 42,
  "hit_rate": 82.86
}
```

#### Response Fields

| Field       | Type   | Description                            |
| ----------- | ------ | -------------------------------------- |
| `size`      | number | Current number of entries in the cache |
| `hits`      | number | Total cache hits since server start    |
| `misses`    | number | Total cache misses since server start  |
| `evictions` | number | Entries removed by LRU eviction policy |
| `hit_rate`  | number | Cache hit rate as percentage (0-100)   |

#### HTTP Status Codes

- `200 OK` - Statistics retrieved successfully
- `404 Not Found` - Cache plugin not configured
- `500 Internal Server Error` - Plugin access failed

#### Interpretation

The hit rate tells you how often cached results are used:

```
hit_rate = (hits / (hits + misses)) * 100
```

- **90%+ hit rate**: Excellent cache effectiveness
- **70-90% hit rate**: Good cache performance
- **< 70% hit rate**: Consider optimization or cache size increase

#### Use Cases

- Performance monitoring
- Cache effectiveness analysis
- Capacity planning
- SLA reporting

---

### Cache Control

**Endpoint**: `POST /api/cache/control`

Perform control operations on the cache system.

#### Request

**Clear Cache**

```bash
curl -X POST http://127.0.0.1:8080/api/cache/control \
  -H "Content-Type: application/json" \
  -d '{"action": "clear"}'
```

#### Request Body

```json
{
  "action": "clear"
}
```

#### Response (Success)

```json
{
  "message": "Cache cleared successfully"
}
```

#### Response (Error)

```json
{
  "error": "Cache not configured"
}
```

#### HTTP Status Codes

- `200 OK` - Operation completed successfully
- `400 Bad Request` - Unknown action or invalid request
- `404 Not Found` - Cache not configured
- `500 Internal Server Error` - Plugin access failed

#### Supported Actions

| Action  | Description                       |
| ------- | --------------------------------- |
| `clear` | Remove all entries from the cache |

#### Use Cases

- Flush cache after configuration changes
- Test cache behavior
- Emergency cache clearing
- Performance testing

#### Important Notes

- Cache clear is **immediate** and affects all domains
- All cached DNS records will be re-fetched
- May cause **temporary increased latency** during refetch
- No cache invalidation granularity (no per-domain clear)

---

### Configuration Reload

**Endpoint**: `POST /api/config/reload`

Reload configuration from a file and validate it. This is a **hot-reload** operation that updates the in-memory configuration.

#### Request

**With Custom Path**

```bash
curl -X POST http://127.0.0.1:8080/api/config/reload \
  -H "Content-Type: application/json" \
  -d '{"path": "/etc/lazydns/config.yaml"}'
```

**With Default Path**

```bash
curl -X POST http://127.0.0.1:8080/api/config/reload \
  -H "Content-Type: application/json" \
  -d '{"path": null}'
```

Or:

```bash
curl -X POST http://127.0.0.1:8080/api/config/reload \
  -H "Content-Type: application/json" \
  -d '{}'
```

#### Request Body

```json
{
  "path": "/etc/lazydns/config.yaml"
}
```

#### Response (Success)

```json
{
  "message": "Configuration reloaded from /etc/lazydns/config.yaml"
}
```

#### Response (Error)

```json
{
  "error": "Configuration validation failed: invalid port number"
}
```

#### HTTP Status Codes

- `200 OK` - Configuration reloaded and validated successfully
- `400 Bad Request` - Configuration validation failed
- `500 Internal Server Error` - Failed to load file

#### Behavior

1. Loads the configuration file from disk
2. Validates the configuration structure
3. Updates the in-memory configuration
4. **Does not restart plugins** (requires full server restart for some changes)

#### Configuration Fields That Take Effect Immediately

- Log level and settings
- Admin server settings
- Some plugin parameters

#### Configuration Fields That Require Server Restart

- Server bindings (listen addresses)
- TLS certificates
- Plugin chain definitions

#### Use Cases

- Adjust log levels without restart
- Enable/disable features
- Update allow/blocklists
- Modify timeouts

#### Important Notes

- Configuration changes are **best-effort**
- Some changes (like server listen addresses) require a full restart
- The endpoint validates syntax but doesn't test actual functionality
- Always test configuration changes in a dev environment first

---

## Examples

### curl Examples

#### Check if server is running

```bash
curl -s http://127.0.0.1:8080/api/server/stats | jq .
```

#### Get cache statistics and parse with jq

```bash
curl -s http://127.0.0.1:8080/api/cache/stats | jq .
```

#### Get only the hit rate

```bash
curl -s http://127.0.0.1:8080/api/cache/stats | jq .hit_rate
```

#### Clear cache and show result

```bash
curl -s -X POST http://127.0.0.1:8080/api/cache/control \
  -H "Content-Type: application/json" \
  -d '{"action": "clear"}' | jq .
```

#### Monitor cache in real-time (every 5 seconds)

```bash
watch -n 5 'curl -s http://127.0.0.1:8080/api/cache/stats | jq .'
```

#### Reload config with error checking

```bash
response=$(curl -s -w "\n%{http_code}" -X POST \
  http://127.0.0.1:8080/api/config/reload \
  -H "Content-Type: application/json" \
  -d '{}')

body=$(echo "$response" | head -n -1)
code=$(echo "$response" | tail -n 1)

if [ "$code" = "200" ]; then
  echo "Config reloaded successfully"
  echo "$body" | jq .
else
  echo "Config reload failed with code $code"
  echo "$body" | jq .error
fi
```

### HTTP Status Codes

| Code  | Meaning               | Common Causes                                    |
| ----- | --------------------- | ------------------------------------------------ |
| `200` | OK                    | Request succeeded                                |
| `400` | Bad Request           | Invalid action, malformed JSON, validation error |
| `404` | Not Found             | Cache not configured, plugin not available       |
| `500` | Internal Server Error | Plugin downcast failed, file I/O errors          |

#### Status Code Decision Tree

```
Request fails?
├─ Network error → Retry with backoff
├─ 404 Not Found → Check configuration
├─ 400 Bad Request → Check request format and action
├─ 500 Internal Server Error → Check logs and plugin status
└─ 200 OK → Operation succeeded
```

## Monitoring Integration

### Prometheus Integration

The Admin API complements the separate [Monitoring Server](MONITORING_USAGE.md) which provides:

- **Admin API** (this): Runtime management and real-time statistics
- **Monitoring Server**: Prometheus-compatible metrics at `/metrics`

**Note on the Monitoring server `/stats` endpoint aggregation:**
- The monitoring `/stats` handler computes `total_queries` and `active_connections` by aggregating Prometheus samples from the server registry rather than maintaining separate counters:
  - `total_queries` = sum of all samples of the `dns_queries_total` metric across labels (covers all protocols/label variants).
  - `active_connections` = sum of all samples of the `dns_active_connections` gauge across labels (covers `udp`, `tcp`, `doh`, `dot`, etc.).
- This approach ensures the `/stats` snapshot automatically accounts for newly-added protocols and label combinations without additional bookkeeping. For extreme throughput environments you may prefer to maintain dedicated aggregated metrics at the source for lower latency.

For production monitoring, use both:

```yaml
admin:
  enabled: true
  addr: "127.0.0.1:8080"

monitoring:
  enabled: true
  addr: "127.0.0.1:9090"
```

#### Example Prometheus Scrape Config

```yaml
global:
  scrape_interval: 15s

scrape_configs:
  - job_name: "lazydns"
    static_configs:
      - targets: ["localhost:9090"]
```

### Grafana Dashboards

You can create dashboards using metrics from the monitoring server:

- `dns_queries_total` - Total DNS queries
- `dns_responses_total` - DNS responses by status
- `dns_query_duration_seconds` - Query latency histogram
- `dns_cache_hits_total` - Cache hits
- `dns_cache_misses_total` - Cache misses
- `dns_cache_size` - Current cache size
- `dns_uptime_seconds` - Process uptime in seconds (set by the monitoring server when `/metrics` is scraped)

For real-time management, use Admin API endpoints directly in dashboard plugins.

#### Cache metrics mapping & Prometheus usage

The cache subsystem exposes the following Prometheus metrics when the crate is built with the `metrics` feature (enabled by default):

- **`dns_cache_hits_total`** (counter): total number of cache hits recorded by the `CachePlugin`.
- **`dns_cache_misses_total`** (counter): total number of cache misses recorded by the `CachePlugin`.
- **`dns_cache_size`** (gauge): current number of entries in the cache (updated on insert/evict/clear).

Notes:

- These metrics are updated by the `CachePlugin` implementation and are only available when the `metrics` feature is enabled.
- The Admin API endpoint `GET /api/cache/stats` returns a snapshot (size, hits, misses, evictions, hit_rate). Use Prometheus metrics for time-series and alerting, and the Admin API for on-demand inspection or control.

##### Useful PromQL examples

- Instant cache hit rate (5m window):

```promql
(sum(rate(dns_cache_hits_total[5m]))
  / (sum(rate(dns_cache_hits_total[5m])) + sum(rate(dns_cache_misses_total[5m]))))
```

- Alert when hit rate is below 70% for 5 minutes:

```yaml
- alert: LowCacheHitRate
  expr: (sum(rate(dns_cache_hits_total[5m]))
    / (sum(rate(dns_cache_hits_total[5m])) + sum(rate(dns_cache_misses_total[5m])))) < 0.7
  for: 5m
  labels:
    severity: warning
  annotations:
    summary: "Cache hit rate below 70%"
    description: "Cache hit rate is below 70% for at least 5 minutes."
```

- Monitor cache size and alert when it grows unexpectedly (example):

```yaml
- alert: HighCacheSize
  expr: max(dns_cache_size) > 10000
  for: 10m
  labels:
    severity: warning
  annotations:
    summary: "Cache size exceeds threshold"
    description: "Cache size is larger than expected (threshold = 10000)."
```

##### Cross-check with Admin API

For quick, on-demand verification you can cross-check Prometheus metrics against the Admin API snapshot:

```bash
curl http://127.0.0.1:8080/api/cache/stats | jq .
# compare size/hits/misses with Prometheus values for sanity checks
```

### Alerting Examples

#### Alert: Low Cache Hit Rate

```yaml
- alert: LowCacheHitRate
  expr: cache_hit_rate < 0.7
  for: 5m
  annotations:
    summary: "Cache hit rate below 70%"
```

#### Alert: High Cache Evictions

```yaml
- alert: HighEvictions
  expr: increase(cache_evictions_total[5m]) > 1000
  for: 5m
  annotations:
    summary: "High number of cache evictions"
```

## Troubleshooting

### Connection Refused

**Problem**: `curl: (7) Failed to connect`

**Causes**:

- Admin API not enabled in config
- Wrong address/port
- Firewall blocking connection

**Solution**:

```bash
# Verify enabled in config
grep -A 2 'admin:' config.yaml

# Check if port is listening
netstat -tlnp | grep 8080

# Check firewall
sudo ufw allow 8080
```

### 404 Not Found on Cache Endpoints

**Problem**: `{"error": "Cache not configured"}`

**Causes**:

- Cache plugin not enabled
- Cache plugin disabled in configuration

**Solution**:

```yaml
# Ensure cache is in plugin chain
plugins:
  - type: cache
    args:
      size: 10000
```


### High Latency After Cache Clear

**Problem**: DNS queries slow after clearing cache

**Expected Behavior**: This is normal. Cache clear forces upstream queries for all domains.

**Solution**:

- Clear cache during low-traffic periods
- Monitor cache hit rate before clearing: `curl http://127.0.0.1:8080/api/cache/stats`
- Use selective configuration reloads instead of full cache clear when possible

### Admin API Not Responding

**Problem**: Admin API hangs or times out

**Diagnostic Steps**:

```bash
# Test connectivity
timeout 5 curl -v http://127.0.0.1:8080/api/server/stats

# Check server logs
tail -f /var/log/lazydns/main.log

# Check system resources
ps aux | grep lazydns
free -h
df -h
```

### Unicode/Special Characters in Error Messages

If you see mojibake (garbled characters) in responses, it's likely a terminal encoding issue:

```bash
# Force UTF-8
export LANG=en_US.UTF-8
curl http://127.0.0.1:8080/api/cache/stats | jq .
```