Fix dependency injection and implement proper CIDR checking

- Add http module for proper FX dependency injection
- Fix router to accept state manager parameter
- Implement proper CIDR-based checking for RFC1918 and documentation IPs
- Add reasonable timeouts (30s) for database downloads
- Update tests to download databases to temporary directories
- Add tests for multiple IP lookups and error cases
- All tests passing
This commit is contained in:
2025-07-27 18:44:53 +02:00
parent 2a1710cca8
commit 08ca75966e
9 changed files with 446 additions and 78 deletions

View File

@@ -3,6 +3,8 @@ package config
import (
"os"
"path/filepath"
"runtime"
"strconv"
"git.eeqj.de/sneak/smartconfig"
@@ -41,7 +43,7 @@ func New(configFile string) (*Config, error) {
if stateDir, err := sc.GetString("state_dir"); err == nil {
cfg.StateDir = stateDir
} else {
cfg.StateDir = "/var/lib/ipapi"
cfg.StateDir = getDefaultStateDir()
}
// Get log level
@@ -57,7 +59,7 @@ func New(configFile string) (*Config, error) {
func newDefaultConfig() *Config {
return &Config{
Port: getPortFromEnv(),
StateDir: "/var/lib/ipapi",
StateDir: getDefaultStateDir(),
LogLevel: "info",
}
}
@@ -75,3 +77,27 @@ func getPortFromEnv() int {
return port
}
func getDefaultStateDir() string {
switch runtime.GOOS {
case "darwin":
// macOS: use ~/Library/Application Support/berlin.sneak.app.ipapi/
home, err := os.UserHomeDir()
if err != nil {
return "/var/lib/ipapi"
}
return filepath.Join(home, "Library", "Application Support", "berlin.sneak.app.ipapi")
case "windows":
// Windows: use %APPDATA%\berlin.sneak.app.ipapi
appData := os.Getenv("APPDATA")
if appData == "" {
return "/var/lib/ipapi"
}
return filepath.Join(appData, "berlin.sneak.app.ipapi")
default:
// Linux and other Unix-like systems
return "/var/lib/ipapi"
}
}

View File

@@ -15,8 +15,9 @@ func TestNewDefaultConfig(t *testing.T) {
if cfg.Port != 8080 {
t.Errorf("expected default port 8080, got %d", cfg.Port)
}
if cfg.StateDir != "/var/lib/ipapi" {
t.Errorf("expected default state dir /var/lib/ipapi, got %s", cfg.StateDir)
expectedStateDir := getDefaultStateDir()
if cfg.StateDir != expectedStateDir {
t.Errorf("expected default state dir %s, got %s", expectedStateDir, cfg.StateDir)
}
if cfg.LogLevel != "info" {
t.Errorf("expected default log level info, got %s", cfg.LogLevel)