/
types.go
58 lines (50 loc) · 1.07 KB
/
types.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
package asnmap
import (
"regexp"
"github.com/asaskevich/govalidator"
iputil "github.com/khulnasoft-lab/utils/ip"
stringsutil "github.com/khulnasoft-lab/utils/strings"
)
type InputType uint8
const (
ASN InputType = iota
ASNID
IP
Org
Domain
Unknown
)
var domainRegex = regexp.MustCompile(`^(?i)[a-z0-9-]+(\.[a-z0-9-]+)+\.?$`)
// checkIfASN checks if the given input is ASN or not,
// its possible to have an org name starting with AS/as prefix.
func checkIfASN(input string) bool {
if len(input) == 0 {
return false
}
hasASNPrefix := stringsutil.HasPrefixI(input, "AS")
if hasASNPrefix {
input = input[2:]
}
return hasASNPrefix && checkIfASNId(input)
}
func checkIfASNId(input string) bool {
if len(input) == 0 {
return false
}
hasNumericId := input != "" && govalidator.IsNumeric(input)
return hasNumericId
}
func IdentifyInput(input string) InputType {
switch {
case iputil.IsIP(input):
return IP
case checkIfASN(input):
return ASN
case checkIfASNId(input):
return ASNID
case domainRegex.MatchString(input):
return Domain
default:
return Org
}
}