-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlink-checker.go
164 lines (133 loc) · 3.56 KB
/
link-checker.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
package main
import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
"time"
)
type Result struct {
url string
source string
status int
message string
body string
contentType string
}
type DiscoveredUrl struct {
url string
source string
}
func findRoot(url string) string {
url = url + "/" //just in case url has no leading slash. More than one won't harm
re := regexp.MustCompile("https?://([^/]*/)")
root := re.FindString(url)
return root
}
func checkWebsite(url string, limit int, urlsToIgnore []string, statsChan chan Result) bool {
resultChan := make(chan Result)
urlDiscoveryChan := make(chan DiscoveredUrl)
pendingChecks := 1
count := 0
knownUrls := make(map[string]string)
for _, knownUrl := range urlsToIgnore {
knownUrls[knownUrl] = knownUrl
}
finishOrLimitChan := make(chan bool)
urlRoot := findRoot(url)
go func(finishOrLimitChan chan bool, urlDiscoveryChan chan DiscoveredUrl) {
for {
if count == limit {
finishOrLimitChan <- true
break
}
result := <- resultChan
pendingChecks--
statsChan <- result
if strings.HasPrefix(result.url, urlRoot) && isHtmlContentType(result.contentType) {
//parse page urls only if this page is on our domain
newUrls := findUrls(result.body)
for _,newUrl := range newUrls {
if strings.HasPrefix(newUrl, "//") { //protocol relative url
newUrl = urlRoot[0:strings.Index(urlRoot, ":") + 1] + newUrl
}
if string([]rune(newUrl)[0]) == "/" { //make sure we have an absolute URL
newUrl = strings.Replace(newUrl, "/", urlRoot, 1)
}
if string([]rune(newUrl)[0]) == "#" { //skip if this is an internal link
continue
}
if strings.HasPrefix(newUrl, "mailto:") { //skip email urls
continue
}
if strings.HasPrefix(newUrl, "data:") { //skip email urls
continue
}
if _, ok := knownUrls[newUrl]; ok {
continue
}
if !strings.HasPrefix(newUrl, urlRoot) {
//fmt.Println(newUrl)
//continue
}
knownUrls[newUrl] = result.url
if pendingChecks > 5 {
time.Sleep(1)
}
if pendingChecks > 10 {
time.Sleep(5)
}
pendingChecks++
go checkUrl(newUrl, result.url, resultChan)
}
} else {
//fmt.Println(result.url)
}
count++
if pendingChecks == 0 {
finishOrLimitChan <- true
break
}
}
}(finishOrLimitChan, urlDiscoveryChan)
//init the first check
go checkUrl(url, "", resultChan)
<-finishOrLimitChan
return true
}
func removeLineContent() {
fmt.Print("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b")
}
func main() {
config := getConfiguration()
fmt.Println("Website to be checked " + config.Url + ".")
fmt.Println("Max count of URLs to be checked " + strconv.Itoa(config.Limit))
count := 0
errorCount := 0
statsChan := make(chan Result)
go func(statsChan chan Result) {
for {
result := <- statsChan
count++
removeLineContent()
if result.status != 200 {
errorCount++
fmt.Println("Error: HTTP status " + strconv.Itoa(result.status) + ", Url " + result.url + ", Source " + result.source + " " + result.message)
}
if config.DisplayProgress {
fmt.Print("URLs checked " + strconv.Itoa(count))
}
}
}(statsChan)
checkWebsite(config.Url, config.Limit, config.UrlsToIgnore, statsChan)
removeLineContent()
fmt.Println()
fmt.Println("Total URLs checked: " + strconv.Itoa(count))
if errorCount == 0 {
fmt.Println("No broken URLs")
os.Exit(0)
}
fmt.Println("Broken URLs: " + strconv.Itoa(errorCount))
os.Exit(1)
}