-
Notifications
You must be signed in to change notification settings - Fork 1
/
parsecmd.go
193 lines (152 loc) · 3.35 KB
/
parsecmd.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"encoding/json"
"io"
"net/netip"
"regexp"
"strconv"
"strings"
"github.com/BourgeoisBear/nicsearch/rdap"
"github.com/pkg/errors"
)
type CmdIP struct {
IP netip.Addr
Assoc bool
}
type CmdEmail struct {
IP netip.Addr
}
type CmdASN struct {
ASN uint32
Assoc bool
}
type CmdCC struct {
CC string
}
type CmdAsName struct {
Name string
Assoc bool
}
type CmdRDAP_IP struct {
RIR rdap.RIRKey
IP netip.Addr
}
type CmdRDAP_Org struct {
RIR rdap.RIRKey
OrgId string
NetsOnly bool
}
type CmdAll struct{}
var g_cmdRegex []*regexp.Regexp
func init() {
szRegex := []string{
`(AS)N?\s+(\d+)\s*(\s\+)?`,
`(IP)\s+(.*?)\s*(\s\+)?`,
`(NA)(?:ME)?\s+(.*?)\s*(\s\+)?`,
`(CC)\s+([A-Z]{2})\s*`,
`(ALL)\s*`,
`(RDAP\.EMAIL)\s+(.*?)\s*`,
`(RDAP\.IP)\s+(.*?)\s+(.*?)\s*`,
`(RDAP\.ORG)\s+(.*?)\s+(.*?)\s*`,
`(RDAP\.ORGNETS)\s+(.*?)\s+(.*?)\s*`,
}
for _, txt := range szRegex {
g_cmdRegex = append(
g_cmdRegex,
regexp.MustCompile(`(?i)^\s*`+txt+`$`),
)
}
}
type Modes struct {
Color bool
Pretty bool
PrependQuery bool
}
func (m *Modes) PrintJSON(iWri io.Writer, bsJSON []byte) error {
// unmarshal / re-marshal + indent in pretty mode
if m.Pretty {
mTmp := make(map[string]interface{})
err := json.Unmarshal(bsJSON, &mTmp)
if err != nil {
return err
}
bsJSON, err = json.MarshalIndent(mTmp, "", " ")
if err != nil {
return err
}
}
_, err := iWri.Write(append(bsJSON, '\n'))
return err
}
// cmd should be upper-case and without leading/trailing whitespace
func (m *Modes) ParseCmd(cmd string) (CmdExec, error) {
// TODO: quick command reference, point out closest command on fail
for _, rx := range g_cmdRegex {
sArg := rx.FindStringSubmatch(cmd)
if len(sArg) < 2 {
continue
}
sArg = sArg[1:]
// TODO: validate commands
bGetAssociated := strings.HasSuffix(cmd, "+")
switch sArg[0] {
// ASN
case "AS":
nASN, e2 := strconv.ParseUint(sArg[1], 10, 32)
if e2 != nil {
return nil, errors.WithMessage(e2, "invalid ASN")
}
return CmdASN{ASN: uint32(nASN), Assoc: bGetAssociated}, nil
// IP
case "IP":
ip, e2 := netip.ParseAddr(sArg[1])
if e2 != nil {
return nil, errors.WithMessage(e2, "invalid IP")
}
return CmdIP{IP: ip, Assoc: bGetAssociated}, nil
// NA
case "NA":
if len(sArg[1]) == 0 {
return nil, errors.New("empty name search regex")
}
return CmdAsName{Name: sArg[1], Assoc: bGetAssociated}, nil
// CC
case "CC":
return CmdCC{CC: sArg[1]}, nil
// ALL
case "ALL":
return CmdAll{}, nil
// EMAIL
case "RDAP.EMAIL":
ip, e2 := netip.ParseAddr(sArg[1])
if e2 != nil {
return nil, errors.WithMessage(e2, "invalid IP")
}
return CmdEmail{IP: ip}, nil
// RDAP
case "RDAP.IP":
rk, e2 := rdap.RegistryNameToKey(sArg[1])
if e2 != nil {
return nil, e2
}
ip, e2 := netip.ParseAddr(sArg[2])
if e2 != nil {
return nil, errors.WithMessage(e2, "invalid IP")
}
return CmdRDAP_IP{RIR: rk, IP: ip}, nil
case "RDAP.ORG":
rk, e2 := rdap.RegistryNameToKey(sArg[1])
if e2 != nil {
return nil, e2
}
return CmdRDAP_Org{RIR: rk, OrgId: sArg[2]}, nil
case "RDAP.ORGNETS":
rk, e2 := rdap.RegistryNameToKey(sArg[1])
if e2 != nil {
return nil, e2
}
return CmdRDAP_Org{RIR: rk, OrgId: sArg[2], NetsOnly: true}, nil
}
}
return nil, EInvalidQuery
}