tor_multipath_voip/src/faketor.c

293 lines
8.5 KiB
C
Raw Normal View History

2020-02-20 17:42:46 +00:00
#include <stdio.h>
#include <stdlib.h>
#include "evt_core.h"
#include "socks5.h"
#include "tor_ctl.h"
2020-02-24 16:22:22 +00:00
#include "timer.h"
2020-02-20 17:42:46 +00:00
int faketor_socks5_listen(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
int conn_sock1;
struct sockaddr_in addr;
socklen_t in_len;
in_len = sizeof(addr);
conn_sock1 = accept(fdinfo->fd, (struct sockaddr*)&addr, &in_len);
if (conn_sock1 == -1 && errno == EAGAIN) return EVT_CORE_FD_EXHAUSTED;
if (conn_sock1 == -1) goto co_error;
make_socket_non_blocking (conn_sock1);
2020-02-25 15:08:42 +00:00
printf("[%s][faketor] Accepted a new connection for socks5 \n", current_human_datetime ());
2020-02-21 17:12:06 +00:00
socks5_server_handle_req (ctx, conn_sock1);
2020-02-20 17:42:46 +00:00
return EVT_CORE_FD_UNFINISHED;
co_error:
perror("Failed to handle new socks5 connection");
2020-02-20 17:42:46 +00:00
exit(EXIT_FAILURE);
}
int faketor_control_listen(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
int conn_sock;
struct sockaddr_in addr;
socklen_t in_len;
in_len = sizeof(addr);
conn_sock = accept(fdinfo->fd, (struct sockaddr*)&addr, &in_len);
if (conn_sock == -1 && errno == EAGAIN) return EVT_CORE_FD_EXHAUSTED;
if (conn_sock == -1) goto co_error;
make_socket_non_blocking (conn_sock);
2020-02-25 15:08:42 +00:00
printf("[%s][faketor] Accepted a new connection for control port \n", current_human_datetime ());
tor_ctl_server_handle(ctx, conn_sock);
2020-02-20 17:42:46 +00:00
return EVT_CORE_FD_UNFINISHED;
co_error:
perror("Failed to handle new control port connection");
exit(EXIT_FAILURE);
2020-02-20 17:42:46 +00:00
}
2020-02-21 17:12:06 +00:00
int faketor_socks5_server_success(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
2020-02-25 10:52:37 +00:00
if (strstr(fdinfo->url, ":write") != NULL) return EVT_CORE_FD_EXHAUSTED;
2020-02-24 18:37:37 +00:00
// ctos-read and stoc-write
char buffer[512];
sprintf(buffer, "%s:write", fdinfo->url);
struct evt_core_fdinfo *fdinfo2 = evt_core_dup(ctx, fdinfo, buffer);
evt_core_mv_fd2(ctx, fdinfo, "ctos-read");
evt_core_mv_fd2(ctx, fdinfo2, "stoc-write");
2020-02-25 15:08:42 +00:00
printf("[%s][faketor] success socks5:\n\t+read: %s\n\t+write: %s\n", current_human_datetime (), fdinfo->url, fdinfo2->url);
2020-02-21 17:12:06 +00:00
return EVT_CORE_FD_EXHAUSTED;
}
2020-02-24 14:59:22 +00:00
int faketor_torctl_server_success(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
2020-02-25 10:52:37 +00:00
if (strstr(fdinfo->url, ":write") != NULL) return EVT_CORE_FD_EXHAUSTED;
2020-02-24 18:37:37 +00:00
// ctos-write and stoc-read
char buffer[512];
sprintf(buffer, "%s:write", fdinfo->url);
struct evt_core_fdinfo *fdinfo2 = evt_core_dup(ctx, fdinfo, buffer);
evt_core_mv_fd2(ctx, fdinfo, "stoc-read");
evt_core_mv_fd2(ctx, fdinfo2, "ctos-write");
2020-02-25 15:08:42 +00:00
printf("[%s][faketor] success torctl:\n\t+read: %s\n\t+write: %s\n", current_human_datetime (), fdinfo->url, fdinfo2->url);
2020-02-24 18:37:37 +00:00
return EVT_CORE_FD_EXHAUSTED;
}
int faketor_socks5_server_failed(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
printf("failure socks5!\n");
evt_core_rm_fd(ctx, fdinfo->fd);
2020-02-24 14:59:22 +00:00
return EVT_CORE_FD_EXHAUSTED;
}
2020-02-25 10:52:37 +00:00
void get_fdinfo_couple(struct evt_core_ctx* ctx, struct evt_core_fdinfo *fdinfo, struct evt_core_fdinfo **source, struct evt_core_fdinfo **destination) {
char *other_prefix, *current_prefix;
char url_build[256];
if (strstr(fdinfo->url, "socket:") != NULL) {
current_prefix = "socket:";
other_prefix = "torctl:";
} else {
current_prefix = "torctl:";
other_prefix = "socket:";
}
sprintf(url_build, "%s%s", other_prefix, fdinfo->url + strlen(current_prefix));
if (strstr(fdinfo->url, ":write") != NULL) {
*destination = fdinfo;
url_build[strlen(url_build) - strlen(":write")] = '\0';
*source = evt_core_get_from_url(ctx, url_build);
} else {
*source = fdinfo;
strcpy(url_build + strlen(url_build), ":write");
*destination = evt_core_get_from_url (ctx, url_build);
}
2020-02-25 15:08:42 +00:00
/*if (*source == NULL || *destination == NULL) {
2020-02-25 10:52:37 +00:00
fprintf(stderr, "[%s][faketor][WARN] Called with %s, computed %s but is not in fdpool.\n", current_human_datetime (), fdinfo->url, url_build);
2020-02-25 15:08:42 +00:00
}*/
2020-02-25 10:52:37 +00:00
}
int faketor_bridge(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
2020-02-24 18:37:37 +00:00
char buffer[1600];
ssize_t nread, nwrite;
char url[512];
2020-02-24 18:46:30 +00:00
struct evt_core_fdinfo *target, *source;
// 0. find source
2020-02-25 10:52:37 +00:00
get_fdinfo_couple (ctx, fdinfo, &source, &target);
if (source == NULL || target == NULL) return EVT_CORE_FD_EXHAUSTED;
2020-02-25 15:08:42 +00:00
//printf("triggered event: %s, source: %s, target: %s\n", fdinfo->url, source->url, target->url);
2020-02-24 18:37:37 +00:00
// 1. read some data
2020-02-25 10:52:37 +00:00
nread = recv(source->fd, buffer, sizeof(buffer), MSG_PEEK);
2020-02-24 18:37:37 +00:00
if (nread == -1 && errno == EAGAIN) return EVT_CORE_FD_EXHAUSTED;
if (nread == -1) goto error;
2020-02-25 10:52:37 +00:00
// 2. write to target
2020-02-24 18:46:30 +00:00
nwrite = send(target->fd, buffer, nread, 0);
2020-02-24 18:37:37 +00:00
if (nwrite == -1 && errno == EAGAIN) return EVT_CORE_FD_EXHAUSTED;
if (nwrite == -1) goto error;
2020-02-25 10:52:37 +00:00
nread = recv(source->fd, buffer, nwrite, 0);
2020-02-24 18:37:37 +00:00
if (nread == -1) goto error;
2020-02-24 14:59:22 +00:00
return EVT_CORE_FD_UNFINISHED;
2020-02-25 10:52:37 +00:00
2020-02-24 18:37:37 +00:00
error:
2020-02-25 10:52:37 +00:00
perror("oopsie");
2020-02-24 18:37:37 +00:00
exit(EXIT_FAILURE);
2020-02-24 14:59:22 +00:00
}
2020-03-02 13:47:51 +00:00
int main(int argc, char *argv[]) {
int opt = 0, port_socks5 = 9050, port_control = 9051;
char str_port_socks5[6], str_port_control[6];
while ((opt = getopt(argc, argv, "p:")) != -1) {
switch(opt) {
case 'p':
port_socks5 = atoi(optarg);
port_control = port_socks5+1;
break;
default:
break;
}
}
printf("ports socks5:%d control:%d\n", port_socks5, port_control);
sprintf(str_port_socks5, "%d", port_socks5);
sprintf(str_port_control, "%d", port_control);
2020-02-20 17:42:46 +00:00
struct evt_core_ctx evts = {0};
struct evt_core_cat socks5_listen = {
.app_ctx = NULL,
.free_app_ctx = NULL,
.cb = faketor_socks5_listen,
.err_cb = NULL,
.name = "socks5-listen",
.flags = EPOLLIN | EPOLLET,
.socklist = NULL
};
struct evt_core_cat control_listen = {
.app_ctx = NULL,
.free_app_ctx = NULL,
.cb = faketor_control_listen,
.err_cb = NULL,
.name = "control-listen",
.flags = EPOLLIN | EPOLLET,
.socklist = NULL
};
2020-02-21 17:12:06 +00:00
struct evt_core_cat socks5_server_success = {
.app_ctx = NULL,
.free_app_ctx = NULL,
.cb = faketor_socks5_server_success,
.err_cb = NULL,
.name = "socks5-server-success",
.flags = EPOLLIN | EPOLLET,
.socklist = NULL
};
struct evt_core_cat socks5_server_failed = {
.app_ctx = NULL,
.free_app_ctx = NULL,
.cb = faketor_socks5_server_failed,
.err_cb = NULL,
.name = "socks5-server-failed",
.flags = EPOLLIN | EPOLLET,
.socklist = NULL
};
2020-02-24 14:59:22 +00:00
struct evt_core_cat torctl_server_success = {
.app_ctx = NULL,
.free_app_ctx = NULL,
.cb = faketor_torctl_server_success,
.err_cb = NULL,
.name = "torctl-server-success",
.flags = EPOLLIN | EPOLLET,
.socklist = NULL
};
struct evt_core_cat ctos_read = {
.app_ctx = NULL,
.free_app_ctx = NULL,
2020-02-25 10:52:37 +00:00
.cb = faketor_bridge,
2020-02-24 14:59:22 +00:00
.err_cb = NULL,
.name = "ctos-read",
.flags = EPOLLIN | EPOLLET,
.socklist = NULL
};
struct evt_core_cat ctos_write = {
.app_ctx = NULL,
.free_app_ctx = NULL,
2020-02-25 10:52:37 +00:00
.cb = faketor_bridge,
2020-02-24 14:59:22 +00:00
.err_cb = NULL,
.name = "ctos-write",
.flags = EPOLLOUT | EPOLLET,
.socklist = NULL
};
struct evt_core_cat stoc_read = {
.app_ctx = NULL,
.free_app_ctx = NULL,
2020-02-25 10:52:37 +00:00
.cb = faketor_bridge,
2020-02-24 14:59:22 +00:00
.err_cb = NULL,
.name = "stoc-read",
.flags = EPOLLIN | EPOLLET,
.socklist = NULL
};
struct evt_core_cat stoc_write = {
.app_ctx = NULL,
.free_app_ctx = NULL,
2020-02-25 10:52:37 +00:00
.cb = faketor_bridge,
2020-02-24 14:59:22 +00:00
.err_cb = NULL,
.name = "stoc-write",
.flags = EPOLLOUT | EPOLLET,
.socklist = NULL
};
2020-02-21 17:12:06 +00:00
2020-02-20 17:42:46 +00:00
evt_core_init(&evts, 0);
evt_core_add_cat(&evts, &socks5_listen);
evt_core_add_cat(&evts, &control_listen);
2020-02-21 17:12:06 +00:00
evt_core_add_cat(&evts, &socks5_server_success);
evt_core_add_cat(&evts, &socks5_server_failed);
2020-02-24 14:59:22 +00:00
evt_core_add_cat(&evts, &torctl_server_success);
evt_core_add_cat(&evts, &ctos_read);
evt_core_add_cat(&evts, &ctos_write);
evt_core_add_cat(&evts, &stoc_read);
evt_core_add_cat(&evts, &stoc_write);
2020-02-20 17:42:46 +00:00
struct evt_core_cat cat = {0};
struct evt_core_fdinfo fdinfo = {0};
fdinfo.cat = &cat;
int err, sock = 0;
2020-03-02 13:47:51 +00:00
sock = create_tcp_server ("0.0.0.0", str_port_socks5);
2020-02-20 17:42:46 +00:00
if (sock < 0) return EXIT_FAILURE;
err = listen(sock, SOMAXCONN);
if (err != 0) return EXIT_FAILURE;
fdinfo.cat->name = "socks5-listen";
fdinfo.fd = sock;
fdinfo.url = "socks5:listen:9050";
evt_core_add_fd(&evts, &fdinfo);
2020-03-02 13:47:51 +00:00
sock = create_tcp_server ("0.0.0.0", str_port_control);
2020-02-20 17:42:46 +00:00
if (sock < 0) return EXIT_FAILURE;
err = listen(sock, SOMAXCONN);
if (err != 0) return EXIT_FAILURE;
fdinfo.cat->name = "control-listen";
fdinfo.fd = sock;
fdinfo.url = "control:listen:9051";
evt_core_add_fd(&evts, &fdinfo);
2020-02-24 16:22:22 +00:00
init_timer (&evts);
2020-02-20 17:42:46 +00:00
socks5_server_init(&evts);
tor_ctl_server_init (&evts);
2020-02-20 17:42:46 +00:00
2020-02-21 17:12:06 +00:00
evt_core_loop (&evts);
2020-02-20 17:42:46 +00:00
return EXIT_SUCCESS;
}