-
Notifications
You must be signed in to change notification settings - Fork 45
/
utils.go
103 lines (88 loc) · 2.07 KB
/
utils.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 (
"bufio"
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"os"
"strings"
)
func getSSLCert(ip string, dialer *net.Dialer) (*x509.Certificate, error) {
conn, err := tls.DialWithDialer(dialer, "tcp", ip, &tls.Config{
InsecureSkipVerify: true,
})
if err != nil {
return nil, err
}
defer conn.Close()
cert := conn.ConnectionState().PeerCertificates[0]
return cert, nil
}
// IPsFromCIDR generates a slice of IP strings from the given CIDR
func IPsFromCIDR(cidr string, chanInput chan string, ports []string) error {
ip, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return err
}
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
for _, port := range ports {
chanInput <- ip.String() + ":" + port
}
}
return nil
}
// inc increments an IP address
func inc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
func extractNames(cert *x509.Certificate) []string {
names := append([]string{cert.Subject.CommonName}, cert.DNSNames...)
return names
}
func intakeFunction(chanInput chan string, ports []string, input string) {
if _, err := os.Stat(input); err == nil {
readFile, err := os.Open(input)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
for fileScanner.Scan() {
line := fileScanner.Text()
processInput(line, chanInput, ports)
}
readFile.Close()
} else {
for _, argItem := range strings.Split(input, ",") {
processInput(argItem, chanInput, ports)
}
}
}
func isCIDR(value string) bool {
return strings.Contains(value, `/`)
}
func isHostPort(value string) bool {
return strings.Contains(value, `:`)
}
func processInput(argItem string, chanInput chan string, ports []string) {
argItem = strings.TrimSpace(argItem)
if isHostPort(argItem) {
chanInput <- argItem
} else if isCIDR(argItem) {
err := IPsFromCIDR(argItem, chanInput, ports)
if err != nil {
panic("unable to parse CIDR" + argItem)
}
} else {
for _, port := range ports {
chanInput <- argItem + ":" + port
}
}
}