go-gemini/examples/client.go

164 lines
3.9 KiB
Go
Raw Normal View History

2020-10-12 20:34:52 +00:00
// +build ignore
2020-09-21 21:23:51 +00:00
package main
import (
"bufio"
2020-09-28 03:49:41 +00:00
"crypto/tls"
2020-09-26 20:38:26 +00:00
"crypto/x509"
2020-09-21 21:23:51 +00:00
"fmt"
2020-10-27 23:16:55 +00:00
"io/ioutil"
2020-10-27 17:27:52 +00:00
"net/url"
2020-09-21 21:23:51 +00:00
"os"
2020-09-28 03:49:41 +00:00
"time"
2020-10-24 19:15:32 +00:00
gmi "git.sr.ht/~adnano/go-gemini"
2020-09-21 21:23:51 +00:00
)
2020-09-25 23:53:50 +00:00
var (
2020-09-27 23:45:48 +00:00
scanner = bufio.NewScanner(os.Stdin)
2020-09-28 03:49:41 +00:00
client = &gmi.Client{}
2020-09-25 23:53:50 +00:00
)
2020-09-21 21:23:51 +00:00
func init() {
2020-09-27 23:45:48 +00:00
// Initialize the client
client.KnownHosts.LoadDefault() // Load known hosts
2020-09-28 00:20:59 +00:00
client.TrustCertificate = func(hostname string, cert *x509.Certificate, knownHosts *gmi.KnownHosts) error {
err := knownHosts.Lookup(hostname, cert)
2020-09-26 20:38:26 +00:00
if err != nil {
switch err {
2020-09-28 00:20:59 +00:00
case gmi.ErrCertificateNotTrusted:
2020-09-26 20:38:26 +00:00
// Alert the user that the certificate is not trusted
2020-09-27 23:45:48 +00:00
fmt.Printf("Warning: Certificate for %s is not trusted!\n", hostname)
2020-09-26 20:38:26 +00:00
fmt.Println("This could indicate a Man-in-the-Middle attack.")
case gmi.ErrCertificateUnknown:
2020-09-26 20:38:26 +00:00
// Prompt the user to trust the certificate
2020-09-27 23:45:48 +00:00
trust := trustCertificate(cert)
switch trust {
case trustOnce:
2020-09-26 20:38:26 +00:00
// Temporarily trust the certificate
2020-09-27 21:41:41 +00:00
knownHosts.AddTemporary(hostname, cert)
2020-09-26 20:38:26 +00:00
return nil
2020-09-27 23:45:48 +00:00
case trustAlways:
2020-09-26 20:38:26 +00:00
// Add the certificate to the known hosts file
knownHosts.Add(hostname, cert)
2020-09-26 20:38:26 +00:00
return nil
}
}
}
return err
}
2020-10-12 20:34:52 +00:00
client.GetCertificate = func(hostname string, store *gmi.CertificateStore) *tls.Certificate {
2020-09-28 04:29:11 +00:00
// If the certificate is in the store, return it
2020-10-12 03:48:18 +00:00
if cert, err := store.Lookup(hostname); err == nil {
2020-09-28 03:49:41 +00:00
return cert
}
2020-09-28 04:29:11 +00:00
// Otherwise, generate a certificate
2020-09-28 18:26:09 +00:00
fmt.Println("Generating client certificate for", hostname)
2020-09-28 03:49:41 +00:00
duration := time.Hour
cert, err := gmi.NewCertificate(hostname, duration)
if err != nil {
return nil
}
2020-09-28 04:29:11 +00:00
// Store and return the certificate
2020-10-12 03:48:18 +00:00
store.Add(hostname, cert)
2020-09-28 03:49:41 +00:00
return &cert
}
}
2020-09-28 02:13:50 +00:00
// sendRequest sends a request to the given URL.
2020-09-28 00:20:59 +00:00
func sendRequest(req *gmi.Request) error {
2020-10-27 23:21:33 +00:00
resp, err := client.Do(req)
2020-09-21 21:23:51 +00:00
if err != nil {
2020-09-27 23:45:48 +00:00
return err
2020-09-21 21:23:51 +00:00
}
2020-09-28 01:13:42 +00:00
// TODO: More fine-grained analysis of the status code.
2020-10-27 18:17:14 +00:00
switch resp.Status.Class() {
2020-09-28 00:20:59 +00:00
case gmi.StatusClassInput:
2020-09-21 21:23:51 +00:00
fmt.Printf("%s: ", resp.Meta)
scanner.Scan()
2020-10-27 17:27:52 +00:00
req.URL.RawQuery = url.QueryEscape(scanner.Text())
2020-09-27 23:45:48 +00:00
return sendRequest(req)
2020-09-28 00:20:59 +00:00
case gmi.StatusClassSuccess:
2020-10-27 23:16:55 +00:00
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
fmt.Print(string(body))
2020-09-27 23:45:48 +00:00
return nil
2020-09-28 00:20:59 +00:00
case gmi.StatusClassRedirect:
fmt.Println("Redirecting to", resp.Meta)
2020-10-27 17:27:52 +00:00
target, err := url.Parse(resp.Meta)
2020-09-27 23:45:48 +00:00
if err != nil {
return err
}
2020-10-27 17:27:52 +00:00
// TODO: Prompt the user if the redirect is to another domain.
redirect, err := gmi.NewRequestFromURL(req.URL.ResolveReference(target))
if err != nil {
return err
}
return sendRequest(redirect)
2020-09-28 00:20:59 +00:00
case gmi.StatusClassTemporaryFailure:
2020-09-27 23:45:48 +00:00
return fmt.Errorf("Temporary failure: %s", resp.Meta)
2020-09-28 00:20:59 +00:00
case gmi.StatusClassPermanentFailure:
2020-09-27 23:45:48 +00:00
return fmt.Errorf("Permanent failure: %s", resp.Meta)
2020-09-28 01:13:42 +00:00
case gmi.StatusClassCertificateRequired:
2020-09-28 18:26:09 +00:00
// Note that this should not happen unless the server responds with
// CertificateRequired even after we send a certificate.
// CertificateNotAuthorized and CertificateNotValid are handled here.
return fmt.Errorf("Certificate required: %s", resp.Meta)
2020-09-21 21:23:51 +00:00
}
2020-09-28 00:11:45 +00:00
panic("unreachable")
2020-09-21 21:23:51 +00:00
}
2020-09-27 23:45:48 +00:00
type trust int
const (
trustAbort trust = iota
trustOnce
trustAlways
)
const trustPrompt = `The certificate offered by this server is of unknown trust. Its fingerprint is:
%s
2020-09-26 20:38:26 +00:00
2020-09-27 23:45:48 +00:00
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(cert *x509.Certificate) trust {
2020-09-28 00:20:59 +00:00
fmt.Printf(trustPrompt, gmi.Fingerprint(cert))
2020-09-26 20:38:26 +00:00
scanner.Scan()
2020-09-27 23:45:48 +00:00
switch scanner.Text() {
case "t":
return trustAlways
case "o":
return trustOnce
default:
return trustAbort
}
2020-09-26 20:38:26 +00:00
}
2020-09-21 21:23:51 +00:00
func main() {
if len(os.Args) < 2 {
fmt.Printf("usage: %s gemini://...", os.Args[0])
2020-09-27 23:45:48 +00:00
os.Exit(1)
}
url := os.Args[1]
2020-10-27 23:21:33 +00:00
req, err := gmi.NewRequest(url)
2020-09-27 23:45:48 +00:00
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if err := sendRequest(req); err != nil {
fmt.Println(err)
os.Exit(1)
2020-09-21 21:23:51 +00:00
}
}