forked from Loyalsoldier/geoip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookup.go
143 lines (126 loc) · 3.82 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
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
"github.com/Loyalsoldier/geoip/lib"
"github.com/spf13/cobra"
)
var supportedInputFormats = map[string]bool{
strings.ToLower("maxmindMMDB"): true,
strings.ToLower("clashRuleSetClassical"): true,
strings.ToLower("clashRuleSet"): true,
strings.ToLower("surgeRuleSet"): true,
strings.ToLower("text"): true,
strings.ToLower("singboxSRS"): true,
strings.ToLower("v2rayGeoIPDat"): true,
}
func init() {
rootCmd.AddCommand(lookupCmd)
lookupCmd.Flags().StringP("format", "f", "", "The input format, available options: text, v2rayGeoIPDat, maxmindMMDB, singboxSRS, clashRuleSet, clashRuleSetClassical, surgeRuleSet")
lookupCmd.Flags().StringP("name", "n", "", "The name of the list, use with \"uri\" flag")
lookupCmd.Flags().StringP("uri", "u", "", "URI of the input file, support both local file path and remote HTTP(S) URL")
lookupCmd.Flags().StringP("dir", "d", "", "Path to the input directory. The filename without extension will be as the name of the list")
lookupCmd.Flags().StringSliceP("searchlist", "l", []string{}, "The lists to search from, separated by comma")
lookupCmd.MarkFlagRequired("format")
lookupCmd.MarkFlagsOneRequired("uri", "dir")
lookupCmd.MarkFlagsRequiredTogether("name", "uri")
lookupCmd.MarkFlagDirname("dir")
}
var lookupCmd = &cobra.Command{
Use: "lookup",
Aliases: []string{"find"},
Short: "Lookup specified IP or CIDR 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")
}
// Get name
name, _ := cmd.Flags().GetString("name")
name = strings.ToLower(strings.TrimSpace(name))
// Get uri
uri, _ := cmd.Flags().GetString("uri")
// Get dir
dir, _ := cmd.Flags().GetString("dir")
// Get searchlist
searchList, _ := cmd.Flags().GetStringSlice("searchlist")
searchListStr := strings.Join(searchList, `", "`)
if searchListStr != "" {
searchListStr = fmt.Sprint(`"`, searchListStr, `"`) // `"cn", "en"`
}
switch len(args) > 0 {
case true: // With search arg, run in once mode
search := strings.ToLower(args[0])
config := generateConfigForLookup(format, name, uri, dir, search, searchListStr)
instance, err := lib.NewInstance()
if err != nil {
log.Fatal(err)
}
if err := instance.InitFromBytes([]byte(config)); err != nil {
log.Fatal(err)
}
if err := instance.Run(); err != nil {
log.Fatal(err)
}
case false: // No search arg, run in REPL mode
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 == "exit" {
break
}
config := generateConfigForLookup(format, name, uri, dir, search, searchListStr)
instance, err := lib.NewInstance()
if err != nil {
log.Fatal(err)
}
if err := instance.InitFromBytes([]byte(config)); err != nil {
log.Fatal(err)
}
if err := instance.Run(); err != nil {
log.Fatal(err)
}
fmt.Println()
fmt.Print(">> ")
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
},
}
func generateConfigForLookup(format, name, uri, dir, search, searchListStr string) string {
return fmt.Sprintf(`
{
"input": [
{
"type": "%s",
"action": "add",
"args": {
"name": "%s",
"uri": "%s",
"inputDir": "%s"
}
}
],
"output": [
{
"type": "lookup",
"action": "output",
"args": {
"search": "%s",
"searchList": [%s]
}
}
]
}
`, format, name, uri, dir, search, searchListStr)
}