Skip to content

Commit

Permalink
logproto: add support for auto-detecting haproxy handshakes
Browse files Browse the repository at this point in the history
Signed-off-by: Balazs Scheidler <balazs.scheidler@axoflow.com>
  • Loading branch information
bazsi committed Feb 1, 2025
1 parent b207227 commit 75602d6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
57 changes: 57 additions & 0 deletions lib/logproto/logproto-auto-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "logproto-framed-server.h"
#include "messages.h"

#include "transport/transport-haproxy.h"

enum
{
LPAS_FAILURE,
Expand All @@ -37,6 +39,7 @@ typedef struct _LogProtoAutoServer
LogProtoServer super;

gboolean tls_detected;
gboolean haproxy_detected;
} LogProtoAutoServer;

static LogProtoServer *
Expand Down Expand Up @@ -118,6 +121,32 @@ _is_tls_client_hello(const gchar *buf, gsize buf_len)
return LPAS_SUCCESS;
}

static gint
_is_haproxy_header(const gchar *buf, gsize buf_len)
{
const gchar proxy_v1_signature[] = { 0x50, 0x52, 0x4F, 0x58, 0x59, 0x20 };
const gchar proxy_v2_signature[] = { 0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A };

if (buf_len < sizeof(proxy_v1_signature))
return LPAS_NEED_MORE_DATA;

if (memcmp(buf, proxy_v1_signature, sizeof(proxy_v1_signature)) == 0)
return LPAS_SUCCESS;

if (buf_len < sizeof(proxy_v2_signature))
return LPAS_NEED_MORE_DATA;

if (memcmp(buf, proxy_v2_signature, sizeof(proxy_v2_signature)) != 0)
return LPAS_FAILURE;

if (buf_len < sizeof(proxy_v2_signature) + 1)
return LPAS_NEED_MORE_DATA;

if ((buf[sizeof(proxy_v2_signature)] & 0xf0) != 0x20)
return LPAS_FAILURE;
return LPAS_SUCCESS;
}

static LogProtoPrepareAction
log_proto_auto_server_poll_prepare(LogProtoServer *s, GIOCondition *cond, gint *timeout G_GNUC_UNUSED)
{
Expand Down Expand Up @@ -176,6 +205,34 @@ log_proto_auto_handshake(LogProtoServer *s, gboolean *handshake_finished, LogPro
break;
}
}
if (!self->haproxy_detected)
{
switch (_is_haproxy_header(detect_buffer, rc))
{
case LPAS_NEED_MORE_DATA:
if (moved_forward)
return LPS_AGAIN;
break;
case LPAS_SUCCESS:
self->haproxy_detected = TRUE;

/* this is a haproxy header */
if (log_transport_stack_switch(&self->super.transport_stack, LOG_TRANSPORT_HAPROXY))
{
msg_debug("HAProxy header detected, switching to haproxy");
return LPS_AGAIN;
}
else
{
msg_error("HAProxy header detected, but haproxy transport is not set up");
return LPS_ERROR;
}
break;
default:
break;

}
}
*proto_replacement = _construct_detected_proto(self, detect_buffer, rc);
return LPS_SUCCESS;
}
Expand Down
7 changes: 7 additions & 0 deletions modules/afsocket/transport-mapper-inet.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ transport_mapper_inet_setup_stack(TransportMapper *s, LogTransportStack *stack)
return FALSE;
initial_transport_index = LOG_TRANSPORT_HAPROXY;
}
else
{
/* NOTE: haproxy auto-detection, HAProxy transport should use the active
* transport as it starts */
if (!_setup_haproxy_transport(self, stack, LOG_TRANSPORT_NONE, LOG_TRANSPORT_NONE))
return FALSE;
}

if (!log_transport_stack_switch(stack, initial_transport_index))
g_assert_not_reached();
Expand Down

0 comments on commit 75602d6

Please sign in to comment.