summaryrefslogtreecommitdiff
path: root/plugin/special/stdout.go
blob: e356c5777dfd698fbeb7c1779f2fe9396b85e82c (plain)
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
package special

import (
	"encoding/json"
	"errors"
	"io"
	"os"
	"slices"
	"strings"

	"github.com/Loyalsoldier/geoip/lib"
)

const (
	TypeStdout = "stdout"
	DescStdout = "Convert data to plaintext CIDR format and output to standard output"
)

func init() {
	lib.RegisterOutputConfigCreator(TypeStdout, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
		return NewStdoutFromBytes(action, data)
	})
	lib.RegisterOutputConverter(TypeStdout, &stdout{
		Description: DescStdout,
	})
}

type stdout struct {
	Type        string
	Action      lib.Action
	Description string
	Want        []string
	Exclude     []string
	OnlyIPType  lib.IPType
}

func NewStdout(action lib.Action, opts ...lib.OutputOption) lib.OutputConverter {
	s := &stdout{
		Type:        TypeStdout,
		Action:      action,
		Description: DescStdout,
	}

	for _, opt := range opts {
		if opt != nil {
			opt(s)
		}
	}

	return s
}

func WithStdoutWantedList(lists []string) lib.OutputOption {
	return func(s lib.OutputConverter) {
		s.(*stdout).Want = lists
	}
}

func WithStdoutExcludedList(lists []string) lib.OutputOption {
	return func(s lib.OutputConverter) {
		s.(*stdout).Exclude = lists
	}
}

func WithStdoutOnlyIPType(onlyIPType lib.IPType) lib.OutputOption {
	return func(s lib.OutputConverter) {
		s.(*stdout).OnlyIPType = onlyIPType
	}
}

func NewStdoutFromBytes(action lib.Action, data []byte) (lib.OutputConverter, error) {
	var tmp struct {
		Want       []string   `json:"wantedList"`
		Exclude    []string   `json:"excludedList"`
		OnlyIPType lib.IPType `json:"onlyIPType"`
	}

	if len(data) > 0 {
		if err := json.Unmarshal(data, &tmp); err != nil {
			return nil, err
		}
	}

	return NewStdout(
		action,
		WithStdoutWantedList(tmp.Want),
		WithStdoutExcludedList(tmp.Exclude),
		WithStdoutOnlyIPType(tmp.OnlyIPType),
	), nil
}

func (s *stdout) GetType() string {
	return s.Type
}

func (s *stdout) GetAction() lib.Action {
	return s.Action
}

func (s *stdout) GetDescription() string {
	return s.Description
}

func (s *stdout) Output(container lib.Container) error {
	for _, name := range s.filterAndSortList(container) {
		entry, found := container.GetEntry(name)
		if !found {
			continue
		}

		cidrList, err := s.generateCIDRList(entry)
		if err != nil {
			continue
		}

		for _, cidr := range cidrList {
			io.WriteString(os.Stdout, cidr+"\n")
		}
	}

	return nil
}

func (s *stdout) filterAndSortList(container lib.Container) []string {
	excludeMap := make(map[string]bool)
	for _, exclude := range s.Exclude {
		if exclude = strings.ToUpper(strings.TrimSpace(exclude)); exclude != "" {
			excludeMap[exclude] = true
		}
	}

	wantList := make([]string, 0, len(s.Want))
	for _, want := range s.Want {
		if want = strings.ToUpper(strings.TrimSpace(want)); want != "" && !excludeMap[want] {
			wantList = append(wantList, want)
		}
	}

	if len(wantList) > 0 {
		// Sort the list
		slices.Sort(wantList)
		return wantList
	}

	list := make([]string, 0, 300)
	for entry := range container.Loop() {
		name := entry.GetName()
		if excludeMap[name] {
			continue
		}
		list = append(list, name)
	}

	// Sort the list
	slices.Sort(list)

	return list
}

func (s *stdout) generateCIDRList(entry *lib.Entry) ([]string, error) {
	entryList, err := entry.MarshalText(lib.GetIgnoreIPType(s.OnlyIPType))
	if err != nil {
		return nil, err
	}

	if len(entryList) == 0 {
		return nil, errors.New("empty CIDR list")
	}

	return entryList, nil
}