#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)); } void init_socks5_client(struct donar_client_ctx* app_ctx, int pos) { char target_host[255]; 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); 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}; char url[1024]; struct socks5_ctx* s5ctx = fdinfo->other; fdinfo_n.cat = &cat_n; fdinfo_n.url = url; fdinfo_n.fd = dup(fdinfo->fd); fdinfo_n.cat->name = "tcp-write"; sprintf(fdinfo_n.url, "tcp:write:127.0.0.1:%d", s5ctx->port); evt_core_add_fd (ctx, &fdinfo_n); fdinfo_n.fd = dup(fdinfo->fd); fdinfo_n.cat->name = "tcp-read"; sprintf(fdinfo_n.url, "tcp:read:127.0.0.1:%d", s5ctx->port); 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 = s5ctx->port - 7500; 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); } void donar_client(struct donar_client_ctx* ctx, char* algoname, char* onion_file, GPtrArray* exposed_ports, GPtrArray* remote_ports) { struct algo_skel algo = {0}; evt_core_init (&(ctx->evts)); init_algo(&ctx->evts, &algo, algoname); socks5_init (&ctx->evts); init_socks5_sinks(ctx); 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"); for (int i = 0; i < CLIENT_PORT_SIZE; i++) { init_socks5_client(ctx, i); } printf("--- TCP Clients Connected\n"); 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)); }