tor_multipath_voip/src/donar_client.c

109 lines
3.2 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];
2019-02-15 14:45:56 +00:00
if (strlen(app_ctx->tos.keys[pos].pub) > 254) {
fprintf(stderr, "Domain name is too long\n");
exit(EXIT_FAILURE);
}
sprintf(target_host, "%s.onion", app_ctx->tos.keys[pos].pub);
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);
}
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;
2019-03-27 10:21:18 +00:00
int pos = s5ctx->port - 7500;
2019-03-25 14:26:30 +00:00
evt_core_rm_fd (ctx, fdinfo->fd);
init_socks5_client (app_ctx, pos);
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
}
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-04-24 14:23:41 +00:00
.is_healing = dp->is_healing,
.algo_name = dp->algo
};
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-03-28 10:47:14 +00:00
load_onion_services (ctx, dp->onion_file, CLIENT_PORT_SIZE);
printf("--- Onion services loaded\n");
2019-02-14 14:40:05 +00:00
2019-02-15 14:45:56 +00:00
for (int i = 0; i < CLIENT_PORT_SIZE; i++) {
2019-03-25 14:26:30 +00:00
init_socks5_client(ctx, i);
2019-02-15 14:45:56 +00:00
}
2019-02-12 20:45:15 +00:00
printf("--- TCP Clients Connected\n");
2019-02-14 14:40:05 +00:00
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-03-28 10:47:14 +00:00
g_ptr_array_foreach (dp->exposed_ports, (void(*)(void*, void*))init_udp_exposed, &(ctx->evts));
2019-02-19 18:15:37 +00:00
printf("--- Local UDP services are exposed\n");
evt_core_loop(&(ctx->evts));
tor_os_free (&(ctx->tos));
}