Files
auth-proxy/main.go
db123-test f525eaa8f3 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
2026-05-05 11:34:35 +03:30

68 lines
1.7 KiB
Go

package main
import (
"context"
"log"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
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)))
db, err := InitDB(cfg.DBPath)
if err != nil {
slog.Error("failed to init database", "error", err)
os.Exit(1)
}
defer db.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go cleanupLoop(ctx, db, cfg.CleanupInterval)
mux := http.NewServeMux()
mux.HandleFunc("/auth", authHandler(cfg, db))
mux.HandleFunc("/api/whitelist/temp", whitelistTempHandler(cfg, db))
mux.HandleFunc("/api/whitelist/temp/list", whitelistListHandler(cfg, db))
mux.HandleFunc("/api/whitelist/temp/{ip}", whitelistDeleteHandler(cfg, db))
mux.HandleFunc("/api/whitelist/perm", permWhitelistAddHandler(cfg, db))
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(db))
server := &http.Server{
Addr: ":" + cfg.Port,
Handler: mux,
}
go func() {
slog.Info("server listening", "addr", server.Addr)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
slog.Error("server error", "error", err)
os.Exit(1)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT)
<-quit
slog.Info("shutting down...")
cancel()
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 30*time.Second)
defer shutdownCancel()
server.Shutdown(shutdownCtx)
}