-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathraven.go
127 lines (115 loc) · 2.64 KB
/
raven.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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
"golang.org/x/net/html"
)
var (
internalURLs = make(map[string]bool)
externalURLs = make(map[string]bool)
maxURLs int
maxDepth int
wg sync.WaitGroup
mutex sync.Mutex
concurrency int
client = &http.Client{
Timeout: 10 * time.Second,
}
)
func isValidURL(u string) bool {
parsed, err := url.Parse(u)
if err != nil {
return false
}
return parsed.Scheme != "" && parsed.Host != ""
}
func getAllWebsiteLinks(u string) []string {
var urls []string
resp, err := client.Get(u)
if err != nil {
log.Printf("[raven] Error fetching URL %s: %v\n", u, err)
return urls
}
defer resp.Body.Close()
tokenizer := html.NewTokenizer(resp.Body)
for {
tokenType := tokenizer.Next()
if tokenType == html.ErrorToken {
break
}
token := tokenizer.Token()
if tokenType == html.StartTagToken && token.Data == "a" {
for _, attr := range token.Attr {
if attr.Key == "href" {
href := attr.Val
if href == "" {
continue
}
if strings.HasPrefix(href, "#") || strings.HasPrefix(href, "mailto:") || strings.HasPrefix(href, "tel:") {
continue
}
if !isValidURL(href) {
continue
}
urls = append(urls, href)
}
}
}
}
return urls
}
func crawl(u string, depth int) {
defer wg.Done()
if depth > maxDepth {
return
}
log.Printf("[raven] [*] Crawling: %s (Depth: %d)\n", u, depth)
links := getAllWebsiteLinks(u)
mutex.Lock()
for _, link := range links {
if len(internalURLs)+len(externalURLs) > maxURLs {
break
}
_, isInternal := internalURLs[link]
_, isExternal := externalURLs[link]
if isInternal || isExternal {
continue
}
if strings.Contains(link, u) {
log.Printf("[raven] [*] Internal link: %s\n", link)
internalURLs[link] = true
} else {
log.Printf("[raven] [!] External link: %s\n", link)
externalURLs[link] = true
}
wg.Add(1)
go crawl(link, depth+1)
}
mutex.Unlock()
}
func main() {
flag.IntVar(&maxURLs, "maxURLs", 100, "Maximum number of URLs to crawl")
flag.IntVar(&maxDepth, "maxDepth", 3, "Maximum depth of crawling")
flag.IntVar(&concurrency, "concurrency", 10, "Number of concurrent requests")
flag.Parse()
startURL := flag.Arg(0)
if startURL == "" {
fmt.Println("Usage: raven [options] [URL]")
flag.PrintDefaults()
os.Exit(1)
}
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
wg.Add(1)
go crawl(startURL, 0)
wg.Wait()
fmt.Println("[+] Total Internal links:", len(internalURLs))
fmt.Println("[+] Total External links:", len(externalURLs))
fmt.Println("[+] Total URLs:", len(internalURLs)+len(externalURLs))
}