| 1 | // Package webbrowser provides a simple API for opening web pages on your |
| 2 | // default browser. |
| 3 | package webbrowser |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "net/url" |
| 9 | "os" |
| 10 | "os/exec" |
| 11 | "runtime" |
| 12 | "strings" |
| 13 | ) |
| 14 | |
| 15 | var ( |
| 16 | ErrCantOpenBrowser = errors.New("webbrowser: can't open browser") |
| 17 | ErrNoCandidates = errors.New("webbrowser: no browser candidate found for your OS") |
| 18 | ) |
| 19 | |
| 20 | // Candidates contains a list of registered `Browser`s that will be tried with Open. |
| 21 | var Candidates []Browser |
| 22 | |
| 23 | type Browser interface { |
| 24 | // Command returns a ready to be used Cmd that will open an URL. |
| 25 | Command(string) (*exec.Cmd, error) |
| 26 | // Open tries to open a URL in your default browser. NOTE: This may cause |
| 27 | // your program to hang until the browser process is closed in some OSes, |
| 28 | // see https://github.com/toqueteos/webbrowser/issues/4. |
| 29 | Open(string) error |
| 30 | } |
| 31 | |
| 32 | // Open tries to open a URL in your default browser ensuring you have a display |
| 33 | // set up and not running this from SSH. |
| 34 | // NOTE: This may cause your program to hang until the browser process is closed in |
| 35 | // some OSes, see https://github.com/toqueteos/webbrowser/issues/4. |
| 36 | // SECURITY(windows): Do not use a file:// URLs pointing to an executable, this |
| 37 | // can execute programs, see https://github.com/toqueteos/webbrowser/issues/10 |
| 38 | func Open(s string) (err error) { |
| 39 | if len(Candidates) == 0 { |
| 40 | return ErrNoCandidates |
| 41 | } |
| 42 | |
| 43 | // Try to determine if there's a display available (only linux) and we |
| 44 | // aren't on a terminal (all but windows). |
| 45 | switch runtime.GOOS { |
| 46 | case "linux": |
| 47 | // No display, no need to open a browser. Lynx users **MAY** have |
| 48 | // something to say about this. |
| 49 | if os.Getenv("DISPLAY") == "" { |
| 50 | return fmt.Errorf("webbrowser: tried to open %q, no screen found", s) |
| 51 | } |
| 52 | fallthrough |
| 53 | case "darwin": |
| 54 | // Check SSH env vars. |
| 55 | if os.Getenv("SSH_CLIENT") != "" || os.Getenv("SSH_TTY") != "" { |
| 56 | return fmt.Errorf("webbrowser: tried to open %q, but you are running a shell session", s) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // Try all candidates |
| 61 | for _, candidate := range Candidates { |
| 62 | err := candidate.Open(s) |
| 63 | if err == nil { |
| 64 | return nil |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return ErrCantOpenBrowser |
| 69 | } |
| 70 | |
| 71 | func init() { |
| 72 | // Register the default Browser for current OS, if it exists. |
| 73 | if os, ok := osCommand[runtime.GOOS]; ok { |
| 74 | Candidates = append(Candidates, browserCommand{os.cmd, os.args}) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | var ( |
| 79 | osCommand = map[string]*browserCommand{ |
| 80 | "android": &browserCommand{"xdg-open", nil}, |
| 81 | "darwin": &browserCommand{"open", nil}, |
| 82 | "freebsd": &browserCommand{"xdg-open", nil}, |
| 83 | "linux": &browserCommand{"xdg-open", nil}, |
| 84 | "netbsd": &browserCommand{"xdg-open", nil}, |
| 85 | "openbsd": &browserCommand{"xdg-open", nil}, // It may be open instead |
| 86 | "windows": &browserCommand{"cmd", []string{"/c", "start"}}, |
| 87 | } |
| 88 | winSchemes = [3]string{"https", "http", "file"} |
| 89 | ) |
| 90 | |
| 91 | type browserCommand struct { |
| 92 | cmd string |
| 93 | args []string |
| 94 | } |
| 95 | |
| 96 | func (b browserCommand) Command(s string) (*exec.Cmd, error) { |
| 97 | u, err := url.Parse(s) |
| 98 | if err != nil { |
| 99 | return nil, err |
| 100 | } |
| 101 | |
| 102 | validUrl := ensureValidURL(u) |
| 103 | |
| 104 | b.args = append(b.args, validUrl) |
| 105 | |
| 106 | return exec.Command(b.cmd, b.args...), nil |
| 107 | } |
| 108 | |
| 109 | func (b browserCommand) Open(s string) error { |
| 110 | cmd, err := b.Command(s) |
| 111 | if err != nil { |
| 112 | return err |
| 113 | } |
| 114 | |
| 115 | return cmd.Run() |
| 116 | } |
| 117 | |
| 118 | func ensureScheme(u *url.URL) { |
| 119 | for _, s := range winSchemes { |
| 120 | if u.Scheme == s { |
| 121 | return |
| 122 | } |
| 123 | } |
| 124 | u.Scheme = "http" |
| 125 | } |
| 126 | |
| 127 | func ensureValidURL(u *url.URL) string { |
| 128 | // Enforce a scheme (windows requires scheme to be set to work properly). |
| 129 | ensureScheme(u) |
| 130 | s := u.String() |
| 131 | |
| 132 | // Escape characters not allowed by cmd/bash |
| 133 | switch runtime.GOOS { |
| 134 | case "windows": |
| 135 | s = strings.Replace(s, "&", `^&`, -1) |
| 136 | } |
| 137 | |
| 138 | return s |
| 139 | } |