Skip to content

Commit 412f26a

Browse files
authored
Merge pull request #16 from rusq/ua-fix
User-Agent fix + optimisations
2 parents 21a386e + d402cdf commit 412f26a

9 files changed

+601
-251
lines changed

browser.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
}

cmd/playground/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func browserLogin(ctx context.Context) {
4141
ctx, cancel := context.WithTimeoutCause(ctx, 180*time.Second, errors.New("user too slow"))
4242
defer cancel()
4343

44-
token, cookies, err := slackauth.Browser(ctx, workspace, slackauth.WithNoConsentPrompt(), slackauth.WithUserAgentAuto())
44+
token, cookies, err := slackauth.Browser(ctx, workspace, slackauth.WithNoConsentPrompt())
4545
if err != nil {
4646
log.Fatal(err)
4747
}
@@ -57,12 +57,14 @@ func autoLogin(ctx context.Context) {
5757
username := envOrScan("EMAIL", "Enter email: ")
5858
password := envOrScan("PASSWORD", "Enter password: ")
5959

60-
token, cookies, err := slackauth.Headless(ctx, workspace, username, password, slackauth.WithDebug(enableTrace), slackauth.WithNoConsentPrompt(), slackauth.WithUserAgentAuto())
60+
start := time.Now()
61+
token, cookies, err := slackauth.Headless(ctx, workspace, username, password, slackauth.WithDebug(enableTrace), slackauth.WithNoConsentPrompt())
6162
if err != nil {
6263
log.Fatal(err)
6364
}
6465
fmt.Println(token)
6566
fmt.Println(cookies)
67+
fmt.Println("login took:", time.Since(start))
6668
}
6769

6870
func envOrScan(env, prompt string) string {

hijacker.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"strings"
88

99
"github.com/go-rod/rod"
10-
"github.com/go-rod/rod/lib/proto"
1110
)
1211

1312
// hijacker is a helper for hijacking requests.
@@ -70,20 +69,6 @@ func (h *hijacker) Token(ctx context.Context) (string, error) {
7069
}
7170
}
7271

73-
func setCookies(browser *rod.Browser, cookies []*http.Cookie) error {
74-
if len(cookies) == 0 {
75-
return nil
76-
}
77-
for _, c := range cookies {
78-
if err := browser.SetCookies([]*proto.NetworkCookieParam{
79-
{Name: c.Name, Value: c.Value, Domain: c.Domain, Path: c.Path, Expires: proto.TimeSinceEpoch(c.Expires.Unix())},
80-
}); err != nil {
81-
return fmt.Errorf("failed to set cookies: %w", err)
82-
}
83-
}
84-
return nil
85-
}
86-
8772
const (
8873
maxMem = 131072
8974
paramToken = "token"

0 commit comments

Comments
 (0)