Skip to content

Commit f9531e6

Browse files
committed
mmdb: add overwriteList
1 parent 986d910 commit f9531e6

File tree

1 file changed

+58
-27
lines changed

1 file changed

+58
-27
lines changed

plugin/maxmind/mmdb.go

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func newMMDB(action lib.Action, data json.RawMessage) (lib.OutputConverter, erro
3838
OutputName string `json:"outputName"`
3939
OutputDir string `json:"outputDir"`
4040
Want []string `json:"wantedList"`
41+
Overwrite []string `json:"overwriteList"`
4142
OnlyIPType lib.IPType `json:"onlyIPType"`
4243
}
4344

@@ -62,6 +63,7 @@ func newMMDB(action lib.Action, data json.RawMessage) (lib.OutputConverter, erro
6263
OutputName: tmp.OutputName,
6364
OutputDir: tmp.OutputDir,
6465
Want: tmp.Want,
66+
Overwrite: tmp.Overwrite,
6567
OnlyIPType: tmp.OnlyIPType,
6668
}, nil
6769
}
@@ -73,6 +75,7 @@ type mmdb struct {
7375
OutputName string
7476
OutputDir string
7577
Want []string
78+
Overwrite []string
7679
OnlyIPType lib.IPType
7780
}
7881

@@ -89,14 +92,6 @@ func (m *mmdb) GetDescription() string {
8992
}
9093

9194
func (m *mmdb) Output(container lib.Container) error {
92-
// Filter want list
93-
wantList := make(map[string]bool)
94-
for _, want := range m.Want {
95-
if want = strings.ToUpper(strings.TrimSpace(want)); want != "" {
96-
wantList[want] = true
97-
}
98-
}
99-
10095
writer, err := mmdbwriter.New(
10196
mmdbwriter.Options{
10297
DatabaseType: "GeoIP2-Country",
@@ -109,27 +104,16 @@ func (m *mmdb) Output(container lib.Container) error {
109104
}
110105

111106
updated := false
112-
switch len(wantList) {
113-
case 0:
114-
for entry := range container.Loop() {
115-
if err := m.marshalData(writer, entry); err != nil {
116-
return err
117-
}
118-
updated = true
107+
for _, name := range m.getEntryNameListInOrder(container) {
108+
entry, found := container.GetEntry(name)
109+
if !found {
110+
log.Printf("❌ entry %s not found", name)
111+
continue
119112
}
120-
121-
default:
122-
for name := range wantList {
123-
entry, found := container.GetEntry(name)
124-
if !found {
125-
log.Printf("❌ entry %s not found", name)
126-
continue
127-
}
128-
if err := m.marshalData(writer, entry); err != nil {
129-
return err
130-
}
131-
updated = true
113+
if err := m.marshalData(writer, entry); err != nil {
114+
return err
132115
}
116+
updated = true
133117
}
134118

135119
if updated {
@@ -143,6 +127,53 @@ func (m *mmdb) Output(container lib.Container) error {
143127
return nil
144128
}
145129

130+
func (m *mmdb) getEntryNameListInOrder(container lib.Container) []string {
131+
/*
132+
Note: The IPs and/or CIDRs of the latter list will overwrite those of the former one
133+
when duplicated data found due to MaxMind mmdb file format constraint.
134+
135+
Be sure to place the name of the most important list at last
136+
when writing wantedList and overwriteList in config file.
137+
138+
The order of names in wantedList has a higher priority than which of the overwriteList.
139+
*/
140+
141+
wantList := make([]string, 0, 200)
142+
for _, want := range m.Want {
143+
if want = strings.ToUpper(strings.TrimSpace(want)); want != "" {
144+
wantList = append(wantList, want)
145+
}
146+
}
147+
148+
if len(wantList) > 0 {
149+
return wantList
150+
}
151+
152+
overwriteList := make([]string, 0, 200)
153+
overwriteMap := make(map[string]bool)
154+
for _, overwrite := range m.Overwrite {
155+
if overwrite = strings.ToUpper(strings.TrimSpace(overwrite)); overwrite != "" {
156+
overwriteList = append(overwriteList, overwrite)
157+
overwriteMap[overwrite] = true
158+
}
159+
}
160+
161+
list := make([]string, 0, 200)
162+
for entry := range container.Loop() {
163+
name := entry.GetName()
164+
_, found := overwriteMap[name]
165+
if found {
166+
continue
167+
}
168+
list = append(list, name)
169+
}
170+
171+
// Make sure the names in overwriteList are written at last
172+
list = append(list, overwriteList...)
173+
174+
return list
175+
}
176+
146177
func (m *mmdb) marshalData(writer *mmdbwriter.Tree, entry *lib.Entry) error {
147178
var entryCidr []string
148179
var err error

0 commit comments

Comments
 (0)