-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
56 lines (46 loc) · 944 Bytes
/
http.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
package main
import (
"fmt"
"log"
"net/http"
"net/url"
"github.com/temoto/robotstxt"
)
/*
* Send a GET request to check if url is reachable
*/
func canReach(url string) bool {
resp, err := http.Get(url)
if err != nil {
log.Println(err)
return false
}
return resp.StatusCode == 200
}
/*
* Sent a GET request and try to locate robots.txt
* When found check if our UserAgent is allowed to access requested path
* also check if we can access /
*/
func robots(rawUrl string) bool {
u, _ := url.Parse(rawUrl)
resp, err := http.Get(fmt.Sprintf("%s://%s/robots.txt", u.Scheme, u.Host))
if err != nil {
return false
}
// Check if allowed
robots, err := robotstxt.FromResponse(resp)
group := robots.FindGroup("Busyboi")
if err != nil {
// Handle missing robots txt
// Handle unreachable robots txt
}
can := true
if !group.Test(u.Path) {
can = false
}
if !group.Test("/") {
can = false
}
return can
}