-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (92 loc) · 2.05 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
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"sync"
"time"
)
const (
userAgent = "Mozilla/5.0 (compatible; robotxtxt; +https://github.com/NkxxkN/robotstxt)"
)
var httpClient = &http.Client{}
func main() {
// concurrency flag
var (
httpClient http.Client
concurrency int
to int
wildcard bool
)
flag.IntVar(&concurrency, "c", 20, "Set the concurrency level")
flag.BoolVar(&wildcard, "w", true, "Should include wildcards")
flag.IntVar(&to, "t", 10000, "timeout (milliseconds)")
flag.Parse()
// make an actual time.Duration out of the timeout
timeout := time.Duration(to * 1000000)
httpClient = http.Client{
Timeout: timeout,
}
sc := bufio.NewScanner(os.Stdin)
var wg sync.WaitGroup
jobs := make(chan string)
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for host := range jobs {
req, err := http.NewRequest("GET", fmt.Sprintf(
"%s/robots.txt", host,
), nil)
if err != nil {
return
}
req.Header.Add("User-Agent", userAgent)
resp, err := httpClient.Do(req)
if err != nil {
continue
}
if resp.Body == nil {
continue
}
defer resp.Body.Close()
// always read the full body so we can re-use the tcp connection
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
continue
}
body := string(b)
lines := bufio.NewScanner(strings.NewReader(body))
for lines.Scan() {
line := lines.Text()
if !wildcard && strings.Contains(line, "*") {
continue
}
p := strings.Split(line, "llow: ") // Disallow: && Allow:
if len(p) == 2 {
path := p[1]
if strings.HasPrefix(path, "https://") || strings.HasPrefix(path, "http://") {
fmt.Printf("%s\n", path)
} else {
if strings.HasPrefix(path, "/") {
fmt.Printf("%s%s\n", host, path)
} else {
fmt.Printf("%s/%s\n", host, path)
}
}
}
}
}
}()
}
for sc.Scan() {
host := sc.Text()
jobs <- host
}
close(jobs)
wg.Wait()
}