summaryrefslogtreecommitdiff
path: root/plugin/maxmind/common_in.go
blob: a937ef988a145f96fb3d441a2e39d5b4a694ad9c (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
package maxmind

import (
	"encoding/json"
	"path/filepath"
	"strings"

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

var (
	defaultGeoLite2CountryMMDBFile = filepath.Join("./", "geolite2", "GeoLite2-Country.mmdb")
	defaultDBIPCountryMMDBFile     = filepath.Join("./", "db-ip", "dbip-country-lite.mmdb")
	defaultIPInfoCountryMMDBFile   = filepath.Join("./", "ipinfo", "country.mmdb")
)

func NewGeoLite2CountryMMDBIn(iType string, iDesc string, action lib.Action, opts ...lib.InputOption) lib.InputConverter {
	g := &geoLite2CountryMMDBIn{
		Type:        iType,
		Action:      action,
		Description: iDesc,
	}

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

	return g
}

func WithGeoLite2CountryMMDBInURI(iType, uri string) lib.InputOption {
	return func(s lib.InputConverter) {
		uri = strings.TrimSpace(uri)
		if uri == "" {
			switch iType {
			case TypeGeoLite2CountryMMDBIn:
				uri = defaultGeoLite2CountryMMDBFile
			case TypeDBIPCountryMMDBIn:
				uri = defaultDBIPCountryMMDBFile
			case TypeIPInfoCountryMMDBIn:
				uri = defaultIPInfoCountryMMDBFile
			}
		}

		s.(*geoLite2CountryMMDBIn).URI = uri
	}
}

func WithGeoLite2CountryMMDBInWantedList(lists []string) lib.InputOption {
	return func(s lib.InputConverter) {
		wantList := make(map[string]bool)
		for _, want := range lists {
			if want = strings.ToUpper(strings.TrimSpace(want)); want != "" {
				wantList[want] = true
			}
		}

		s.(*geoLite2CountryMMDBIn).Want = wantList
	}
}

func WithGeoLite2CountryMMDBInOnlyIPType(onlyIPType lib.IPType) lib.InputOption {
	return func(s lib.InputConverter) {
		s.(*geoLite2CountryMMDBIn).OnlyIPType = onlyIPType
	}
}

func NewGeoLite2CountryMMDBInFromBytes(iType string, iDesc string, action lib.Action, data []byte) (lib.InputConverter, error) {
	var tmp struct {
		URI        string     `json:"uri"`
		Want       []string   `json:"wantedList"`
		OnlyIPType lib.IPType `json:"onlyIPType"`
	}

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

	return NewGeoLite2CountryMMDBIn(
		iType,
		iDesc,
		action,
		WithGeoLite2CountryMMDBInURI(iType, tmp.URI),
		WithGeoLite2CountryMMDBInWantedList(tmp.Want),
		WithGeoLite2CountryMMDBInOnlyIPType(tmp.OnlyIPType),
	), nil
}