Debian Codegraph @

modules/caddyhttp/proxyprotocol/listenerwrapper.go

raw symbols SCIP
1 // Copyright 2015 Matthew Holt and The Caddy Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package proxyprotocol
16
17 import (
18 "net"
19 "net/netip"
20 "time"
21
22 goproxy "github.com/pires/go-proxyproto"
23
24 "github.com/caddyserver/caddy/v2"
25 )
26
27 // ListenerWrapper provides PROXY protocol support to Caddy by implementing
28 // the caddy.ListenerWrapper interface. If a connection is received via Unix
29 // socket, it's trusted. Otherwise, it's checked against the Allow/Deny lists,
30 // then it's handled by the FallbackPolicy.
31 //
32 // It must be loaded before the `tls` listener because the PROXY protocol
33 // encapsulates the TLS data.
34 //
35 // Credit goes to https://github.com/mastercactapus/caddy2-proxyprotocol for having
36 // initially implemented this as a plugin.
37 type ListenerWrapper struct {
38 // Timeout specifies an optional maximum time for
39 // the PROXY header to be received.
40 // If zero, timeout is disabled. Default is 5s.
41 Timeout caddy.Duration `json:"timeout,omitempty"`
42
43 // Allow is an optional list of CIDR ranges to
44 // allow/require PROXY headers from.
45 Allow []string `json:"allow,omitempty"`
46 allow []netip.Prefix
47
48 // Deny is an optional list of CIDR ranges to
49 // deny PROXY headers from.
50 Deny []string `json:"deny,omitempty"`
51 deny []netip.Prefix
52
53 // FallbackPolicy specifies the policy to use if the downstream
54 // IP address is not in the Allow list nor is in the Deny list.
55 //
56 // NOTE: The generated docs which describe the value of this
57 // field is wrong because of how this type unmarshals JSON in a
58 // custom way. The field expects a string, not a number.
59 //
60 // Accepted values are: IGNORE, USE, REJECT, REQUIRE, SKIP
61 //
62 // - IGNORE: address from PROXY header, but accept connection
63 //
64 // - USE: address from PROXY header
65 //
66 // - REJECT: connection when PROXY header is sent
67 // Note: even though the first read on the connection returns an error if
68 // a PROXY header is present, subsequent reads do not. It is the task of
69 // the code using the connection to handle that case properly.
70 //
71 // - REQUIRE: connection to send PROXY header, reject if not present
72 // Note: even though the first read on the connection returns an error if
73 // a PROXY header is not present, subsequent reads do not. It is the task
74 // of the code using the connection to handle that case properly.
75 //
76 // - SKIP: accepts a connection without requiring the PROXY header.
77 // Note: an example usage can be found in the SkipProxyHeaderForCIDR
78 // function.
79 //
80 // Default: IGNORE
81 //
82 // Policy definitions are here: https://pkg.go.dev/github.com/pires/[email protected]#Policy
83 FallbackPolicy Policy `json:"fallback_policy,omitempty"`
84
85 policy goproxy.ConnPolicyFunc
86 }
87
88 // Provision sets up the listener wrapper.
89 func (pp *ListenerWrapper) Provision(ctx caddy.Context) error {
90 for _, cidr := range pp.Allow {
91 ipnet, err := netip.ParsePrefix(cidr)
92 if err != nil {
93 return err
94 }
95 pp.allow = append(pp.allow, ipnet)
96 }
97 for _, cidr := range pp.Deny {
98 ipnet, err := netip.ParsePrefix(cidr)
99 if err != nil {
100 return err
101 }
102 pp.deny = append(pp.deny, ipnet)
103 }
104
105 pp.policy = func(options goproxy.ConnPolicyOptions) (goproxy.Policy, error) {
106 // trust unix sockets
107 if network := options.Upstream.Network(); caddy.IsUnixNetwork(network) || caddy.IsFdNetwork(network) {
108 return goproxy.USE, nil
109 }
110 ret := pp.FallbackPolicy
111 host, _, err := net.SplitHostPort(options.Upstream.String())
112 if err != nil {
113 return goproxy.REJECT, err
114 }
115
116 ip, err := netip.ParseAddr(host)
117 if err != nil {
118 return goproxy.REJECT, err
119 }
120 for _, ipnet := range pp.deny {
121 if ipnet.Contains(ip) {
122 return goproxy.REJECT, nil
123 }
124 }
125 for _, ipnet := range pp.allow {
126 if ipnet.Contains(ip) {
127 ret = PolicyUSE
128 break
129 }
130 }
131 return policyToGoProxyPolicy[ret], nil
132 }
133 return nil
134 }
135
136 // WrapListener adds PROXY protocol support to the listener.
137 func (pp *ListenerWrapper) WrapListener(l net.Listener) net.Listener {
138 pl := &goproxy.Listener{
139 Listener: l,
140 ReadHeaderTimeout: time.Duration(pp.Timeout),
141 }
142 pl.ConnPolicy = pp.policy
143 return pl
144 }