aboutsummaryrefslogtreecommitdiff
path: root/examples/client.go
blob: 1c98bf53f7a7aca80c8522604a9cae90fa19423f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// +build ignore

package main

import (
	"bufio"
	"crypto/x509"
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"time"

	"git.sr.ht/~adnano/go-gemini"
	"git.sr.ht/~adnano/go-xdg"
)

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 main() {
	if len(os.Args) < 2 {
		fmt.Printf("usage: %s <url> [host]", os.Args[0])
		os.Exit(1)
	}

	// Load known hosts file
	var knownHosts gemini.KnownHostsFile
	if err := knownHosts.Load(filepath.Join(xdg.DataHome(), "gemini", "known_hosts")); err != nil {
		log.Println(err)
	}

	scanner := bufio.NewScanner(os.Stdin)

	var client gemini.Client
	client.TrustCertificate = func(hostname string, cert *x509.Certificate) error {
		knownHost, ok := knownHosts.Lookup(hostname)
		if ok && time.Now().Before(knownHost.Expires) {
			// Certificate is in known hosts file and is not expired
			return nil
		}

		fingerprint := gemini.NewFingerprint(cert.Raw, cert.NotAfter)
		fmt.Printf(trustPrompt, hostname, fingerprint.Hex)
		scanner.Scan()
		switch scanner.Text() {
		case "t":
			knownHosts.Add(hostname, fingerprint)
			knownHosts.Write(hostname, fingerprint)
			return nil
		case "o":
			knownHosts.Add(hostname, fingerprint)
			return nil
		default:
			return errors.New("certificate not trusted")
		}
	}
	client.GetInput = func(prompt string, sensitive bool) (string, bool) {
		fmt.Printf("%s ", prompt)
		scanner.Scan()
		return scanner.Text(), true
	}

	// 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 := client.Do(req)
	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: %s\n", resp.Status, resp.Status.Message(), resp.Meta)
		os.Exit(1)
	}
}