X-Forwarded-For handler for labstack echo and Fastly
This commit is contained in:
parent
62e28b71f1
commit
2bff6d8ef3
1
xff/fastlyxff/fastly.json
Normal file
1
xff/fastlyxff/fastly.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"addresses":["23.235.32.0/20","43.249.72.0/22","103.244.50.0/24","103.245.222.0/23","103.245.224.0/24","104.156.80.0/20","140.248.64.0/18","140.248.128.0/17","146.75.0.0/17","151.101.0.0/16","157.52.64.0/18","167.82.0.0/17","167.82.128.0/20","167.82.160.0/20","167.82.224.0/20","172.111.64.0/18","185.31.16.0/22","199.27.72.0/21","199.232.0.0/16"],"ipv6_addresses":["2a04:4e40::/32","2a04:4e42::/32"]}
|
51
xff/fastlyxff/xff.go
Normal file
51
xff/fastlyxff/xff.go
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package fastlyxff
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net"
|
||||||
|
"net/netip"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
type FastlyXFF struct {
|
||||||
|
IPv4 []string `json:"addresses"`
|
||||||
|
IPv6 []string `json:"ipv6_addresses"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type TrustedNets struct {
|
||||||
|
prefixes []netip.Prefix
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(fileName string) (*FastlyXFF, error) {
|
||||||
|
b, err := os.ReadFile(fileName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
d := FastlyXFF{}
|
||||||
|
|
||||||
|
err = json.Unmarshal(b, &d)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &d, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (xff *FastlyXFF) EchoTrustOption() ([]echo.TrustOption, error) {
|
||||||
|
ranges := []echo.TrustOption{}
|
||||||
|
|
||||||
|
for _, s := range append(xff.IPv4, xff.IPv6...) {
|
||||||
|
_, cidr, err := net.ParseCIDR(s)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
trust := echo.TrustIPRange(cidr)
|
||||||
|
ranges = append(ranges, trust)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ranges, nil
|
||||||
|
}
|
23
xff/fastlyxff/xff_test.go
Normal file
23
xff/fastlyxff/xff_test.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package fastlyxff
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestFastlyIPRanges(t *testing.T) {
|
||||||
|
|
||||||
|
fastlyxff, err := New("fastly.json")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("could not load test data: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := fastlyxff.EchoTrustOption()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("could not parse test data: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(data) < 10 {
|
||||||
|
t.Logf("only got %d prefixes, expected more", len(data))
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user