-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaddy_smallshield.go
201 lines (174 loc) · 5.18 KB
/
caddy_smallshield.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
200
201
package caddy_smallshield
import (
"errors"
"fmt"
"net/http"
"slices"
"strconv"
"strings"
"sync"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/proofrock/caddy_smallshield/iptree"
"go.uber.org/zap"
)
const VERSION = "v0.3.3"
func init() {
caddy.RegisterModule(CaddySmallShield{})
httpcaddyfile.RegisterHandlerDirective("caddy_smallshield", parseCaddyfile)
}
type CaddySmallShield struct {
BlacklistURL string `json:"blacklist_url,omitempty"`
Whitelist string `json:"whitelist,omitempty"`
ClosingHours string `json:"closing_hours,omitempty"`
LogBlockings string `json:"log_blockings,omitempty"`
blacklistCidrs *iptree.IPTree
whitelist []string
closingHours [24]bool
closingHoursPrintable string
closingHoursCount int
logBlockings bool
logger *zap.Logger
mutexForBlacklist *sync.RWMutex
mutexForWhitelist *sync.RWMutex
}
func (CaddySmallShield) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.caddy_smallshield",
New: func() caddy.Module { return new(CaddySmallShield) },
}
}
func (m *CaddySmallShield) Provision(ctx caddy.Context) error {
m.logger = ctx.Logger()
m.mutexForBlacklist = &sync.RWMutex{}
m.mutexForWhitelist = &sync.RWMutex{}
m.mutexForBlacklist.Lock()
defer m.mutexForBlacklist.Unlock()
if m.BlacklistURL != "" {
cidrs, err := iptree.NewFromURL(m.BlacklistURL, false)
if err != nil {
return err
}
m.blacklistCidrs = cidrs
} else {
m.blacklistCidrs = iptree.Empty()
}
m.mutexForWhitelist.Lock()
defer m.mutexForWhitelist.Unlock()
if m.Whitelist != "" {
m.whitelist = strings.Split(m.Whitelist, ",")
}
if m.ClosingHours != "" {
for _, ch := range strings.Split(m.ClosingHours, ",") {
hour, err := strconv.Atoi(strings.TrimSpace(ch))
if err != nil || hour < 0 || hour > 23 {
return fmt.Errorf("'%s' is not a valid closing hour", ch)
}
m.closingHours[hour] = true
m.closingHoursCount++
}
m.closingHoursPrintable = getPrintableSlice(m.closingHours)
}
if m.LogBlockings != "" {
lb, err := strconv.ParseBool(m.LogBlockings)
if err != nil {
return fmt.Errorf("'%s' is not a valid config for log_blockings", m.LogBlockings)
}
m.logBlockings = lb
}
loggingString := ""
if m.logBlockings {
loggingString = " Logging active."
}
m.logger.Sugar().Infof("SmallShield %s: init'd with %d items in blacklist and %d in whitelist, %d closing hours.%s", VERSION, m.blacklistCidrs.IPRangesIngested(), len(m.whitelist), m.closingHoursCount, loggingString)
// return fmt.Errorf("myerror")
return nil
}
func (m *CaddySmallShield) IsBlacklisted(ip string) bool {
if ip == "" {
return false // Invalid IP TODO check for integrity
}
m.mutexForBlacklist.RLock()
defer m.mutexForBlacklist.RUnlock()
ret, err := m.blacklistCidrs.CheckIP(ip)
if err != nil {
m.logger.Error(fmt.Sprintf("error in checking IP %s", ip))
return true
}
return ret
}
func (m *CaddySmallShield) IsWhitelisted(ip string) bool {
m.mutexForWhitelist.RLock()
defer m.mutexForWhitelist.RUnlock()
return slices.Contains(m.whitelist, ip)
}
func (m CaddySmallShield) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
blockedReason := ""
var hour = time.Now().Hour()
if m.closingHours[hour] {
blockedReason = fmt.Sprintf("shop is closed, hour is %d and closing_hours is set to %s", hour, m.closingHoursPrintable)
} else {
var ip = cutToColon(r.RemoteAddr)
if m.IsBlacklisted(ip) && !m.IsWhitelisted(ip) {
blockedReason = fmt.Sprintf("IP %s is blocked", ip)
}
}
if blockedReason != "" {
if m.logBlockings {
m.logger.Sugar().Infof("blocked: %s", blockedReason)
}
return caddyhttp.Error(403, errors.New(blockedReason))
}
return next.ServeHTTP(w, r)
}
func (m *CaddySmallShield) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
for d.NextBlock(0) {
switch d.Val() {
case "whitelist":
if !d.Args(&m.Whitelist) {
return d.Err("invalid whitelist configuration")
}
case "blacklist_url":
if !d.Args(&m.BlacklistURL) {
return d.Err("invalid blacklist_url configuration")
}
case "closing_hours":
if !d.Args(&m.ClosingHours) {
return d.Err("invalid closing_hours configuration")
}
case "log_blockings":
if !d.Args(&m.LogBlockings) {
return d.Err("invalid log_blockings configuration")
}
default:
return d.Errf("unknown directive: %s", d.Val())
}
}
}
return nil
}
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var m CaddySmallShield
err := m.UnmarshalCaddyfile(h.Dispenser)
return m, err
}
// func (m *CaddySmallShield) Validate() error {
// if m.whitelist == nil {
// return fmt.Errorf("no whitelist")
// }
// if m.blacklist == nil {
// return fmt.Errorf("no whitelist")
// }
// return nil
// }
// Interface guards
var (
_ caddy.Provisioner = (*CaddySmallShield)(nil)
_ caddyhttp.MiddlewareHandler = (*CaddySmallShield)(nil)
_ caddyfile.Unmarshaler = (*CaddySmallShield)(nil)
// _ caddy.Validator = (*CaddySmallShield)(nil)
)