aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authoraqua <aqua@iserlohn-fortress.net>2021-07-29 16:11:39 +0300
committeraqua <aqua@iserlohn-fortress.net>2021-07-29 16:11:39 +0300
commite1174b74985e33c7123cb9cd8edf3601e4026053 (patch)
treecf0c66591a8943779e8b0f04ebf0d283c80a715d /main.go
parentProperly get hostname from requests (diff)
downloadgemcat-e1174b74985e33c7123cb9cd8edf3601e4026053.tar.xz
Split off Gemini code into gemini.go
Diffstat (limited to 'main.go')
-rw-r--r--main.go69
1 files changed, 10 insertions, 59 deletions
diff --git a/main.go b/main.go
index 2667f2a..22d9fbf 100644
--- a/main.go
+++ b/main.go
@@ -1,29 +1,14 @@
package main
import (
- "crypto/tls"
"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
-}
+var host = ""
+var port = GeminiPort
func usage() {
fmt.Printf("Usage: %s [options] url\n", os.Args[0])
@@ -32,7 +17,10 @@ func usage() {
func main() {
flag.Usage = usage
+ flag.StringVar(&host, "h", host, "server host name")
+ flag.StringVar(&host, "host", host, "server host name")
flag.IntVar(&port, "p", port, "server port number")
+ flag.IntVar(&port, "port", port, "server port number")
flag.Parse()
if flag.NArg() != 1 {
fmt.Printf("Got %d arguments, expected 1\n", flag.NArg())
@@ -40,52 +28,15 @@ func main() {
os.Exit(1)
}
- 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)
-
+ conn, err := NewGeminiConn(flag.Arg(0), port)
if err != nil {
- panic("failed to connect: " + err.Error())
- } else {
- log.Println("connected to " + url)
+ log.Fatalf("Error parsing hostname: %s\n", err.Error())
}
- defer conn.Close()
+ log.Printf("connecting to: %s\n", conn.host)
- n, err := conn.Write([]byte(request))
+ err = conn.Get(FormatRequest(flag.Arg(0)), os.Stdout)
if err != nil {
- log.Println(n, err)
- return
+ log.Fatalf("Error: %s\n", err.Error())
}
-
- buf := make([]byte, 1024)
-
- for {
- n, err = conn.Read(buf)
- if err != nil {
- log.Println(n, err)
- return
- } else {
- println(string(buf[:n]))
- }
-
- if n == 0 {
- break
- }
- }
-
}