-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeadLinks.go
88 lines (75 loc) · 2.13 KB
/
deadLinks.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
package main
import (
"io/ioutil"
"log"
"net/http"
"os"
"github.com/fatih/color"
"mvdan.cc/xurls"
)
// Count number of elements we will need in the string array.
func determineArraySize(content []byte) int {
count := 0
for i := 0; i < len(content); i++ {
if string(content[i]) == "\n" {
count++
}
}
return count
}
// For each element in the stringArray, add a character (stored in context) until a character is equal to a new-line.
// Do this n amount of times for every element in stringArray
func fillStringArray(emptyArray []string, content []byte) {
characterCount := 0
for k := 0; k < len(emptyArray); k++ {
for x := characterCount; string(content[x]) != "\n"; x++ {
emptyArray[k] += string(content[x])
characterCount = x + 2
}
}
}
// Extract links from entire html tags
func extractLinks(stringArray []string) {
rxRelaxed := xurls.Relaxed()
for i := 0; i < len(stringArray); i++ {
stringArray[i] = rxRelaxed.FindString(stringArray[i])
}
}
// Send GET request to all links in array
func httpRequestLinks(stringArray []string) {
//colorGreen := "\033[32m"
c1 := color.New(color.FgHiGreen).Add()
c2 := color.New(color.FgHiRed).Add()
c3 := color.New(color.FgHiBlack).Add()
for i := 0; i < len(stringArray); i++ {
resp, err := http.Head(stringArray[i])
if err != nil {
c3.Printf("%-10v", "UNKNOWN")
c3.Println(stringArray[i])
} else {
if resp.StatusCode == 200 {
c1.Printf("%-10v", "GOOD")
c1.Println(stringArray[i])
} else if resp.StatusCode == 400 || resp.StatusCode == 404 {
c2.Printf("%-10v", "BAD")
c2.Println(stringArray[i])
}
}
}
}
func main() {
// Read file in local directory - store contents in content.
fileName := os.Args[1]
content, err := ioutil.ReadFile(fileName)
if err != nil {
log.Fatal(err)
}
// Make array of strings with specific number of elements.
stringArray := make([]string, determineArraySize(content))
// Fill the made array of strings with content which is an array of bytes.
fillStringArray(stringArray, content)
// Extract all links from the array.
extractLinks(stringArray)
// Send GET Requests to array of links
httpRequestLinks(stringArray)
}