summaryrefslogtreecommitdiff
path: root/plugin/special/stdout.go
blob: 614deaac8395721e6e444ab8f7c8152e6e5328b6 (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
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 newStdout(action, data)
	})
	lib.RegisterOutputConverter(TypeStdout, &Stdout{
		Description: DescStdout,
	})
}

func newStdout(action lib.Action, data json.RawMessage) (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 &Stdout{
		Type:        TypeStdout,
		Action:      action,
		Description: DescStdout,
		Want:        tmp.Want,
		Exclude:     tmp.Exclude,
		OnlyIPType:  tmp.OnlyIPType,
	}, nil
}

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

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
}