-
Notifications
You must be signed in to change notification settings - Fork 634
/
hybrid.go
177 lines (153 loc) · 4.95 KB
/
hybrid.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
168
169
170
171
172
173
174
175
176
177
package hybrid
import (
"fmt"
"os"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/launcher/flags"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/katana/pkg/engine/common"
"github.com/projectdiscovery/katana/pkg/types"
errorutil "github.com/projectdiscovery/utils/errors"
urlutil "github.com/projectdiscovery/utils/url"
)
// Crawler is a standard crawler instance
type Crawler struct {
*common.Shared
browser *rod.Browser
// TODO: Remove the Chrome PID kill code in favor of using Leakless(true).
// This change will be made if there are no complaints about zombie Chrome processes.
// References:
// https://github.com/projectdiscovery/katana/issues/632
// https://github.com/projectdiscovery/httpx/issues/1425
// previousPIDs map[int32]struct{} // track already running PIDs
tempDir string
}
// New returns a new standard crawler instance
func New(options *types.CrawlerOptions) (*Crawler, error) {
var dataStore string
var err error
if options.Options.ChromeDataDir != "" {
dataStore = options.Options.ChromeDataDir
} else {
dataStore, err = os.MkdirTemp("", "katana-*")
if err != nil {
return nil, errorutil.NewWithTag("hybrid", "could not create temporary directory").Wrap(err)
}
}
// previousPIDs := processutil.FindProcesses(processutil.IsChromeProcess)
var launcherURL string
var chromeLauncher *launcher.Launcher
if options.Options.ChromeWSUrl != "" {
launcherURL = options.Options.ChromeWSUrl
} else {
// create new chrome launcher instance
chromeLauncher, err = buildChromeLauncher(options, dataStore)
if err != nil {
return nil, err
}
// launch chrome headless process
launcherURL, err = chromeLauncher.Launch()
if err != nil {
return nil, err
}
}
browser := rod.New().ControlURL(launcherURL)
if browserErr := browser.Connect(); browserErr != nil {
return nil, errorutil.NewWithErr(browserErr).Msgf("failed to connect to chrome instance at %s", launcherURL)
}
// create a new browser instance (default to incognito mode)
if !options.Options.HeadlessNoIncognito {
incognito, err := browser.Incognito()
if err != nil {
if chromeLauncher != nil {
chromeLauncher.Kill()
}
return nil, errorutil.NewWithErr(err).Msgf("failed to create incognito browser")
}
browser = incognito
}
shared, err := common.NewShared(options)
if err != nil {
return nil, errorutil.NewWithErr(err).WithTag("hybrid")
}
crawler := &Crawler{
Shared: shared,
browser: browser,
// previousPIDs: previousPIDs,
tempDir: dataStore,
}
return crawler, nil
}
// Close closes the crawler process
func (c *Crawler) Close() error {
if c.Options.Options.ChromeDataDir == "" {
if err := os.RemoveAll(c.tempDir); err != nil {
return err
}
}
// processutil.CloseProcesses(processutil.IsChromeProcess, c.previousPIDs)
return nil
}
// Crawl crawls a URL with the specified options
func (c *Crawler) Crawl(rootURL string) error {
crawlSession, err := c.NewCrawlSessionWithURL(rootURL)
crawlSession.Browser = c.browser
if err != nil {
return errorutil.NewWithErr(err).WithTag("hybrid")
}
defer crawlSession.CancelFunc()
gologger.Info().Msgf("Started headless crawling for => %v", rootURL)
if err := c.Do(crawlSession, c.navigateRequest); err != nil {
return errorutil.NewWithErr(err).WithTag("standard")
}
return nil
}
// buildChromeLauncher builds a new chrome launcher instance
func buildChromeLauncher(options *types.CrawlerOptions, dataStore string) (*launcher.Launcher, error) {
chromeLauncher := launcher.New().
Leakless(true).
Set("disable-gpu", "true").
Set("ignore-certificate-errors", "true").
Set("ignore-certificate-errors", "1").
Set("disable-crash-reporter", "true").
Set("disable-notifications", "true").
Set("hide-scrollbars", "true").
Set("window-size", fmt.Sprintf("%d,%d", 1080, 1920)).
Set("mute-audio", "true").
Delete("use-mock-keychain").
UserDataDir(dataStore)
if options.Options.UseInstalledChrome {
if options.Options.SystemChromePath != "" {
chromeLauncher.Bin(options.Options.SystemChromePath)
} else {
if chromePath, hasChrome := launcher.LookPath(); hasChrome {
chromeLauncher.Bin(chromePath)
} else {
return nil, errorutil.NewWithTag("hybrid", "the chrome browser is not installed").WithLevel(errorutil.Fatal)
}
}
}
if options.Options.SystemChromePath != "" {
chromeLauncher.Bin(options.Options.SystemChromePath)
}
if options.Options.ShowBrowser {
chromeLauncher = chromeLauncher.Headless(false)
} else {
chromeLauncher = chromeLauncher.Headless(true)
}
if options.Options.HeadlessNoSandbox {
chromeLauncher.Set("no-sandbox", "true")
}
if options.Options.Proxy != "" && options.Options.Headless {
proxyURL, err := urlutil.Parse(options.Options.Proxy)
if err != nil {
return nil, err
}
chromeLauncher.Set("proxy-server", proxyURL.String())
}
for k, v := range options.Options.ParseHeadlessOptionalArguments() {
chromeLauncher.Set(flags.Flag(k), v)
}
return chromeLauncher, nil
}