-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
96 lines (83 loc) · 1.88 KB
/
main.go
File metadata and controls
96 lines (83 loc) · 1.88 KB
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
package main
import (
"bufio"
"flag"
"fmt"
"net/http"
"os"
"strings"
"sync"
"time"
)
func main() {
var concurrency int
flag.IntVar(&concurrency, "c", 20, "max concurrency")
var timeout int
flag.IntVar(&timeout, "t", 100000, "timeout in millisecond")
var headers string
flag.StringVar(&headers, "H", "", "headers")
flag.Parse()
maxChan := make(chan struct{}, concurrency)
scanner := bufio.NewScanner(os.Stdin)
wg := sync.WaitGroup{}
p := NewProbber("OPTIONS", time.Duration(timeout)*time.Millisecond, headers)
for scanner.Scan() {
maxChan <- struct{}{}
url := scanner.Text()
if url[:4] != "http" || url[:5] != "https" {
url = "https://" + url
}
wg.Add(1)
go func(u string) {
defer func() { <-maxChan }()
defer wg.Done()
p.OptionProbe(u)
}(url)
}
defer close(maxChan)
wg.Wait()
}
type Probber struct {
method string
headers string
timeout time.Duration
client *http.Client
}
func NewProbber(method string, timeout time.Duration, headers string) *Probber {
return &Probber{
method: method,
timeout: timeout,
headers: headers,
client: &http.Client{
Timeout: timeout,
},
}
}
func (p *Probber) OptionProbe(url string) {
optionRequest, _ := http.NewRequest(p.method, url, nil)
for _, header := range strings.Split(p.headers, ",") {
header = strings.TrimSpace(header)
if header != "" {
headerName := header[:strings.Index(header, ":")]
headerValue := header[strings.Index(header, ":")+1:]
optionRequest.Header.Set(headerName, headerValue)
}
}
resp, err := p.client.Do(optionRequest)
if err != nil {
return
}
defer resp.Body.Close()
if err != nil {
return
}
p.printMethods(url, resp)
}
func (p *Probber) printMethods(url string, resp *http.Response) {
if resp.Header.Get("Allow") == "" {
return
}
for _, method := range strings.Split(resp.Header.Get("Allow"), ",") {
fmt.Print(url + " " + method + "\n")
}
}