2019-02-11 09:23:38 +00:00
|
|
|
#include "donar_server.h"
|
|
|
|
|
2019-09-04 14:40:02 +00:00
|
|
|
void create_onion_services(struct tor_os_str* tos, struct tor_ctl* tctl, uint16_t* ports, int ports_count, enum TOR_ONION_FLAGS tof) {
|
2020-02-01 22:33:45 +00:00
|
|
|
tor_os_create (tos, "onion_services.pub", "onion_services.txt", 1);
|
2019-02-11 09:23:38 +00:00
|
|
|
tor_os_read (tos);
|
|
|
|
|
|
|
|
int err = 0;
|
|
|
|
err = tor_ctl_connect (tctl, "127.0.0.1", "9051");
|
|
|
|
if (err < 0) {
|
|
|
|
fprintf(stderr, "Unable to open Tor Socket\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2020-02-01 22:33:45 +00:00
|
|
|
err = tor_ctl_add_onion (tctl, tos, ports, ports_count, tof);
|
2019-02-11 09:23:38 +00:00
|
|
|
if (err != 0) {
|
|
|
|
fprintf(stderr, "Unable to create Onion Services (error: %d)\n", err);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroy_resources(struct tor_os_str* tos, struct tor_ctl* tctl) {
|
|
|
|
tor_ctl_close (tctl);
|
|
|
|
tor_os_free (tos);
|
|
|
|
}
|
|
|
|
|
2019-09-17 13:12:37 +00:00
|
|
|
void init_tcp_servers(struct donar_server_ctx* ctx, int nlinks) {
|
2019-02-18 13:35:09 +00:00
|
|
|
char url[1024];
|
|
|
|
struct evt_core_cat cat = {0};
|
|
|
|
struct evt_core_fdinfo fdinfo = {0};
|
|
|
|
fdinfo.cat = &cat;
|
|
|
|
fdinfo.url = url;
|
|
|
|
|
2019-02-11 15:23:20 +00:00
|
|
|
char buffer[6];
|
2019-02-11 21:40:00 +00:00
|
|
|
int err, sock = 0;
|
2019-09-17 13:12:37 +00:00
|
|
|
for (int i = 0; i < nlinks; i++) {
|
2019-02-11 15:23:20 +00:00
|
|
|
sprintf (buffer, "%d", ctx->ports[i]);
|
2019-03-06 16:46:12 +00:00
|
|
|
//@FIXME shouldn't listen on 0.0.0.0 but 127.13.3.7 is not compatible with docker
|
|
|
|
sock = create_tcp_server ("0.0.0.0", buffer);
|
2019-02-11 21:40:00 +00:00
|
|
|
if (sock < 0) goto socket_create_err;
|
|
|
|
err = listen(sock, SOMAXCONN);
|
2019-02-11 15:23:20 +00:00
|
|
|
if (err != 0) goto socket_create_err;
|
2019-02-18 13:35:09 +00:00
|
|
|
|
|
|
|
fdinfo.cat->name = "tcp-listen";
|
|
|
|
fdinfo.fd = sock;
|
2019-02-18 14:11:12 +00:00
|
|
|
sprintf(fdinfo.url, "tcp:listen:127.0.0.1:%d", ctx->ports[i]);
|
2019-02-18 13:35:09 +00:00
|
|
|
evt_core_add_fd(&(ctx->evts), &fdinfo);
|
2019-02-11 15:23:20 +00:00
|
|
|
}
|
|
|
|
return;
|
2019-02-11 09:23:38 +00:00
|
|
|
|
2019-02-11 15:23:20 +00:00
|
|
|
socket_create_err:
|
|
|
|
fprintf(stderr, "Unable to create a TCP socket\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2020-01-20 22:35:02 +00:00
|
|
|
struct tor_ctl* ugly_global_tctl;
|
2020-01-23 19:04:33 +00:00
|
|
|
int donar_server_stream_repair(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
|
|
|
|
fprintf(stdout, "[%s][donar-server] %s broke\n", current_human_datetime (), fdinfo->url);
|
|
|
|
|
|
|
|
struct evt_core_fdinfo* fdtarget = NULL;
|
|
|
|
int port = url_get_port_int (fdinfo->url);
|
|
|
|
int pos = port - 7500, removed = 0;
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
printf("[%s][donar-server] removed %d links\n", current_human_datetime (), removed);
|
|
|
|
|
2020-01-20 15:35:52 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-01-20 22:35:02 +00:00
|
|
|
struct donar_server_os_republish_params {
|
|
|
|
struct donar_server_ctx* dctx;
|
|
|
|
struct donar_params* dp;
|
|
|
|
};
|
2020-02-01 22:33:45 +00:00
|
|
|
/*
|
2020-01-20 22:35:02 +00:00
|
|
|
void republish_tor_os(struct evt_core_ctx* ctx, void* user_data) {
|
|
|
|
struct donar_server_os_republish_params* dsorp = user_data;
|
|
|
|
|
|
|
|
int err = 0;
|
|
|
|
printf("Republish Tor OS\n");
|
|
|
|
err = tor_ctl_add_onion (&dsorp->dctx->tctl, &dsorp->dctx->tos, dsorp->dctx->ports, dsorp->dp->tof);
|
|
|
|
if (err != 0) {
|
|
|
|
fprintf(stderr, "Unable to create Onion Services (error: %d)\n", err);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
printf("Republish Tor OS\n");
|
2020-02-01 22:33:45 +00:00
|
|
|
}*/
|
2020-01-20 22:35:02 +00:00
|
|
|
|
2019-03-28 10:47:14 +00:00
|
|
|
void donar_server(struct donar_server_ctx* ctx, struct donar_params* dp) {
|
2019-03-28 10:54:01 +00:00
|
|
|
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 17:53:47 +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_server_stream_repair
|
2019-03-28 10:54:01 +00:00
|
|
|
};
|
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-02-11 22:40:37 +00:00
|
|
|
|
2019-09-17 13:12:37 +00:00
|
|
|
for (uint16_t i = 0; i < dp->links ; i++) {
|
2019-02-11 15:23:20 +00:00
|
|
|
ctx->ports[i] = 7500 + i;
|
|
|
|
}
|
2019-09-17 13:12:37 +00:00
|
|
|
create_onion_services (&(ctx->tos), &(ctx->tctl), ctx->ports, dp->links, dp->tof);
|
2020-01-20 22:35:02 +00:00
|
|
|
ugly_global_tctl = &(ctx->tctl);
|
|
|
|
/*struct donar_server_os_republish_params dsorp = { .dctx = ctx, dp = dp};
|
|
|
|
init_timer(&(ctx->evts));
|
|
|
|
set_timeout(&(ctx->evts), 1000, &dsorp, republish_tor_os); */
|
2019-02-11 15:23:20 +00:00
|
|
|
printf("--- Onion services created\n");
|
2019-02-19 18:15:37 +00:00
|
|
|
|
2019-09-17 13:12:37 +00:00
|
|
|
init_tcp_servers(ctx, dp->links);
|
2019-02-12 16:12:20 +00:00
|
|
|
printf("--- TCP servers are listening\n");
|
2019-02-11 15:23:20 +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-02-15 17:09:51 +00:00
|
|
|
|
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);
|
2019-06-21 14:50:49 +00:00
|
|
|
|
2019-02-11 21:40:00 +00:00
|
|
|
evt_core_loop (&(ctx->evts));
|
2019-02-11 15:23:20 +00:00
|
|
|
|
2020-01-20 22:35:02 +00:00
|
|
|
//stop_timer(&(ctx->evts));
|
2019-02-11 15:23:20 +00:00
|
|
|
destroy_resources (&(ctx->tos), &(ctx->tctl));
|
2019-02-11 09:23:38 +00:00
|
|
|
}
|