Commit d69e61c
authored
feat: add admin API endpoints for monitoring and auditing (#158)
Add comprehensive admin API endpoints to enable programmatic monitoring and auditing of
WStunnel server operations:
- **`/admin/monitoring`**: Provides aggregate statistics (unique tunnels, connections,
request counts) for dashboards and alerting systems
- **`/admin/auditing`**: Provides detailed tunnel information including active connections,
client details, and request history for security auditing
- **Consolidated admin functionality** in `tunnel/admin_service.go` for maintainability
and extensibility
- **SQLite-based persistence** with automatic cleanup of records older than 7 days
- **Thread-safe operations** with proper mutex usage for concurrent access
- **Extensible design** allowing easy addition of new admin endpoints
- **Real-time tunnel monitoring**: Track active tunnels, connections, and request volumes
- **Request lifecycle tracking**: Complete audit trail from request start to
completion/error
- **Client identification**: IP addresses, reverse DNS, WHOIS data, and client version
tracking
- **Base path support**: Full compatibility with existing base path configuration
- **JSON API responses**: Machine-readable format suitable for automation and web UIs
- `request_events` table: Tracks all HTTP requests with status, timing, and client
information
- `tunnel_events` table: Records tunnel lifecycle events (connect/disconnect/errors)
- Automatic indexing for performance on common queries
- Built-in cleanup to prevent unbounded growth
```bash
curl http://localhost:8080/admin/monitoring
```
```json
{
"timestamp": "2025-01-09T15:30:45Z",
"unique_tunnels": 3,
"tunnel_connections": 3,
"pending_requests": 5,
"completed_requests": 1247,
"errored_requests": 23
}
```
```bash
curl http://localhost:8080/admin/auditing
```
```json
{
"timestamp": "2025-01-09T15:30:45Z",
"tunnels": {
"my_secret_token": {
"token": "my_secret_token",
"remote_addr": "192.168.1.100:54321",
"remote_name": "client.example.com",
"client_version": "wstunnel v1.0.0",
"last_activity": "2025-01-09T15:30:40Z",
"active_connections": [...],
"last_error_time": "2025-01-09T15:25:00Z",
"last_success_time": "2025-01-09T15:30:35Z",
"pending_requests": 1
}
}
}
```
- **Comprehensive test coverage** including lifecycle testing, error handling, and edge
cases
- **Integration tests** for HTTP handlers with proper status codes and JSON validation
- **Mock server testing** to ensure compatibility with existing WSTunnelServer
architecture
- **Base path integration testing** to verify proper path handling
- **User documentation** added to README.md with API examples and field descriptions
- **Operations guide** in `docs/ADMIN_API.md` with monitoring scripts, alerting rules,
and troubleshooting
- **Security recommendations** for production deployment
- **Monitoring dashboards**: Integrate with Prometheus, Grafana, or custom monitoring
systems
- **Security auditing**: Track client connections, identify suspicious activity, generate
compliance reports
- **Debugging and troubleshooting**: Inspect active connections, view recent errors,
identify performance issues
- **Capacity planning**: Monitor usage patterns and plan infrastructure scaling
- **Web UI development**: JSON APIs provide foundation for future web-based admin
interfaces
- **No breaking changes** to existing functionality
- **Optional initialization** - admin service gracefully handles initialization failures
- **Existing endpoints unchanged** - all current APIs remain fully functional
- **Configuration compatibility** - works with all existing server configuration
options
Resolves #143 #1441 parent 13d32db commit d69e61c
File tree
9 files changed
+1580
-2
lines changed- docs
- tunnel
9 files changed
+1580
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
22 | 25 | | |
23 | 26 | | |
24 | 27 | | |
25 | 28 | | |
26 | 29 | | |
| 30 | + | |
| 31 | + | |
27 | 32 | | |
28 | 33 | | |
29 | 34 | | |
| |||
99 | 104 | | |
100 | 105 | | |
101 | 106 | | |
102 | | - | |
| 107 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
151 | 151 | | |
152 | 152 | | |
153 | 153 | | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
154 | 160 | | |
155 | 161 | | |
156 | 162 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
358 | 358 | | |
359 | 359 | | |
360 | 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 | + | |
361 | 467 | | |
362 | 468 | | |
363 | 469 | | |
| |||
0 commit comments