Skip to content

Commit 772c268

Browse files
committed
✨ adiciona módulo de firewall para Caddy com listas de bloqueio e liberação baseadas em IPs; inclui configuração via arquivo TOML e suporte básico para Caddyfile; adiciona dependências necessárias no go.mod e go.sum para suporte ao módulo
1 parent 1bd9cfb commit 772c268

3 files changed

Lines changed: 384 additions & 0 deletions

File tree

firewall.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package firewall
2+
3+
import (
4+
"net"
5+
"net/http"
6+
7+
"github.com/BurntSushi/toml"
8+
"github.com/caddyserver/caddy/v2"
9+
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
10+
)
11+
12+
func init() {
13+
caddy.RegisterModule(Firewall{})
14+
}
15+
16+
type Firewall struct {
17+
Allow []string `json:"allow,omitempty"`
18+
Block []string `json:"block,omitempty"`
19+
}
20+
21+
func (Firewall) CaddyModule() caddy.ModuleInfo {
22+
return caddy.ModuleInfo{
23+
ID: "http.handlers.firewall",
24+
New: func() caddy.Module { return new(Firewall) },
25+
}
26+
}
27+
28+
func (m *Firewall) Provision(ctx caddy.Context) error {
29+
// carrega o arquivo firewall.toml
30+
var conf struct {
31+
Allow []string `toml:"allow"`
32+
Block []string `toml:"block"`
33+
}
34+
if _, err := toml.DecodeFile("/etc/caddy/firewall.toml", &conf); err != nil {
35+
return err
36+
}
37+
m.Allow = conf.Allow
38+
m.Block = conf.Block
39+
return nil
40+
}
41+
42+
func (m Firewall) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
43+
clientIP, _, err := net.SplitHostPort(r.RemoteAddr)
44+
if err != nil {
45+
return err
46+
}
47+
48+
// Bloqueio prioritário
49+
for _, blocked := range m.Block {
50+
if blocked == clientIP {
51+
http.Error(w, "Forbidden", http.StatusForbidden)
52+
return nil
53+
}
54+
}
55+
56+
// Liberação se listado
57+
if len(m.Allow) > 0 {
58+
allowed := false
59+
for _, allowedIP := range m.Allow {
60+
if allowedIP == clientIP {
61+
allowed = true
62+
break
63+
}
64+
}
65+
if !allowed {
66+
http.Error(w, "Forbidden", http.StatusForbidden)
67+
return nil
68+
}
69+
}
70+
71+
// Se não está bloqueado, e não tem allow list, passa
72+
return next.ServeHTTP(w, r)
73+
}
74+
75+
// Para suporte a Caddyfile (opcional)
76+
func (m *Firewall) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
77+
return nil
78+
}

go.mod

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module github.com/makecodes/caddy-simple-firewall
2+
3+
go 1.24
4+
5+
toolchain go1.24.2
6+
7+
require (
8+
github.com/BurntSushi/toml v1.5.0
9+
github.com/caddyserver/caddy/v2 v2.10.0
10+
)
11+
12+
require (
13+
github.com/beorn7/perks v1.0.1 // indirect
14+
github.com/caddyserver/certmagic v0.23.0 // indirect
15+
github.com/caddyserver/zerossl v0.1.3 // indirect
16+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
17+
github.com/francoispqt/gojay v1.2.13 // indirect
18+
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
19+
github.com/google/pprof v0.0.0-20231212022811-ec68065c825e // indirect
20+
github.com/google/uuid v1.6.0 // indirect
21+
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
22+
github.com/libdns/libdns v1.0.0-beta.1 // indirect
23+
github.com/mholt/acmez/v3 v3.1.2 // indirect
24+
github.com/miekg/dns v1.1.63 // indirect
25+
github.com/onsi/ginkgo/v2 v2.13.2 // indirect
26+
github.com/prometheus/client_golang v1.19.1 // indirect
27+
github.com/prometheus/client_model v0.5.0 // indirect
28+
github.com/prometheus/common v0.48.0 // indirect
29+
github.com/prometheus/procfs v0.12.0 // indirect
30+
github.com/quic-go/qpack v0.5.1 // indirect
31+
github.com/quic-go/quic-go v0.50.1 // indirect
32+
github.com/zeebo/blake3 v0.2.4 // indirect
33+
go.uber.org/mock v0.5.0 // indirect
34+
go.uber.org/multierr v1.11.0 // indirect
35+
go.uber.org/zap v1.27.0 // indirect
36+
go.uber.org/zap/exp v0.3.0 // indirect
37+
golang.org/x/crypto v0.36.0 // indirect
38+
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
39+
golang.org/x/mod v0.24.0 // indirect
40+
golang.org/x/net v0.38.0 // indirect
41+
golang.org/x/sync v0.12.0 // indirect
42+
golang.org/x/sys v0.31.0 // indirect
43+
golang.org/x/term v0.30.0 // indirect
44+
golang.org/x/text v0.23.0 // indirect
45+
golang.org/x/time v0.11.0 // indirect
46+
golang.org/x/tools v0.31.0 // indirect
47+
google.golang.org/protobuf v1.35.1 // indirect
48+
)

0 commit comments

Comments
 (0)