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