package proxy import ( "encoding/binary" "net" ) var v2sig = []byte("\r\n\r\n\x00\r\nQUIT\n") // proxyV2Header builds a PROXY protocol v2 header carrying the client's real // address so the backend can see it. Falls back to a LOCAL header if the // addresses aren't TCP. func proxyV2Header(src, dst net.Addr) []byte { s, sok := src.(*net.TCPAddr) d, dok := dst.(*net.TCPAddr) h := append([]byte{}, v2sig...) if !sok || !dok { return append(h, 0x20, 0x00, 0, 0) } if s4, d4 := s.IP.To4(), d.IP.To4(); s4 != nil && d4 != nil { h = append(h, 0x21, 0x11, 0, 12) h = append(h, s4...) h = append(h, d4...) } else { h = append(h, 0x21, 0x21, 0, 36) h = append(h, s.IP.To16()...) h = append(h, d.IP.To16()...) } h = binary.BigEndian.AppendUint16(h, uint16(s.Port)) return binary.BigEndian.AppendUint16(h, uint16(d.Port)) }