Files
urutau/internal/enforce/infra.go
T
Lucas RayanandClaude Opus 5.5 c33cca22e3
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 28s
CI / Dependency audit (push) Successful in 16s
CI / Tests (Linux, race detector) (push) Successful in 1m3s
Release / Plan (reuse the published image?) (push) Successful in 4s
Release / ci (push) Skipped
Release / Build image (amd64) (push) Skipped
Release / Build image (arm64) (push) Skipped
Release / Publish tags (push) Successful in 13s
Release / Gitea release (push) Successful in 3s
feat: 1.4.0 second firewall, egress analysis and HTML report
Optional second firewall (URUTAU_ENFORCE off/observe/on) through the host
fail2ban, outbound traffic analysis (beaconing, mining, spam, DNS bypass,
unauthorised scans) with authorised scanner workloads, host kit in
deploy/host and a printable HTML report.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 23:35:34 -03:00

79 lines
2.3 KiB
Go

package enforce
import (
"net"
"net/netip"
)
// SkipInfra is the skip reason of the fixed infrastructure allowlist.
const SkipInfra = "infrastructure"
func mustPrefixes(l ...string) []netip.Prefix {
out := make([]netip.Prefix, 0, len(l))
for _, s := range l {
out = append(out, netip.MustParsePrefix(s))
}
return out
}
// cloudflare are the published Cloudflare ranges (https://www.cloudflare.com/ips/).
// They are never banned: behind the CDN the "client" of a request is an edge
// address shared by thousands of innocent visitors.
var cloudflare = mustPrefixes(
"173.245.48.0/20", "103.21.244.0/22", "103.22.200.0/22", "103.31.4.0/22", "141.101.64.0/18",
"108.162.192.0/18", "190.93.240.0/20", "188.114.96.0/20", "197.234.240.0/22", "198.41.128.0/17",
"162.158.0.0/15", "104.16.0.0/13", "104.24.0.0/14", "172.64.0.0/13", "131.0.72.0/22",
"2400:cb00::/32", "2606:4700::/32", "2803:f800::/32", "2405:b500::/32", "2405:8100::/32",
"2a06:98c0::/29", "2c0f:f248::/32")
// publicResolvers are the well known public DNS resolvers.
var publicResolvers = func() map[netip.Addr]bool {
m := map[netip.Addr]bool{}
for _, s := range []string{"1.1.1.1", "1.0.0.1", "8.8.8.8", "8.8.4.4", "9.9.9.9", "149.112.112.112",
"208.67.222.222", "208.67.220.220", "2606:4700:4700::1111", "2606:4700:4700::1001",
"2001:4860:4860::8888", "2001:4860:4860::8844", "2620:fe::fe", "2620:fe::9",
"2620:119:35::35", "2620:119:53::53"} {
m[netip.MustParseAddr(s)] = true
}
return m
}()
// IsCloudflare reports whether a belongs to a Cloudflare range.
func IsCloudflare(a netip.Addr) bool {
a = a.Unmap()
for _, p := range cloudflare {
if p.Contains(a) {
return true
}
}
return false
}
// IsInfrastructure reports whether a is in the fixed allowlist (Cloudflare, public resolvers).
func IsInfrastructure(a netip.Addr) bool {
a = a.Unmap()
return publicResolvers[a] || IsCloudflare(a)
}
// interfaceAddrs lists the addresses of the local interfaces.
func interfaceAddrs() []netip.Addr {
var out []netip.Addr
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil
}
for _, x := range addrs {
var ip net.IP
switch v := x.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if a, ok := netip.AddrFromSlice(ip); ok {
out = append(out, a.Unmap())
}
}
return out
}