2020-10-12 14:34:52 -06:00
|
|
|
// +build ignore
|
2020-09-21 15:23:51 -06:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-09-27 21:49:41 -06:00
|
|
|
"crypto/tls"
|
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-09-21 15:23:51 -06:00
|
|
|
"os"
|
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-09-21 15:23:51 -06:00
|
|
|
)
|
|
|
|
|
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-09-25 17:53:50 -06:00
|
|
|
var (
|
2020-09-27 17:45:48 -06:00
|
|
|
scanner = bufio.NewScanner(os.Stdin)
|
2020-10-27 20:12:10 -06:00
|
|
|
client = &gemini.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-10-31 18:55:56 -06:00
|
|
|
client.Timeout = 2 * time.Minute
|
2020-10-28 11:40:25 -06:00
|
|
|
client.CreateCertificate = func(hostname, path string) (tls.Certificate, error) {
|
|
|
|
fmt.Println("Generating client certificate for", hostname, path)
|
|
|
|
return gemini.CreateCertificate(gemini.CertificateOptions{
|
|
|
|
Duration: time.Hour,
|
|
|
|
})
|
2020-09-27 21:49:41 -06:00
|
|
|
}
|
2020-10-27 21:34:06 -06:00
|
|
|
client.GetInput = func(prompt string, sensitive bool) (string, bool) {
|
|
|
|
fmt.Printf("%s: ", prompt)
|
|
|
|
scanner.Scan()
|
|
|
|
return scanner.Text(), true
|
|
|
|
}
|
2020-09-21 16:21:42 -06:00
|
|
|
}
|
|
|
|
|
2020-09-21 15:23:51 -06:00
|
|
|
func main() {
|
|
|
|
if len(os.Args) < 2 {
|
2020-10-28 14:50:17 -06:00
|
|
|
fmt.Printf("usage: %s gemini://... [host]", os.Args[0])
|
2020-09-27 17:45:48 -06:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
url := os.Args[1]
|
2020-10-27 20:12:10 -06:00
|
|
|
req, err := gemini.NewRequest(url)
|
2020-10-28 14:50:17 -06:00
|
|
|
|
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-09-27 17:45:48 -06:00
|
|
|
|
2020-10-31 20:34:51 -06:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Status.Class() == gemini.StatusClassSuccess {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Print(string(body))
|
|
|
|
} else {
|
|
|
|
log.Fatalf("request failed: %d %s: %s", resp.Status, resp.Status.Message(), resp.Meta)
|
2020-09-21 15:23:51 -06:00
|
|
|
}
|
|
|
|
}
|