decoy

package
v0.2.0-dev.5 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: AGPL-3.0 Imports: 29 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 decoy is the neutral static site shown to everyone who is not a Hysteria2 client (or an admin): HTTP/3 masquerade, HTTPS on TCP 443, later the panel's public listener.

It is a plain http.Handler with no state and no Server header. "/" answers 200 with a tiny page; every other path and every method other than GET/HEAD answers the identical 404, so probing cannot tell "unknown path" from "wrong method" from "auth failed".

One fixed page for every install (recognisable across installs). A per-install site bundle pushed from the panel replaces this handler later (engine.Env.Masquerade already takes any http.Handler).

Index

Constants

View Source
const (
	ReasonEmpty        = "empty"          // nothing to serve
	ReasonBadArchive   = "bad_archive"    // not a readable ZIP, TAR or TAR.GZ
	ReasonTooLarge     = "too_large"      // the archive or its files are over MaxBundleBytes
	ReasonTooManyFiles = "too_many_files" // more than 4096 entries
	ReasonBadPath      = "bad_path"       // "..", an absolute path, a backslash, a colon or a NUL byte in a name
	ReasonNotStatic    = "not_static"     // a symlink, device, FIFO or other entry that is not a plain file
	ReasonNoIndex      = "no_index"       // no index.html at the top level
	ReasonDuplicate    = "duplicate"      // one path twice, or a file where a directory is needed
)

Reasons a bundle is refused. They are stable: the admin shows a sentence for each.

View Source
const (
	WarnNo404         = "no_404"
	WarnExternalURLs  = "external_urls"
	WarnTelltale      = "telltale_words"
	WarnScripts       = "scripts_or_forms"
	WarnDuplicateName = "duplicate_title"
	WarnFewPages      = "few_pages"
	// WarnSharedDomains is decided when a site is put on nodes, not when it is saved.
	WarnSharedDomains = "shared_domains"
)

The warnings of a site (design: the library's "checks"). The required checks (index.html, size, file count, plain files and safe paths) are the bundle rules themselves: a bundle that exists has passed them.

View Source
const (
	// MaxPreviewBytes caps the document: the bundle is at most 2 MiB and data: URIs are a third bigger, and a file used many
	// times is inlined every time.
	MaxPreviewBytes = 6 << 20
	// MaxTextBytes is how much of a text file GetFile returns.
	MaxTextBytes = 256 << 10
)
View Source
const (
	ModeBuiltin = "builtin"
	ModePanel   = "panel"
	ModeOwn     = "own"
)
View Source
const (
	// MaxBundleBytes caps the sum of the file contents and, separately, an uploaded archive. The same cap applies to a
	// bundle an owner uploads for one node and to the snapshot of the panel's own --decoy-dir.
	MaxBundleBytes = 2 << 20
)
View Source
const MinAssistantPages = 3

MinAssistantPages is the fewest HTML pages a site made by the assistant should have.

Variables

View Source
var (
	ErrPageNotFound    = errors.New("decoy page not found")
	ErrNotText         = errors.New("decoy file is not text")
	ErrPreviewTooLarge = errors.New("decoy preview too large")
)
View Source
var ErrInvalidBundle = errors.New("invalid decoy bundle")

ErrInvalidBundle is the base of every bundle refusal; the concrete error is a *BundleError.

Functions

func BuiltinHTML

func BuiltinHTML() string

BuiltinHTML is the built-in page: the one the node, the panel's public listener and the library's preview all show.

func BundleHandler added in v0.1.36

func BundleHandler(b *Bundle) http.Handler

BundleHandler serves a validated site using the same index, dot-path, robots and 404 rules as the panel decoy directory.

func ClientMessage added in v0.1.36

func ClientMessage(err error) string

ClientMessage is the coded message for any bundle error.

func Handler

func Handler() http.Handler

Handler serves the built-in page on "/" and the identical 404 everywhere else.

func NotFound

func NotFound() http.Handler

NotFound answers every request with the same 404 page (masquerade "none").

func NotFoundHTML

func NotFoundHTML() string

NotFoundHTML is the built-in 404 page (a site without a 404.html answers with it).

func PreviewDocument

func PreviewDocument(b *Bundle, page string) (string, error)

PreviewDocument is the page as one self-contained HTML document (see the top of this file).

func ReasonOf added in v0.1.36

func ReasonOf(err error) string

ReasonOf returns the reason of a refusal, ReasonBadArchive for an error that is not a *BundleError.

func TextFile

func TextFile(b *Bundle, name string) (text string, size int, truncated bool, err error)

TextFile returns the text of a file (the first MaxTextBytes of it) and whether it was cut.

Types

type Bundle added in v0.1.36

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

Bundle is a validated, immutable static site. Hash is independent of archive order and metadata.

func BuiltinBundle

func BuiltinBundle() *Bundle

BuiltinBundle is the built-in page as a bundle of one file, so the library reads it as it reads a site (preview, files). The text is BuiltinHTML, the one the node and the panel serve.

func FromDir added in v0.1.36

func FromDir(dir string) (*Bundle, error)

FromDir snapshots a local --decoy-dir bundle. Paths the panel hides by its dot-path rule are omitted; symlinks and non-regular files are refused. The directory is held to the same 2 MiB cap as an uploaded bundle.

func FromFiles added in v0.1.36

func FromFiles(files []File) (*Bundle, error)

FromFiles validates files received over the agent protocol and makes a canonical bundle.

func ParseArchive added in v0.1.36

func ParseArchive(data []byte) (*Bundle, error)

ParseArchive accepts a ZIP, TAR or gzip-compressed TAR upload. The upload and the expanded contents are each capped at 2 MiB. Dot-files and macOS "__MACOSX" entries are dropped (the site never serves them), a single wrapping folder is removed when index.html is inside it, and symlinks, devices, ".." and absolute paths are refused.

func ParseStoredArchive added in v0.1.36

func ParseStoredArchive(data []byte) (*Bundle, error)

ParseStoredArchive reads the canonical ZIP kept by the panel and node. Its ZIP metadata may exceed the file-content cap.

func (*Bundle) Archive added in v0.1.36

func (b *Bundle) Archive() []byte

Archive returns a copy of the stable ZIP representation used for content-addressed storage.

func (*Bundle) FS

func (b *Bundle) FS() fs.FS

FS is the bundle as a read-only file system: the panel serves its own public site from it with the rules of a directory. Directories exist only as the parents of files.

func (*Bundle) File

func (b *Bundle) File(name string) ([]byte, bool)

File returns the content of one file. The slice belongs to the bundle: do not change it.

func (*Bundle) Files added in v0.1.36

func (b *Bundle) Files() []File

func (*Bundle) Hash added in v0.1.36

func (b *Bundle) Hash() string

func (*Bundle) Infos

func (b *Bundle) Infos() []FileInfo

Infos lists the files of the bundle, sorted by path.

func (*Bundle) Pages

func (b *Bundle) Pages() []string

Pages lists the HTML files of the bundle, index.html first.

func (*Bundle) Size

func (b *Bundle) Size() int

Size is the sum of the file contents.

func (*Bundle) With

func (b *Bundle) With(name string, content []byte) (*Bundle, error)

With returns the bundle with one file added or replaced (the other files and the rules are those of FromFiles).

func (*Bundle) Without

func (b *Bundle) Without(name string) (nb *Bundle, ok bool, err error)

Without returns the bundle without one file. ok is false when the bundle has no such file.

type BundleError added in v0.1.36

type BundleError struct {
	Reason string
	Path   string
}

BundleError says why a bundle was refused; Path names the offending entry when there is one.

func (*BundleError) ClientMessage added in v0.1.36

func (e *BundleError) ClientMessage() string

ClientMessage is the coded message the admin and the MCP tools show: "decoy_bundle_<reason>" or "decoy_bundle_<reason>: path=<entry>" (the convention of the panel's refusal codes, web/src/lib/errors.ts).

func (*BundleError) Error added in v0.1.36

func (e *BundleError) Error() string

func (*BundleError) Is added in v0.1.36

func (e *BundleError) Is(target error) bool

type File added in v0.1.36

type File struct {
	Path    string
	Content []byte
}

File is one regular file in a decoy bundle. Paths are slash-separated and relative to the site root.

type FileInfo

type FileInfo struct {
	Path string
	Size int
}

FileInfo is a file of a bundle without its content.

type Report

type Report struct {
	Title    string
	Pages    int
	Warnings []Warning
}

Report is what is known about a bundle without comparing it with other sites.

func Analyze

func Analyze(b *Bundle, assistant bool) Report

Analyze runs the checks that need only the bundle. assistant is true for a site the assistant made (it should have pages enough to look like a site). The warning duplicate_title needs the other sites and is added by the caller.

type Site added in v0.1.36

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

Site atomically swaps the handler used by every node masquerade listener and persists its active bundle.

func BuiltinSite added in v0.1.36

func BuiltinSite(stateDir string) *Site

BuiltinSite returns an in-memory builtin site. It is the safe fallback when persisted node state is unreadable.

func OpenSite added in v0.1.36

func OpenSite(stateDir string) (*Site, error)

OpenSite loads the site's persisted mode and bundle from stateDir. Missing state means the historical built-in page.

func (*Site) Apply added in v0.1.36

func (s *Site) Apply(mode, hash string, files []File) (bool, error)

Apply validates a panel update, stores it atomically, and switches all current requests to the new site. An empty files slice reuses the currently installed bundle only when its hash already matches.

func (*Site) Handler added in v0.1.36

func (s *Site) Handler() http.Handler

Handler returns a stable handler whose active site can be changed while listeners are running.

func (*Site) State added in v0.1.36

func (s *Site) State() (mode, hash string)

State reports the selected mode and the hash of its active bundle. Builtin has an empty hash.

type Warning

type Warning struct {
	Code  string   `json:"code"`
	Paths []string `json:"paths,omitempty"`
}

Warning is one fired check; Paths are the files that fired it (at most maxWarnPaths).

Jump to

Keyboard shortcuts

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