-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
97 lines (89 loc) · 2.07 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
package main
import (
"bufio"
"crypto/tls"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"sync"
"time"
)
func main() {
input := flag.String("f", "", "path to list of urls or ip addresses, defaults to stdin if left blank")
concurrent := flag.Int("c", 10, "number of concurrent requests to make")
timeout := flag.Int("t", 5, "timeout in seconds")
redirect := flag.Bool("r", false, "follow redirects")
showUrls := flag.Bool("u", false, "show urls in output")
flag.Parse()
var f io.ReadCloser
if *input == "" {
f = os.Stdin
} else {
file, err := os.Open(*input)
if err != nil {
fmt.Fprintln(os.Stderr, "unable to open input file:", err)
os.Exit(1)
}
f = file
}
defer f.Close()
work := make(chan string)
go func() {
s := bufio.NewScanner(f)
for s.Scan() {
work <- s.Text()
}
if s.Err() != nil {
fmt.Fprintln(os.Stderr, "error while scanning input:", s.Err())
}
close(work)
}()
client := &http.Client{
Timeout: time.Second * time.Duration(*timeout),
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
if !*redirect {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
wg := &sync.WaitGroup{}
for i := 0; i < *concurrent; i++ {
wg.Add(1)
go getCommon(client, work, wg, *showUrls)
}
wg.Wait()
}
func getCommon(client *http.Client, work chan string, wg *sync.WaitGroup, showUrls bool) {
for url := range work {
if !strings.HasPrefix(url, "https://") {
url = "https://" + url
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
continue
}
req.Close = true
resp, err := client.Do(req)
if err != nil {
continue
}
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
if resp.TLS == nil || len(resp.TLS.PeerCertificates) == 0 || resp.TLS.PeerCertificates[0].Subject.CommonName == "" {
continue
}
if showUrls {
fmt.Println(url, resp.TLS.PeerCertificates[0].Subject.CommonName)
} else {
fmt.Println(resp.TLS.PeerCertificates[0].Subject.CommonName)
}
}
wg.Done()
}