linkpreview

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: BSD-3-Clause Imports: 13 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 linkpreview is chalkd's server-side page fetcher for sender-built link previews (phase 57, docs/phases/PHASE-57-LINKPREVIEW.md).

Privacy model: the SENDER's client asks chalkd to fetch a page they are about to link, chalkd returns OpenGraph metadata (and, separately, the thumbnail bytes), and the client embeds the preview inside the normal E2E-encrypted body. Recipients render from ciphertext and never fetch anything. chalkd learns the URL and the requesting user -- the same trust carve-out as Giphy search -- and never the channel or recipients.

Security model: users can whitelist arbitrary domains client-side, so this fetcher must survive arbitrary URLs. The SSRF boundary is IP vetting at DIAL time (net.Dialer.Control): the vetted address is the exact one being connected to, which covers DNS rebinding, and every redirect hop re-dials through the same guard. Plus: https only, bounded redirects, bounded response sizes, content-type enforcement, no cookies, a generic UA.

YouTube oEmbed fast-path. YouTube's bot mitigation intermittently serves the fetcher an og-less shell page (empty " - YouTube" title, geo-localized generic description), which the <title>/meta-description fallback turned into a wrong-but-plausible card. The public oEmbed endpoint returns the real title, channel, and thumbnail as ~1 KB of JSON and 404s on unknown ids, so video URLs skip HTML fetching entirely.

Index

Constants

This section is empty.

Variables

View Source
var ErrBadURL = errors.New("linkpreview: invalid url")

ErrBadURL marks a request the client formed wrong (not https, no host, userinfo present, unparseable). Handlers map it to 400; everything else from Fetch/FetchImage is an upstream problem and maps to 502.

Functions

This section is empty.

Types

type Client

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

Client fetches pages and thumbnails with the guards above. Construct with New.

func New

func New(timeout time.Duration, opts ...Option) *Client

New builds a Client with the given per-fetch timeout.

func (*Client) Fetch

func (c *Client) Fetch(ctx context.Context, rawURL string) (*Preview, error)

Fetch retrieves rawURL and extracts its OpenGraph metadata. YouTube video URLs bypass the HTML path entirely and go through oEmbed (see oembed.go).

func (*Client) FetchImage

func (c *Client) FetchImage(ctx context.Context, rawURL string) ([]byte, string, error)

FetchImage retrieves a thumbnail (typically the og:image from Fetch). Returns the raw bytes and their content type; the caller re-serves them to the sender's client, which encrypts them into an attachment.

type Option

type Option func(*Client)

Option customizes a Client.

func WithAllowPrivateAddrs

func WithAllowPrivateAddrs() Option

WithAllowPrivateAddrs disables the dial-time address guard. TESTS ONLY -- httptest servers listen on loopback, which production must reject.

func WithHTTPClient

func WithHTTPClient(hc *http.Client) Option

WithHTTPClient replaces the guarded HTTP client wholesale (tests). The dial guard, redirect policy, and timeout all live in the default client, so production callers must never use this.

func WithIPv4Only added in v0.12.2

func WithIPv4Only() Option

WithIPv4Only makes the default HTTP client dial with network "tcp4". The client then tries no IPv6 address. WithHTTPClient replaces the client, and then this option has no effect.

type Preview

type Preview struct {
	URL         string `json:"url"` // final URL after redirects
	Title       string `json:"title"`
	Description string `json:"description"`
	SiteName    string `json:"site_name"`
	ImageURL    string `json:"image_url"`
}

Preview is the metadata handed back to the sender's client. All fields may be empty (the composer then offers nothing). ImageURL is absolute https or empty; the client fetches it through FetchImage's endpoint, encrypts the bytes, and ships them as a normal attachment.

Jump to

Keyboard shortcuts

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