gm

an extremely simple gemini browser/formatter
git clone git://bvnf.space/gm.git
Log | Files | Refs | LICENSE

gemini.go (1082B)


      1 package main
      2 
      3 import (
      4 	"bufio"
      5 	"crypto/tls"
      6 	"flag"
      7 	"fmt"
      8 	"io/ioutil"
      9 	"net/url"
     10 	"os"
     11 	"strings"
     12 )
     13 
     14 func main() {
     15 	port := flag.Int("p", 1965, "port number")
     16 	plain := flag.Bool("plain", false, "print raw header and response (no formatting)")
     17 	flag.Parse()
     18 	// only accept one url
     19 	if flag.NArg() > 1 {
     20 		fmt.Fprintf(os.Stderr, "only give one url\n")
     21 		os.Exit(1)
     22 	}
     23 
     24 	u := flag.Args()[0]
     25 
     26 	if !strings.HasPrefix(u, "gemini://") {
     27 		u = "gemini://" + u
     28 	}
     29 
     30 	parsed, err := url.Parse(u)
     31 	if err != nil {
     32 		fmt.Fprintf(os.Stderr, "error parsing url (%s): %s\n", u, err.Error())
     33 		os.Exit(1)
     34 	}
     35 
     36 	conn, err := tls.Dial("tcp", parsed.Host+fmt.Sprintf(":%d", *port), &tls.Config{InsecureSkipVerify: true})
     37 	if err != nil {
     38 		fmt.Fprintf(os.Stderr, "tls error: %s\n", err.Error())
     39 		os.Exit(1)
     40 	}
     41 	defer conn.Close()
     42 	conn.Write([]byte(u + "\r\n"))
     43 	resp, err := ioutil.ReadAll(bufio.NewReader(conn))
     44 	if err != nil {
     45 		fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
     46 		os.Exit(2)
     47 	}
     48 	if *plain {
     49 		fmt.Println(string(resp))
     50 		os.Exit(0)
     51 	}
     52 	fmt.Println(Prettify(string(resp)))
     53 }