diff --git a/bombadillo.1 b/bombadillo.1 index c8b19a0..ea650b2 100644 --- a/bombadillo.1 +++ b/bombadillo.1 @@ -257,6 +257,10 @@ theme Can toggle between visual modes. Valid values are \fInormal\fP, \fIcolor\fP, and \fIinverse\fP. When set to inverse, the normal mode colors are inverted. Both normal and inverse modes filter out terminal escape sequences. When set to color, Bombadillo will render terminal escape sequences representing colors when it finds them in documents. .TP .B +timeout +The number of seconds after which connections to gopher or gemini servers should time out if the server has not responded. +.TP +.B tlscertificate A path to a tls certificate file on a user's local filesystem. Defaults to NULL. Both \fItlscertificate\fP and \fItlskey\fP must be set for client certificates to work in gemini. .TP diff --git a/client.go b/client.go index dafb5af..6b2ac43 100644 --- a/client.go +++ b/client.go @@ -455,6 +455,8 @@ func (c *client) doCommandAs(action string, values []string) { c.Certs.LoadCertificate(c.Options["tlscertificate"], c.Options["tlskey"]) } else if values[0] == "geminiblocks" { gemini.BlockBehavior = c.Options[values[0]] + } else if values[0] == "timeout" { + updateTimeouts(c.Options[values[0]]) } else if values[0] == "configlocation" { c.SetMessage("Cannot set READ ONLY setting 'configlocation'", true) c.DrawMessage() @@ -1202,3 +1204,16 @@ func findAvailableFileName(fpath, fname string) (string, error) { return savePath, nil } + +func updateTimeouts(timeoutString string) error { + sec, err := strconv.Atoi(timeoutString) + if err != nil { + return err + } + timeout := time.Duration(sec) * time.Second + + gopher.Timeout = timeout + gemini.TlsTimeout = timeout + + return nil +} diff --git a/defaults.go b/defaults.go index fc47a64..b0f28a8 100644 --- a/defaults.go +++ b/defaults.go @@ -54,9 +54,10 @@ var defaultOptions = map[string]string{ "showimages": "true", "telnetcommand": "telnet", "theme": "normal", // "normal", "inverted", "color" + "timeout": "15", // connection timeout for gopher/gemini in seconds "tlscertificate": "", "tlskey": "", - "webmode": "none", // "none", "gui", "lynx", "w3m", "elinks" + "webmode": "none", // "none", "gui", "lynx", "w3m", "elinks" } // homePath will return the path to your home directory as a string diff --git a/gemini/gemini.go b/gemini/gemini.go index b0a87a8..19fbd54 100644 --- a/gemini/gemini.go +++ b/gemini/gemini.go @@ -6,6 +6,7 @@ import ( "crypto/tls" "fmt" "io/ioutil" + "net" "net/url" "strconv" "strings" @@ -25,7 +26,8 @@ type TofuDigest struct { ClientCert tls.Certificate } -var BlockBehavior = "block" +var BlockBehavior string = "block" +var TlsTimeout time.Duration = time.Duration(15) * time.Second //------------------------------------------------\\ // + + + R E C E I V E R S + + + \\ @@ -189,7 +191,7 @@ func Retrieve(host, port, resource string, td *TofuDigest) (string, error) { return &td.ClientCert, nil } - conn, err := tls.Dial("tcp", addr, conf) + conn, err := tls.DialWithDialer(&net.Dialer{Timeout: TlsTimeout}, "tcp", addr, conf) if err != nil { return "", fmt.Errorf("TLS Dial Error: %s", err.Error()) } @@ -331,6 +333,9 @@ func Visit(host, port, resource string, td *TofuDigest) (Capsule, error) { case 2: mimeAndCharset := strings.Split(header[1], ";") meta = mimeAndCharset[0] + if meta == "" { + meta = "text/gemini" + } minMajMime := strings.Split(meta, "/") if len(minMajMime) < 2 { return capsule, fmt.Errorf("Improperly formatted mimetype received from server") diff --git a/gopher/gopher.go b/gopher/gopher.go index 20e76e9..fbe579f 100644 --- a/gopher/gopher.go +++ b/gopher/gopher.go @@ -38,6 +38,8 @@ var types = map[string]string{ "T": "TEL", } +var Timeout time.Duration = time.Duration(15) * time.Second + //------------------------------------------------\\ // + + + F U N C T I O N S + + + \\ //--------------------------------------------------\\ @@ -49,7 +51,6 @@ var types = map[string]string{ // be better. func Retrieve(host, port, resource string) ([]byte, error) { nullRes := make([]byte, 0) - timeOut := time.Duration(5) * time.Second if host == "" || port == "" { return nullRes, errors.New("Incomplete request url") @@ -57,7 +58,7 @@ func Retrieve(host, port, resource string) ([]byte, error) { addr := host + ":" + port - conn, err := net.DialTimeout("tcp", addr, timeOut) + conn, err := net.DialTimeout("tcp", addr, Timeout) if err != nil { return nullRes, err } diff --git a/http/open_browser_darwin.go b/http/open_browser_darwin.go index dd7da7a..d57a290 100644 --- a/http/open_browser_darwin.go +++ b/http/open_browser_darwin.go @@ -1,4 +1,4 @@ -// +build darwin +// This will build for osx without a build tag based on the filename package http diff --git a/http/open_browser_linux.go b/http/open_browser_linux.go deleted file mode 100644 index 3b7dfee..0000000 --- a/http/open_browser_linux.go +++ /dev/null @@ -1,30 +0,0 @@ -// +build linux - -package http - -import ( - "fmt" - "os" - "os/exec" -) - -// OpenInBrowser checks for the presence of a display server -// and environment variables indicating a gui is present. If found -// then xdg-open is called on a url to open said url in the default -// gui web browser for the system -func OpenInBrowser(url string) (string, error) { - disp := os.Getenv("DISPLAY") - wayland := os.Getenv("WAYLAND_DISPLAY") - _, err := exec.LookPath("Xorg") - if disp == "" && wayland == "" && err != nil { - return "", fmt.Errorf("No gui is available, check 'webmode' setting") - } - - // Use start rather than run or output in order - // to release the process and not block - err = exec.Command("xdg-open", url).Start() - if err != nil { - return "", err - } - return "Opened in system default web browser", nil -} diff --git a/http/open_browser_other.go b/http/open_browser_other.go index 1388c6b..2f74d22 100644 --- a/http/open_browser_other.go +++ b/http/open_browser_other.go @@ -1,11 +1,30 @@ -// +build !linux -// +build !darwin -// +build !windows +// +build !darwin,!windows package http -import "fmt" +import ( + "fmt" + "os" + "os/exec" +) +// OpenInBrowser checks for the presence of a display server +// and environment variables indicating a gui is present. If found +// then xdg-open is called on a url to open said url in the default +// gui web browser for the system func OpenInBrowser(url string) (string, error) { - return "", fmt.Errorf("Unsupported os for 'webmode' 'gui' setting") + disp := os.Getenv("DISPLAY") + wayland := os.Getenv("WAYLAND_DISPLAY") + _, err := exec.LookPath("Xorg") + if disp == "" && wayland == "" && err != nil { + return "", fmt.Errorf("No gui is available, check 'webmode' setting") + } + + // Use start rather than run or output in order + // to release the process and not block + err = exec.Command("xdg-open", url).Start() + if err != nil { + return "", err + } + return "Opened in system default web browser", nil } diff --git a/http/open_browser_windows.go b/http/open_browser_windows.go index 0ddf6c7..496d00b 100644 --- a/http/open_browser_windows.go +++ b/http/open_browser_windows.go @@ -1,5 +1,5 @@ -// +build windows - +// This will only build for windows based on the filename +// no build tag required package http import "os/exec" diff --git a/main.go b/main.go index 28fcf36..a9b5d83 100644 --- a/main.go +++ b/main.go @@ -82,6 +82,14 @@ func validateOpt(opt, val string) bool { } return false } + + if opt == "timeout" { + _, err := strconv.Atoi(val) + if err != nil { + return false + } + } + return true } @@ -126,6 +134,8 @@ func loadConfig() { bombadillo.Options[lowerkey] = v.Value if lowerkey == "geminiblocks" { gemini.BlockBehavior = v.Value + } else if lowerkey == "timeout" { + updateTimeouts(v.Value) } } else { bombadillo.Options[lowerkey] = defaultOptions[lowerkey]