package client import ( "encoding/binary" "net" ) // proxyV2Signature is the fixed 12-byte HAProxy v2 signature. var proxyV2Signature = []byte{ 0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, } // BuildProxyV2 builds a HAProxy protocol v2 PROXY header conveying the real // source (player) and destination addresses (PROTOCOL.md ยง8). func BuildProxyV2(srcIP net.IP, srcPort int, dstIP net.IP, dstPort int) []byte { s4, d4 := srcIP.To4(), dstIP.To4() out := make([]byte, 0, 16+36) out = append(out, proxyV2Signature...) out = append(out, 0x21) // version 2, PROXY command var addr []byte if s4 != nil && d4 != nil { out = append(out, 0x11) // TCP over IPv4 addr = make([]byte, 0, 12) addr = append(addr, s4...) addr = append(addr, d4...) } else { out = append(out, 0x21) // TCP over IPv6 addr = make([]byte, 0, 36) addr = append(addr, srcIP.To16()...) addr = append(addr, dstIP.To16()...) } var ports [4]byte binary.BigEndian.PutUint16(ports[0:], uint16(srcPort)) binary.BigEndian.PutUint16(ports[2:], uint16(dstPort)) addr = append(addr, ports[:]...) var lenField [2]byte binary.BigEndian.PutUint16(lenField[:], uint16(len(addr))) out = append(out, lenField[:]...) out = append(out, addr...) return out }