1
0
mirror of https://git.sr.ht/~adnano/go-gemini synced 2024-11-10 04:41:38 +01:00
go-gemini/examples/client.go

163 lines
3.2 KiB
Go
Raw Normal View History

2020-10-12 22:34:52 +02:00
// +build ignore
2020-09-21 23:23:51 +02:00
2020-12-18 06:47:30 +01:00
// This example illustrates a Gemini client.
2020-09-21 23:23:51 +02:00
package main
import (
"bufio"
tofu: Refactor 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.
2021-01-13 22:33:48 +01:00
"bytes"
2021-02-20 21:39:47 +01:00
"context"
2020-11-01 03:50:42 +01:00
"crypto/x509"
2020-12-18 01:50:26 +01:00
"errors"
2020-09-21 23:23:51 +02:00
"fmt"
2021-02-17 00:57:24 +01:00
"io"
"log"
"net/url"
2020-09-21 23:23:51 +02:00
"os"
2020-11-09 18:04:53 +01:00
"path/filepath"
2020-09-28 05:49:41 +02:00
"time"
2020-10-28 03:12:10 +01:00
"git.sr.ht/~adnano/go-gemini"
2021-01-10 22:44:32 +01:00
"git.sr.ht/~adnano/go-gemini/tofu"
2020-11-09 18:04:53 +01:00
"git.sr.ht/~adnano/go-xdg"
2020-09-21 23:23:51 +02:00
)
var (
2021-01-14 23:28:03 +01:00
hosts tofu.KnownHosts
hostsfile *tofu.HostWriter
scanner *bufio.Scanner
)
func init() {
// Load known hosts file
path := filepath.Join(xdg.DataHome(), "gemini", "known_hosts")
2021-01-14 23:28:03 +01:00
err := hosts.Load(path)
if err != nil {
2021-01-14 23:28:03 +01:00
log.Fatal(err)
}
hostsfile, err = tofu.OpenHostsFile(path)
2021-01-14 23:28:03 +01:00
if err != nil {
log.Fatal(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 {
2021-01-14 23:28:03 +01:00
host := tofu.NewHost(hostname, cert.Raw, cert.NotAfter)
tofu: Refactor 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.
2021-01-13 22:33:48 +01:00
knownHost, ok := hosts.Lookup(hostname)
if ok && time.Now().Before(knownHost.Expires) {
2020-12-19 19:44:33 +01:00
// Check fingerprint
tofu: Refactor 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.
2021-01-13 22:33:48 +01:00
if bytes.Equal(knownHost.Fingerprint, host.Fingerprint) {
2020-12-19 19:44:33 +01:00
return nil
}
return errors.New("error: fingerprint does not match!")
2020-12-18 01:50:26 +01:00
}
tofu: Refactor 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.
2021-01-13 22:33:48 +01:00
fmt.Printf(trustPrompt, hostname, host.Fingerprint)
scanner.Scan()
switch scanner.Text() {
case "t":
tofu: Refactor 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.
2021-01-13 22:33:48 +01:00
hosts.Add(host)
2021-01-14 23:28:03 +01:00
hostsfile.WriteHost(host)
return nil
case "o":
2021-01-14 23:28:03 +01:00
hosts.Add(host)
return nil
default:
return errors.New("certificate not trusted")
2020-12-18 01:50:26 +01:00
}
}
2020-12-18 01:50:26 +01:00
func getInput(prompt string, sensitive bool) (input string, ok bool) {
fmt.Printf("%s ", prompt)
scanner.Scan()
return scanner.Text(), true
}
2020-12-18 01:50:26 +01:00
func do(req *gemini.Request, via []*gemini.Request) (*gemini.Response, error) {
client := gemini.Client{
TrustCertificate: trustCertificate,
}
2021-02-20 21:39:47 +01:00
resp, err := client.Do(context.Background(), req)
if err != nil {
return resp, err
}
switch gemini.StatusClass(resp.Status) {
2021-02-15 00:59:33 +01:00
case gemini.StatusInput:
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)
2021-02-15 00:59:33 +01:00
case gemini.StatusRedirect:
via = append(via, req)
if len(via) > 5 {
return resp, errors.New("too many redirects")
2020-12-18 01:50:26 +01:00
}
2020-09-21 23:23:51 +02:00
target, err := url.Parse(resp.Meta)
if err != nil {
return resp, err
2020-11-01 03:50:42 +01:00
}
target = req.URL.ResolveReference(target)
redirect := *req
redirect.URL = target
return do(&redirect, via)
2020-11-01 03:50:42 +01:00
}
return resp, err
}
func main() {
if len(os.Args) < 2 {
fmt.Printf("usage: %s <url> [host]\n", os.Args[0])
os.Exit(1)
}
2020-09-28 01:45:48 +02:00
2020-12-18 01:50:26 +01:00
// Do the request
2020-09-28 01:45:48 +02:00
url := os.Args[1]
2020-10-28 03:12:10 +01:00
req, err := gemini.NewRequest(url)
2020-09-28 01:45:48 +02:00
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)
}
defer resp.Body.Close()
2020-12-18 01:50:26 +01:00
// Handle response
2021-02-15 00:59:33 +01:00
if gemini.StatusClass(resp.Status) == gemini.StatusSuccess {
2021-02-17 00:57:24 +01:00
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Print(string(body))
} else {
2021-01-10 06:10:57 +01:00
fmt.Printf("%d %s\n", resp.Status, resp.Meta)
2020-12-18 01:50:26 +01:00
os.Exit(1)
2020-09-21 23:23:51 +02:00
}
}