52 lines
811 B
Go
52 lines
811 B
Go
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
|
|
}
|