In the logs from HAProxy I would like to log the content of the TCP connection.
So if a TCP connection sends the word "hello" over TCP through my HAProxy I would like the word "hello" to show up in the logs.
My haproxy-server is running Ubuntu Server 18.04 and has Docker 19.03.12 installed. I'm running the official haproxy docker image version 2.7.3.
I'm starting the container with docker-compose version 1.27.1 and this is the compose file:
services: ftt_tcp_proxy_dev: container_name: ftt_tcp_proxy_dev image: haproxy:2.7.3 ports: - "50000:50000" volumes: - /home/user1/git/ftt_tcp_proxy/dev/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro - /etc/localtime:/etc/localtime:ro - /etc/timezone:/etc/timezone:ro restart: unless-stopped My haproxy configuration looks like this:
global log stdout format raw daemon defaults mode tcp log global log-format "[%t] Connection on port %fp from ip %ci forwarded to %si:%sp with data x" timeout client 60s timeout client-fin 60s timeout connect 60s timeout server 60s timeout server-fin 60s timeout tunnel 60s frontend server1 bind :50000 default_backend server2 backend server2 server server2 10.10.150.82:50000 On server2 (Ubuntu Server 18.04) I run this command to listen on port 50000: nc -nvlkp 50000
From server1 (Ubuntu Server 18.04) I run this command to connect to server2 through HAproxy: nc haproxy01 50000
haproxy01 is a DNS record that resolves to the IP-address of the server running HAProxy docker container.
The connection is initiated and logged from HAProxy container:
[03/Apr/2023:13:29:57.751] Connection on port 50000 from ip 10.10.150.81 forwarded to 10.10.150.82:50000 with data x From server1 I can in netcat write the word "hello"
user1@server1:~$ nc haproxy01 50000 hello and it shows up in server2
user2@server2:~$ nc -nvlkp 50000 Listening on [0.0.0.0] (family 0, port 50000) Connection from 10.10.150.42 34878 received! hello But as the title explains I want the word "hello" to also show up in my HAProxy log instead of symbol x at the end. Obviously it wont work with the above configuration since I neither capture it nor log it. I just have it as an example to show my starting point.
I have tried adding tcp-request inspect-delay 5s and tcp-request content capture req.payload(0,100) len 100 in frontend section and %[capture.req.hdr(0)] in log-format so the configuration looks like this:
global log stdout format raw daemon defaults mode tcp log global log-format "[%t] Connection on port %fp from ip %ci forwarded to %si:%sp with data %[capture.req.hdr(0)]" timeout client 60s timeout client-fin 60s timeout connect 60s timeout server 60s timeout server-fin 60s timeout tunnel 60s frontend server1 bind :50000 default_backend server2 tcp-request inspect-delay 5s tcp-request content capture req.payload(0,100) len 100 backend server2 server server2 10.10.150.82:50000 But when I repeat the experiment above only a dash is logged
[03/Apr/2023:13:48:08.991] Connection on port 50000 from ip 10.10.150.81 forwarded to 10.10.150.82:50000 with data - So what can I do to log the content of the connection?
Update 1
I have successfully logged the word "hello" now by changing to this line in haproxy configuration:
tcp-request content capture req.payload(0,5) len 5
However, if I send a word shorter than 5 characters only a dash is logged. If I send a word longer than 5 characters only the first 5 characters are logged.
How can I log this without knowing the length of the content?
Update 2
I have given up. I now use tcpdump to log the content. It's possible with this command:
sudo tcpdump -i any -nn -A port 50000
Related questions 3277 From inside of a Docker container, how do I connect to the localhost of the machine? 2153 How to get a Docker container's IP address from the host 0 Unexpected HAPROXY acl behaviour tcp payload routing Related questions 3277 From inside of a Docker container, how do I connect to the localhost of the machine? 2153 How to get a Docker container's IP address from the host 0 Unexpected HAPROXY acl behaviour tcp payload routing 14 HAProxy closes long living TCP connections ignoring TCP keepalive 0 jBoss thread count increaed after upgrading haproxy from 1.5dev21 to 1.5.1 0 haproxy use_backend condition (acl) is missing the backend Load 3 more related questions Show fewer related questions
Reset to default