restapi

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package restapi exposes the Telegram account as a JSON REST API. It uses only the standard library (net/http, Go 1.22+ method patterns).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

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

Server is the REST API handler set bound to a Telegram client.

func New

func New(client *telegram.Client, token string) *Server

New creates the REST API server and registers all routes. token is the bearer token required on every route except GET /api/health; pass "" only when the caller explicitly wants authentication disabled.

func (*Server) AddAllowedHost

func (s *Server) AddAllowedHost(host string)

AddAllowedHost adds host (accepted with any port) to the set of Host/Origin values allowed in addition to localhost/127.0.0.1/[::1]. Call once per operator-supplied --allowed-host value. Wildcard addresses (0.0.0.0, ::) are rejected even when passed explicitly: a browser can be tricked into treating 0.0.0.0 as a synonym for localhost (the "0.0.0.0 day" class of bug), so it must never become an accepted Host value.

func (*Server) AllowedHosts

func (s *Server) AllowedHosts() []string

AllowedHosts returns the full effective Host/Origin allowlist — localhost/127.0.0.1/::1 plus every host added via SetListenHost or AddAllowedHost — sorted, for startup logging and for the diagnostic body returned on a 403.

func (*Server) Handler

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

Handler returns the root HTTP handler, wrapped with the security middleware (Host/Origin validation, Content-Type enforcement, and bearer-token authentication).

func (*Server) SetListenHost

func (s *Server) SetListenHost(addr string)

SetListenHost records the host portion of the address the server will listen on (e.g. "192.168.1.5" from "192.168.1.5:8080") as an additional allowed Host/Origin, alongside localhost/127.0.0.1/[::1]. Call this before serving requests; it is safe to call at any point before that.

Wildcard bind addresses (0.0.0.0, ::, or no host at all, e.g. "-addr :8080") are deliberately NOT auto-allowed: binding wide open must not silently accept every Host header, or the Host check stops defending against DNS rebinding. Exposing the server beyond loopback requires an explicit AddAllowedHost call (wired to --allowed-host in cmd/telegram-api).

Jump to

Keyboard shortcuts

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