From fa938b88c8889f294306a4171e7394a028d8d84e Mon Sep 17 00:00:00 2001 From: aqua Date: Thu, 29 Jul 2021 13:41:38 +0300 Subject: Properly get hostname from requests --- main.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index cae68ab..2667f2a 100644 --- a/main.go +++ b/main.go @@ -5,12 +5,26 @@ import ( "flag" "fmt" "log" + "net/url" "os" "strconv" + "strings" ) var port = 1965 +func get_host(path string) (string, error) { + if !strings.HasPrefix(path, "gemini://") { + path = "gemini://" + path + } + url, err := url.Parse(path) + if err != nil { + return "", err + } + + return url.Host + ":" + strconv.Itoa(port), nil +} + func usage() { fmt.Printf("Usage: %s [options] url\n", os.Args[0]) flag.PrintDefaults() @@ -18,9 +32,7 @@ func usage() { func main() { flag.Usage = usage - flag.IntVar(&port, "p", port, "server port number") - flag.Parse() if flag.NArg() != 1 { fmt.Printf("Got %d arguments, expected 1\n", flag.NArg()) @@ -28,14 +40,21 @@ func main() { os.Exit(1) } - url := flag.Arg(0) + ":" + strconv.Itoa(port) - request := "gemini://" + flag.Arg(0) + "/\r\n" + url, err := get_host(flag.Arg(0)) + if err != nil { + panic("Error parsing hostname: " + err.Error()) + } + request := flag.Arg(0) + "\r\n" + if !strings.HasPrefix(request, "gemini://") { + request = "gemini://" + request + } conf := tls.Config{ InsecureSkipVerify: true, } log.Println("connecting to: " + url) + log.Println("request: " + request) conn, err := tls.Dial("tcp", url, &conf) -- cgit v1.2.1