Skip to content

Commit d69e61c

Browse files
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 #144
1 parent 13d32db commit d69e61c

File tree

9 files changed

+1580
-2
lines changed

9 files changed

+1580
-2
lines changed

CLAUDE.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@ WStunnel is a WebSocket-based reverse HTTP/HTTPS tunneling solution that enables
1919
- `go test -run="<test name>" ./tunnel` - Run a single test
2020
- `go test ./...` - Run all tests in the project
2121

22+
## Format Commands
23+
- `make format` - Format all Go files with gofmt
24+
2225
## Lint Commands
2326
- `make lint` - Run gofmt check, go vet, golangci-lint, and yamllint
2427
- `golangci-lint run` - Run comprehensive linting checks
2528
- `yamllint .github/workflows/` - Check YAML formatting in GitHub workflows
2629

30+
**IMPORTANT** Always run `make format`, `make lint`, and `make test` after making code changes
31+
2732
## Code Style Guidelines
2833
- **Formatting**: Use gofmt, tabs for indentation
2934
- **Imports**: Standard library first, third-party after, group related imports
@@ -99,4 +104,4 @@ Use `~/bin/coderabbit-fix` to automatically apply CodeRabbit suggestions:
99104
- `coderabbit-fix 153` - Apply all fixes from PR 153
100105
- `coderabbit-fix 153 --dry-run` - Show what would be changed without applying
101106
- The tool extracts detailed instructions from CodeRabbit comments including "Prompt for AI Agents" sections
102-
- Always run `make lint` and `make test` after applying fixes to ensure code quality
107+
- Always run `make format`, `make lint` and `make test` after applying fixes to ensure code quality

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ yamllint-fix:
151151
echo "yamllint not found. Install with: pip install yamllint"; \
152152
fi
153153

154+
# Format all Go files
155+
format:
156+
@echo "Formatting Go files..."
157+
gofmt -w $(shell find . -type f -not -path './.*' -not -path './vendor/*' -not -name 'version.go' -name '*.go')
158+
@echo "✓ All .go files formatted"
159+
154160
travis-test: lint
155161
go test -race -coverprofile=coverage.txt -covermode=atomic ./...
156162

README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,112 @@ The configuration limits section shows:
358358

359359
Note: Full statistics are only available when the endpoint is accessed from localhost. Remote requests will only see the total number of tunnels.
360360

361+
### Admin API Endpoints
362+
363+
WStunnel server provides two JSON API endpoints for programmatic monitoring and auditing:
364+
365+
#### `/admin/monitoring` - Aggregate Statistics
366+
367+
Returns high-level statistics in JSON format, suitable for monitoring dashboards and alerting systems.
368+
369+
**Example Request:**
370+
```bash
371+
curl http://localhost:8080/admin/monitoring
372+
# With base path:
373+
curl http://localhost:8080/wstunnel/admin/monitoring
374+
```
375+
376+
**Example Response:**
377+
```json
378+
{
379+
"timestamp": "2025-01-09T15:30:45Z",
380+
"unique_tunnels": 3,
381+
"tunnel_connections": 3,
382+
"pending_requests": 5,
383+
"completed_requests": 1247,
384+
"errored_requests": 23
385+
}
386+
```
387+
388+
**Fields:**
389+
- `unique_tunnels`: Number of unique tunnel tokens registered
390+
- `tunnel_connections`: Number of active tunnel connections
391+
- `pending_requests`: Current number of requests waiting for response
392+
- `completed_requests`: Total successful requests since server start
393+
- `errored_requests`: Total failed requests since server start
394+
395+
#### `/admin/auditing` - Detailed Tunnel Information
396+
397+
Returns comprehensive details about all active tunnels and their connections, suitable for security auditing and detailed analysis.
398+
399+
**Example Request:**
400+
```bash
401+
curl http://localhost:8080/admin/auditing
402+
# With base path:
403+
curl http://localhost:8080/wstunnel/admin/auditing
404+
```
405+
406+
**Example Response:**
407+
```json
408+
{
409+
"timestamp": "2025-01-09T15:30:45Z",
410+
"tunnels": {
411+
"my_secret_token": {
412+
"token": "my_secret_token",
413+
"remote_addr": "192.168.1.100:54321",
414+
"remote_name": "client.example.com",
415+
"remote_whois": "Example Corp",
416+
"client_version": "wstunnel v1.0.0",
417+
"last_activity": "2025-01-09T15:30:40Z",
418+
"active_connections": [
419+
{
420+
"request_id": 123,
421+
"method": "GET",
422+
"uri": "/api/data",
423+
"remote_addr": "10.0.0.5",
424+
"start_time": "2025-01-09T15:30:30Z"
425+
}
426+
],
427+
"last_error_time": "2025-01-09T15:25:00Z",
428+
"last_error_addr": "10.0.0.3",
429+
"last_success_time": "2025-01-09T15:30:35Z",
430+
"last_success_addr": "10.0.0.5",
431+
"pending_requests": 1
432+
}
433+
}
434+
}
435+
```
436+
437+
**Tunnel Fields:**
438+
- `token`: The tunnel token (first 8 characters shown in logs)
439+
- `remote_addr`: IP address and port of the tunnel client
440+
- `remote_name`: Reverse DNS lookup of the client IP
441+
- `remote_whois`: WHOIS information for the client IP (if available)
442+
- `client_version`: Version string reported by the tunnel client
443+
- `last_activity`: Timestamp of last tunnel activity
444+
- `active_connections`: Array of currently active HTTP requests
445+
- `last_error_time`: Timestamp of most recent failed request (optional)
446+
- `last_error_addr`: IP address of most recent failed request (optional)
447+
- `last_success_time`: Timestamp of most recent successful request (optional)
448+
- `last_success_addr`: IP address of most recent successful request (optional)
449+
- `pending_requests`: Number of requests currently pending
450+
451+
**Active Connection Fields:**
452+
- `request_id`: Unique identifier for the request
453+
- `method`: HTTP method (GET, POST, etc.)
454+
- `uri`: The requested URI path
455+
- `remote_addr`: IP address of the client making the request
456+
- `start_time`: When the request was initiated
457+
458+
**Use Cases:**
459+
- **Monitoring**: Use `/admin/monitoring` for dashboards, alerting, and performance tracking
460+
- **Security Auditing**: Use `/admin/auditing` to track which clients are connecting from where
461+
- **Debugging**: Use `/admin/auditing` to see active requests and recent errors
462+
- **Capacity Planning**: Monitor request volumes and tunnel usage patterns
463+
- **Web UI Integration**: Both endpoints return JSON suitable for web-based admin interfaces
464+
465+
**Security Note:** These endpoints are accessible without authentication. In production environments, consider placing them behind a reverse proxy with appropriate access controls.
466+
361467
### Reading wstunnel server logs
362468

363469
Sample:

0 commit comments

Comments
 (0)