发送proxy protocol报文
V1
echo -e "PROXY TCP4 192.0.2.1 203.0.113.1 12345 80\r\nGET / HTTP/1.0\r\n\r\n" |nc 10.0.2.15 80
0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 ..............E. 0010 00 61 3b 2d 40 00 40 06 01 68 7f 00 00 01 7f 00 .a;-@.@..h...... 0020 00 01 df 72 23 82 c7 b1 df 22 ed 8b b3 6f 80 18 ...r#...."...o.. 0030 01 56 fe 55 00 00 01 01 08 0a 00 e8 e4 0b 00 e8 .V.U............ 0040 e4 0b 50 52 4f 58 59 20 54 43 50 34 20 31 39 32 ..PROXY TCP4 192 0050 2e 31 36 38 2e 31 2e 31 30 30 20 31 30 2e 30 2e .168.1.100 10.0. 0060 32 2e 31 35 20 31 32 33 34 35 20 39 30 0d 0a 2.15 12345 90..
V2
4 0.000085835 10.0.2.15 -> 10.0.2.15 TCP 94 32952 > 90 [PSH, ACK] Seq=1 Ack=1 Win=43776 Len=28 TSval=15402984 TSecr=15402984 0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 ..............E. 0010 00 50 97 f6 40 00 40 06 8a 94 0a 00 02 0f 0a 00 .P..@.@......... 0020 02 0f 80 b4 00 5a eb ea 39 24 a8 ab 53 88 80 18 .....Z..9$..S... 0030 01 56 18 60 00 00 01 01 08 0a 00 ea ae d7 00 ea .V.`............ 0040 ae d7 0d 0a 0d 0a 00 0d 0a 51 55 49 54 0a 21 11 .........QUIT.!. 0050 00 0c c0 a8 01 64 c0 a8 02 01 30 39 1f 40 .....d....09.@
[root@T19 nginx-1.18.0]# cat ppv2.py #! /bin/python import socket import struct def send_proxy_protocol_v2(server_ip, server_port, source_ip, dest_ip, source_port, dest_port): # Proxy Protocol v2 signature signature = b'\r\n\r\n\x00\r\nQUIT\n' # Version and command (0x21 means version 2 and PROXY command) version_command = b'\x21' # Address family and protocol (0x11 means AF_INET and STREAM) fam_protocol = b'\x11' # Length of the address block (12 bytes for IPv4) length = struct.pack('!H', 12) # Source and destination addresses and ports src_addr = socket.inet_aton(source_ip) dst_addr = socket.inet_aton(dest_ip) src_port = struct.pack('!H', source_port) dst_port = struct.pack('!H', dest_port) # Construct the header header = signature + version_command + fam_protocol + length + src_addr + dst_addr + src_port + dst_port # Create a socket and connect to the server with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((server_ip, server_port)) # Send the Proxy Protocol v2 header s.sendall(header) # Optionally, send additional data if needed # s.sendall(b'Additional data') # Close the connection s.close() # Example usage server_ip = '10.0.2.15' server_port = 90 source_ip = '192.168.1.100' dest_ip = '192.168.2.1' source_port = 12345 dest_port = 8000 send_proxy_protocol_v2(server_ip, server_port, source_ip, dest_ip, source_port, dest_port)