2020-10-12 14:34:52 -06:00
|
|
|
// +build ignore
|
2020-09-21 15:23:51 -06:00
|
|
|
|
2020-12-17 22:47:30 -07:00
|
|
|
// This example illustrates a Gemini client.
|
|
|
|
|
2020-09-21 15:23:51 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-10-31 20:50:42 -06:00
|
|
|
"crypto/x509"
|
2020-12-17 17:50:26 -07:00
|
|
|
"errors"
|
2020-09-21 15:23:51 -06:00
|
|
|
"fmt"
|
2020-10-27 17:16:55 -06:00
|
|
|
"io/ioutil"
|
2020-10-31 20:34:51 -06:00
|
|
|
"log"
|
2020-12-17 22:12:32 -07:00
|
|
|
"net/url"
|
2020-09-21 15:23:51 -06:00
|
|
|
"os"
|
2020-11-09 10:04:53 -07:00
|
|
|
"path/filepath"
|
2020-09-27 21:49:41 -06:00
|
|
|
"time"
|
2020-09-21 16:21:42 -06:00
|
|
|
|
2020-10-27 20:12:10 -06:00
|
|
|
"git.sr.ht/~adnano/go-gemini"
|
2020-11-09 10:04:53 -07:00
|
|
|
"git.sr.ht/~adnano/go-xdg"
|
2020-09-21 15:23:51 -06:00
|
|
|
)
|
|
|
|
|
2020-12-17 22:12:32 -07:00
|
|
|
var (
|
|
|
|
hosts gemini.KnownHostsFile
|
|
|
|
scanner *bufio.Scanner
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Load known hosts file
|
|
|
|
path := filepath.Join(xdg.DataHome(), "gemini", "known_hosts")
|
|
|
|
err := hosts.Load(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner = bufio.NewScanner(os.Stdin)
|
|
|
|
}
|
|
|
|
|
2020-10-31 20:34:51 -06: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-17 22:12:32 -07:00
|
|
|
func trustCertificate(hostname string, cert *x509.Certificate) error {
|
2020-12-19 11:44:33 -07:00
|
|
|
fingerprint := gemini.NewFingerprint(cert.Raw, cert.NotAfter)
|
2020-12-17 22:12:32 -07:00
|
|
|
knownHost, ok := hosts.Lookup(hostname)
|
|
|
|
if ok && time.Now().Before(knownHost.Expires) {
|
2020-12-19 11:44:33 -07:00
|
|
|
// Check fingerprint
|
|
|
|
if knownHost.Hex == fingerprint.Hex {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("error: fingerprint does not match!")
|
2020-12-17 17:50:26 -07:00
|
|
|
}
|
|
|
|
|
2020-12-17 22:12:32 -07:00
|
|
|
fmt.Printf(trustPrompt, hostname, fingerprint.Hex)
|
|
|
|
scanner.Scan()
|
|
|
|
switch scanner.Text() {
|
|
|
|
case "t":
|
|
|
|
hosts.Add(hostname, fingerprint)
|
|
|
|
hosts.Write(hostname, fingerprint)
|
|
|
|
return nil
|
|
|
|
case "o":
|
|
|
|
hosts.Add(hostname, fingerprint)
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return errors.New("certificate not trusted")
|
2020-12-17 17:50:26 -07:00
|
|
|
}
|
2020-12-17 22:12:32 -07:00
|
|
|
}
|
2020-12-17 17:50:26 -07:00
|
|
|
|
2020-12-17 22:12:32 -07:00
|
|
|
func getInput(prompt string, sensitive bool) (input string, ok bool) {
|
|
|
|
fmt.Printf("%s ", prompt)
|
|
|
|
scanner.Scan()
|
|
|
|
return scanner.Text(), true
|
|
|
|
}
|
2020-12-17 17:50:26 -07:00
|
|
|
|
2020-12-17 22:12:32 -07:00
|
|
|
func do(req *gemini.Request, via []*gemini.Request) (*gemini.Response, error) {
|
|
|
|
client := gemini.Client{
|
|
|
|
TrustCertificate: trustCertificate,
|
|
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch resp.Status.Class() {
|
|
|
|
case gemini.StatusClassInput:
|
|
|
|
input, ok := getInput(resp.Meta, resp.Status == gemini.StatusSensitiveInput)
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
req.URL.ForceQuery = true
|
|
|
|
req.URL.RawQuery = gemini.QueryEscape(input)
|
|
|
|
return do(req, via)
|
|
|
|
|
|
|
|
case gemini.StatusClassRedirect:
|
|
|
|
via = append(via, req)
|
|
|
|
if len(via) > 5 {
|
|
|
|
return resp, errors.New("too many redirects")
|
2020-12-17 17:50:26 -07:00
|
|
|
}
|
2020-09-21 15:23:51 -06:00
|
|
|
|
2020-12-17 22:12:32 -07:00
|
|
|
target, err := url.Parse(resp.Meta)
|
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
2020-10-31 20:50:42 -06:00
|
|
|
}
|
2020-12-17 22:12:32 -07:00
|
|
|
target = req.URL.ResolveReference(target)
|
|
|
|
redirect := *req
|
|
|
|
redirect.URL = target
|
|
|
|
return do(&redirect, via)
|
2020-10-31 20:50:42 -06:00
|
|
|
}
|
2020-12-17 22:12:32 -07: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-27 21:34:06 -06:00
|
|
|
}
|
2020-09-27 17:45:48 -06:00
|
|
|
|
2020-12-17 17:50:26 -07:00
|
|
|
// Do the request
|
2020-09-27 17:45:48 -06:00
|
|
|
url := os.Args[1]
|
2020-10-27 20:12:10 -06:00
|
|
|
req, err := gemini.NewRequest(url)
|
2020-09-27 17:45:48 -06:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-10-28 14:50:17 -06:00
|
|
|
if len(os.Args) == 3 {
|
|
|
|
req.Host = os.Args[2]
|
|
|
|
}
|
2020-12-17 22:12:32 -07:00
|
|
|
resp, err := do(req, nil)
|
2020-10-31 20:34:51 -06:00
|
|
|
if err != nil {
|
2020-10-31 21:04:47 -06:00
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
2020-10-31 20:34:51 -06:00
|
|
|
}
|
|
|
|
|
2020-12-17 17:50:26 -07:00
|
|
|
// Handle response
|
2020-10-31 20:34:51 -06:00
|
|
|
if resp.Status.Class() == gemini.StatusClassSuccess {
|
2020-11-08 16:38:08 -07:00
|
|
|
defer resp.Body.Close()
|
2020-10-31 20:34:51 -06:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Print(string(body))
|
|
|
|
} else {
|
2021-01-09 22:10:57 -07:00
|
|
|
fmt.Printf("%d %s\n", resp.Status, resp.Meta)
|
2020-12-17 17:50:26 -07:00
|
|
|
os.Exit(1)
|
2020-09-21 15:23:51 -06:00
|
|
|
}
|
|
|
|
}
|