Prepend sslproxy line using evbuffer_add_printf() in non-debug mode for non-http protos

This prevents unnecessary malloc and memmove calls in non-debug mode.
This change is for correctness not for speed, because it improves
conn handling only of the first packet and for non-http protos.
pull/48/head
Soner Tari 3 years ago
parent 74cf3800f2
commit 45b34678de

@ -175,28 +175,10 @@ protoautossl_bev_readcb_src(struct bufferevent *bev, pxy_conn_ctx_t *ctx)
// @todo Validate proto?
if (ctx->spec->opts->divert && !ctx->sent_sslproxy_header) {
size_t packet_size = evbuffer_get_length(inbuf);
// +2 for \r\n
unsigned char *packet = pxy_malloc_packet(packet_size + ctx->sslproxy_header_len + 2, ctx);
if (!packet) {
return;
}
evbuffer_remove(inbuf, packet, packet_size);
log_finest_va("ORIG packet, size=%zu:\n%.*s", packet_size, (int)packet_size, packet);
pxy_insert_sslproxy_header(ctx, packet, &packet_size);
evbuffer_add(outbuf, packet, packet_size);
log_finest_va("NEW packet, size=%zu:\n%.*s", packet_size, (int)packet_size, packet);
free(packet);
}
else {
evbuffer_add_buffer(outbuf, inbuf);
if (pxy_try_prepend_sslproxy_header(ctx, inbuf, outbuf) != 0) {
return;
}
pxy_try_set_watermark(bev, ctx, ctx->dst.bev);
}

@ -289,28 +289,10 @@ prototcp_bev_readcb_src(struct bufferevent *bev, pxy_conn_ctx_t *ctx)
return;
}
if (ctx->spec->opts->divert && !ctx->sent_sslproxy_header) {
size_t packet_size = evbuffer_get_length(inbuf);
// +2 for \r\n
unsigned char *packet = pxy_malloc_packet(packet_size + ctx->sslproxy_header_len + 2, ctx);
if (!packet) {
return;
}
evbuffer_remove(inbuf, packet, packet_size);
log_finest_va("ORIG packet, size=%zu:\n%.*s", packet_size, (int)packet_size, packet);
pxy_insert_sslproxy_header(ctx, packet, &packet_size);
evbuffer_add(outbuf, packet, packet_size);
log_finest_va("NEW packet, size=%zu:\n%.*s", packet_size, (int)packet_size, packet);
free(packet);
}
else {
evbuffer_add_buffer(outbuf, inbuf);
if (pxy_try_prepend_sslproxy_header(ctx, inbuf, outbuf) != 0) {
return;
}
pxy_try_set_watermark(bev, ctx, ctx->dst.bev);
}

@ -858,7 +858,8 @@ pxy_discard_inbuf(struct bufferevent *bev)
evbuffer_drain(inbuf, inbuf_size);
}
void
#ifdef DEBUG_PROXY
static void
pxy_insert_sslproxy_header(pxy_conn_ctx_t *ctx, unsigned char *packet, size_t *packet_size)
{
log_finer("ENTER");
@ -870,6 +871,44 @@ pxy_insert_sslproxy_header(pxy_conn_ctx_t *ctx, unsigned char *packet, size_t *p
*packet_size += ctx->sslproxy_header_len + 2;
ctx->sent_sslproxy_header = 1;
}
#endif /* DEBUG_PROXY */
int
pxy_try_prepend_sslproxy_header(pxy_conn_ctx_t *ctx, struct evbuffer *inbuf, struct evbuffer *outbuf)
{
log_finer("ENTER");
if (ctx->spec->opts->divert && !ctx->sent_sslproxy_header) {
#ifdef DEBUG_PROXY
size_t packet_size = evbuffer_get_length(inbuf);
// +2 for \r\n
unsigned char *packet = pxy_malloc_packet(packet_size + ctx->sslproxy_header_len + 2, ctx);
if (!packet) {
return -1;
}
evbuffer_remove(inbuf, packet, packet_size);
log_finest_va("ORIG packet, size=%zu:\n%.*s", packet_size, (int)packet_size, packet);
pxy_insert_sslproxy_header(ctx, packet, &packet_size);
evbuffer_add(outbuf, packet, packet_size);
log_finest_va("NEW packet, size=%zu:\n%.*s", packet_size, (int)packet_size, packet);
free(packet);
}
else {
evbuffer_add_buffer(outbuf, inbuf);
}
#else /* DEBUG_PROXY */
evbuffer_add_printf(outbuf, "%s\r\n", ctx->sslproxy_header);
ctx->sent_sslproxy_header = 1;
}
evbuffer_add_buffer(outbuf, inbuf);
#endif /* !DEBUG_PROXY */
return 0;
}
void
pxy_try_remove_sslproxy_header(pxy_conn_child_ctx_t *ctx, unsigned char *packet, size_t *packet_size)

@ -378,7 +378,7 @@ unsigned char *pxy_malloc_packet(size_t, pxy_conn_ctx_t *) MALLOC NONNULL(2) WUN
int pxy_set_dstaddr(pxy_conn_ctx_t *) NONNULL(1);
void pxy_insert_sslproxy_header(pxy_conn_ctx_t *, unsigned char *, size_t *) NONNULL(1,2,3);
int pxy_try_prepend_sslproxy_header(pxy_conn_ctx_t *ctx, struct evbuffer *, struct evbuffer *) NONNULL(1,2,3);
void pxy_try_remove_sslproxy_header(pxy_conn_child_ctx_t *, unsigned char *, size_t *) NONNULL(1,2,3);
void pxy_try_set_watermark(struct bufferevent *, pxy_conn_ctx_t *, struct bufferevent *) NONNULL(1,2,3);

Loading…
Cancel
Save