forked from Davincible/chromedp-undetected
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chrome.go
167 lines (134 loc) · 3.68 KB
/
chrome.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Package chromedpundetected provides a chromedp context with an undetected
// Chrome browser.
package chromedpundetected
import (
"context"
"net"
"os"
"path"
"strconv"
"strings"
"github.com/Xuanwo/go-locale"
"github.com/chromedp/chromedp"
"github.com/google/uuid"
"golang.org/x/exp/slog"
)
// Defaults.
var (
DefaultUserDirPrefix = "chromedp-undetected-"
)
// New creates a context with an undetected Chrome executor.
func New(config Config) (context.Context, context.CancelFunc, error) {
var (
opts []chromedp.ExecAllocatorOption
tempDir bool
)
if config.UserDataDir == "" {
tempDir = true
config.UserDataDir = path.Join(os.TempDir(), DefaultUserDirPrefix+uuid.NewString())
}
headlessOpts, closeFrameBuffer, err := headlessFlag(config)
if err != nil {
return nil, func() {}, err
}
if config.Language == "" {
opts = append(opts, localeFlag())
} else {
opts = append(opts, chromedp.Flag("lang", config.Language))
}
opts = append(opts, supressWelcomeFlag()...)
opts = append(opts, logLevelFlag(config))
opts = append(opts, debuggerAddrFlag(config)...)
opts = append(opts, noSandboxFlag(config)...)
opts = append(opts, chromedp.UserDataDir(config.UserDataDir))
opts = append(opts, headlessOpts...)
opts = append(opts, config.ChromeFlags...)
ctx := context.Background()
if config.Ctx != nil {
ctx = config.Ctx
}
cancelT := func() {}
if config.Timeout > 0 {
ctx, cancelT = context.WithTimeout(ctx, config.Timeout)
}
ctx, cancelA := chromedp.NewExecAllocator(ctx, opts...)
ctx, cancelC := chromedp.NewContext(ctx, config.ContextOptions...)
cancel := func() {
cancelT()
cancelA()
cancelC()
if err := closeFrameBuffer(); err != nil {
slog.Error("failed to close Xvfb", err)
}
if tempDir {
_ = os.RemoveAll(config.UserDataDir) //nolint:errcheck
}
}
return ctx, cancel, nil
}
func supressWelcomeFlag() []chromedp.ExecAllocatorOption {
return []chromedp.ExecAllocatorOption{
chromedp.Flag("no-first-run", true),
chromedp.Flag("no-default-browser-check", true),
}
}
func debuggerAddrFlag(config Config) []chromedp.ExecAllocatorOption {
port := strconv.Itoa(config.Port)
if config.Port == 0 {
port = getRandomPort()
}
return []chromedp.ExecAllocatorOption{
chromedp.Flag("remote-debugging-host", "127.0.0.1"),
chromedp.Flag("remote-debugging-port", port),
}
}
func localeFlag() chromedp.ExecAllocatorOption {
lang := "en-US"
if tag, err := locale.Detect(); err != nil && len(tag.String()) > 0 {
lang = tag.String()
}
return chromedp.Flag("lang", lang)
}
func noSandboxFlag(config Config) []chromedp.ExecAllocatorOption {
var opts []chromedp.ExecAllocatorOption
if config.NoSandbox {
opts = append(opts,
chromedp.Flag("no-sandbox", true),
chromedp.Flag("test-type", true))
}
return opts
}
func logLevelFlag(config Config) chromedp.ExecAllocatorOption {
return chromedp.Flag("log-level", strconv.Itoa(config.LogLevel))
}
func headlessFlag(config Config) ([]chromedp.ExecAllocatorOption, func() error, error) {
var opts []chromedp.ExecAllocatorOption
cleanup := func() error { return nil }
if config.Headless {
var (
optx []chromedp.ExecAllocatorOption
err error
)
optx, cleanup, err = headlessOpts()
if err != nil {
return nil, cleanup, err
}
opts = append(opts,
// chromedp.Flag("headless", true),
chromedp.Flag("window-size", "1920,1080"),
chromedp.Flag("start-maximized", true),
chromedp.Flag("no-sandbox", true),
)
opts = append(opts, optx...)
}
return opts, cleanup, nil
}
func getRandomPort() string {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err == nil {
addr := l.Addr().String()
l.Close() //nolint:errcheck,gosec
return strings.Split(addr, ":")[1]
}
return "42069"
}