Documentation
¶
Overview ¶
Package imagefetch fetches and checks images: the reader's image proxy and prefetch download remote ones through a client of its own (public hosts only, no cookies, no Referer, a generic User-Agent), and a message's inline ones count against one Budget. Every image must be a PNG, JPEG, GIF or WebP within the per-image limits.
Index ¶
- Constants
- Variables
- func CheckAvatarImage(data []byte, source string) (out []byte, format string, width, height int, err error)
- func Config(data []byte) (format string, px int64, err error)
- func DataURL(src string) ([]byte, error)
- func Dims(data []byte) (format string, width, height int, err error)
- func Download(ctx context.Context, c *http.Client, src string) ([]byte, error)
- func DownloadAvatar(ctx context.Context, client *http.Client, src string) ([]byte, error)
- func Encode(format string, data []byte) string
- func Fetch(ctx context.Context, c *http.Client, src string) (data []byte, format string, width, height int, err error)
- func IsHTTP(u *url.URL) bool
- func NewAvatarClient(base *http.Client) *http.Client
- func NewClient(allowLoopback bool) *http.Client
- func PublicIP(ip netip.Addr) bool
- type Budget
- type Transport
Constants ¶
const ( MaxImages = 50 // images, remote and inline MaxImageBytes = 5 << 20 // one image MaxTotalBytes = 20 << 20 // all images together MaxPixels = 40_000_000 MaxTotalPixels = 100_000_000 // all images together, decoded Timeout = 15 * time.Second MaxRedirects = 5 )
The budget of one message's images (global constraints).
const MaxAvatarBytes = 256 << 10
const UserAgent = "Mozilla/5.0 (compatible; ImageProxy)"
UserAgent is what the proxy sends: no browser, no app version.
Variables ¶
var ( ErrTooMany = errors.New("too many images") ErrTooLarge = errors.New("image too large") ErrTotalTooLarge = errors.New("images too large together") ErrPixels = errors.New("image has too many pixels") ErrTotalPixels = errors.New("images have too many pixels together") ErrFormat = errors.New("not a PNG, JPEG, GIF or WebP image") ErrScheme = errors.New("image address is not http or https") ErrHost = errors.New("image host is not public") )
The reasons an image is refused.
var ErrAvatarMiss = errors.New("avatar unavailable")
ErrAvatarMiss is a deterministic miss (404 or a rejected image), safe to cache for 24 hours.
Formats are the formats shown, as image.DecodeConfig names them.
var SystemProxy func(u *url.URL) *url.URL
SystemProxy is the proxy for a request to u, nil for none (only http:// and socks5:// ones are used); the app sets it to the user's (sysproxy).
Functions ¶
func CheckAvatarImage ¶ added in v0.3.0
func CheckAvatarImage(data []byte, source string) (out []byte, format string, width, height int, err error)
CheckAvatarImage validates source-specific image bytes; it does not establish BIMI message trust. ICO is converted to PNG; SVG remains vector data.
func Dims ¶ added in v0.1.0
Dims is data's format and size, read from its header: a PNG, JPEG, GIF or WebP image (by content) of at most MaxPixels.
func Download ¶ added in v0.1.0
Download gets src (http or https) with c: at most MaxImageBytes of it.
func DownloadAvatar ¶ added in v0.3.0
DownloadAvatar streams at most 256 KiB and permits only HTTPS, including redirects.
func Fetch ¶ added in v0.1.0
func Fetch(ctx context.Context, c *http.Client, src string) (data []byte, format string, width, height int, err error)
Fetch downloads src and checks it: its bytes, format and size in pixels.
func NewAvatarClient ¶ added in v0.3.0
NewAvatarClient retains the base transport's public-address dial checks and redirect policy. The injected base is trusted, just as for ordinary images.
Types ¶
type Budget ¶
type Budget struct {
// contains filtered or unexported fields
}
Budget is one message's images' budget: their count, bytes and decoded pixels, inline and remote together. It is safe for concurrent use.
func (*Budget) TakeInline ¶
TakeInline takes an inline image of size bytes and px pixels: its count, bytes and pixels, all or none of them but the count (as a remote image's, which is counted once asked for).
func (*Budget) TakePixels ¶
TakePixels takes px decoded pixels; ErrTotalPixels when fewer are left.
type Transport ¶ added in v1.16.0
Transport is NewTransport's: an http.Transport whose dials know the scheme of the request they are for, which picks the proxy.
func NewTransport ¶ added in v1.14.0
NewTransport reaches public hosts only (ErrHost otherwise). It resolves a host itself and dials the addresses it checked (pinned), so a name that resolves differently on the second ask (DNS rebinding) changes nothing: every answer must be public, or every one a fake-ip one (a TUN proxy's). Through the system proxy (SystemProxy) it tunnels to the pinned address, never the name, so the proxy cannot resolve it elsewhere; for a fake-ip name it gives the name to a proxy on this computer, which knows what the fake address stands for, and refuses a proxy elsewhere. allowLoopback lets tests reach httptest servers, which listen on loopback.