filtering

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package filtering provides filter management and compilation for content blocking.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrFiltersNotReady indicates that filters are not yet compiled/loaded
	ErrFiltersNotReady = errors.New("filters not ready")

	// ErrInvalidFilterFormat indicates malformed filter data
	ErrInvalidFilterFormat = errors.New("invalid filter format")

	// ErrCacheCorrupted indicates corrupted cache data
	ErrCacheCorrupted = errors.New("cache corrupted")

	// ErrNetworkError indicates network-related errors during filter download
	ErrNetworkError = errors.New("network error")
)

Functions

func InitializeFiltersAsync

func InitializeFiltersAsync(manager *FilterManager) error

InitializeFiltersAsync initializes the filtering system asynchronously

Types

type CacheMetadata

type CacheMetadata struct {
	Version      int       `json:"version"`
	CreatedAt    time.Time `json:"created_at"`
	LastUsed     time.Time `json:"last_used"`
	DataHash     string    `json:"data_hash"`
	FilterHashes []string  `json:"filter_hashes"` // Hashes of source filter lists
}

CacheMetadata stores information about the cache

type CompiledFilters

type CompiledFilters struct {
	NetworkRules  []converter.WebKitRule `json:"network_rules"`
	CosmeticRules map[string][]string    `json:"cosmetic_rules"`
	GenericHiding []string               `json:"generic_hiding"`
	CompiledAt    time.Time              `json:"compiled_at"`
	Version       string                 `json:"version"`
	// contains filtered or unexported fields
}

CompiledFilters represents the compiled filter data

func NewCompiledFilters

func NewCompiledFilters() *CompiledFilters

NewCompiledFilters creates a new CompiledFilters instance

func (*CompiledFilters) GetRulesForDomain

func (cf *CompiledFilters) GetRulesForDomain(domain string) []string

GetRulesForDomain returns cosmetic rules for a specific domain

func (*CompiledFilters) Merge

func (cf *CompiledFilters) Merge(other *CompiledFilters)

Merge combines two CompiledFilters instances

func (*CompiledFilters) ToJSON

func (cf *CompiledFilters) ToJSON() ([]byte, error)

ToJSON converts network rules to JSON format

type CosmeticInjector

type CosmeticInjector interface {
	InjectRules(rules map[string][]string) error
	GetScriptForDomain(domain string) string
}

CosmeticInjector defines the interface for cosmetic filter injection

type DefaultFilterCompiler

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

DefaultFilterCompiler implements FilterCompiler using the converter package

func NewDefaultFilterCompiler

func NewDefaultFilterCompiler() *DefaultFilterCompiler

NewDefaultFilterCompiler creates a new default filter compiler

func (*DefaultFilterCompiler) CompileFromData

func (dfc *DefaultFilterCompiler) CompileFromData(data []byte) (*CompiledFilters, error)

CompileFromData compiles filters from raw data bytes

func (*DefaultFilterCompiler) CompileFromSources

func (dfc *DefaultFilterCompiler) CompileFromSources(ctx context.Context, sources []string) (*CompiledFilters, error)

CompileFromSources downloads and compiles filters from multiple URLs

type DiffEngine

type DiffEngine struct {
}

DiffEngine handles differential updates between filter lists

func (*DiffEngine) Diff

func (de *DiffEngine) Diff(previous, current []byte) (added []string, removed []string)

Diff finds added and removed lines between two versions

type FileFilterStore

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

FileFilterStore implements FilterStore using filesystem storage

func NewFileFilterStore

func NewFileFilterStore() (*FileFilterStore, error)

NewFileFilterStore creates a new file-based filter store

func (*FileFilterStore) GetCacheInfo

func (fs *FileFilterStore) GetCacheInfo() (bool, time.Time, error)

GetCacheInfo returns cache existence and modification time

func (*FileFilterStore) InvalidateCache

func (fs *FileFilterStore) InvalidateCache() error

InvalidateCache removes the cache file

func (*FileFilterStore) LoadCached

func (fs *FileFilterStore) LoadCached() (*CompiledFilters, error)

LoadCached loads compiled filters from cache

func (*FileFilterStore) SaveCache

func (fs *FileFilterStore) SaveCache(filters *CompiledFilters) error

SaveCache saves compiled filters to cache

type FilterCompiler

type FilterCompiler interface {
	CompileFromSources(ctx context.Context, sources []string) (*CompiledFilters, error)
	CompileFromData(data []byte) (*CompiledFilters, error)
}

FilterCompiler defines the interface for filter compilation

type FilterListInfo

type FilterListInfo struct {
	URL          string
	LastModified time.Time
	ETag         string
	ContentHash  string
	Size         int64
}

FilterListInfo stores metadata about a filter list

type FilterManager

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

FilterManager manages filter loading, compilation, and updates

func NewFilterManager

func NewFilterManager(store FilterStore, compiler FilterCompiler) *FilterManager

NewFilterManager creates a new filter manager

func SetupFilterSystem

func SetupFilterSystem() (*FilterManager, error)

SetupFilterSystem creates and initializes the complete filtering system

func (*FilterManager) GetCosmeticCleanupScript

func (fm *FilterManager) GetCosmeticCleanupScript() string

GetCosmeticCleanupScript returns JavaScript to cleanup cosmetic filtering

func (*FilterManager) GetCosmeticRulesForDomain

func (fm *FilterManager) GetCosmeticRulesForDomain(domain string) []string

GetCosmeticRulesForDomain returns cosmetic rules for a specific domain

func (*FilterManager) GetCosmeticScript

func (fm *FilterManager) GetCosmeticScript() string

GetCosmeticScript returns the cosmetic filtering script

func (*FilterManager) GetCosmeticScriptForDomain

func (fm *FilterManager) GetCosmeticScriptForDomain(domain string) string

GetCosmeticScriptForDomain returns the cosmetic filtering script for a specific domain

func (*FilterManager) GetCosmeticUpdateScript

func (fm *FilterManager) GetCosmeticUpdateScript(selectors []string) string

GetCosmeticUpdateScript returns JavaScript to update cosmetic rules dynamically

func (*FilterManager) GetNetworkFilters

func (fm *FilterManager) GetNetworkFilters() ([]byte, error)

GetNetworkFilters returns the compiled network filters for WebKit

func (*FilterManager) InitAsync

func (fm *FilterManager) InitAsync(ctx context.Context) error

InitAsync initializes filter loading in background

func (*FilterManager) SetFiltersReadyCallback

func (fm *FilterManager) SetFiltersReadyCallback(callback FilterReadyCallback)

SetFiltersReadyCallback sets a callback to be called when filters are loaded/updated

func (*FilterManager) UpdateCosmeticRules

func (fm *FilterManager) UpdateCosmeticRules(domain string, selectors []string)

UpdateCosmeticRules adds new cosmetic rules for a domain

type FilterReadyCallback

type FilterReadyCallback func()

FilterReadyCallback is called when filters are loaded/updated

type FilterStore

type FilterStore interface {
	LoadCached() (*CompiledFilters, error)
	SaveCache(filters *CompiledFilters) error
	GetCacheInfo() (bool, time.Time, error)
}

FilterStore defines the interface for filter storage operations

type FilterUpdate

type FilterUpdate struct {
	ListID   string
	URL      string
	Previous []byte
	Current  []byte
	Hash     string
	Compiled *CompiledFilters
}

FilterUpdate represents an update to a filter list

type FilterUpdater

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

FilterUpdater manages differential filter list updates

func NewFilterUpdater

func NewFilterUpdater(manager *FilterManager) *FilterUpdater

NewFilterUpdater creates a new filter updater

func (*FilterUpdater) CheckAndUpdate

func (fu *FilterUpdater) CheckAndUpdate(ctx context.Context) error

CheckAndUpdate checks for filter list updates and applies them

Directories

Path Synopsis
Package converter provides types and functions for converting EasyList filters to WebKit content blocking format.
Package converter provides types and functions for converting EasyList filters to WebKit content blocking format.

Jump to

Keyboard shortcuts

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