-
Notifications
You must be signed in to change notification settings - Fork 666
/
lookup.go
272 lines (232 loc) · 7.21 KB
/
lookup.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package main
import (
"bufio"
"fmt"
"log"
"net/netip"
"os"
"strings"
"github.com/Loyalsoldier/geoip/lib"
"github.com/Loyalsoldier/geoip/plugin/maxmind"
"github.com/Loyalsoldier/geoip/plugin/mihomo"
"github.com/Loyalsoldier/geoip/plugin/plaintext"
"github.com/Loyalsoldier/geoip/plugin/singbox"
"github.com/Loyalsoldier/geoip/plugin/special"
"github.com/Loyalsoldier/geoip/plugin/v2ray"
"github.com/spf13/cobra"
)
var supportedInputFormats = map[string]bool{
strings.ToLower("clashRuleSet"): true,
strings.ToLower("clashRuleSetClassical"): true,
strings.ToLower("dbipCountryMMDB"): true,
strings.ToLower("ipinfoCountryMMDB"): true,
strings.ToLower("maxmindMMDB"): true,
strings.ToLower("mihomoMRS"): true,
strings.ToLower("singboxSRS"): true,
strings.ToLower("surgeRuleSet"): true,
strings.ToLower("text"): true,
strings.ToLower("v2rayGeoIPDat"): true,
}
func init() {
rootCmd.AddCommand(lookupCmd)
lookupCmd.Flags().StringP("format", "f", "", "(Required) The input format. Available formats: text, v2rayGeoIPDat, maxmindMMDB, dbipCountryMMDB, ipinfoCountryMMDB, mihomoMRS, singboxSRS, clashRuleSet, clashRuleSetClassical, surgeRuleSet")
lookupCmd.Flags().StringP("uri", "u", "", "URI of the input file, support both local file path and remote HTTP(S) URL. (Cannot be used with \"dir\" flag)")
lookupCmd.Flags().StringP("dir", "d", "", "Path to the input directory. The filename without extension will be as the name of the list. (Cannot be used with \"uri\" flag)")
lookupCmd.Flags().StringSliceP("searchlist", "l", []string{}, "The lists to search from, separated by comma")
lookupCmd.MarkFlagRequired("format")
lookupCmd.MarkFlagsOneRequired("uri", "dir")
lookupCmd.MarkFlagsMutuallyExclusive("uri", "dir")
lookupCmd.MarkFlagDirname("dir")
}
var lookupCmd = &cobra.Command{
Use: "lookup",
Aliases: []string{"find"},
Short: "Lookup if specified IP or CIDR is in specified lists",
Args: cobra.RangeArgs(0, 1),
Run: func(cmd *cobra.Command, args []string) {
// Validate format
format, _ := cmd.Flags().GetString("format")
format = strings.ToLower(strings.TrimSpace(format))
if _, found := supportedInputFormats[format]; !found {
log.Fatal("unsupported input format")
}
// Set name
name := "true"
// Get uri
uri, _ := cmd.Flags().GetString("uri")
// Get dir
dir, _ := cmd.Flags().GetString("dir")
// Get searchlist
searchList, _ := cmd.Flags().GetStringSlice("searchlist")
switch len(args) > 0 {
case true: // With search arg, run in once mode
search := strings.ToLower(strings.TrimSpace(args[0]))
if !isValidIPOrCIDR(search) {
fmt.Println("false")
return
}
instance, err := lib.NewInstance()
if err != nil {
log.Fatal(err)
}
instance.AddInput(getInputForLookup(format, name, uri, dir))
instance.AddOutput(getOutputForLookup(search, searchList...))
if err := instance.Run(); err != nil {
log.Fatal(err)
}
case false: // No search arg, run in REPL mode
instance, err := lib.NewInstance()
if err != nil {
log.Fatal(err)
}
instance.AddInput(getInputForLookup(format, name, uri, dir))
container := lib.NewContainer()
if err := instance.RunInput(container); err != nil {
log.Fatal(err)
}
fmt.Println(`Enter IP or CIDR (type "exit" to quit):`)
fmt.Print(">> ")
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
search := strings.ToLower(strings.TrimSpace(scanner.Text()))
if search == "" {
fmt.Println()
fmt.Print(">> ")
continue
}
if search == "exit" || search == `"exit"` {
break
}
if !isValidIPOrCIDR(search) {
fmt.Println("false")
fmt.Println()
fmt.Print(">> ")
continue
}
instance.ResetOutput()
instance.AddOutput(getOutputForLookup(search, searchList...))
if err := instance.RunOutput(container); err != nil {
log.Fatal(err)
}
fmt.Println()
fmt.Print(">> ")
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
},
}
// Check if the input is a valid IP or CIDR
func isValidIPOrCIDR(search string) bool {
if search == "" {
return false
}
var err error
switch strings.Contains(search, "/") {
case true: // CIDR
_, err = netip.ParsePrefix(search)
case false: // IP
_, err = netip.ParseAddr(search)
}
return err == nil
}
func getInputForLookup(format, name, uri, dir string) lib.InputConverter {
var input lib.InputConverter
switch strings.ToLower(format) {
case strings.ToLower(maxmind.TypeMaxmindMMDBIn):
input = &maxmind.MMDBIn{
Type: maxmind.TypeMaxmindMMDBIn,
Action: lib.ActionAdd,
Description: maxmind.DescMaxmindMMDBIn,
URI: uri,
}
case strings.ToLower(maxmind.TypeDBIPCountryMMDBIn):
input = &maxmind.MMDBIn{
Type: maxmind.TypeDBIPCountryMMDBIn,
Action: lib.ActionAdd,
Description: maxmind.DescDBIPCountryMMDBIn,
URI: uri,
}
case strings.ToLower(maxmind.TypeIPInfoCountryMMDBIn):
input = &maxmind.MMDBIn{
Type: maxmind.TypeIPInfoCountryMMDBIn,
Action: lib.ActionAdd,
Description: maxmind.DescIPInfoCountryMMDBIn,
URI: uri,
}
case strings.ToLower(mihomo.TypeMRSIn):
input = &mihomo.MRSIn{
Type: mihomo.TypeMRSIn,
Action: lib.ActionAdd,
Description: mihomo.DescMRSIn,
Name: name,
URI: uri,
InputDir: dir,
}
case strings.ToLower(singbox.TypeSRSIn):
input = &singbox.SRSIn{
Type: singbox.TypeSRSIn,
Action: lib.ActionAdd,
Description: singbox.DescSRSIn,
Name: name,
URI: uri,
InputDir: dir,
}
case strings.ToLower(v2ray.TypeGeoIPdatIn):
input = &v2ray.GeoIPDatIn{
Type: v2ray.TypeGeoIPdatIn,
Action: lib.ActionAdd,
Description: v2ray.DescGeoIPdatIn,
URI: uri,
}
case strings.ToLower(plaintext.TypeTextIn):
input = &plaintext.TextIn{
Type: plaintext.TypeTextIn,
Action: lib.ActionAdd,
Description: plaintext.DescTextIn,
Name: name,
URI: uri,
InputDir: dir,
}
case strings.ToLower(plaintext.TypeClashRuleSetIPCIDRIn):
input = &plaintext.TextIn{
Type: plaintext.TypeClashRuleSetIPCIDRIn,
Action: lib.ActionAdd,
Description: plaintext.DescClashRuleSetIn,
Name: name,
URI: uri,
InputDir: dir,
}
case strings.ToLower(plaintext.TypeClashRuleSetClassicalIn):
input = &plaintext.TextIn{
Type: plaintext.TypeClashRuleSetClassicalIn,
Action: lib.ActionAdd,
Description: plaintext.DescClashClassicalIn,
Name: name,
URI: uri,
InputDir: dir,
}
case strings.ToLower(plaintext.TypeSurgeRuleSetIn):
input = &plaintext.TextIn{
Type: plaintext.TypeSurgeRuleSetIn,
Action: lib.ActionAdd,
Description: plaintext.DescSurgeRuleSetIn,
Name: name,
URI: uri,
InputDir: dir,
}
default:
log.Fatal("unsupported input format")
}
return input
}
func getOutputForLookup(search string, searchList ...string) lib.OutputConverter {
return &special.Lookup{
Type: special.TypeLookup,
Action: lib.ActionOutput,
Description: special.DescLookup,
Search: search,
SearchList: searchList,
}
}