diff options
| author | loyalsoldier <[email protected]> | 2021-08-27 18:27:16 +0800 |
|---|---|---|
| committer | loyalsoldier <[email protected]> | 2021-08-29 20:09:57 +0800 |
| commit | 85a343aca99d864c517f13cd3169ebcc910ec0d8 (patch) | |
| tree | eccfd3680d9dc6e22f265a9525dccac85902c2ab /plugin/special | |
| parent | 2b32e8845d9e55b6c23ebb41bd0f382100094386 (diff) | |
Refactor: use plugin architecture to support multiple I/O formats
Diffstat (limited to 'plugin/special')
| -rw-r--r-- | plugin/special/private.go | 94 | ||||
| -rw-r--r-- | plugin/special/test.go | 76 |
2 files changed, 170 insertions, 0 deletions
diff --git a/plugin/special/private.go b/plugin/special/private.go new file mode 100644 index 00000000..e8f231ef --- /dev/null +++ b/plugin/special/private.go @@ -0,0 +1,94 @@ +package special + +import ( + "encoding/json" + + "github.com/Loyalsoldier/geoip/lib" +) + +const ( + entryNamePrivate = "private" + typePrivate = "private" + descPrivate = "Convert LAN and private network CIDR to other formats" +) + +var privateCIDRs = []string{ + "0.0.0.0/8", + "10.0.0.0/8", + "100.64.0.0/10", + "127.0.0.0/8", + "169.254.0.0/16", + "172.16.0.0/12", + "192.0.0.0/24", + "192.0.2.0/24", + "192.88.99.0/24", + "192.168.0.0/16", + "198.18.0.0/15", + "198.51.100.0/24", + "203.0.113.0/24", + "224.0.0.0/4", + "240.0.0.0/4", + "255.255.255.255/32", + "::1/128", + "fc00::/7", + "fe80::/10", +} + +func init() { + lib.RegisterInputConfigCreator(typePrivate, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) { + return newPrivate(action, data) + }) + lib.RegisterInputConverter(typePrivate, &private{ + Description: descPrivate, + }) +} + +func newPrivate(action lib.Action, data json.RawMessage) (lib.InputConverter, error) { + return &private{ + Type: typePrivate, + Action: action, + Description: descPrivate, + }, nil +} + +type private struct { + Type string + Action lib.Action + Description string +} + +func (p *private) GetType() string { + return p.Type +} + +func (p *private) GetAction() lib.Action { + return p.Action +} + +func (p *private) GetDescription() string { + return p.Description +} + +func (p *private) Input(container lib.Container) (lib.Container, error) { + entry := lib.NewEntry(entryNamePrivate) + for _, cidr := range privateCIDRs { + if err := entry.AddPrefix(cidr); err != nil { + return nil, err + } + } + + switch p.Action { + case lib.ActionAdd: + if err := container.Add(entry); err != nil { + return nil, err + } + case lib.ActionRemove: + container.Remove(entryNamePrivate) + case lib.ActionReplace: + container.Replace(entry) + default: + return nil, lib.ErrUnknownAction + } + + return container, nil +} diff --git a/plugin/special/test.go b/plugin/special/test.go new file mode 100644 index 00000000..2700b61e --- /dev/null +++ b/plugin/special/test.go @@ -0,0 +1,76 @@ +package special + +import ( + "encoding/json" + + "github.com/Loyalsoldier/geoip/lib" +) + +const ( + entryNameTest = "test" + typeTest = "test" + descTest = "Convert specific CIDR to other formats (for test only)" +) + +var testCIDRs = []string{ + "127.0.0.0/8", +} + +func init() { + lib.RegisterInputConfigCreator(typeTest, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) { + return newTest(action, data) + }) + lib.RegisterInputConverter(typeTest, &test{ + Description: descTest, + }) +} + +func newTest(action lib.Action, data json.RawMessage) (lib.InputConverter, error) { + return &test{ + Type: typeTest, + Action: action, + Description: descTest, + }, nil +} + +type test struct { + Type string + Action lib.Action + Description string +} + +func (t *test) GetType() string { + return t.Type +} + +func (t *test) GetAction() lib.Action { + return t.Action +} + +func (t *test) GetDescription() string { + return t.Description +} + +func (t *test) Input(container lib.Container) (lib.Container, error) { + entry := lib.NewEntry(entryNameTest) + for _, cidr := range testCIDRs { + if err := entry.AddPrefix(cidr); err != nil { + return nil, err + } + } + + switch t.Action { + case lib.ActionAdd: + if err := container.Add(entry); err != nil { + return nil, err + } + case lib.ActionRemove: + container.Remove(entryNameTest) + case lib.ActionReplace: + container.Replace(entry) + default: + return nil, lib.ErrUnknownAction + } + + return container, nil +} |
