Improve retry mechanism

This commit is contained in:
Quentin 2020-02-01 20:21:41 +01:00
parent e1300791b8
commit f94427ecca
2 changed files with 31 additions and 13 deletions

View file

@ -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));
}

View file

@ -6,6 +6,7 @@
#include "socks5.h"
#include "proxy.h"
#include "donar_init.h"
#include "timer.h"
#define CLIENT_PORT_SIZE 64