-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker.go
103 lines (86 loc) · 2.13 KB
/
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
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strconv"
"time"
)
var url string
var checked map[string]bool
type error interface {
Error() string
}
func checkURL(link string) {
if checked[link] == true {
return
}
checked[link] = true
file, err := os.OpenFile("result.txt", os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
fmt.Println(errors.New("Can't open file"))
log.Fatal(err)
}
defer file.Close()
resp, err := http.Get(link)
if err != nil {
file.WriteString(link + " -> Error : " + err.Error() + "\n")
file.Sync()
return
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
file.WriteString(link + " -> OK\n")
file.Sync()
} else {
file.WriteString(link + " -> Error " + strconv.Itoa(resp.StatusCode) + "\n")
file.Sync()
}
check := regexp.MustCompile(url + `.*`)
if check.MatchString(link) {
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
file.WriteString(link + " -> Error : " + err.Error() + "\n")
file.Sync()
return
}
html := string(content)
findURL(html)
}
return
}
func findURL(html string) {
aurl := regexp.MustCompile(`(http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?`)
rurl := regexp.MustCompile(`"\/([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])+"`)
findaurl := aurl.FindAllString(html, -1)
findrurl := rurl.FindAllString(html, -1)
for _, value := range findaurl {
checkURL(value)
}
for _, value := range findrurl {
value = url + value[2:len(value)-1]
checkURL(value)
}
}
func main() {
start := time.Now()
printStart := []byte("Start : " + start.String() + "\n\n")
ioutil.WriteFile("result.txt", printStart, 0644)
reg := regexp.MustCompile(`https?://.*?/`)
url = reg.FindString(os.Args[1])
checked = make(map[string]bool)
checkURL(url)
file, err := os.OpenFile("result.txt", os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
fmt.Println(errors.New("Can't open file"))
log.Fatal(err)
}
defer file.Close()
elapsed := time.Since(start)
file.WriteString("\nUrls found : " + strconv.Itoa(len(checked)) + " | Time spent : " + elapsed.String())
file.Sync()
}