Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/fail2ban-dashboard/fail2ban-dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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,
}
Expand Down
21 changes: 10 additions & 11 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,7 +41,7 @@ var jailCardHtml []byte
var bannedHtml []byte

type Configuration struct {
Port int
Address string
AuthUser string
AuthPassword string
}
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 10 additions & 10 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
}
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -465,15 +465,15 @@ func TestConfiguration(t *testing.T) {
{
name: "default configuration",
config: Configuration{
Port: 8080,
Address: ":8080",
AuthUser: "",
AuthPassword: "",
},
},
{
name: "with authentication",
config: Configuration{
Port: 3000,
Address: ":3000",
AuthUser: "admin",
AuthPassword: "secret",
},
Expand All @@ -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")
}
})
Expand Down Expand Up @@ -691,7 +691,7 @@ func TestAuthenticationScenarios(t *testing.T) {
{
name: "no authentication",
config: Configuration{
Port: 8080,
Address: ":8080",
AuthUser: "",
AuthPassword: "",
},
Expand All @@ -700,7 +700,7 @@ func TestAuthenticationScenarios(t *testing.T) {
{
name: "user only",
config: Configuration{
Port: 8080,
Address: ":8080",
AuthUser: "admin",
AuthPassword: "",
},
Expand All @@ -709,7 +709,7 @@ func TestAuthenticationScenarios(t *testing.T) {
{
name: "password only",
config: Configuration{
Port: 8080,
Address: ":8080",
AuthUser: "",
AuthPassword: "secret",
},
Expand All @@ -718,7 +718,7 @@ func TestAuthenticationScenarios(t *testing.T) {
{
name: "full authentication",
config: Configuration{
Port: 8080,
Address: ":8080",
AuthUser: "admin",
AuthPassword: "secret",
},
Expand Down