tor_multipath_voip/src/donar_client.c

176 lines
5.9 KiB
C
Raw Normal View History

#include "donar_client.h"
void load_onion_services(struct donar_client_ctx* ctx, char* onion_file, int ports_count) {
tor_os_create (&(ctx->tos), onion_file, NULL, ports_count);
tor_os_read (&(ctx->tos));
}
2019-03-25 14:26:30 +00:00
void init_socks5_client(struct donar_client_ctx* app_ctx, int pos) {
char target_host[255];
2020-02-01 22:33:45 +00:00
if (strlen(app_ctx->tos.keys[0].pub) > 254) {
fprintf(stderr, "Domain name is too long\n");
exit(EXIT_FAILURE);
}
sprintf(target_host, "%s.onion", app_ctx->tos.keys[0].pub);
2019-02-15 14:45:56 +00:00
2019-03-25 14:26:30 +00:00
app_ctx->ports[pos] = 7500 + pos;
socks5_create_dns_client (&app_ctx->evts, "127.0.0.1", "9050", target_host, app_ctx->ports[pos]);
}
int on_socks5_success(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
struct evt_core_fdinfo fdinfo_n = {0};
struct evt_core_cat cat_n = {0};
2019-03-25 16:20:47 +00:00
char url[1024];
2019-03-25 14:26:30 +00:00
struct socks5_ctx* s5ctx = fdinfo->other;
fdinfo_n.cat = &cat_n;
2019-03-25 16:20:47 +00:00
fdinfo_n.url = url;
2019-03-25 14:26:30 +00:00
fdinfo_n.fd = dup(fdinfo->fd);
fdinfo_n.cat->name = "tcp-write";
2019-03-25 16:20:47 +00:00
sprintf(fdinfo_n.url, "tcp:write:127.0.0.1:%d", s5ctx->port);
2019-03-25 14:26:30 +00:00
evt_core_add_fd (ctx, &fdinfo_n);
fdinfo_n.fd = dup(fdinfo->fd);
fdinfo_n.cat->name = "tcp-read";
2019-03-25 16:20:47 +00:00
sprintf(fdinfo_n.url, "tcp:read:127.0.0.1:%d", s5ctx->port);
2019-03-25 14:26:30 +00:00
evt_core_add_fd (ctx, &fdinfo_n);
evt_core_rm_fd (ctx, fdinfo->fd);
return 1;
failed:
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
2020-02-01 19:21:41 +00:00
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);
}
2019-03-25 14:26:30 +00:00
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;
2020-02-01 19:21:41 +00:00
int64_t pos = 0;
pos = s5ctx->port - 7500;
int64_t to_wait_sec = 30 + pos*3;
2019-03-25 14:26:30 +00:00
2020-02-01 19:21:41 +00:00
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);
2019-03-25 14:26:30 +00:00
evt_core_rm_fd (ctx, fdinfo->fd);
2020-02-01 19:21:41 +00:00
//init_socks5_client (app_ctx, pos);
2019-03-25 14:26:30 +00:00
return 1;
}
void init_socks5_sinks(struct donar_client_ctx* app_ctx) {
struct evt_core_cat template = { 0 };
template.cb = on_socks5_success;
template.name = "socks5-success";
template.flags = EPOLLET;
evt_core_add_cat(&app_ctx->evts, &template);
template.cb = on_socks5_failed;
template.app_ctx = app_ctx;
template.name = "socks5-failed";
template.flags = EPOLLET;
evt_core_add_cat(&app_ctx->evts, &template);
2019-02-15 14:45:56 +00:00
}
2020-01-20 15:35:52 +00:00
int donar_client_stream_repair(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
2020-01-23 09:48:36 +00:00
fprintf(stdout, "[%s][donar-client] %s broke\n", current_human_datetime (), fdinfo->url);
2020-01-20 15:35:52 +00:00
struct evt_core_fdinfo* fdtarget = NULL;
int port = url_get_port_int (fdinfo->url);
2020-02-01 19:21:41 +00:00
int64_t pos = 0, removed = 0;
pos = port - 7500;
2020-01-20 15:35:52 +00:00
char buffer[256];
sprintf(buffer, "tcp:read:127.0.0.1:%d", port);
fdtarget = evt_core_get_from_url (ctx, buffer);
if (fdtarget != NULL) {
evt_core_rm_fd(ctx, fdtarget->fd);
removed++;
}
sprintf(buffer, "tcp:write:127.0.0.1:%d", port);
fdtarget = evt_core_get_from_url (ctx, buffer);
if (fdtarget != NULL) {
evt_core_rm_fd(ctx, fdtarget->fd);
removed++;
}
if (removed == 2) {
2020-02-01 19:21:41 +00:00
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);
2020-01-23 19:04:33 +00:00
return 1;
2020-01-20 15:35:52 +00:00
} else if (removed == 0) {
2020-01-23 09:48:36 +00:00
fprintf(stdout, "[%s][donar-client] Socks5 has already been retriggered for port %d\n", current_human_datetime (), port);
2020-01-23 19:04:33 +00:00
return 1;
2020-01-20 15:35:52 +00:00
} else {
2020-01-31 21:49:54 +00:00
fprintf(stderr, "[%s][donar-client] We only removed 1 link and not 2 for port %d\n", current_human_datetime (), port);
//exit(EXIT_FAILURE);
2020-01-20 15:35:52 +00:00
}
}
2019-03-28 10:47:14 +00:00
void donar_client(struct donar_client_ctx* ctx, struct donar_params* dp) {
struct algo_params ap = {
.is_waiting_bootstrap = dp->is_waiting_bootstrap,
2019-09-06 13:28:42 +00:00
.algo_specific_params = dp->algo_specific_params,
2019-05-09 13:33:27 +00:00
.algo_name = dp->algo,
.links = dp->links,
.fresh_data = dp->fresh_data,
2019-05-27 15:32:00 +00:00
.redundant_data = dp->redundant_data,
2020-01-20 15:35:52 +00:00
.capture_file = dp->capture_file,
.sr = donar_client_stream_repair
};
2019-03-19 16:09:42 +00:00
2019-04-01 12:16:41 +00:00
evt_core_init (&(ctx->evts), dp->verbose);
2019-05-09 09:24:05 +00:00
signal_init(&ctx->evts);
printf("--- Signal initialized\n");
2019-04-24 14:23:41 +00:00
algo_main_init(&ctx->evts, &ap);
printf("--- Algorithm initialized\n");
2019-03-25 14:26:30 +00:00
socks5_init (&ctx->evts);
init_socks5_sinks(ctx);
2019-04-24 14:23:41 +00:00
printf("--- Socks5 connection process started\n");
2019-09-17 13:12:37 +00:00
load_onion_services (ctx, dp->onion_file, dp->links);
printf("--- Onion services loaded\n");
2019-02-14 14:40:05 +00:00
2020-02-01 19:21:41 +00:00
init_timer(&ctx->evts);
printf("--- Inited Timer\n");
2020-02-01 21:14:34 +00:00
for (int64_t i = 0; i < dp->links; i++) {
int64_t to_wait_sec = 10 + i * 3;
fprintf(stdout, "[%s][donar-client] Triggering socks5 in %ld seconds for port %d\n", current_human_datetime (), to_wait_sec, i+7500);
set_timeout(&ctx->evts, 1000 * to_wait_sec, (void*) i, reinit_socks5);
}
printf("--- TCP Clients Connected\n");
2019-03-28 10:47:14 +00:00
g_ptr_array_foreach (dp->remote_ports, (void(*)(void*, void*))init_udp_remote, &(ctx->evts));
2019-02-19 18:15:37 +00:00
printf("--- Remote ports are binded locally\n");
2019-06-21 14:50:49 +00:00
for (int i = 0; i < dp->exposed_ports->len; i++) {
init_udp_exposed(dp->bound_ip, g_ptr_array_index (dp->exposed_ports, i), &(ctx->evts));
}
2019-06-21 15:05:13 +00:00
printf("--- Local UDP services (on %s) are exposed\n", dp->bound_ip);
evt_core_loop(&(ctx->evts));
2020-02-01 19:21:41 +00:00
//stop_timer(&(ctx->evts));
tor_os_free (&(ctx->tos));
}