diff --git a/cmd/fail2ban-dashboard/fail2ban-dashboard.go b/cmd/fail2ban-dashboard/fail2ban-dashboard.go index 324f30a..29a665d 100644 --- a/cmd/fail2ban-dashboard/fail2ban-dashboard.go +++ b/cmd/fail2ban-dashboard/fail2ban-dashboard.go @@ -18,7 +18,7 @@ var GitHash = "none" var supportedVersions = []string{"1.1.0"} -var port int +var address string var cacheDir string var user string var password string @@ -41,7 +41,7 @@ var versionCmd = &cobra.Command{ } func init() { - rootCmd.Flags().IntVarP(&port, "port", "p", 3000, "port to serve the dashboard on") + rootCmd.Flags().StringVarP(&address, "address", "a", "127.0.0.1:3000", "address to serve the dashboard on") rootCmd.Flags().StringVarP(&cacheDir, "cache-dir", "c", "", "directory to cache GeoIP data (default current working directory)") rootCmd.Flags().StringVar(&user, "auth-user", "", "username for basic auth") rootCmd.Flags().StringVar(&password, "auth-password", "", "password for basic auth") @@ -117,7 +117,7 @@ func run(cmd *cobra.Command, args []string) { geoIP := geoip.NewGeoIP(absolutCacheDir) configuration := &server.Configuration{ - Port: port, + Address: address, AuthUser: user, AuthPassword: password, } diff --git a/server/server.go b/server/server.go index 3ffefa1..4d19ded 100644 --- a/server/server.go +++ b/server/server.go @@ -4,19 +4,19 @@ import ( "crypto/rand" _ "embed" "encoding/binary" - "fmt" - "github.com/gofiber/fiber/v2" - "github.com/gofiber/fiber/v2/log" - "github.com/gofiber/fiber/v2/middleware/basicauth" - client "github.com/webishdev/fail2ban-dashboard/fail2ban-client" - "github.com/webishdev/fail2ban-dashboard/geoip" - "github.com/webishdev/fail2ban-dashboard/store" "html/template" "net" "sort" "strconv" "strings" "time" + + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/log" + "github.com/gofiber/fiber/v2/middleware/basicauth" + client "github.com/webishdev/fail2ban-dashboard/fail2ban-client" + "github.com/webishdev/fail2ban-dashboard/geoip" + "github.com/webishdev/fail2ban-dashboard/store" ) //go:embed resources/css/daisyui@5.css @@ -41,7 +41,7 @@ var jailCardHtml []byte var bannedHtml []byte type Configuration struct { - Port int + Address string AuthUser string AuthPassword string } @@ -190,10 +190,9 @@ func Serve(version string, fail2banVersion string, store *store.DataStore, geoIP store.Start() - log.Infof("Listening on port %d", configuration.Port) + log.Infof("Listening on address %d", configuration.Address) - address := fmt.Sprintf(":%d", configuration.Port) - return app.Listen(address) + return app.Listen(configuration.Address) } func sortSlice(sorting string, order string, banned []client.BanEntry) func(i, j int) bool { diff --git a/server/server_test.go b/server/server_test.go index dd1393d..2ec0873 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -72,7 +72,7 @@ func createTestJail(name string, entries []client.BanEntry) store.Jail { func createTestApp(mockStore *MockDataStore, mockGeoIP *MockGeoIP, config *Configuration) *fiber.App { if config == nil { config = &Configuration{ - Port: 8080, + Address: ":8080", AuthUser: "", AuthPassword: "", } @@ -438,14 +438,14 @@ func TestStaticFileHandlers(t *testing.T) { func TestConfigurationStructure(t *testing.T) { config := &Configuration{ - Port: 0, // Use port 0 to avoid conflicts + Address: ":0", // Use port 0 to avoid conflicts AuthUser: "", AuthPassword: "", } // Test configuration structure t.Run("configuration validation", func(t *testing.T) { - if config.Port != 0 { + if config.Address != ":0" { t.Error("Expected port to be 0 for test") } if config.AuthUser != "" { @@ -465,7 +465,7 @@ func TestConfiguration(t *testing.T) { { name: "default configuration", config: Configuration{ - Port: 8080, + Address: ":8080", AuthUser: "", AuthPassword: "", }, @@ -473,7 +473,7 @@ func TestConfiguration(t *testing.T) { { name: "with authentication", config: Configuration{ - Port: 3000, + Address: ":3000", AuthUser: "admin", AuthPassword: "secret", }, @@ -482,7 +482,7 @@ func TestConfiguration(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if tt.config.Port <= 0 && tt.name != "default configuration" { + if tt.config.Address <= "0" && tt.name != "default configuration" { t.Error("Port should be positive") } }) @@ -691,7 +691,7 @@ func TestAuthenticationScenarios(t *testing.T) { { name: "no authentication", config: Configuration{ - Port: 8080, + Address: ":8080", AuthUser: "", AuthPassword: "", }, @@ -700,7 +700,7 @@ func TestAuthenticationScenarios(t *testing.T) { { name: "user only", config: Configuration{ - Port: 8080, + Address: ":8080", AuthUser: "admin", AuthPassword: "", }, @@ -709,7 +709,7 @@ func TestAuthenticationScenarios(t *testing.T) { { name: "password only", config: Configuration{ - Port: 8080, + Address: ":8080", AuthUser: "", AuthPassword: "secret", }, @@ -718,7 +718,7 @@ func TestAuthenticationScenarios(t *testing.T) { { name: "full authentication", config: Configuration{ - Port: 8080, + Address: ":8080", AuthUser: "admin", AuthPassword: "secret", },