💾 Archived View for godocs.io › git.sr.ht › ~adnano › go-gemini › tofu captured on 2023-09-28 at 17:06:29. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
import "git.sr.ht/~adnano/go-gemini/tofu"
Package tofu implements trust on first use using hosts and fingerprints.
type Host struct { Hostname string // hostname Algorithm string // fingerprint algorithm e.g. sha256 Fingerprint string // fingerprint }
Host represents a host entry with a fingerprint using a certain algorithm.
func NewHost(hostname string, raw []byte) Host
NewHost returns a new host with a SHA256 fingerprint of the provided raw data.
func ParseHost(text []byte) (Host, error)
ParseHost parses a host from the provided text.
func (h Host) String() string
String returns a string representation of the host.
func (h *Host) UnmarshalText(text []byte) error
UnmarshalText unmarshals the host from the provided text.
type HostWriter struct { // contains filtered or unexported fields }
HostWriter writes host entries to an io.WriteCloser.
HostWriter is safe for concurrent use by multiple goroutines.
func NewHostWriter(w io.WriteCloser) *HostWriter
NewHostWriter returns a new host writer that writes to the provided io.WriteCloser.
func OpenHostsFile(path string) (*HostWriter, error)
OpenHostsFile returns a new host writer that appends to the file at the given path. The file is created if it does not exist.
func (h *HostWriter) Close() error
Close closes the underlying io.Closer.
func (h *HostWriter) WriteHost(host Host) error
WriteHost writes the host to the underlying io.Writer.
type KnownHosts struct { // contains filtered or unexported fields }
KnownHosts represents a list of known hosts. The zero value for KnownHosts represents an empty list ready to use.
KnownHosts is safe for concurrent use by multiple goroutines.
func (k *KnownHosts) Add(h Host)
Add adds a host to the list of known hosts.
func (k *KnownHosts) Entries() []Host
Entries returns the known host entries sorted by hostname.
func (k *KnownHosts) Load(path string) error
Load loads the known hosts entries from the provided path.
func (k *KnownHosts) Lookup(hostname string) (Host, bool)
Lookup returns the known host entry corresponding to the given hostname.
func (k *KnownHosts) Parse(r io.Reader) error
Parse parses the provided io.Reader and adds the parsed hosts to the list. Invalid entries are ignored.
For more control over errors encountered during parsing, use bufio.Scanner in combination with ParseHost. For example:
var knownHosts tofu.KnownHosts scanner := bufio.NewScanner(r) for scanner.Scan() { host, err := tofu.ParseHost(scanner.Bytes()) if err != nil { // handle error } else { knownHosts.Add(host) } } err := scanner.Err() if err != nil { // handle error }
func (k *KnownHosts) TOFU(hostname string, cert *x509.Certificate) error
TOFU implements basic trust on first use.
If the host is not on file, it is added to the list. If the fingerprint does not match the one on file, an error is returned.
func (k *KnownHosts) WriteTo(w io.Writer) (int64, error)
WriteTo writes the list of known hosts to the provided io.Writer.
type PersistentHosts struct { // contains filtered or unexported fields }
PersistentHosts represents a persistent set of known hosts.
func LoadPersistentHosts(path string) (*PersistentHosts, error)
LoadPersistentHosts loads persistent hosts from the file at the given path.
func NewPersistentHosts(hosts *KnownHosts, writer *HostWriter) *PersistentHosts
NewPersistentHosts returns a new persistent set of known hosts that stores known hosts in hosts and writes new hosts to writer.
func (p *PersistentHosts) Add(h Host) error
Add adds a host to the list of known hosts. It returns an error if the host could not be persisted.
func (p *PersistentHosts) Close() error
Close closes the underlying HostWriter.
func (p *PersistentHosts) Entries() []Host
Entries returns the known host entries sorted by hostname.
func (p *PersistentHosts) Lookup(hostname string) (Host, bool)
Lookup returns the known host entry corresponding to the given hostname.
func (p *PersistentHosts) TOFU(hostname string, cert *x509.Certificate) error
TOFU implements trust on first use with a persistent set of known hosts.
If the host is not on file, it is added to the list. If the fingerprint does not match the one on file, an error is returned.