Skip to content

Commit

Permalink
use netip package & trust unix sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammed90 committed Nov 3, 2023
1 parent 8facd51 commit 3a63b42
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions modules/caddyhttp/proxyprotocol/listenerwrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package proxyprotocol

import (
"net"
"net/netip"
"time"

goproxy "github.com/pires/go-proxyproto"
Expand All @@ -37,12 +38,12 @@ type ListenerWrapper struct {
// Allow is an optional list of CIDR ranges to
// allow/require PROXY headers from.
Allow []string `json:"allow,omitempty"`
allow []*net.IPNet
allow []netip.Prefix

// Denby is an optional list of CIDR ranges to
// deny PROXY headers from.
Deny []string `json:"deny,omitempty"`
deny []*net.IPNet
deny []netip.Prefix

// Accepted values are: ignore, use, reject, require, skip
// default: ignore
Expand All @@ -55,27 +56,32 @@ type ListenerWrapper struct {
// Provision sets up the listener wrapper.
func (pp *ListenerWrapper) Provision(ctx caddy.Context) error {
for _, cidr := range pp.Allow {
_, ipnet, err := net.ParseCIDR(cidr)
ipnet, err := netip.ParsePrefix(cidr)
if err != nil {
return err
}
pp.allow = append(pp.allow, ipnet)
}
for _, cidr := range pp.Deny {
_, ipnet, err := net.ParseCIDR(cidr)
ipnet, err := netip.ParsePrefix(cidr)
if err != nil {
return err
}
pp.deny = append(pp.deny, ipnet)
}
pp.policy = func(upstream net.Addr) (goproxy.Policy, error) {
// trust unix sockets
if network := upstream.Network(); caddy.IsUnixNetwork(network) {
return goproxy.USE, nil
}
ret := pp.FallbackPolicy
host, _, err := net.SplitHostPort(upstream.String())
if err != nil {
return goproxy.REJECT, err
}
ip := net.ParseIP(host)
if ip == nil {

ip, err := netip.ParseAddr(host)
if err != nil {
return goproxy.REJECT, err
}
for _, ipnet := range pp.deny {
Expand Down

0 comments on commit 3a63b42

Please sign in to comment.