From 374d643acd505d83028d82a2accce1cc11a07ebc Mon Sep 17 00:00:00 2001
From: cbdev <cb@cbcdn.com>
Date: Fri, 28 Mar 2025 16:03:48 +0100
Subject: Implement configurable send delay

---
 slowloris.c | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

(limited to 'slowloris.c')

diff --git a/slowloris.c b/slowloris.c
index 1bb5337..846afde 100644
--- a/slowloris.c
+++ b/slowloris.c
@@ -3,6 +3,24 @@
 #include <dlfcn.h>
 #include <netinet/tcp.h>
 #include <netinet/in.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+static int silent_mode = 0;
+static unsigned send_delay = 0;
+
+char* xgetenv(const char* name){
+	char* rv = getenv(name);
+	return rv ? rv : "";
+}
+
+__attribute__ ((constructor)) void slowloris_init(){
+	silent_mode = strtoul(xgetenv("SLOWLORIS_SILENT"), NULL, 10);
+	send_delay = strtoul(xgetenv("SLOWLORIS_DELAY"), NULL, 10);
+	if(!silent_mode){
+		printf("preload-slowloris active\nDelay: %d\n", send_delay);
+	}
+}
 
 int socket(int domain, int type, int protocol){
 	static int (*real_socket) (int, int, int) = NULL;
@@ -14,7 +32,11 @@ int socket(int domain, int type, int protocol){
 	int enable = 1;
 
 	if(domain == AF_INET && type == SOCK_STREAM){
-		setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable));
+		if(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable)) < 0){
+			if(!silent_mode){
+				perror("setsockopt/tcp_nodelay");
+			}
+		}
 	}
 	return fd;
 }
@@ -33,6 +55,9 @@ ssize_t send(int sockfd, const void* buf, size_t len, int flags){
 			return -1;
 		}
 		rv += sent;
+		if(send_delay){
+			usleep(send_delay);
+		}
 	}
 	return rv;
 }
-- 
cgit v1.2.3