summaryrefslogtreecommitdiff
path: root/plugin/special/private.go
blob: 74dc1f442ee1c7f92814f7428cef49d587649387 (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
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",
	"::/128",
	"::1/128",
	"fc00::/7",
	"ff00::/8",
	"fe80::/10",
}

func init() {
	lib.RegisterInputConfigCreator(TypePrivate, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
		return NewPrivateFromBytes(action, data)
	})
	lib.RegisterInputConverter(TypePrivate, &private{
		Description: DescPrivate,
	})
}

func NewPrivate(action lib.Action, opts ...lib.InputOption) lib.InputConverter {
	p := &private{
		Type:        TypePrivate,
		Action:      action,
		Description: DescPrivate,
	}

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

	return p
}

func WithPrivateOnlyIPType(onlyIPType lib.IPType) lib.InputOption {
	return func(p lib.InputConverter) {
		p.(*private).OnlyIPType = onlyIPType
	}
}

func NewPrivateFromBytes(action lib.Action, data []byte) (lib.InputConverter, error) {
	var tmp struct {
		OnlyIPType lib.IPType `json:"onlyIPType"`
	}

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

	return NewPrivate(
		action,
		WithPrivateOnlyIPType(tmp.OnlyIPType),
	), nil
}

type private struct {
	Type        string
	Action      lib.Action
	Description string
	OnlyIPType  lib.IPType
}

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, found := container.GetEntry(entryNamePrivate)
	if !found {
		entry = lib.NewEntry(entryNamePrivate)
	}

	for _, cidr := range privateCIDRs {
		if err := entry.AddPrefix(cidr); err != nil {
			return nil, err
		}
	}

	ignoreIPType := lib.GetIgnoreIPType(p.OnlyIPType)

	switch p.Action {
	case lib.ActionAdd:
		if err := container.Add(entry, ignoreIPType); err != nil {
			return nil, err
		}
	case lib.ActionRemove:
		if err := container.Remove(entry, lib.CaseRemovePrefix, ignoreIPType); err != nil {
			return nil, err
		}
	default:
		return nil, lib.ErrUnknownAction
	}

	return container, nil
}