|
| 1 | +package slackauth |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "runtime" |
| 10 | + |
| 11 | + "github.com/go-rod/rod" |
| 12 | + "github.com/go-rod/rod/lib/launcher" |
| 13 | + "github.com/go-rod/rod/lib/proto" |
| 14 | +) |
| 15 | + |
| 16 | +func browserLauncher(headless bool) *launcher.Launcher { |
| 17 | + var l *launcher.Launcher |
| 18 | + if binpath, ok := lookPath(); ok { |
| 19 | + l = launcher.New(). |
| 20 | + Bin(binpath). |
| 21 | + Headless(headless). |
| 22 | + Leakless(isLeaklessEnabled). // Causes false positive on Windows, see #260 |
| 23 | + Devtools(false) |
| 24 | + } else { |
| 25 | + l = launcher.New(). |
| 26 | + Leakless(isLeaklessEnabled). // Causes false positive on Windows, see #260 |
| 27 | + Headless(headless). |
| 28 | + Devtools(false) |
| 29 | + } |
| 30 | + return l |
| 31 | +} |
| 32 | + |
| 33 | +// lookPath is extended launcher.LookPath that includes support for Brave |
| 34 | +// browser. |
| 35 | +// |
| 36 | +// (c) MIT license: Copyright 2019 Yad Smood |
| 37 | +func lookPath() (found string, has bool) { |
| 38 | + list := map[string][]string{ |
| 39 | + "darwin": { |
| 40 | + "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser", |
| 41 | + "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", |
| 42 | + "/Applications/Chromium.app/Contents/MacOS/Chromium", |
| 43 | + "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge", |
| 44 | + "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary", |
| 45 | + "/usr/bin/google-chrome-stable", |
| 46 | + "/usr/bin/google-chrome", |
| 47 | + "/usr/bin/chromium", |
| 48 | + "/usr/bin/chromium-browser", |
| 49 | + }, |
| 50 | + "linux": { |
| 51 | + "brave-browser", |
| 52 | + "chrome", |
| 53 | + "google-chrome", |
| 54 | + "/usr/bin/brave-browser", |
| 55 | + "/usr/bin/google-chrome", |
| 56 | + "microsoft-edge", |
| 57 | + "/usr/bin/microsoft-edge", |
| 58 | + "chromium", |
| 59 | + "chromium-browser", |
| 60 | + "/usr/bin/google-chrome-stable", |
| 61 | + "/usr/bin/chromium", |
| 62 | + "/usr/bin/chromium-browser", |
| 63 | + "/snap/bin/chromium", |
| 64 | + "/data/data/com.termux/files/usr/bin/chromium-browser", |
| 65 | + }, |
| 66 | + "openbsd": { |
| 67 | + "chrome", |
| 68 | + "chromium", |
| 69 | + }, |
| 70 | + "windows": append([]string{"chrome", "edge"}, expandWindowsExePaths( |
| 71 | + `BraveSoftware\Brave-Browser\Application\brave.exe`, |
| 72 | + `Google\Chrome\Application\chrome.exe`, |
| 73 | + `Chromium\Application\chrome.exe`, |
| 74 | + `Microsoft\Edge\Application\msedge.exe`, |
| 75 | + )...), |
| 76 | + }[runtime.GOOS] |
| 77 | + |
| 78 | + for _, path := range list { |
| 79 | + var err error |
| 80 | + found, err = exec.LookPath(path) |
| 81 | + has = err == nil |
| 82 | + if has { |
| 83 | + break |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return |
| 88 | +} |
| 89 | + |
| 90 | +// expandWindowsExePaths is a verbatim copy of the function from rod's |
| 91 | +// browser.go. |
| 92 | +// |
| 93 | +// (c) MIT license: Copyright 2019 Yad Smood |
| 94 | +func expandWindowsExePaths(list ...string) []string { |
| 95 | + newList := []string{} |
| 96 | + for _, p := range list { |
| 97 | + newList = append( |
| 98 | + newList, |
| 99 | + filepath.Join(os.Getenv("ProgramFiles"), p), |
| 100 | + filepath.Join(os.Getenv("ProgramFiles(x86)"), p), |
| 101 | + filepath.Join(os.Getenv("LocalAppData"), p), |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + return newList |
| 106 | +} |
| 107 | + |
| 108 | +func setCookies(browser *rod.Browser, cookies []*http.Cookie) error { |
| 109 | + if len(cookies) == 0 { |
| 110 | + return nil |
| 111 | + } |
| 112 | + for _, c := range cookies { |
| 113 | + if err := browser.SetCookies([]*proto.NetworkCookieParam{ |
| 114 | + {Name: c.Name, Value: c.Value, Domain: c.Domain, Path: c.Path, Expires: proto.TimeSinceEpoch(c.Expires.Unix())}, |
| 115 | + }); err != nil { |
| 116 | + return fmt.Errorf("failed to set cookies: %w", err) |
| 117 | + } |
| 118 | + } |
| 119 | + return nil |
| 120 | +} |
0 commit comments