go-gemini/examples/client/client.go

124 lines
2.9 KiB
Go
Raw Normal View History

2020-09-21 21:23:51 +00:00
// +build example
package main
import (
"bufio"
"crypto/tls"
2020-09-26 20:38:26 +00:00
"crypto/x509"
2020-09-21 21:23:51 +00:00
"fmt"
"log"
"os"
"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-26 18:01:06 +00:00
client *gemini.Client
cert tls.Certificate
2020-09-25 23:53:50 +00:00
)
2020-09-21 21:23:51 +00:00
func init() {
client = &gemini.Client{}
client.KnownHosts.Load()
2020-09-26 18:01:06 +00:00
client.TrustCertificate = func(hostname string, cert *x509.Certificate, knownHosts *gemini.KnownHosts) error {
err := knownHosts.Lookup(hostname, cert)
2020-09-26 20:38:26 +00:00
if err != nil {
switch err {
case gemini.ErrCertificateNotTrusted:
// Alert the user that the certificate is not trusted
2020-09-27 20:21:56 +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 gemini.ErrCertificateUnknown:
// Prompt the user to trust the certificate
if userTrustsCertificateTemporarily() {
// 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
} else if userTrustsCertificatePermanently() {
// 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-09-27 20:21:56 +00:00
client.GetCertificate = func(req *gemini.Request, store *gemini.CertificateStore) *tls.Certificate {
return &cert
}
// Configure a client side certificate.
// To generate a TLS key pair, run:
//
// go run -tags=example ../cert
2020-09-27 18:23:25 +00:00
var err error
2020-09-27 17:50:48 +00:00
cert, err = tls.LoadX509KeyPair("examples/client/localhost.crt", "examples/client/localhost.key")
if err != nil {
log.Fatal(err)
}
}
2020-09-21 21:23:51 +00:00
func makeRequest(url string) {
req, err := gemini.NewRequest(url)
if err != nil {
log.Fatal(err)
}
2020-09-27 21:39:44 +00:00
req.Certificate = &cert
2020-09-25 23:53:50 +00:00
2020-09-26 03:06:54 +00:00
resp, err := client.Send(req)
2020-09-21 21:23:51 +00:00
if err != nil {
log.Fatal(err)
}
fmt.Println("Status code:", resp.Status)
fmt.Println("Meta:", resp.Meta)
switch resp.Status / 10 {
case gemini.StatusClassInput:
scanner := bufio.NewScanner(os.Stdin)
fmt.Printf("%s: ", resp.Meta)
scanner.Scan()
query := scanner.Text()
makeRequest(url + "?" + query)
return
case gemini.StatusClassSuccess:
fmt.Print("Body:\n", string(resp.Body))
case gemini.StatusClassRedirect:
log.Print("Redirecting to ", resp.Meta)
makeRequest(resp.Meta)
return
case gemini.StatusClassTemporaryFailure:
log.Fatal("Temporary failure")
case gemini.StatusClassPermanentFailure:
log.Fatal("Permanent failure")
case gemini.StatusClassClientCertificateRequired:
2020-09-25 22:53:20 +00:00
log.Fatal("Client certificate required")
2020-09-21 21:23:51 +00:00
default:
2020-09-25 22:53:20 +00:00
log.Fatal("Protocol error")
2020-09-21 21:23:51 +00:00
}
}
2020-09-26 20:38:26 +00:00
func userTrustsCertificateTemporarily() bool {
2020-09-26 22:56:04 +00:00
fmt.Print("Do you want to trust the certificate temporarily? (y/n) ")
2020-09-26 20:38:26 +00:00
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
return scanner.Text() == "y"
}
func userTrustsCertificatePermanently() bool {
2020-09-26 22:56:04 +00:00
fmt.Print("How about permanently? (y/n) ")
2020-09-26 20:38:26 +00:00
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
return scanner.Text() == "y"
}
2020-09-21 21:23:51 +00:00
func main() {
if len(os.Args) < 2 {
log.Fatalf("usage: %s gemini://...", os.Args[0])
}
makeRequest(os.Args[1])
}