-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
154 lines (124 loc) · 4.24 KB
/
main.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
package main
import (
"fmt"
"os"
"os/signal"
"regexp"
"syscall"
"github.com/alecthomas/kong"
"github.com/fatih/color"
"github.com/jamesits/goinvoke"
"github.com/neutrino2211/go-result"
"github.com/playwright-community/playwright-go"
//#include<stdio.h>
"C"
)
var browser playwright.Browser
type ValRadar struct {
Debug bool `short:"!" name:"debug" name:"Show debug messaging"`
Site string `required short:"s" name:"site" help:"The website to scan"`
Depth uint `short:"d" name:"depth" help:"How deep to search" default:"1"`
Concurrency uint `short:"c" name:"concurrency" help:"How many coroutines to use" default:"10"`
Regex string `required short:"p" name:"pattern" help:"The regex pattern to try matching"`
UseHeadlessBrowser bool `name:"use-headless-browser" help:"Use a headless chrome browser to fetch the webpages"`
}
func (v *ValRadar) Run(globals *ValRadar) error {
re := result.SomePair(regexp.Compile(globals.Regex)).Expect("unable to compile the regex pattern " + globals.Regex)
sm := NewSiteMap(globals.Site)
ccr := NewCCR(int(globals.Concurrency), globals.Debug)
stateStorage := ""
// Set up signal handling
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
// Ensure cleanup happens
defer func() {
<-sigChan
print("Exiting...") // Not sure if I need the terminal input cleanup?
os.Exit(0)
}()
if globals.UseHeadlessBrowser {
playwright.Install(&playwright.RunOptions{
Browsers: []string{"chromium"},
Verbose: false,
})
pw := result.SomePair(playwright.Run()).Expect("unable to run playwright")
browser = result.SomePair(pw.Chromium.Launch()).Expect("unable to launch chromium")
sm.fetcherFunc = func(s string) string {
return fetchWithChrome(s, stateStorage)
}
}
BuildSiteMap(ccr, sm, sm.url, 0, int(globals.Depth))
ccr.wait()
sm.spinner.Stop()
found := 0
println("Starting search...")
for p, r := range sm.resources {
ccr.start(&CCRJob{
routine: func () {
matches := re.FindAllString(r.content, -1)
for _, match := range matches {
found += 1
fmt.Println("🔎 Found " + color.HiGreenString(match) + " at the url " + color.GreenString(p))
}
},
name: "Searching " + p,
})
}
ccr.wait()
//sm.spinner.Stop()
if found == 0 {
fmt.Println(color.RedString("No matches found for " + globals.Regex))
}
os.Exit(0)
return nil
}
func fetchWithChrome(url string, statePath string) string {
userAgent := "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
jsEnabled := true
waitUntil := "load"
waitUntilDomLoaded := "domcontentloaded"
loadTimeout := float64(7000)
resHtml := result.Try(func() string {
page := result.SomePair(browser.NewPage(playwright.BrowserNewPageOptions{
UserAgent: &userAgent,
JavaScriptEnabled: &jsEnabled,
})).Expect("unable to create playwright page")
if statePath != "" {
result.SomePair(page.Context().StorageState(statePath)).Expect("unable to get storage state")
}
if _, err := page.Goto(url, playwright.PageGotoOptions{
Timeout: &loadTimeout,
WaitUntil: (*playwright.WaitUntilState)(&waitUntil),
}); err != nil {
result.SomePair(page.Goto(url, playwright.PageGotoOptions{
Timeout: &loadTimeout,
WaitUntil: (*playwright.WaitUntilState)(&waitUntilDomLoaded),
})).Expect("unable to visit page " + url + " with playwright")
}
resHtml := result.SomePair(page.Locator(`html`).InnerHTML()).Expect("unable to get page inner html for " + url)
page.Close()
return resHtml
})
if resHtml.Error() != "" {
println("\n ❌ " + resHtml.Error())
}
return resHtml.Or("")
}
type ValradarPlugin struct {
VALRADAR_ARGS **C.char
}
func main() {
// wip: loading plugins
plugin := ValradarPlugin{}
goinvoke.Unmarshal("plugins/test_c/c_plugin.dylib", &plugin)
ctx := kong.Parse(
&ValRadar{},
kong.Name("valradar"),
kong.Description("Search for patterns and strings over a website's footprint"),
kong.UsageOnError(),
)
err := ctx.Run()
if err != nil {
panic(err)
}
}