From f94427ecca937742d4dc224d592c63a931892721 Mon Sep 17 00:00:00 2001 From: Quentin Date: Sat, 1 Feb 2020 20:21:41 +0100 Subject: [PATCH] Improve retry mechanism --- src/donar_client.c | 43 ++++++++++++++++++++++++++++++------------- src/donar_client.h | 1 + 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/donar_client.c b/src/donar_client.c index dd57647..0de2797 100644 --- a/src/donar_client.c +++ b/src/donar_client.c @@ -44,13 +44,31 @@ failed: exit(EXIT_FAILURE); } +void reinit_socks5(struct evt_core_ctx* ctx, void* user_data) { + // @FIXME: Ugly way to get donar_client_ctx. Shame on me :/ + struct evt_core_cat* cat = evt_core_get_from_cat (ctx, "socks5-failed"); + if (cat == NULL) { + fprintf(stderr, "Unable to reconnect stream as socks5-failed cat is not available...\n"); + exit(EXIT_FAILURE); + } + struct donar_client_ctx* app_ctx = cat->app_ctx; + int64_t pos = (int64_t) user_data; // trust me... + + fprintf(stdout, "[%s][donar-client] We have waited enough, retriggering socks5 for port %ld\n", current_human_datetime (), pos+7500); + init_socks5_client (app_ctx, pos); +} + int on_socks5_failed(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { struct donar_client_ctx* app_ctx = fdinfo->cat->app_ctx; struct socks5_ctx* s5ctx = fdinfo->other; - int pos = s5ctx->port - 7500; + int64_t pos = 0; + pos = s5ctx->port - 7500; + int64_t to_wait_sec = 30 + pos*3; + fprintf(stdout, "[%s][donar-client] Retriggering socks5 in %ld seconds for port %d\n", current_human_datetime (), to_wait_sec, s5ctx->port); + set_timeout(ctx, 1000 * to_wait_sec, (void*) pos, reinit_socks5); evt_core_rm_fd (ctx, fdinfo->fd); - init_socks5_client (app_ctx, pos); + //init_socks5_client (app_ctx, pos); return 1; } @@ -70,18 +88,11 @@ void init_socks5_sinks(struct donar_client_ctx* app_ctx) { } int donar_client_stream_repair(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { - // @FIXME: Ugly way to get donar_client_ctx. Shame on me :/ fprintf(stdout, "[%s][donar-client] %s broke\n", current_human_datetime (), fdinfo->url); - struct evt_core_cat* cat = evt_core_get_from_cat (ctx, "socks5-failed"); - if (cat == NULL) { - fprintf(stderr, "Unable to reconnect stream as socks5-failed cat is not available...\n"); - exit(EXIT_FAILURE); - } - struct donar_client_ctx* app_ctx = cat->app_ctx; - struct evt_core_fdinfo* fdtarget = NULL; int port = url_get_port_int (fdinfo->url); - int pos = port - 7500, removed = 0; + int64_t pos = 0, removed = 0; + pos = port - 7500; char buffer[256]; sprintf(buffer, "tcp:read:127.0.0.1:%d", port); @@ -99,8 +110,10 @@ int donar_client_stream_repair(struct evt_core_ctx* ctx, struct evt_core_fdinfo* } if (removed == 2) { - fprintf(stdout, "[%s][donar-client] Retriggering socks5 for port %d\n", current_human_datetime (), port); - init_socks5_client (app_ctx, pos); + int64_t to_wait_sec = 10 + pos*3; + fprintf(stdout, "[%s][donar-client] Retriggering socks5 in %ld seconds for port %d\n", current_human_datetime (), to_wait_sec, port); + set_timeout(ctx, 1000 * to_wait_sec, (void*) pos, reinit_socks5); + //init_socks5_client (app_ctx, pos); return 1; } else if (removed == 0) { fprintf(stdout, "[%s][donar-client] Socks5 has already been retriggered for port %d\n", current_human_datetime (), port); @@ -143,6 +156,9 @@ void donar_client(struct donar_client_ctx* ctx, struct donar_params* dp) { } printf("--- TCP Clients Connected\n"); + init_timer(&ctx->evts); + printf("--- Inited Timer\n"); + g_ptr_array_foreach (dp->remote_ports, (void(*)(void*, void*))init_udp_remote, &(ctx->evts)); printf("--- Remote ports are binded locally\n"); @@ -153,5 +169,6 @@ void donar_client(struct donar_client_ctx* ctx, struct donar_params* dp) { evt_core_loop(&(ctx->evts)); + //stop_timer(&(ctx->evts)); tor_os_free (&(ctx->tos)); } diff --git a/src/donar_client.h b/src/donar_client.h index ad44f12..41fdfc5 100644 --- a/src/donar_client.h +++ b/src/donar_client.h @@ -6,6 +6,7 @@ #include "socks5.h" #include "proxy.h" #include "donar_init.h" +#include "timer.h" #define CLIENT_PORT_SIZE 64