urlcat

package
v1.0.134 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package urlcat is the admin-managed URL-category engine: named categories with lowercase host-set indexing for O(labels) membership checks during policy evaluation, JSON file persistence, and the built-in + embedded-SaaS default seed list. Extracted from package main's policy.go per ADR-0002 (policy.go decomposition Phase A).

package main keeps the surfaces: the `catStore` singleton, the TWO-TIER category resolution (matchCategory / lookupHostCategory compose this store with the community BadgerDB feed), the API handlers, cluster sync, and config-version rollback — all through aliases.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Category

type Category string

Category names a URL category referenced by policy rules.

const (
	Social    Category = "Social Media"
	Malicious Category = "Malicious"
	News      Category = "News"
	Streaming Category = "Streaming"
	Gambling  Category = "Gambling"
	Adult     Category = "Adult"
	Any       Category = "Any"
)

Built-in category names (the policy engine's rule vocabulary).

type Entry

type Entry struct {
	Name    string   `json:"name"`
	Hosts   []string `json:"hosts"`
	BuiltIn bool     `json:"builtIn"` // seeded from built-in defaults; editable by admin
}

Entry is one named URL category with its list of host patterns.

func DefaultEntries

func DefaultEntries() []*Entry

DefaultEntries returns the built-in hardcoded categories merged with the embedded SaaS category seed list.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store manages URL categories with thread-safe, file-backed persistence. index maps lowercase(category-name) → set of lowercase host strings for O(1) host membership checks during policy evaluation.

func New

func New(entries []*Entry) *Store

New builds a store over entries and its derived host index.

func (*Store) AddHost

func (s *Store) AddHost(category, host string) error

AddHost appends a host to the named category (no-op if already present).

func (*Store) All

func (s *Store) All() []Entry

All returns a copy of all category entries.

func (*Store) Delete

func (s *Store) Delete(name string) error

Delete removes a category by name. Returns an error if not found.

func (*Store) GetByName

func (s *Store) GetByName(name string) *Entry

GetByName finds a category by name (case-insensitive). Returns the live entry pointer (callers treat it as read-only outside the store's lock — pre-extraction contract preserved).

func (*Store) Load

func (s *Store) Load(path string) error

Load reads categories from a JSON file. If the file does not exist the built-in defaults are seeded and written to disk.

func (*Store) LookupHost

func (s *Store) LookupHost(host string) (category, matchedBy string, ok bool)

LookupHost resolves a hostname to its category by scanning entries (exact + suffix match), returning the original-case category name and the pattern that matched. Deliberately scans the entry lists — not the lowercase index — so matchedBy reports the admin's configured pattern verbatim (admin URL-lookup API contract).

func (*Store) MatchesHost

func (s *Store) MatchesHost(cat Category, host string) bool

MatchesHost checks whether host belongs to the named URL category. Uses the pre-built index for O(labels) lookup instead of O(N×M) iteration.

func (*Store) Path

func (s *Store) Path() string

Path reports the persistence path ("" = persistence disabled).

func (*Store) RemoveHost

func (s *Store) RemoveHost(category, host string) error

RemoveHost deletes a host from the named category.

func (*Store) ReplaceAll

func (s *Store) ReplaceAll(cats []Entry)

ReplaceAll atomically replaces all categories (used by cluster config sync).

func (*Store) Save

func (s *Store) Save()

Save atomically persists categories to disk.

func (*Store) Set

func (s *Store) Set(name string, hosts []string, builtIn bool) error

Set creates or replaces the host list for a named category.

func (*Store) SetPathForTest

func (s *Store) SetPathForTest(path string)

SetPathForTest points persistence at path without loading (Load on a missing file would seed the defaults; tests often want an EMPTY store that saves to a temp location).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL