feat: enhance auth, add input validation and pagination

- Add IP validation in auth handler with detailed logging
- Implement apiKeyMiddleware helper for API key verification
- Add IP and CIDR validation for whitelist operations
- Support pagination for listing temp and permanent entries
- Add parsePagination helper to extract limit and offset
- Include comprehensive unit tests for IsIP, IsCIDR, and IPMatch
This commit is contained in:
db123-test
2026-05-05 11:34:35 +03:30
parent 93c9eb2a45
commit f525eaa8f3
5 changed files with 383 additions and 17 deletions

18
main.go
View File

@@ -2,6 +2,7 @@ package main
import (
"context"
"log"
"log/slog"
"net/http"
"os"
@@ -12,6 +13,9 @@ import (
func main() {
cfg := loadConfig()
if cfg.APIToken == "" {
log.Fatal("AUTH_PROXY_API_TOKEN environment variable is required")
}
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, nil)))
@@ -22,7 +26,10 @@ func main() {
}
defer db.Close()
go cleanupLoop(db, cfg.CleanupInterval)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go cleanupLoop(ctx, db, cfg.CleanupInterval)
mux := http.NewServeMux()
mux.HandleFunc("/auth", authHandler(cfg, db))
@@ -33,7 +40,7 @@ func main() {
mux.HandleFunc("/api/whitelist/perm/list", permWhitelistListHandler(cfg, db))
mux.HandleFunc("/api/whitelist/perm/{entry}", permWhitelistDeleteHandler(cfg, db))
mux.HandleFunc("/api/logs", logsHandler(cfg, db))
mux.HandleFunc("/status", statusHandler)
mux.HandleFunc("/status", statusHandler(db))
server := &http.Server{
Addr: ":" + cfg.Port,
@@ -53,7 +60,8 @@ func main() {
<-quit
slog.Info("shutting down...")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
server.Shutdown(ctx)
cancel()
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 30*time.Second)
defer shutdownCancel()
server.Shutdown(shutdownCtx)
}