forked from sensepost/gowitness
/
helpers.go
135 lines (105 loc) 路 2.35 KB
/
helpers.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package lib
import (
"net"
"net/url"
"path/filepath"
"regexp"
"strconv"
"strings"
)
// ScreenshotPath determines a full path and file name for a screenshot image
func ScreenshotPath(destination string, url *url.URL, path string) string {
var fname, dst string
if destination == "" {
fname = SafeFileName(url.String())
dst = filepath.Join(path, fname)
} else {
fname = destination
if filepath.IsAbs(fname) {
dst = fname
} else {
dst = filepath.Join(path, fname)
}
}
return dst
}
// SafeFileName return a safe string that can be used in file names
func SafeFileName(str string) string {
name := strings.ToLower(str)
name = strings.Trim(name, " ")
separators, err := regexp.Compile(`[ &_=+:/]`)
if err == nil {
name = separators.ReplaceAllString(name, "-")
}
legal, err := regexp.Compile(`[^[:alnum:]-.]`)
if err == nil {
name = legal.ReplaceAllString(name, "")
}
for strings.Contains(name, "--") {
name = strings.Replace(name, "--", "-", -1)
}
return name + `.png`
}
// PortsFromString returns a slice of ports parsed from a string
func PortsFromString(ports string) ([]int, error) {
parsed := strings.Split(ports, ",")
var m = make(map[int]bool)
var r []int
for _, port := range parsed {
p, err := strconv.Atoi(port)
if err != nil {
continue
}
// uniq
if m[p] {
continue
}
r = append(r, p)
m[p] = true
}
return r, nil
}
// HostsInCIDR returns the IP's from a provided CIDR
func HostsInCIDR(cidr string) ([]string, error) {
ip, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, err
}
var ips []string
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
ips = append(ips, ip.String())
}
if len(ips) > 1 {
// remove network address and broadcast address
return ips[1 : len(ips)-1], nil
}
// suppose this will only really happen with /32's
return ips, nil
}
// helper method: https://play.golang.org/p/m8TNTtygK0
func inc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
// SliceContainsInt checks if a slice has an int
func SliceContainsInt(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
// SliceContainsString checks if a slice has a string
func SliceContainsString(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}