@marcel91200 / caddy Mirror · read-only / commits / d05d715a
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 13:15 UTC

reverseproxy: HTTP transport: fix PROXY protocol initialization (#6301)

Mohammed Al Sahaf committed May 7, 2024 at 05:02 UTC d05d715a006322e6d512f308b4f9543cdf013187
1 file changed +20 -14
modules/caddyhttp/reverseproxy/httptransport.go
+20 -14
@@ -225,41 +225,47 @@ func (h *HTTPTransport) NewTransport(caddyCtx caddy.Context) (*http.Transport, e
225 if !ok {
226 return nil, fmt.Errorf("failed to get proxy protocol info from context")
227 }
228 - header := proxyproto.Header{
229 - SourceAddr: &net.TCPAddr{
230 - IP: proxyProtocolInfo.AddrPort.Addr().AsSlice(),
231 - Port: int(proxyProtocolInfo.AddrPort.Port()),
232 - Zone: proxyProtocolInfo.AddrPort.Addr().Zone(),
233 - },
228 + var proxyv byte
229 + switch h.ProxyProtocol {
230 + case "v1":
231 + proxyv = 1
232 + case "v2":
233 + proxyv = 2
234 + default:
235 + return nil, fmt.Errorf("unexpected proxy protocol version")
236 }
237 +
238 // The src and dst have to be of the same address family. As we don't know the original
239 // dst address (it's kind of impossible to know) and this address is generally of very
240 // little interest, we just set it to all zeros.
241 + var destAddr net.Addr
242 switch {
243 case proxyProtocolInfo.AddrPort.Addr().Is4():
240 - header.TransportProtocol = proxyproto.TCPv4
241 - header.DestinationAddr = &net.TCPAddr{
244 + destAddr = &net.TCPAddr{
245 IP: net.IPv4zero,
246 }
247 case proxyProtocolInfo.AddrPort.Addr().Is6():
245 - header.TransportProtocol = proxyproto.TCPv6
246 - header.DestinationAddr = &net.TCPAddr{
248 + destAddr = &net.TCPAddr{
249 IP: net.IPv6zero,
250 }
251 default:
252 return nil, fmt.Errorf("unexpected remote addr type in proxy protocol info")
253 }
254 + sourceAddr := &net.TCPAddr{
255 + IP: proxyProtocolInfo.AddrPort.Addr().AsSlice(),
256 + Port: int(proxyProtocolInfo.AddrPort.Port()),
257 + Zone: proxyProtocolInfo.AddrPort.Addr().Zone(),
258 + }
259 + header := proxyproto.HeaderProxyFromAddrs(proxyv, sourceAddr, destAddr)
260
261 + // retain the log message structure
262 switch h.ProxyProtocol {
263 case "v1":
255 - header.Version = 1
264 caddyCtx.Logger().Debug("sending proxy protocol header v1", zap.Any("header", header))
265 case "v2":
258 - header.Version = 2
266 caddyCtx.Logger().Debug("sending proxy protocol header v2", zap.Any("header", header))
260 - default:
261 - return nil, fmt.Errorf("unexpected proxy protocol version")
267 }
268 +
269 _, err = header.WriteTo(conn)
270 if err != nil {
271 // identify this error as one that occurred during