telemirror

package
v0.45.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package telemirror is an optional, removable backup feed. It fetches public Telegram channel widgets through Google Translate's public web-page proxy so users can browse public channels even when Telegram itself is blocked. The implementation is intentionally self-contained so the feature can be removed by deleting this package and the matching handlers in internal/web/telemirror.go.

Index

Constants

View Source
const (
	FreshTTL = 1 * time.Minute
	StaleTTL = 24 * time.Hour
)

Cache TTLs. FreshTTL is intentionally short so opening a channel a second time triggers a background refresh — without it, a "live" feed feels frozen for the first 10 minutes after every fetch.

View Source
const ImageStaleTTL = 30 * 24 * time.Hour

Variables

View Source
var (
	ErrEmptyUsername = errors.New("empty username")
	// ErrChannelNotFound means the username has no public web preview — it
	// doesn't exist, was renamed, or is private. The proxy signals this by
	// redirecting the /s/ request instead of serving the 200 widget.
	ErrChannelNotFound = errors.New("telemirror: channel not found or not available")
)

Sentinel errors returned by Store.

View Source
var DefaultChannels = []string{"VahidOnline", "networkti", "thefeedconfig"}

DefaultChannels are listed first on a fresh install. Removable — a removal is recorded in the store's Hidden set.

Functions

func IsDefault

func IsDefault(username string) bool

IsDefault reports whether username is one of the bundled defaults.

func ParseHTML

func ParseHTML(htmlBody string) (*Channel, []Post, error)

ParseHTML extracts channel info and recent posts from a t.me/s/ widget. The HTML is the rendered output of Google Translate's proxy, which keeps the original Telegram class names but rewrites cross-domain URLs onto translate.goog so the browser can load them through the same proxy.

func SanitizeUsername

func SanitizeUsername(s string) string

SanitizeUsername strips @ / t.me/ prefixes and rejects characters not allowed by Telegram's username rules. Returns "" if the result is empty.

Types

type Cache

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

Cache is a per-channel disk cache backed by a small in-memory map.

func NewCache

func NewCache(dir string) *Cache

func (*Cache) Clear

func (c *Cache) Clear()

Clear drops all in-memory and on-disk entries.

func (*Cache) Get

func (c *Cache) Get(username string) (*FetchResult, bool)

Get returns (entry, fresh). fresh=false means the entry is older than FreshTTL but still within StaleTTL, so the caller can serve it while refreshing in the background.

func (*Cache) Put

func (c *Cache) Put(username string, r *FetchResult) error

type Channel

type Channel struct {
	Username    string `json:"username"`
	Title       string `json:"title,omitempty"`
	Description string `json:"description,omitempty"`
	Photo       string `json:"photo,omitempty"`
	Subscribers string `json:"subscribers,omitempty"`
}

Channel describes the public channel header.

type Client

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

Client mirrors HttpClient from teleMirror's http-client.js.

func NewClient

func NewClient() *Client

func (*Client) FetchHTML

func (c *Client) FetchHTML(ctx context.Context, username string) (string, error)

FetchHTML returns the rendered widget HTML for the username.

func (*Client) FetchHTMLBefore added in v0.17.4

func (c *Client) FetchHTMLBefore(ctx context.Context, username string, beforeID int) (string, error)

FetchHTMLBefore fetches the widget filtered to posts strictly older than beforeID. Used for "Load older" pagination.

func (*Client) FetchURL

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

FetchURL fetches an arbitrary URL through the same fronting/uTLS attempts used for the channel widget, but with the request Host set to the URL's actual host (not proxyHost) so Google's edge routes it to the right backend. Returns the bytes plus the upstream Content-Type so the caller can stream it back to the browser.

Used to proxy translate.goog-rewritten image URLs (channel avatars, post photos/videos) without each one having to go through DNS.

type FetchResult

type FetchResult struct {
	Channel   Channel   `json:"channel"`
	Posts     []Post    `json:"posts"`
	FetchedAt time.Time `json:"fetchedAt"`
}

FetchResult is what we cache per channel.

type Forward added in v0.16.0

type Forward struct {
	Author string `json:"author,omitempty"`
	URL    string `json:"url,omitempty"`
}

Forward is the "Forwarded from <name>" header.

type ImageCache added in v0.16.1

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

ImageCache stores image bytes on disk under <dir>/<key>.bin with a <key>.json metadata sidecar. Use Put/Get for URL-hash keys or PutByKey/GetByKey for stable identifiers like channel usernames.

func NewImageCache added in v0.16.1

func NewImageCache(dir string) *ImageCache

func (*ImageCache) Clear added in v0.16.1

func (c *ImageCache) Clear()

func (*ImageCache) Get added in v0.16.1

func (c *ImageCache) Get(rawURL string) ([]byte, string, bool)

func (*ImageCache) GetByKey added in v0.16.1

func (c *ImageCache) GetByKey(key string) ([]byte, string, bool)

func (*ImageCache) Put added in v0.16.1

func (c *ImageCache) Put(rawURL, contentType string, body []byte) error

func (*ImageCache) PutByKey added in v0.16.1

func (c *ImageCache) PutByKey(key, contentType string, body []byte) error

func (*ImageCache) SetMaxBytes added in v0.27.0

func (c *ImageCache) SetMaxBytes(n int64)

SetMaxBytes sets the byte budget (0 = unlimited) and enforces immediately.

func (*ImageCache) Size added in v0.27.0

func (c *ImageCache) Size() int64

Size returns the total bytes on disk (.bin + .json). Scans once if unknown.

type Media

type Media struct {
	Type     string   `json:"type"` // photo | video | voice | audio | document | sticker | poll
	URL      string   `json:"url,omitempty"`
	Thumb    string   `json:"thumb,omitempty"`
	Ratio    float64  `json:"ratio,omitempty"` // width/height; lets the UI reserve the exact box (no scroll jump)
	Duration string   `json:"duration,omitempty"`
	Title    string   `json:"title,omitempty"` // file name / poll question / audio title
	Subtitle string   `json:"subtitle,omitempty"`
	Options  []string `json:"options,omitempty"` // poll options
}

Media is one attachment on a post.

type Post

type Post struct {
	ID        string     `json:"id"` // "<channel>/<msgid>"
	Author    string     `json:"author,omitempty"`
	Text      string     `json:"text,omitempty"` // sanitised inner HTML
	Reply     *Reply     `json:"reply,omitempty"`
	Forward   *Forward   `json:"forward,omitempty"`
	Media     []Media    `json:"media,omitempty"`
	Reactions []Reaction `json:"reactions,omitempty"`
	Time      time.Time  `json:"time,omitempty"`
	Views     string     `json:"views,omitempty"`
	Edited    bool       `json:"edited,omitempty"`
}

Post is a single message from the channel feed.

type Reaction added in v0.15.1

type Reaction struct {
	Emoji string `json:"emoji"`
	Count string `json:"count,omitempty"`
}

type Reply added in v0.16.0

type Reply struct {
	Author string `json:"author,omitempty"`
	Text   string `json:"text,omitempty"` // inner HTML of the snippet
	URL    string `json:"url,omitempty"`  // link to the replied-to post
}

Reply is the quoted preview shown above a reply.

type Store

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

Store persists the user-added channel list. Defaults are listed first, and stay there unless the user removes one — List() re-adds them on every call, so a removal has to be recorded explicitly in Hidden.

func NewStore

func NewStore(dataDir string) *Store

func (*Store) Add

func (s *Store) Add(username string) error

func (*Store) List

func (s *Store) List() []string

List returns the full channel list, defaults first, minus any the user removed.

func (*Store) Remove

func (s *Store) Remove(username string) error

func (*Store) SetTitle added in v0.30.3

func (s *Store) SetTitle(username, title string)

SetTitle records/updates a channel's latest display title (keyed by lowercase username). Called whenever a channel is fetched, so the newest title wins. In-memory cache → no disk read per call; only writes when the title changed.

func (*Store) Titles added in v0.30.3

func (s *Store) Titles() map[string]string

Titles returns a COPY of the lowercase-username → title map (callers must not mutate the cache).

Jump to

Keyboard shortcuts

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