From b8666239fbedcefb18f2f4bf6e0f965e61eef06c Mon Sep 17 00:00:00 2001 From: Michael Frister Date: Thu, 28 Jun 2018 13:35:11 +0200 Subject: [PATCH] Automatically open browser --- Gopkg.lock | 15 +++++ Gopkg.toml | 34 ++++++++++ main.go | 39 +++++++----- vendor/github.com/pkg/browser/LICENSE | 23 +++++++ vendor/github.com/pkg/browser/README.md | 55 ++++++++++++++++ vendor/github.com/pkg/browser/browser.go | 62 +++++++++++++++++++ .../github.com/pkg/browser/browser_darwin.go | 5 ++ .../github.com/pkg/browser/browser_linux.go | 5 ++ .../github.com/pkg/browser/browser_openbsd.go | 14 +++++ .../pkg/browser/browser_unsupported.go | 12 ++++ .../github.com/pkg/browser/browser_windows.go | 10 +++ 11 files changed, 257 insertions(+), 17 deletions(-) create mode 100644 Gopkg.lock create mode 100644 Gopkg.toml create mode 100644 vendor/github.com/pkg/browser/LICENSE create mode 100644 vendor/github.com/pkg/browser/README.md create mode 100644 vendor/github.com/pkg/browser/browser.go create mode 100644 vendor/github.com/pkg/browser/browser_darwin.go create mode 100644 vendor/github.com/pkg/browser/browser_linux.go create mode 100644 vendor/github.com/pkg/browser/browser_openbsd.go create mode 100644 vendor/github.com/pkg/browser/browser_unsupported.go create mode 100644 vendor/github.com/pkg/browser/browser_windows.go diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..fa84bb9 --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,15 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + branch = "master" + name = "github.com/pkg/browser" + packages = ["."] + revision = "c90ca0c84f15f81c982e32665bffd8d7aac8f097" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "68d49c04b6ed1c7c0dedc9884e56f5f5b27a703660e812cade105fa7f36a4d23" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..d0d3875 --- /dev/null +++ b/Gopkg.toml @@ -0,0 +1,34 @@ +# Gopkg.toml example +# +# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md +# for detailed Gopkg.toml documentation. +# +# required = ["github.com/user/thing/cmd/thing"] +# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] +# +# [[constraint]] +# name = "github.com/user/project" +# version = "1.0.0" +# +# [[constraint]] +# name = "github.com/user/project2" +# branch = "dev" +# source = "github.com/myfork/project2" +# +# [[override]] +# name = "github.com/x/y" +# version = "2.4.0" +# +# [prune] +# non-go = false +# go-tests = true +# unused-packages = true + + +[prune] + go-tests = true + unused-packages = true + +[[constraint]] + branch = "master" + name = "github.com/pkg/browser" diff --git a/main.go b/main.go index 3f78454..0c71a89 100644 --- a/main.go +++ b/main.go @@ -9,11 +9,9 @@ import ( "net/url" "time" - "golang.org/x/net/publicsuffix" + "github.com/pkg/browser" ) -//https://github.com/pkg/browser - func main() { proxyAddr := ":8124" @@ -22,7 +20,10 @@ func main() { log.Fatalf("Failed to parse baseURL: %v", err) } - jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List}) + // We don't need a public suffix list for cookies - we only make requests + // to a single host, so there's no need to prevent cookies from being + // sent across domains. + jar, err := cookiejar.New(nil) if err != nil { log.Fatalf("Failed to create cookie jar: %v", err) } @@ -55,24 +56,14 @@ func main() { } } - w.Write([]byte(fmt.Sprintf( - "Authentication complete. You can now use the proxy at %s\n\n"+ - "Upstream: %s\n\n"+ - "You can close this window.", - proxyAddr, baseURL))) - log.Printf("Cookie: %+v", authCookie) - log.Printf("Response: %+v", resp) - reverseProxyDirector := func(r *http.Request) { - log.Printf("Original request: %+v", r) r.Host = baseURL.Host r.URL.Scheme = baseURL.Scheme r.URL.Host = baseURL.Host r.AddCookie(authCookie) - log.Printf("Forwarding request: %+v", r) - log.Printf("Request URI: %v", r.RequestURI) + log.Printf("Forwarding request: %v", r.RequestURI) } proxyHandler := &httputil.ReverseProxy{ @@ -85,12 +76,20 @@ func main() { } go proxyServer.ListenAndServe() + authCompleteText := fmt.Sprintf("Authentication complete. You can now use the proxy at %s\n\n"+ + "Upstream: %s\n\n", proxyAddr, baseURL) + + w.Write([]byte(authCompleteText + "You can close this window.")) + + log.Printf(authCompleteText + + "The proxy will automatically terminate in 12 hours (reauthentication required)") }) server := &http.Server{ Addr: ":8123", Handler: serverMux, } + // TODO check server is actually running to prevent sending auth code to another app go server.ListenAndServe() resp, err := client.Get(baseURL.String() + "/oauth2/start") @@ -103,6 +102,12 @@ func main() { log.Fatalf("Got empty identity provider URL in response: %+v", resp) } - log.Printf("Go to %s", identityProviderURL) - time.Sleep(1 * time.Hour) + if err := browser.OpenURL(identityProviderURL); err != nil { + log.Printf("Failed to open browser, please go to %s", identityProviderURL) + } + log.Printf("Please authenticate in your browser via our identity provider.") + + // TODO Fetch expiration date from cookie + time.Sleep(12 * time.Hour) + log.Printf("12 hours are up, please reauthenticate.") } diff --git a/vendor/github.com/pkg/browser/LICENSE b/vendor/github.com/pkg/browser/LICENSE new file mode 100644 index 0000000..65f78fb --- /dev/null +++ b/vendor/github.com/pkg/browser/LICENSE @@ -0,0 +1,23 @@ +Copyright (c) 2014, Dave Cheney +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/vendor/github.com/pkg/browser/README.md b/vendor/github.com/pkg/browser/README.md new file mode 100644 index 0000000..72b1976 --- /dev/null +++ b/vendor/github.com/pkg/browser/README.md @@ -0,0 +1,55 @@ + +# browser + import "github.com/pkg/browser" + +Package browser provides helpers to open files, readers, and urls in a browser window. + +The choice of which browser is started is entirely client dependant. + + + + + +## Variables +``` go +var Stderr io.Writer = os.Stderr +``` +Stderr is the io.Writer to which executed commands write standard error. + +``` go +var Stdout io.Writer = os.Stdout +``` +Stdout is the io.Writer to which executed commands write standard output. + + +## func OpenFile +``` go +func OpenFile(path string) error +``` +OpenFile opens new browser window for the file path. + + +## func OpenReader +``` go +func OpenReader(r io.Reader) error +``` +OpenReader consumes the contents of r and presents the +results in a new browser window. + + +## func OpenURL +``` go +func OpenURL(url string) error +``` +OpenURL opens a new browser window pointing to url. + + + + + + + + + +- - - +Generated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md) diff --git a/vendor/github.com/pkg/browser/browser.go b/vendor/github.com/pkg/browser/browser.go new file mode 100644 index 0000000..d92c4cd --- /dev/null +++ b/vendor/github.com/pkg/browser/browser.go @@ -0,0 +1,62 @@ +// Package browser provides helpers to open files, readers, and urls in a browser window. +// +// The choice of which browser is started is entirely client dependant. +package browser + +import ( + "fmt" + "io" + "io/ioutil" + "os" + "os/exec" + "path/filepath" +) + +// Stdout is the io.Writer to which executed commands write standard output. +var Stdout io.Writer = os.Stdout + +// Stderr is the io.Writer to which executed commands write standard error. +var Stderr io.Writer = os.Stderr + +// OpenFile opens new browser window for the file path. +func OpenFile(path string) error { + path, err := filepath.Abs(path) + if err != nil { + return err + } + return OpenURL("file://" + path) +} + +// OpenReader consumes the contents of r and presents the +// results in a new browser window. +func OpenReader(r io.Reader) error { + f, err := ioutil.TempFile("", "browser") + if err != nil { + return fmt.Errorf("browser: could not create temporary file: %v", err) + } + if _, err := io.Copy(f, r); err != nil { + f.Close() + return fmt.Errorf("browser: caching temporary file failed: %v", err) + } + if err := f.Close(); err != nil { + return fmt.Errorf("browser: caching temporary file failed: %v", err) + } + oldname := f.Name() + newname := oldname + ".html" + if err := os.Rename(oldname, newname); err != nil { + return fmt.Errorf("browser: renaming temporary file failed: %v", err) + } + return OpenFile(newname) +} + +// OpenURL opens a new browser window pointing to url. +func OpenURL(url string) error { + return openBrowser(url) +} + +func runCmd(prog string, args ...string) error { + cmd := exec.Command(prog, args...) + cmd.Stdout = Stdout + cmd.Stderr = Stderr + return cmd.Run() +} diff --git a/vendor/github.com/pkg/browser/browser_darwin.go b/vendor/github.com/pkg/browser/browser_darwin.go new file mode 100644 index 0000000..8507cf7 --- /dev/null +++ b/vendor/github.com/pkg/browser/browser_darwin.go @@ -0,0 +1,5 @@ +package browser + +func openBrowser(url string) error { + return runCmd("open", url) +} diff --git a/vendor/github.com/pkg/browser/browser_linux.go b/vendor/github.com/pkg/browser/browser_linux.go new file mode 100644 index 0000000..bed47dd --- /dev/null +++ b/vendor/github.com/pkg/browser/browser_linux.go @@ -0,0 +1,5 @@ +package browser + +func openBrowser(url string) error { + return runCmd("xdg-open", url) +} diff --git a/vendor/github.com/pkg/browser/browser_openbsd.go b/vendor/github.com/pkg/browser/browser_openbsd.go new file mode 100644 index 0000000..4fc7ff0 --- /dev/null +++ b/vendor/github.com/pkg/browser/browser_openbsd.go @@ -0,0 +1,14 @@ +package browser + +import ( + "errors" + "os/exec" +) + +func openBrowser(url string) error { + err := runCmd("xdg-open", url) + if e, ok := err.(*exec.Error); ok && e.Err == exec.ErrNotFound { + return errors.New("xdg-open: command not found - install xdg-utils from ports(8)") + } + return err +} diff --git a/vendor/github.com/pkg/browser/browser_unsupported.go b/vendor/github.com/pkg/browser/browser_unsupported.go new file mode 100644 index 0000000..e29d220 --- /dev/null +++ b/vendor/github.com/pkg/browser/browser_unsupported.go @@ -0,0 +1,12 @@ +// +build !linux,!windows,!darwin,!openbsd + +package browser + +import ( + "fmt" + "runtime" +) + +func openBrowser(url string) error { + return fmt.Errorf("openBrowser: unsupported operating system: %v", runtime.GOOS) +} diff --git a/vendor/github.com/pkg/browser/browser_windows.go b/vendor/github.com/pkg/browser/browser_windows.go new file mode 100644 index 0000000..f65e0ee --- /dev/null +++ b/vendor/github.com/pkg/browser/browser_windows.go @@ -0,0 +1,10 @@ +package browser + +import ( + "strings" +) + +func openBrowser(url string) error { + r := strings.NewReplacer("&", "^&") + return runCmd("cmd", "/c", "start", r.Replace(url)) +}