mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-18 07:13:50 +01:00
Add `relay_proxy_protocol_header` configuration option for TCP routes that enables forwarding the original client IP address to upstream services via PROXY protocol v2 headers. This feature is only available for TCP routes and includes validation to prevent misuse on UDP routes. - Add RelayProxyProtocolHeader field to Route struct with JSON tag - Implement writeProxyProtocolHeader in stream package to craft v2 headers - Update TCPTCPStream to conditionally send PROXY header to upstream - Add validation ensuring feature is TCP-only - Include tests for both enabled/disabled states and incoming proxy header relay
38 lines
876 B
Go
38 lines
876 B
Go
package stream
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
|
|
"github.com/pires/go-proxyproto"
|
|
)
|
|
|
|
func writeProxyProtocolHeader(dst io.Writer, src net.Conn) error {
|
|
srcAddr, ok := src.RemoteAddr().(*net.TCPAddr)
|
|
if !ok {
|
|
return fmt.Errorf("unexpected source address type %T", src.RemoteAddr())
|
|
}
|
|
dstAddr, ok := src.LocalAddr().(*net.TCPAddr)
|
|
if !ok {
|
|
return fmt.Errorf("unexpected destination address type %T", src.LocalAddr())
|
|
}
|
|
|
|
header := &proxyproto.Header{
|
|
Version: 2,
|
|
Command: proxyproto.PROXY,
|
|
TransportProtocol: transportProtocol(srcAddr, dstAddr),
|
|
SourceAddr: srcAddr,
|
|
DestinationAddr: dstAddr,
|
|
}
|
|
_, err := header.WriteTo(dst)
|
|
return err
|
|
}
|
|
|
|
func transportProtocol(src, dst *net.TCPAddr) proxyproto.AddressFamilyAndProtocol {
|
|
if src.IP.To4() != nil && dst.IP.To4() != nil {
|
|
return proxyproto.TCPv4
|
|
}
|
|
return proxyproto.TCPv6
|
|
}
|