-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmain.go
147 lines (128 loc) · 3.09 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
package main
import (
"fmt"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
"golang.org/x/net/html"
)
type result struct {
url string
err error
code int
}
var (
visited = make(map[string]bool)
mux sync.Mutex
baseURL = "http://localhost:1313" // Change to your local server address
concurrency = 3 // Set the desired concurrency level
requestDelay = 2000 * time.Millisecond // Set the desired delay between requests
semaphoreChan = make(chan struct{}, concurrency)
)
func main() {
var wg sync.WaitGroup
results := make(chan result)
go func() {
for r := range results {
if r.err != nil || r.code >= 400 {
fmt.Printf("BROKEN: %s (status: %d, error: %v)\n", r.url, r.code, r.err)
}
}
}()
// Start crawling from the base URL
wg.Add(1)
go crawl(baseURL, &wg, results)
// Wait for all crawling to finish, then close the results channel
wg.Wait()
close(results)
}
func crawl(link string, wg *sync.WaitGroup, results chan<- result) {
defer wg.Done()
mux.Lock()
if visited[link] {
// Skip the request and sleep if already visited
mux.Unlock()
return
}
visited[link] = true
mux.Unlock()
// Limit concurrency by acquiring a semaphore
semaphoreChan <- struct{}{}
defer func() { <-semaphoreChan }()
// Throttle each request only for the first time
time.Sleep(requestDelay)
fmt.Println("FETCH:", link)
resp, err := http.Get(link)
if err != nil {
results <- result{url: link, err: err, code: 0}
return
}
defer resp.Body.Close()
results <- result{url: link, code: resp.StatusCode}
// Only continue if the page is OK (status 200)
if resp.StatusCode == 200 {
links := extractLinks(resp, link)
for _, l := range links {
if strings.HasPrefix(l, baseURL) {
// Internal link: crawl recursively
wg.Add(1)
go crawl(l, wg, results)
} else {
// External link: check once
wg.Add(1)
go func(link string) {
defer wg.Done()
mux.Lock()
if visited[link] {
mux.Unlock()
return
}
visited[link] = true
mux.Unlock()
semaphoreChan <- struct{}{}
defer func() { <-semaphoreChan }()
time.Sleep(requestDelay) // Throttle external link requests
fmt.Println("FETCH:", link)
resp, err := http.Get(link)
code := 0
if err == nil {
code = resp.StatusCode
resp.Body.Close()
}
results <- result{url: link, err: err, code: code}
}(l)
}
}
}
}
func extractLinks(resp *http.Response, base string) []string {
links := []string{}
baseURL, err := url.Parse(base)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing base URL %s: %v\n", base, err)
return links
}
doc := html.NewTokenizer(resp.Body)
for {
tt := doc.Next()
switch tt {
case html.ErrorToken:
return links
case html.StartTagToken, html.SelfClosingTagToken:
t := doc.Token()
if t.Data == "a" {
for _, a := range t.Attr {
if a.Key == "href" {
href, err := baseURL.Parse(a.Val)
if err == nil && (href.Scheme == "http" || href.Scheme == "https") {
links = append(links, href.String())
}
}
}
}
}
}
}