95aff9c573
This commit changes underlying file handling and known hosts parsing. A known hosts file opened through Load() never closed the underlying file. During known hosts parsing most errors were unchecked, or just led to the line being skipped. I removed the KnownHosts type, which didn't really have a role after the refactor. The embedding of KnownHosts in KnownHosts file has been removed as it also leaked the map unprotected by the mutex. The Fingerprint type is now KnownHost and has taken over the responsibility of marshalling and unmarshalling. SetOutput now takes a WriteCloser so that we can close the underlying writer when it's replaced, or when it's explicitly closed through the new Close() function. KnownHostsFile.Add() now also writes the known host to the output if set. I think that makes sense expectation-wise for the type. Turned WriteAll() into WriteTo() to conform with the io.WriterTo interface. Load() is now Open() to better reflect the fact that a file is opened, and kept open. It can now also return errors from the parsing process. The parser does a lot more error checking, and this might be an area where I've changed a desired behaviour as invalid entries no longer are ignored, but aborts the parsing process. That could be changed to a warning, or some kind of parsing feedback. I added KnownHostsFile.TOFU() to fill the developer experience gap that was left after the client no longer knows about KnownHostsFile. It implements a basic non-interactive TOFU flow.
154 lines
3.0 KiB
Go
154 lines
3.0 KiB
Go
// +build ignore
|
|
|
|
// This example illustrates a Gemini client.
|
|
|
|
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"crypto/x509"
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"git.sr.ht/~adnano/go-gemini"
|
|
"git.sr.ht/~adnano/go-gemini/tofu"
|
|
"git.sr.ht/~adnano/go-xdg"
|
|
)
|
|
|
|
var (
|
|
hosts tofu.KnownHostsFile
|
|
scanner *bufio.Scanner
|
|
)
|
|
|
|
func init() {
|
|
// Load known hosts file
|
|
path := filepath.Join(xdg.DataHome(), "gemini", "known_hosts")
|
|
err := hosts.Open(path)
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
|
|
scanner = bufio.NewScanner(os.Stdin)
|
|
}
|
|
|
|
const trustPrompt = `The certificate offered by %s is of unknown trust. Its fingerprint is:
|
|
%s
|
|
|
|
If you knew the fingerprint to expect in advance, verify that this matches.
|
|
Otherwise, this should be safe to trust.
|
|
|
|
[t]rust always; trust [o]nce; [a]bort
|
|
=> `
|
|
|
|
func trustCertificate(hostname string, cert *x509.Certificate) error {
|
|
host := tofu.NewKnownHost(hostname, cert.Raw, cert.NotAfter)
|
|
|
|
knownHost, ok := hosts.Lookup(hostname)
|
|
if ok && time.Now().Before(knownHost.Expires) {
|
|
// Check fingerprint
|
|
if bytes.Equal(knownHost.Fingerprint, host.Fingerprint) {
|
|
return nil
|
|
}
|
|
return errors.New("error: fingerprint does not match!")
|
|
}
|
|
|
|
fmt.Printf(trustPrompt, hostname, host.Fingerprint)
|
|
scanner.Scan()
|
|
switch scanner.Text() {
|
|
case "t":
|
|
hosts.Add(host)
|
|
return nil
|
|
case "o":
|
|
return nil
|
|
default:
|
|
return errors.New("certificate not trusted")
|
|
}
|
|
}
|
|
|
|
func getInput(prompt string, sensitive bool) (input string, ok bool) {
|
|
fmt.Printf("%s ", prompt)
|
|
scanner.Scan()
|
|
return scanner.Text(), true
|
|
}
|
|
|
|
func do(req *gemini.Request, via []*gemini.Request) (*gemini.Response, error) {
|
|
client := gemini.Client{
|
|
TrustCertificate: trustCertificate,
|
|
}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
|
|
switch resp.Status.Class() {
|
|
case gemini.StatusClassInput:
|
|
input, ok := getInput(resp.Meta, resp.Status == gemini.StatusSensitiveInput)
|
|
if !ok {
|
|
break
|
|
}
|
|
req.URL.ForceQuery = true
|
|
req.URL.RawQuery = gemini.QueryEscape(input)
|
|
return do(req, via)
|
|
|
|
case gemini.StatusClassRedirect:
|
|
via = append(via, req)
|
|
if len(via) > 5 {
|
|
return resp, errors.New("too many redirects")
|
|
}
|
|
|
|
target, err := url.Parse(resp.Meta)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
target = req.URL.ResolveReference(target)
|
|
redirect := *req
|
|
redirect.URL = target
|
|
return do(&redirect, via)
|
|
}
|
|
|
|
return resp, err
|
|
}
|
|
|
|
func main() {
|
|
if len(os.Args) < 2 {
|
|
fmt.Printf("usage: %s <url> [host]\n", os.Args[0])
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Do the request
|
|
url := os.Args[1]
|
|
req, err := gemini.NewRequest(url)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
if len(os.Args) == 3 {
|
|
req.Host = os.Args[2]
|
|
}
|
|
resp, err := do(req, nil)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Handle response
|
|
if resp.Status.Class() == gemini.StatusClassSuccess {
|
|
defer resp.Body.Close()
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Print(string(body))
|
|
} else {
|
|
fmt.Printf("%d %s\n", resp.Status, resp.Meta)
|
|
os.Exit(1)
|
|
}
|
|
}
|