You've already forked caddy-opnsense-blocker
Simplify the dashboard recent IP view
This commit is contained in:
@@ -76,6 +76,23 @@ func (s *Service) ListIPs(ctx context.Context, limit int, state string) ([]model
|
||||
return s.store.ListIPStates(ctx, limit, state)
|
||||
}
|
||||
|
||||
func (s *Service) ListRecentIPs(ctx context.Context, since time.Time, limit int) ([]model.RecentIPRow, error) {
|
||||
items, err := s.store.ListRecentIPRows(ctx, since, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for index := range items {
|
||||
state := model.IPState{
|
||||
IP: items[index].IP,
|
||||
State: items[index].State,
|
||||
ManualOverride: items[index].ManualOverride,
|
||||
}
|
||||
backend := s.resolveOPNsenseStatus(ctx, state)
|
||||
items[index].Actions = actionAvailability(state, backend)
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetIPDetails(ctx context.Context, ip string) (model.IPDetails, error) {
|
||||
normalized, err := normalizeIP(ip)
|
||||
if err != nil {
|
||||
|
||||
@@ -126,6 +126,21 @@ sources:
|
||||
t.Fatalf("expected observed state, got %+v", observedState)
|
||||
}
|
||||
|
||||
recentRows, err := svc.ListRecentIPs(context.Background(), time.Now().UTC().Add(-time.Hour), 10)
|
||||
if err != nil {
|
||||
t.Fatalf("list recent ips: %v", err)
|
||||
}
|
||||
blockedRow, found := findRecentIPRow(recentRows, "203.0.113.10")
|
||||
if !found {
|
||||
t.Fatalf("expected blocked IP row in recent rows: %+v", recentRows)
|
||||
}
|
||||
if blockedRow.SourceName != "main" || blockedRow.Events != 1 {
|
||||
t.Fatalf("unexpected blocked recent row: %+v", blockedRow)
|
||||
}
|
||||
if !blockedRow.Actions.CanUnblock || blockedRow.Actions.CanBlock {
|
||||
t.Fatalf("unexpected blocked recent row actions: %+v", blockedRow.Actions)
|
||||
}
|
||||
|
||||
if err := svc.ForceAllow(context.Background(), "203.0.113.10", "test", "manual unblock"); err != nil {
|
||||
t.Fatalf("force allow: %v", err)
|
||||
}
|
||||
@@ -245,3 +260,12 @@ func waitFor(t *testing.T, timeout time.Duration, condition func() bool) {
|
||||
}
|
||||
t.Fatalf("condition was not met within %s", timeout)
|
||||
}
|
||||
|
||||
func findRecentIPRow(items []model.RecentIPRow, ip string) (model.RecentIPRow, bool) {
|
||||
for _, item := range items {
|
||||
if item.IP == ip {
|
||||
return item, true
|
||||
}
|
||||
}
|
||||
return model.RecentIPRow{}, false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user