tor_multipath_voip/src/faketor.c

213 lines
5.4 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-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-20 17:42:46 +00:00
printf("[%s][torfake] 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);
printf("[%s][torfake] 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) {
printf("success!\n");
2020-02-21 17:46:50 +00:00
struct socks5_ctx *s5ctx = fdinfo->other;
2020-02-21 17:12:06 +00:00
return EVT_CORE_FD_EXHAUSTED;
}
int faketor_socks5_server_failed(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
printf("failure!\n");
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) {
printf("success!\n");
evt_core_mv_fd2(ctx, fdinfo, "");
return EVT_CORE_FD_EXHAUSTED;
}
int faketor_ctos(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
return EVT_CORE_FD_UNFINISHED;
}
int faketor_stoc(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
return EVT_CORE_FD_UNFINISHED;
}
2020-02-20 17:42:46 +00:00
int main(void) {
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,
.cb = faketor_ctos,
.err_cb = NULL,
.name = "ctos-read",
.flags = EPOLLIN | EPOLLET,
.socklist = NULL
};
struct evt_core_cat ctos_write = {
.app_ctx = NULL,
.free_app_ctx = NULL,
.cb = faketor_ctos,
.err_cb = NULL,
.name = "ctos-write",
.flags = EPOLLOUT | EPOLLET,
.socklist = NULL
};
struct evt_core_cat stoc_read = {
.app_ctx = NULL,
.free_app_ctx = NULL,
.cb = faketor_stoc,
.err_cb = NULL,
.name = "stoc-read",
.flags = EPOLLIN | EPOLLET,
.socklist = NULL
};
struct evt_core_cat stoc_write = {
.app_ctx = NULL,
.free_app_ctx = NULL,
.cb = faketor_stoc,
.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;
sock = create_tcp_server ("0.0.0.0", "9050");
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);
sock = create_tcp_server ("0.0.0.0", "9051");
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);
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;
}