/
text_in.go
200 lines (166 loc) · 4.38 KB
/
text_in.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
package plaintext
import (
"encoding/json"
"fmt"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/Loyalsoldier/geoip/lib"
)
const (
typeTextIn = "text"
descTextIn = "Convert plaintext IP & CIDR to other formats"
)
func init() {
lib.RegisterInputConfigCreator(typeTextIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
return newTextIn(typeTextIn, action, data)
})
lib.RegisterInputConverter(typeTextIn, &textIn{
Description: descTextIn,
})
}
func newTextIn(iType string, action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
var tmp struct {
Name string `json:"name"`
URI string `json:"uri"`
InputDir string `json:"inputDir"`
OnlyIPType lib.IPType `json:"onlyIPType"`
}
if strings.TrimSpace(iType) == "" {
return nil, fmt.Errorf("type is required")
}
if len(data) > 0 {
if err := json.Unmarshal(data, &tmp); err != nil {
return nil, err
}
}
if tmp.Name == "" && tmp.URI == "" && tmp.InputDir == "" {
return nil, fmt.Errorf("type %s | action %s missing inputdir or name or uri", typeTextIn, action)
}
if (tmp.Name != "" && tmp.URI == "") || (tmp.Name == "" && tmp.URI != "") {
return nil, fmt.Errorf("type %s | action %s name & uri must be specified together", typeTextIn, action)
}
return &textIn{
Type: iType,
Action: action,
Description: descTextIn,
Name: tmp.Name,
URI: tmp.URI,
InputDir: tmp.InputDir,
OnlyIPType: tmp.OnlyIPType,
}, nil
}
func (t *textIn) GetType() string {
return t.Type
}
func (t *textIn) GetAction() lib.Action {
return t.Action
}
func (t *textIn) GetDescription() string {
return t.Description
}
func (t *textIn) Input(container lib.Container) (lib.Container, error) {
entries := make(map[string]*lib.Entry)
var err error
switch {
case t.InputDir != "":
err = t.walkDir(t.InputDir, entries)
case t.Name != "" && t.URI != "":
switch {
case strings.HasPrefix(t.URI, "http://"), strings.HasPrefix(t.URI, "https://"):
err = t.walkRemoteFile(t.URI, t.Name, entries)
default:
err = t.walkLocalFile(t.URI, t.Name, entries)
}
default:
return nil, fmt.Errorf("config missing argument inputDir or name or uri")
}
if err != nil {
return nil, err
}
var ignoreIPType lib.IgnoreIPOption
switch t.OnlyIPType {
case lib.IPv4:
ignoreIPType = lib.IgnoreIPv6
case lib.IPv6:
ignoreIPType = lib.IgnoreIPv4
}
if len(entries) == 0 {
return nil, fmt.Errorf("type %s | action %s no entry are generated", t.Type, t.Action)
}
for _, entry := range entries {
switch t.Action {
case lib.ActionAdd:
if err := container.Add(entry, ignoreIPType); err != nil {
return nil, err
}
case lib.ActionRemove:
container.Remove(entry.GetName(), ignoreIPType)
}
}
return container, nil
}
func (t *textIn) walkDir(dir string, entries map[string]*lib.Entry) error {
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if err := t.walkLocalFile(path, "", entries); err != nil {
return err
}
return nil
})
return err
}
func (t *textIn) walkLocalFile(path, name string, entries map[string]*lib.Entry) error {
name = strings.TrimSpace(name)
var filename string
if name != "" {
filename = name
} else {
filename = filepath.Base(path)
}
// check filename
if !regexp.MustCompile(`^[a-zA-Z0-9_.\-]+$`).MatchString(filename) {
return fmt.Errorf("filename %s cannot be entry name, please remove special characters in it", filename)
}
dotIndex := strings.LastIndex(filename, ".")
if dotIndex > 0 {
filename = filename[:dotIndex]
}
if _, found := entries[filename]; found {
return fmt.Errorf("found duplicated file %s", filename)
}
entry := lib.NewEntry(filename)
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
if err := t.scanFile(file, entry); err != nil {
return err
}
entries[filename] = entry
return nil
}
func (t *textIn) walkRemoteFile(url, name string, entries map[string]*lib.Entry) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("failed to get remote file %s, http status code %d", url, resp.StatusCode)
}
entry := lib.NewEntry(name)
if err := t.scanFile(resp.Body, entry); err != nil {
return err
}
entries[name] = entry
return nil
}