-
Notifications
You must be signed in to change notification settings - Fork 4
/
source.go
199 lines (172 loc) · 4.96 KB
/
source.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
package mmdbmeld
import (
"encoding/hex"
"errors"
"fmt"
"math"
"net"
"strconv"
"strings"
"github.com/maxmind/mmdbwriter/mmdbtype"
)
// Source describes a generic geoip data source.
type Source interface {
Name() string
NextEntry() (*SourceEntry, error)
Err() error
}
// SourceEntry describes a geoip data source entry.
// Either Net or both From and To must be set.
type SourceEntry struct {
Net *net.IPNet
From net.IP
To net.IP
Values map[string]SourceValue
}
// SourceValue holds an unprocessed source data value, including its type.
type SourceValue struct {
Type string
Value string
}
// LoadSources loads the given input files from the database config.
func LoadSources(dbConfig DatabaseConfig) ([]Source, error) {
sources := make([]Source, 0, len(dbConfig.Inputs))
for _, input := range dbConfig.Inputs {
switch {
case strings.HasSuffix(input.File, ".csv"):
s, err := LoadCSVSource(input, dbConfig.Types)
if err != nil {
return nil, fmt.Errorf("failed to load input file %s: %w", input.File, err)
}
sources = append(sources, s)
case strings.HasSuffix(input.File, ".ipfire.txt"):
s, err := LoadIPFireSource(input, dbConfig.Types)
if err != nil {
return nil, fmt.Errorf("failed to load input file %s: %w", input.File, err)
}
sources = append(sources, s)
default:
return nil, fmt.Errorf("unsupported input file: %s", input.File)
}
}
return sources, nil
}
// ToMMDBMap transforms the source entry to a mmdb map type.
func (se SourceEntry) ToMMDBMap(optim Optimizations) (mmdbtype.Map, error) {
m := mmdbtype.Map{}
for key, entry := range se.Values {
// Transform value to mmdb type.
mmdbVal, err := entry.ToMMDBType(optim)
if err != nil {
return nil, fmt.Errorf("failed to transform %s with value %s (of type %s): %w", key, entry.Value, entry.Type, err)
}
// Get sub map for entry.
keyParts := strings.Split(key, ".")
mapForEntry := m
for i := 0; i < len(keyParts)-1; i++ {
subMapVal, ok := mapForEntry[mmdbtype.String(keyParts[i])]
if !ok {
nextMapForEntry := mmdbtype.Map{}
mapForEntry[mmdbtype.String(keyParts[i])] = nextMapForEntry
mapForEntry = nextMapForEntry
} else {
mapForEntry, ok = subMapVal.(mmdbtype.Map)
if !ok {
return nil, fmt.Errorf("failed to transform %s: submap %s already exists but is a %T, and not a map", key, strings.Join(keyParts[:1], "."), subMapVal)
}
}
}
// Set value in (sub) map.
mapForEntry[mmdbtype.String(keyParts[len(keyParts)-1])] = mmdbVal
}
return m, nil
}
// ToMMDBType transforms the source value to the correct mmdb type.
func (sv SourceValue) ToMMDBType(optim Optimizations) (mmdbtype.DataType, error) {
subType, isArrayType := strings.CutPrefix(sv.Type, "array:")
if isArrayType {
return toMMDBArray(subType, sv.Value, optim)
}
return toMMDBType(sv.Type, sv.Value, optim)
}
func toMMDBType(fieldType, fieldValue string, optim Optimizations) (mmdbtype.DataType, error) {
switch fieldType {
case "bool":
v, err := strconv.ParseBool(fieldValue)
if err != nil {
return nil, err
}
return mmdbtype.Bool(v), nil
case "string":
return mmdbtype.String(fieldValue), nil
case "hexbytes":
v, err := hex.DecodeString(fieldValue)
if err != nil {
return nil, err
}
return mmdbtype.Bytes(v), nil
case "int32":
v, err := strconv.ParseInt(fieldValue, 10, 32)
if err != nil {
return nil, err
}
return mmdbtype.Int32(int32(v)), nil
case "uint16":
v, err := strconv.ParseUint(fieldValue, 10, 16)
if err != nil {
return nil, err
}
return mmdbtype.Uint16(uint16(v)), nil
case "uint32":
v, err := strconv.ParseUint(fieldValue, 10, 32)
if err != nil {
return nil, err
}
return mmdbtype.Uint32(uint32(v)), nil
case "uint64":
v, err := strconv.ParseUint(fieldValue, 10, 64)
if err != nil {
return nil, err
}
return mmdbtype.Uint64(v), nil
case "float32":
v, err := strconv.ParseFloat(fieldValue, 32)
if err != nil {
return nil, err
}
if optim.FloatDecimals != 0 {
v = roundToDecimalPlaces(v, optim.FloatDecimals)
}
return mmdbtype.Float32(v), nil
case "float64":
v, err := strconv.ParseFloat(fieldValue, 64)
if err != nil {
return nil, err
}
if optim.FloatDecimals != 0 {
v = roundToDecimalPlaces(v, optim.FloatDecimals)
}
return mmdbtype.Float64(v), nil
default:
return nil, errors.New("unsupport type")
}
}
func toMMDBArray(fieldType, fieldValue string, optim Optimizations) (mmdbtype.DataType, error) {
fields := strings.Fields(fieldValue)
array := make([]mmdbtype.DataType, 0, len(fields))
for i, field := range strings.Fields(fieldValue) {
entry, err := toMMDBType(fieldType, field, optim)
if err != nil {
return nil, fmt.Errorf("array entry #%d is invalid: %w", i, err)
}
array = append(array, entry)
}
return mmdbtype.Slice(array), nil
}
func roundToDecimalPlaces(num float64, decimalPlaces int) float64 {
if decimalPlaces < 0 {
decimalPlaces = 0
}
shift := math.Pow(10, float64(decimalPlaces))
return math.Round(num*shift) / shift
}