Skip to content

Commit a21aa65

Browse files
committed
Merge remote-tracking branch 'github-pull/128/head' + minor change (PROXY protocol)
2 parents 81445fc + ddba6dc commit a21aa65

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

src/datum_conf.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ const T_DATUM_CONFIG_ITEM datum_config_options[] = {
9090
.required = false, .ptr = &datum_config.stratum_v1_max_threads, .default_int = 8 },
9191
{ .var_type = DATUM_CONF_INT, .category = "stratum", .name = "max_clients", .description = "Maximum total Stratum clients before rejecting connections",
9292
.required = false, .ptr = &datum_config.stratum_v1_max_clients, .default_int = 1024 },
93+
{ .var_type = DATUM_CONF_INT, .category = "stratum", .name = "trust_proxy", .description = "Enable support for the PROXY protocol, trusting up to the specified number of levels deep of proxies (-1 to disable entirely)",
94+
.required = false, .ptr = &datum_config.stratum_v1_trust_proxy, .default_int = -1 },
9395
{ .var_type = DATUM_CONF_INT, .category = "stratum", .name = "vardiff_min", .description = "Work difficulty floor",
9496
.required = false, .ptr = &datum_config.stratum_v1_vardiff_min, .default_int = 16384 },
9597
{ .var_type = DATUM_CONF_INT, .category = "stratum", .name = "vardiff_target_shares_min",.description = "Adjust work difficulty to target this many shares per minute",

src/datum_conf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ typedef struct {
110110
int stratum_v1_max_clients;
111111
int stratum_v1_max_threads;
112112
int stratum_v1_max_clients_per_thread;
113+
int stratum_v1_trust_proxy;
113114

114115
int stratum_v1_vardiff_min;
115116
int stratum_v1_vardiff_target_shares_min;

src/datum_sockets.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ void *datum_threadpool_thread(void *arg) {
141141
my->client_data[i].new_connection = false;
142142
my->client_data[i].in_buf = 0;
143143
my->client_data[i].out_buf = 0;
144+
my->client_data[i].proxy_line_read = 0;
144145

145146
// add to epoll for this thread
146147
my->ev.events = EPOLLIN | EPOLLONESHOT | EPOLLERR; // | EPOLLRDHUP
@@ -269,6 +270,31 @@ void *datum_threadpool_thread(void *arg) {
269270

270271
while (end_line != NULL) {
271272
*end_line = 0; // null terminate the line
273+
if (datum_config.stratum_v1_trust_proxy != -1 && my->client_data[cidx].proxy_line_read != -1) {
274+
if (strncmp(start_line, "PROXY ", 6) == 0) {
275+
my->client_data[cidx].proxy_line_read += 1;
276+
if (my->client_data[cidx].proxy_line_read <= datum_config.stratum_v1_trust_proxy) {
277+
char src_ip[DATUM_MAX_IP_LEN + 1];
278+
int matched = sscanf(start_line, "PROXY TCP4 %15s", src_ip);
279+
if (matched != 1) {
280+
matched = sscanf(start_line, "PROXY TCP6 %45s", src_ip);
281+
}
282+
if (matched == 1 && src_ip[0] != 0) {
283+
DLOG_DEBUG("New proxy IP detected: %s on TID: %d, CID: %d", src_ip, my->thread_id, my->client_data[cidx].cid);
284+
strcpy(my->client_data[cidx].rem_host, src_ip);
285+
}
286+
else {
287+
DLOG_DEBUG("PROXY line present but no valid IP found, keeping original source IP: %s on TID: %d, CID: %d", my->client_data[cidx].rem_host, my->thread_id, my->client_data[cidx].cid);
288+
}
289+
}
290+
start_line = end_line + 1;
291+
end_line = strchr(start_line, '\n');
292+
continue;
293+
} else {
294+
DLOG_DEBUG("Received non-PROXY line from client %d/%d", my->thread_id, my->client_data[cidx].cid);
295+
my->client_data[cidx].proxy_line_read = -1;
296+
}
297+
}
272298
// this function can not be NULL
273299
j = my->app->client_cmd_func(&my->client_data[cidx], start_line);
274300
if (j < 0) {

src/datum_sockets.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ typedef struct T_DATUM_CLIENT_DATA {
7979

8080
void *app_client_data;
8181

82+
int proxy_line_read;
83+
8284
T_DATUM_THREAD_DATA *datum_thread;
8385
} T_DATUM_CLIENT_DATA;
8486

0 commit comments

Comments
 (0)