Skip to content

Commit 636d5cb

Browse files
committed
Add incoming timeout option (not documented yet)
1 parent 83add3d commit 636d5cb

3 files changed

Lines changed: 31 additions & 8 deletions

File tree

config.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ static const mini_argp_opt options[] = {
2525
{ "static-bindings", 'b', 1 },
2626
{ "max-clients" , 'm', 1 },
2727
{ "idle-timeout", 'l', 1 },
28+
{ "in-timeout", 'n', 1 },
2829
{ "max-dummy", 'd', 1 },
2930
{ "fwmark", 'f', 1 },
3031
{ "verbose", 'v', 1 },
@@ -64,6 +65,7 @@ static void show_usage(void)
6465
"Additional options:\n"
6566
" -m, --max-clients=<number> Maximum number of clients (default: 1024)\n"
6667
" -l, --idle-timeout=<sec> Idle timeout in seconds (default: 300)\n"
68+
" -n, --in-timeout=<sec> Incoming timeout in seconds (default: 0 - disabled)\n"
6769
" -d, --max-dummy=<bytes> Maximum length of dummy bytes for data packets\n"
6870
" (default: 4)\n");
6971
}
@@ -83,6 +85,7 @@ static void reset_config(obfuscator_config_t *config)
8385
memset(config, 0, sizeof(*config));
8486
config->max_clients = MAX_CLIENTS_DEFAULT;
8587
config->idle_timeout = IDLE_TIMEOUT_DEFAULT;
88+
config->in_timeout = IN_TIMEOUT_DEFAULT;
8689
config->max_dummy_length_data = MAX_DUMMY_LENGTH_DATA_DEFAULT;
8790
verbose = LL_DEFAULT;
8891
}
@@ -277,6 +280,18 @@ static int parse_opt(const char *lname, char sname, const char *val, void *ctx)
277280
}
278281
config->idle_timeout *= 1000; // Convert to milliseconds
279282
break;
283+
case 'n':
284+
if (!is_integer(val)) {
285+
log(LL_ERROR, "Invalid incoming timeout: %s (must be an integer)", val);
286+
exit(EXIT_FAILURE);
287+
}
288+
config->in_timeout = atol(val);
289+
if (config->in_timeout <= 0) {
290+
log(LL_ERROR, "Invalid incoming timeout: %s (must be greater than 0)", val);
291+
exit(EXIT_FAILURE);
292+
}
293+
config->in_timeout *= 1000; // Convert to milliseconds
294+
break;
280295
case 'd':
281296
if (!is_integer(val)) {
282297
log(LL_ERROR, "Invalid maximum dummy length for data packets: %s (must be an integer)", val);

wg-obfuscator.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ int main(int argc, char *argv[]) {
625625
continue;
626626
}
627627
client_entry->last_activity_time = now;
628+
client_entry->last_incoming_time = 0;
628629
client_entry->masking_handler = masking_handler;
629630
}
630631
if (!obfuscated) {
@@ -863,6 +864,7 @@ int main(int argc, char *argv[]) {
863864
continue;
864865
}
865866
client_entry->last_activity_time = now;
867+
client_entry->last_incoming_time = now;
866868
} // if (event->data.fd != listen_sock)
867869
} // for (int e = 0; e < events_n; e++)
868870

@@ -871,15 +873,18 @@ int main(int argc, char *argv[]) {
871873
// Iterate over all client entries
872874
HASH_ITER(hh, conn_table, current_entry, tmp) {
873875
// Check if the entry is idle for too long
874-
if (
875-
(
876-
(now - current_entry->last_activity_time >= config.idle_timeout)
877-
|| (!current_entry->handshaked && (now - current_entry->last_activity_time >= HANDSHAKE_TIMEOUT))
878-
) && !current_entry->is_static // Do not remove static entries
879-
) {
876+
uint8_t idle = now - current_entry->last_activity_time >= config.idle_timeout;
877+
uint8_t incoming_timeout = config.in_timeout > 0 && now - current_entry->last_incoming_time >= config.in_timeout;
878+
uint8_t handshake_timeout = !current_entry->handshaked && now - current_entry->last_activity_time >= HANDSHAKE_TIMEOUT;
879+
if ((idle || incoming_timeout || handshake_timeout) && !current_entry->is_static) { // Do not remove static entries
880880
// Remove old entry
881-
log(current_entry->handshaked ? LL_INFO : LL_DEBUG, "Removing idle client %s:%d (handshaked=%s)", inet_ntoa(current_entry->client_addr.sin_addr), ntohs(current_entry->client_addr.sin_port),
882-
current_entry->handshaked ? "yes" : "no");
881+
if (idle) {
882+
log(LL_INFO, "Removing idle client %s:%d", inet_ntoa(current_entry->client_addr.sin_addr), ntohs(current_entry->client_addr.sin_port));
883+
} else if (incoming_timeout) {
884+
log(LL_INFO, "Removing client %s:%d due to incoming timeout", inet_ntoa(current_entry->client_addr.sin_addr), ntohs(current_entry->client_addr.sin_port));
885+
} else if (handshake_timeout) {
886+
log(LL_INFO, "Removing client %s:%d due to handshake timeout", inet_ntoa(current_entry->client_addr.sin_addr), ntohs(current_entry->client_addr.sin_port));
887+
}
883888
#ifdef USE_EPOLL
884889
epoll_ctl(epfd, EPOLL_CTL_DEL, current_entry->server_sock, NULL);
885890
#endif

wg-obfuscator.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#define MAX_CLIENTS_DEFAULT 1024 // maximum number of clients
3636
#define IDLE_TIMEOUT_DEFAULT 300000 // in milliseconds
37+
#define IN_TIMEOUT_DEFAULT 0 // in milliseconds
3738
#define MAX_DUMMY_LENGTH_DATA_DEFAULT 4 // maximum length of dummy data for data packets
3839

3940
// Default instance name
@@ -79,6 +80,7 @@ typedef struct {
7980
char static_bindings[10 * 1024]; // Static bindings as a string
8081
int max_clients; // Maximum number of clients
8182
long idle_timeout; // Idle timeout in milliseconds
83+
long in_timeout; // Incoming timeout in milliseconds
8284
int max_dummy_length_data; // Maximum length of dummy data for data packets
8385
uint32_t fwmark; // Firewall mark
8486
masking_handler_t *masking_handler; // Masking handler to use
@@ -96,6 +98,7 @@ typedef struct {
9698
struct sockaddr_in client_addr; // client address and port (key for the hash table)
9799
struct sockaddr_in our_addr; // our address and port on the server connection
98100
long last_activity_time; // last time we received data from/to this client
101+
long last_incoming_time; // last time we received data from the remote server
99102
long last_handshake_request_time; // last time we received a handshake request from/to this client
100103
long last_handshake_time; // last time we received a handshake response from/to this client
101104
long last_masking_timer_time; // last time we called the masking timer handler for this client

0 commit comments

Comments
 (0)