2020-09-21 15:23:51 -06:00
|
|
|
// +build example
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-09-26 14:38:26 -06:00
|
|
|
"crypto/x509"
|
2020-09-21 15:23:51 -06:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-09-21 16:21:42 -06:00
|
|
|
|
|
|
|
"git.sr.ht/~adnano/go-gemini"
|
2020-09-21 15:23:51 -06:00
|
|
|
)
|
|
|
|
|
2020-09-25 17:53:50 -06:00
|
|
|
var (
|
2020-09-27 17:45:48 -06:00
|
|
|
scanner = bufio.NewScanner(os.Stdin)
|
2020-09-27 18:20:59 -06:00
|
|
|
client *gmi.Client
|
2020-09-25 17:53:50 -06:00
|
|
|
)
|
2020-09-21 15:23:51 -06:00
|
|
|
|
2020-09-21 16:21:42 -06:00
|
|
|
func init() {
|
2020-09-27 17:45:48 -06:00
|
|
|
// Initialize the client
|
2020-09-27 18:20:59 -06:00
|
|
|
client = &gmi.Client{}
|
2020-09-27 17:45:48 -06:00
|
|
|
client.KnownHosts.Load() // Load known hosts
|
2020-09-27 18:20:59 -06:00
|
|
|
client.TrustCertificate = func(hostname string, cert *x509.Certificate, knownHosts *gmi.KnownHosts) error {
|
2020-09-27 14:10:36 -06:00
|
|
|
err := knownHosts.Lookup(hostname, cert)
|
2020-09-26 14:38:26 -06:00
|
|
|
if err != nil {
|
|
|
|
switch err {
|
2020-09-27 18:20:59 -06:00
|
|
|
case gmi.ErrCertificateNotTrusted:
|
2020-09-26 14:38:26 -06:00
|
|
|
// Alert the user that the certificate is not trusted
|
2020-09-27 17:45:48 -06:00
|
|
|
fmt.Printf("Warning: Certificate for %s is not trusted!\n", hostname)
|
2020-09-26 14:38:26 -06:00
|
|
|
fmt.Println("This could indicate a Man-in-the-Middle attack.")
|
2020-09-27 18:20:59 -06:00
|
|
|
case gmi.ErrUnknownCertificate:
|
2020-09-26 14:38:26 -06:00
|
|
|
// Prompt the user to trust the certificate
|
2020-09-27 17:45:48 -06:00
|
|
|
trust := trustCertificate(cert)
|
|
|
|
switch trust {
|
|
|
|
case trustOnce:
|
2020-09-26 14:38:26 -06:00
|
|
|
// Temporarily trust the certificate
|
2020-09-27 15:41:41 -06:00
|
|
|
knownHosts.AddTemporary(hostname, cert)
|
2020-09-26 14:38:26 -06:00
|
|
|
return nil
|
2020-09-27 17:45:48 -06:00
|
|
|
case trustAlways:
|
2020-09-26 14:38:26 -06:00
|
|
|
// Add the certificate to the known hosts file
|
2020-09-27 14:10:36 -06:00
|
|
|
knownHosts.Add(hostname, cert)
|
2020-09-26 14:38:26 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2020-09-21 16:21:42 -06:00
|
|
|
}
|
|
|
|
|
2020-09-27 20:13:50 -06:00
|
|
|
// sendRequest sends a request to the given URL.
|
2020-09-27 18:20:59 -06:00
|
|
|
func sendRequest(req *gmi.Request) error {
|
2020-09-25 21:06:54 -06:00
|
|
|
resp, err := client.Send(req)
|
2020-09-21 15:23:51 -06:00
|
|
|
if err != nil {
|
2020-09-27 17:45:48 -06:00
|
|
|
return err
|
2020-09-21 15:23:51 -06:00
|
|
|
}
|
|
|
|
|
2020-09-27 19:13:42 -06:00
|
|
|
// TODO: More fine-grained analysis of the status code.
|
2020-09-21 15:23:51 -06:00
|
|
|
switch resp.Status / 10 {
|
2020-09-27 18:20:59 -06:00
|
|
|
case gmi.StatusClassInput:
|
2020-09-21 15:23:51 -06:00
|
|
|
fmt.Printf("%s: ", resp.Meta)
|
|
|
|
scanner.Scan()
|
2020-09-27 17:45:48 -06:00
|
|
|
req.URL.RawQuery = scanner.Text()
|
|
|
|
return sendRequest(req)
|
2020-09-27 18:20:59 -06:00
|
|
|
case gmi.StatusClassSuccess:
|
2020-09-27 17:45:48 -06:00
|
|
|
fmt.Print(string(resp.Body))
|
|
|
|
return nil
|
2020-09-27 18:20:59 -06:00
|
|
|
case gmi.StatusClassRedirect:
|
2020-09-27 18:34:59 -06:00
|
|
|
fmt.Println("Redirecting to", resp.Meta)
|
|
|
|
// Make the request to the same host
|
2020-09-27 18:37:16 -06:00
|
|
|
red, err := gmi.NewRequestTo(req.Host, resp.Meta)
|
2020-09-27 17:45:48 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-27 18:34:59 -06:00
|
|
|
// Handle relative redirects
|
|
|
|
red.URL = req.URL.ResolveReference(red.URL)
|
|
|
|
fmt.Println(red.URL, red.Host)
|
|
|
|
return sendRequest(red)
|
2020-09-27 18:20:59 -06:00
|
|
|
case gmi.StatusClassTemporaryFailure:
|
2020-09-27 17:45:48 -06:00
|
|
|
return fmt.Errorf("Temporary failure: %s", resp.Meta)
|
2020-09-27 18:20:59 -06:00
|
|
|
case gmi.StatusClassPermanentFailure:
|
2020-09-27 17:45:48 -06:00
|
|
|
return fmt.Errorf("Permanent failure: %s", resp.Meta)
|
2020-09-27 19:13:42 -06:00
|
|
|
case gmi.StatusClassCertificateRequired:
|
2020-09-27 17:45:48 -06:00
|
|
|
fmt.Println("Generating client certificate for", req.Hostname())
|
|
|
|
return nil // TODO: Generate and store client certificate
|
2020-09-21 15:23:51 -06:00
|
|
|
}
|
2020-09-27 18:11:45 -06:00
|
|
|
panic("unreachable")
|
2020-09-21 15:23:51 -06:00
|
|
|
}
|
|
|
|
|
2020-09-27 17:45:48 -06: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 14:38:26 -06:00
|
|
|
|
2020-09-27 17:45:48 -06: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-27 18:20:59 -06:00
|
|
|
fmt.Printf(trustPrompt, gmi.Fingerprint(cert))
|
2020-09-26 14:38:26 -06:00
|
|
|
scanner.Scan()
|
2020-09-27 17:45:48 -06:00
|
|
|
switch scanner.Text() {
|
|
|
|
case "t":
|
|
|
|
return trustAlways
|
|
|
|
case "o":
|
|
|
|
return trustOnce
|
|
|
|
default:
|
|
|
|
return trustAbort
|
|
|
|
}
|
2020-09-26 14:38:26 -06:00
|
|
|
}
|
|
|
|
|
2020-09-21 15:23:51 -06:00
|
|
|
func main() {
|
|
|
|
if len(os.Args) < 2 {
|
2020-09-27 17:45:48 -06:00
|
|
|
fmt.Println("usage: %s gemini://...", os.Args[0])
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
url := os.Args[1]
|
2020-09-27 18:20:59 -06:00
|
|
|
req, err := gmi.NewRequest(url)
|
2020-09-27 17:45:48 -06: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 15:23:51 -06:00
|
|
|
}
|
|
|
|
}
|