penguin/tinyproxy

An L4 proxy designed to act as a tiny transparent shim

commit 83de524f29b8ef0a0cb28893b64ef44f00941bc7

author斟酌 鵬兄 <tgckpg@gmail.com>
date2026-05-25T20:48:39Z
subjectBetter logging output
commit 83de524f29b8ef0a0cb28893b64ef44f00941bc7
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date:   2026-05-25T20:48:39Z

    Better logging output
---
 .gitignore  |  1 +
 Makefile    |  1 +
 file_conf.c |  4 +++-
 route.c     | 23 +++++++++++++++++++++++
 route.h     |  4 ++++
 tcp_route.c | 14 +++++++++++---
 6 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/.gitignore b/.gitignore
index 280e3f0..95d0a5f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,6 +2,7 @@ bin/
 build/
 libevent2/
 *.log
+*.swp
 
 # Prerequisites
 *.d
diff --git a/Makefile b/Makefile
index ce78eaf..61ca864 100644
--- a/Makefile
+++ b/Makefile
@@ -9,6 +9,7 @@ PROJECT_ROOT := $(CURDIR)
 BIN_DIR := $(CURDIR)/bin
 
 SRC := proxy_proto_v2.c \
+	   route.c \
 	   tcp_route.c \
        file_conf.c \
        tinyproxy.c
diff --git a/file_conf.c b/file_conf.c
index a1f1ac3..0d12b82 100644
--- a/file_conf.c
+++ b/file_conf.c
@@ -192,13 +192,15 @@ int load_routes_from_file(const char *path, struct route **routes_out, size_t *c
 			cap = new_cap;
 		}
 
-		if (parse_route_line(line, &routes[count]) != 0) {
+		struct route *r = &routes[count];
+		if (parse_route_line(line, r) != 0) {
 			fprintf(stderr, "%s:%u: invalid route config\n", path, line_no);
 			fclose(fp);
 			free(routes);
 			return -EINVAL;
 		}
 
+		r->line_no = line_no;
 		count++;
 	}
 
diff --git a/route.c b/route.c
new file mode 100644
index 0000000..61e4238
--- /dev/null
+++ b/route.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include "route.h"
+
+void route_options_str(const struct route *r, char *buf, size_t buflen)
+{
+	size_t n = 0;
+
+	buf[0] = '\0';
+
+#define ADD_OPT(name) do { \
+		n += snprintf(buf + n, buflen - n, "%s%s", n == 0 ? "" : ", ", name); \
+		if (n >= buflen) { \
+			buf[buflen - 1] = '\0'; \
+			return; \
+		} \
+	} while (0)
+
+	if (r->send_proxy_v2) {
+		ADD_OPT("proxyv2");
+	}
+
+#undef ADD_OPT
+}
diff --git a/route.h b/route.h
index 0dfbb0d..1c492b2 100644
--- a/route.h
+++ b/route.h
@@ -18,6 +18,8 @@ struct route {
 
 	enum proto proto;
 	bool send_proxy_v2;
+
+	unsigned int line_no;
 };
 
 struct worker {
@@ -50,4 +52,6 @@ struct listener_ctx {
 	struct evconnlistener *listener;
 };
 
+void route_options_str(const struct route *r, char *buf, size_t buflen);
+
 #endif
diff --git a/tcp_route.c b/tcp_route.c
index a57ce61..84cbdeb 100644
--- a/tcp_route.c
+++ b/tcp_route.c
@@ -12,6 +12,7 @@
 
 #include "file_conf.h"
 #include "proxy_proto_v2.h"
+#include "route.h"
 #include "tcp_route.h"
 
 #define PROXY_V2_SIG "\r\n\r\n\0\r\nQUIT\n"
@@ -265,9 +266,16 @@ int start_tcp_route(
 
 	evconnlistener_set_error_cb(ctx->listener, accept_error_cb);
 
-	fprintf(stderr, "listening on %s:%u, forwarding to %s:%u\n",
-			r->listen_host, r->listen_port,
-			r->upstream_host, r->upstream_port);
+	char opts[128];
+
+	route_options_str(r, opts, sizeof(opts));
+
+	fprintf(stderr, "line %u, listening on %s:%u, forwarding to %s:%u%s%s\n",
+		r->line_no,
+		r->listen_host, r->listen_port,
+		r->upstream_host, r->upstream_port,
+		opts[0] ? ", options: " : "", opts
+	);
 
 	*out = ctx;
 	return 0;