tor_multipath_voip/src/donar_client.c

106 lines
3.3 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};
struct socks5_ctx* s5ctx = fdinfo->other;
fdinfo_n.cat = &cat_n;
int ret = 0;
fdinfo_n.fd = dup(fdinfo->fd);
fdinfo_n.cat->name = "tcp-write";
ret = asprintf(&fdinfo_n.url, "tcp:write:127.0.0.1:%d", s5ctx->port);
if (ret < 0) goto failed;
evt_core_add_fd (ctx, &fdinfo_n);
fdinfo_n.fd = dup(fdinfo->fd);
fdinfo_n.cat->name = "tcp-read";
ret = asprintf(&fdinfo_n.url, "tcp:read:127.0.0.1:%d", s5ctx->port);
if (ret < 0) goto failed;
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;
int pos = 7500 - s5ctx->port;
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-19 16:09:42 +00:00
void donar_client(struct donar_client_ctx* ctx, char* algoname,
2019-02-19 18:15:37 +00:00
char* onion_file, GPtrArray* exposed_ports, GPtrArray* remote_ports) {
2019-03-19 16:09:42 +00:00
struct algo_skel algo = {0};
evt_core_init (&(ctx->evts));
2019-03-19 16:09:42 +00:00
init_algo(&ctx->evts, &algo, algoname);
2019-03-25 14:26:30 +00:00
socks5_init (&ctx->evts);
init_socks5_sinks(ctx);
2019-03-19 16:09:42 +00:00
evt_core_add_cat (&(ctx->evts), &(algo.on_tcp_co));
evt_core_add_cat (&(ctx->evts), &(algo.on_udp_read));
evt_core_add_cat (&(ctx->evts), &(algo.on_tcp_read));
evt_core_add_cat (&(ctx->evts), &(algo.on_udp_write));
evt_core_add_cat (&(ctx->evts), &(algo.on_tcp_write));
printf("--- Categories created\n");
load_onion_services (ctx, 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-02-19 18:15:37 +00:00
g_ptr_array_foreach (remote_ports, (void(*)(void*, void*))init_udp_remote, &(ctx->evts));
printf("--- Remote ports are binded locally\n");
g_ptr_array_foreach (exposed_ports, (void(*)(void*, void*))init_udp_exposed, &(ctx->evts));
printf("--- Local UDP services are exposed\n");
evt_core_loop(&(ctx->evts));
tor_os_free (&(ctx->tos));
}