-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaux.go
47 lines (38 loc) · 1.56 KB
/
aux.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
package gonmap
import (
"log"
"regexp"
)
// Gets a string and returns true if
// string represents a valid host false otherwise.
// Examples of valid hosts: 192.168.1.0, 192.0-100.0-3.8, 192.168.1.0/24, 192.0-100.0-3.8/19, 192.0-100.0-3.8,9,10,11/19, www.google.com, github.com/17
func isValidHost(h string) bool {
// Match examples:
// - 192.168.1.0
// - 192.0-100.0-3.8
// - 192.168.1.0/24
// - 192.0-100.0-3.8/19
// - 192,193,194.0-100.0.1
// - 192.168.0.1,2,3/23
ipRegex := `^(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)-(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)((,(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))*))\.){3}((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)-(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(/([0-9]|1[0-9]|2[0-9]|3[0-2]))?|(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)((,(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))*))$`
// Match examples:
// - google.com
// - www.google.com
// - github.com/17
domainRegex := `^[a-zA-Z0-9][a-zA-Z0-9-_]{0,61}[a-zA-Z0-9]{0,1}\.([a-zA-Z]{1,6}|[a-zA-Z0-9-]{1,30}\.[a-zA-Z]{2,3})(/([0-9]|1[0-9]|2[0-9]|3[0-2]))?$`
ipMatched, err := regexp.MatchString(ipRegex, h)
if err != nil {
log.Fatal(err)
}
domainMatched, err := regexp.MatchString(domainRegex, h)
if err != nil {
log.Fatal(err)
}
return (ipMatched || domainMatched)
}
func chunkBy(items []int, chunkSize int) (chunks [][]int) {
for chunkSize < len(items) {
items, chunks = items[chunkSize:], append(chunks, items[0:chunkSize])
}
return append(chunks, items)
}