#include #include #include "evt_core.h" #include "socks5.h" #include "tor_ctl.h" 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); printf("[%s][torfake] Accepted a new connection for socks5 \n", current_human_datetime ()); socks5_server_handle_req (ctx, conn_sock1); return EVT_CORE_FD_UNFINISHED; co_error: perror("Failed to handle new socks5 connection"); 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); return EVT_CORE_FD_UNFINISHED; co_error: perror("Failed to handle new control port connection"); exit(EXIT_FAILURE); } int faketor_socks5_server_success(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { printf("success!\n"); struct socks5_ctx *s5ctx = fdinfo->other; 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; } 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; } 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 }; 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 }; 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 }; evt_core_init(&evts, 0); evt_core_add_cat(&evts, &socks5_listen); evt_core_add_cat(&evts, &control_listen); evt_core_add_cat(&evts, &socks5_server_success); evt_core_add_cat(&evts, &socks5_server_failed); 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); 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); evt_core_loop (&evts); return EXIT_SUCCESS; }