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
|
package special
import (
"bufio"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/Loyalsoldier/geoip/lib"
)
const (
TypeStdin = "stdin"
DescStdin = "Accept plaintext IP & CIDR from standard input, separated by newline"
)
func init() {
lib.RegisterInputConfigCreator(TypeStdin, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
return NewStdinFromBytes(action, data)
})
lib.RegisterInputConverter(TypeStdin, &stdin{
Description: DescStdin,
})
}
func NewStdin(action lib.Action, opts ...lib.InputOption) lib.InputConverter {
s := &stdin{
Type: TypeStdin,
Action: action,
Description: DescStdin,
}
for _, opt := range opts {
if opt != nil {
opt(s)
}
}
return s
}
func WithStdinName(name string) lib.InputOption {
return func(s lib.InputConverter) {
s.(*stdin).Name = name
}
}
func WithStdinOnlyIPType(onlyIPType lib.IPType) lib.InputOption {
return func(s lib.InputConverter) {
s.(*stdin).OnlyIPType = onlyIPType
}
}
func NewStdinFromBytes(action lib.Action, data []byte) (lib.InputConverter, error) {
var tmp struct {
Name string `json:"name"`
OnlyIPType lib.IPType `json:"onlyIPType"`
}
if len(data) > 0 {
if err := json.Unmarshal(data, &tmp); err != nil {
return nil, err
}
}
if tmp.Name == "" {
return nil, fmt.Errorf("❌ [type %s | action %s] missing name", TypeStdin, action)
}
return NewStdin(
action,
WithStdinName(tmp.Name),
WithStdinOnlyIPType(tmp.OnlyIPType),
), nil
}
type stdin struct {
Type string
Action lib.Action
Description string
Name string
OnlyIPType lib.IPType
}
func (s *stdin) GetType() string {
return s.Type
}
func (s *stdin) GetAction() lib.Action {
return s.Action
}
func (s *stdin) GetDescription() string {
return s.Description
}
func (s *stdin) Input(container lib.Container) (lib.Container, error) {
entry := lib.NewEntry(s.Name)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
line, _, _ = strings.Cut(line, "#")
line, _, _ = strings.Cut(line, "//")
line, _, _ = strings.Cut(line, "/*")
line = strings.TrimSpace(line)
if line == "" {
continue
}
if err := entry.AddPrefix(line); err != nil {
continue
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
ignoreIPType := lib.GetIgnoreIPType(s.OnlyIPType)
switch s.Action {
case lib.ActionAdd:
if err := container.Add(entry, ignoreIPType); err != nil {
return nil, err
}
case lib.ActionRemove:
if err := container.Remove(entry, lib.CaseRemovePrefix, ignoreIPType); err != nil {
return nil, err
}
default:
return nil, lib.ErrUnknownAction
}
return container, nil
}
|