2020-09-21 15:23:51 -06:00
|
|
|
// +build example
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-09-21 16:21:42 -06:00
|
|
|
"crypto/tls"
|
2020-09-25 17:53:50 -06:00
|
|
|
"crypto/x509"
|
2020-09-21 15:23:51 -06:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"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-25 21:06:54 -06:00
|
|
|
client = &gemini.Client{
|
|
|
|
TrustCertificate: func(cert *x509.Certificate, knownHosts *gemini.KnownHosts) bool {
|
2020-09-25 19:43:13 -06:00
|
|
|
// Trust all certificates
|
|
|
|
return true
|
2020-09-25 17:53:50 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
cert tls.Certificate
|
|
|
|
)
|
2020-09-21 15:23:51 -06:00
|
|
|
|
2020-09-21 16:21:42 -06:00
|
|
|
func init() {
|
|
|
|
// Configure a client side certificate.
|
|
|
|
// To generate a certificate, run:
|
|
|
|
//
|
|
|
|
// openssl genrsa -out client.key 2048
|
|
|
|
// openssl ecparam -genkey -name secp384r1 -out client.key
|
2020-09-25 19:43:13 -06:00
|
|
|
// openssl req -new -x509 -sha512 -key client.key -out client.crt -days 365
|
2020-09-21 16:21:42 -06:00
|
|
|
//
|
2020-09-21 17:17:10 -06:00
|
|
|
var err error
|
|
|
|
cert, err = tls.LoadX509KeyPair("examples/client/client.crt", "examples/client/client.key")
|
2020-09-21 16:21:42 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 15:23:51 -06:00
|
|
|
func makeRequest(url string) {
|
2020-09-21 17:17:10 -06:00
|
|
|
req, err := gemini.NewRequest(url)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-09-25 16:53:20 -06:00
|
|
|
req.Certificate = cert
|
2020-09-25 17:53:50 -06:00
|
|
|
|
2020-09-25 21:06:54 -06:00
|
|
|
resp, err := client.Send(req)
|
2020-09-21 15:23:51 -06: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 16:53:20 -06:00
|
|
|
log.Fatal("Client certificate required")
|
2020-09-21 15:23:51 -06:00
|
|
|
default:
|
2020-09-25 16:53:20 -06:00
|
|
|
log.Fatal("Protocol error")
|
2020-09-21 15:23:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if len(os.Args) < 2 {
|
|
|
|
log.Fatalf("usage: %s gemini://...", os.Args[0])
|
|
|
|
}
|
|
|
|
makeRequest(os.Args[1])
|
|
|
|
}
|