Set PROXY protocol PP2_TYPE_AUTHORITY TLV

This commit is contained in:
Simon Ser 2020-10-09 12:21:19 +02:00
parent 79e331e8c2
commit 1f16053334
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
1 changed files with 19 additions and 2 deletions

View File

@ -145,7 +145,7 @@ func (ln *Listener) handle(conn net.Conn) error {
return fmt.Errorf("can't find frontend for server name %q", tlsState.ServerName) return fmt.Errorf("can't find frontend for server name %q", tlsState.ServerName)
} }
return fe.handle(tlsConn) return fe.handle(tlsConn, &tlsState)
} }
type Frontend struct { type Frontend struct {
@ -153,7 +153,7 @@ type Frontend struct {
Backend Backend Backend Backend
} }
func (fe *Frontend) handle(downstream net.Conn) error { func (fe *Frontend) handle(downstream net.Conn, tlsState *tls.ConnectionState) error {
defer downstream.Close() defer downstream.Close()
be := &fe.Backend be := &fe.Backend
@ -165,6 +165,15 @@ func (fe *Frontend) handle(downstream net.Conn) error {
if be.Proxy { if be.Proxy {
h := proxyproto.HeaderProxyFromAddrs(2, downstream.RemoteAddr(), downstream.LocalAddr()) h := proxyproto.HeaderProxyFromAddrs(2, downstream.RemoteAddr(), downstream.LocalAddr())
var tlvs []proxyproto.TLV
if tlsState.ServerName != "" {
tlvs = append(tlvs, authorityTLV(tlsState.ServerName))
}
if err := h.SetTLVs(tlvs); err != nil {
return fmt.Errorf("failed to set PROXY protocol header TLVs: %v", err)
}
if _, err := h.WriteTo(upstream); err != nil { if _, err := h.WriteTo(upstream); err != nil {
return fmt.Errorf("failed to write PROXY protocol header: %v", err) return fmt.Errorf("failed to write PROXY protocol header: %v", err)
} }
@ -191,3 +200,11 @@ func duplexCopy(a, b io.ReadWriter) error {
}() }()
return <-done return <-done
} }
func authorityTLV(name string) proxyproto.TLV {
return proxyproto.TLV{
Type: proxyproto.PP2_TYPE_AUTHORITY,
Length: len(name),
Value: []byte(name),
}
}