privacy

package
v0.1.0-alpha Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package privacy implements Sweef's built-in tracker/ad blocking policy: a compact domain blocklist, first-party/third-party classification, HTTPS upgrade, referrer trimming, GPC/DNT headers and third-party cookie filtering. It is pure Go and independent of the engine; internal/app wires it to engine tabs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compile

func Compile(src string) []byte

Compile turns a text list into the compact binary form. Accepted lines: `example.com`, `||example.com^` (Adblock-style domain rule), `@@example.com` or `@@||example.com^` (exception), `# comment`, `! comment`, hosts-file lines `0.0.0.0 example.com`. Each rule covers the domain and all its subdomains.

func IsLocalHost

func IsLocalHost(h string) bool

IsLocalHost reports hosts that must never be upgraded or blocked.

func SameSite

func SameSite(a, b string) bool

SameSite reports whether two hosts share a registrable domain (eTLD+1), falling back to exact match for IPs and unknown suffixes.

Types

type Blocklist

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

Blocklist answers "is this host blocked" over the compiled form.

func FromCompiled

func FromCompiled(data []byte) *Blocklist

FromCompiled wraps already compiled data.

func FromText

func FromText(src string) *Blocklist

FromText compiles text lazily.

func NewBlocklist

func NewBlocklist(loader func() ([]byte, error)) *Blocklist

NewBlocklist creates a lazily loaded blocklist: loader runs on first lookup, never at construction (start-up costs nothing).

func SeedList

func SeedList() *Blocklist

SeedList returns the embedded seed blocklist (compiled lazily on first lookup, so merely linking it costs nothing at start-up). It is seed data, not a complete filter list.

func (*Blocklist) Err

func (b *Blocklist) Err() error

Err returns the load error, if any.

func (*Blocklist) Len

func (b *Blocklist) Len() int

Len is the number of rules (after load).

func (*Blocklist) Loaded

func (b *Blocklist) Loaded() bool

Loaded reports whether the list has been materialised.

func (*Blocklist) Match

func (b *Blocklist) Match(host string) (string, bool)

Match reports whether host (or a parent domain) is blocked and returns the matching rule domain, e.g. host "x.ads.doubleclick.net" -> "doubleclick.net". Exceptions (@@) on the host or any parent override a block.

type Decision

type Decision struct {
	Block  bool
	Rule   string // matching blocklist domain
	Reason string // "tracker" | ""
}

Decision is the verdict for one request.

type Engine

type Engine struct {
	List       *Blocklist
	S          atomic.Pointer[Settings]
	Exceptions func(topOrigin string) (noBlock bool)
	// contains filtered or unexported fields
}

Engine decides per request. Exceptions is consulted with the top-level page's origin so users can turn protection off per site.

func NewEngine

func NewEngine(s Settings) *Engine

NewEngine uses the seed list by default.

func (*Engine) Active

func (e *Engine) Active(topHost, cookieHost string) bool

Active reports whether third-party cookie blocking applies to a response from cookieHost while the top-level page is on topHost.

func (*Engine) Check

func (e *Engine) Check(topURL, reqURL string) Decision

Check decides whether reqURL, requested by a page at topURL, is blocked. Only third-party requests are ever blocked, and never the top-level document itself.

func (*Engine) CookieOK

func (e *Engine) CookieOK(topHost, cookieHost string) bool

CookieOK reports whether a cookie set for cookieHost while the top-level page is topHost may be stored (third-party cookie blocking).

func (*Engine) Headers

func (e *Engine) Headers() map[string]string

Headers returns request headers to add to every request.

func (*Engine) Settings

func (e *Engine) Settings() Settings

Settings returns the current settings.

func (*Engine) Total

func (e *Engine) Total() int64

Total is the number of requests blocked so far.

func (*Engine) TrimReferrer

func (e *Engine) TrimReferrer(referrer, target string) string

TrimReferrer applies the strict-origin-when-cross-origin policy used by default in modern browsers: same-origin keeps the path, cross-origin sends only the origin, and https->http sends nothing. Credentials and fragments are always stripped. With trimming off, only credentials/fragments go.

func (*Engine) Update

func (e *Engine) Update(s Settings)

Update swaps the settings.

func (*Engine) Upgrade

func (e *Engine) Upgrade(raw string) (string, bool)

Upgrade returns the https form of a plain http URL when HTTPS upgrade applies (not for localhost, IPs, single-label hosts or explicit ports other than 80). ok is false when the URL is unchanged.

type Settings

type Settings struct {
	BlockTrackers          bool
	HTTPSUpgrade           bool
	BlockThirdPartyCookies bool
	SendGPC                bool
	TrimReferrer           bool
}

Settings is the privacy configuration (mirrors the stored settings).

func DefaultSettings

func DefaultSettings() Settings

DefaultSettings is what a fresh profile gets.

Jump to

Keyboard shortcuts

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