WIP faketor
This commit is contained in:
parent
01d1b7de34
commit
c9873c9735
5 changed files with 181 additions and 4 deletions
|
@ -49,6 +49,7 @@ add_executable(torecho ${CSOURCES} src/tor_echo.c)
|
||||||
add_executable(capdiff ${CSOURCES} src/capdiff.c)
|
add_executable(capdiff ${CSOURCES} src/capdiff.c)
|
||||||
add_executable(capreplay ${CSOURCES} src/capreplay.c)
|
add_executable(capreplay ${CSOURCES} src/capreplay.c)
|
||||||
add_executable(donar_unit_test ${CSOURCES} src/test.c)
|
add_executable(donar_unit_test ${CSOURCES} src/test.c)
|
||||||
|
add_executable(faketor ${CSOURCES} src/faketor.c)
|
||||||
add_executable(dcall src/dcall.c)
|
add_executable(dcall src/dcall.c)
|
||||||
|
|
||||||
find_package(PkgConfig REQUIRED)
|
find_package(PkgConfig REQUIRED)
|
||||||
|
@ -77,9 +78,12 @@ target_link_libraries(capreplay ${GLIB_LDFLAGS})
|
||||||
target_include_directories(donar_unit_test PRIVATE ${GLIB_INCLUDE_DIRS})
|
target_include_directories(donar_unit_test PRIVATE ${GLIB_INCLUDE_DIRS})
|
||||||
target_link_libraries(donar_unit_test ${GLIB_LDFLAGS})
|
target_link_libraries(donar_unit_test ${GLIB_LDFLAGS})
|
||||||
|
|
||||||
|
target_include_directories(faketor PRIVATE ${GLIB_INCLUDE_DIRS})
|
||||||
|
target_link_libraries(faketor ${GLIB_LDFLAGS})
|
||||||
|
|
||||||
target_include_directories(dcall PRIVATE ${GST_INCLUDE_DIRS})
|
target_include_directories(dcall PRIVATE ${GST_INCLUDE_DIRS})
|
||||||
target_link_libraries(dcall ${GST_LDFLAGS})
|
target_link_libraries(dcall ${GST_LDFLAGS})
|
||||||
|
|
||||||
install(TARGETS donar measlat udpecho torecho capdiff capreplay donar_unit_test dcall
|
install(TARGETS donar measlat udpecho torecho capdiff capreplay donar_unit_test dcall faketor
|
||||||
RUNTIME DESTINATION bin
|
RUNTIME DESTINATION bin
|
||||||
LIBRARY DESTINATION lib)
|
LIBRARY DESTINATION lib)
|
||||||
|
|
|
@ -12,6 +12,11 @@
|
||||||
|
|
||||||
#define EVT_CORE_MAX_EVENTS 10
|
#define EVT_CORE_MAX_EVENTS 10
|
||||||
|
|
||||||
|
enum evt_core_fd_handle {
|
||||||
|
EVT_CORE_FD_UNFINISHED = 0,
|
||||||
|
EVT_CORE_FD_EXHAUSTED = 1,
|
||||||
|
};
|
||||||
|
|
||||||
struct evt_core_ctx;
|
struct evt_core_ctx;
|
||||||
struct evt_core_cat;
|
struct evt_core_cat;
|
||||||
struct evt_core_fdinfo;
|
struct evt_core_fdinfo;
|
||||||
|
|
84
src/faketor.c
Normal file
84
src/faketor.c
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "evt_core.h"
|
||||||
|
#include "socks5.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;
|
||||||
|
|
||||||
|
|
||||||
|
printf("[%s][torfake] Accepted a new connection for socks5 \n", current_human_datetime ());
|
||||||
|
|
||||||
|
return EVT_CORE_FD_UNFINISHED;
|
||||||
|
co_error:
|
||||||
|
perror("Failed to handle new connection");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
int faketor_control_listen(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
|
||||||
|
};
|
||||||
|
|
||||||
|
evt_core_init(&evts, 0);
|
||||||
|
evt_core_add_cat(&evts, &socks5_listen);
|
||||||
|
evt_core_add_cat(&evts, &control_listen);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
87
src/socks5.c
87
src/socks5.c
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
void socks5_free_ctx(void* elem) {
|
void socks5_free_ctx(void* elem) {
|
||||||
struct socks5_ctx* ctx = elem;
|
struct socks5_ctx* ctx = elem;
|
||||||
free(ctx->addr);
|
if (ctx->addr != NULL) free(ctx->addr);
|
||||||
free(ctx);
|
free(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -265,10 +265,10 @@ int on_socks5_recv_server_reply(struct evt_core_ctx* ctx, struct evt_core_fdinfo
|
||||||
printf(" [socks5] read port\n");
|
printf(" [socks5] read port\n");
|
||||||
size_t relative_cursor = s5ctx->sr_cursor - (final_size - sizeof(s5ctx->sr.port));
|
size_t relative_cursor = s5ctx->sr_cursor - (final_size - sizeof(s5ctx->sr.port));
|
||||||
readn = read(fdinfo->fd, (char*)&s5ctx->sr.port + relative_cursor, sizeof(s5ctx->sr.port) - relative_cursor);
|
readn = read(fdinfo->fd, (char*)&s5ctx->sr.port + relative_cursor, sizeof(s5ctx->sr.port) - relative_cursor);
|
||||||
if (readn == -1 && errno == EAGAIN) return 1;
|
if (readn == -1 && errno == EAGAIN) return EVT_CORE_FD_EXHAUSTED;
|
||||||
if (readn < 0) goto move_to_failed;
|
if (readn < 0) goto move_to_failed;
|
||||||
s5ctx->sr_cursor += readn;
|
s5ctx->sr_cursor += readn;
|
||||||
return 0; // Needed as we might have not read enough bytes and free us from writing a loop
|
return EVT_CORE_FD_UNFINISHED; // Needed as we might have not read enough bytes and free us from writing a loop
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do some checks
|
// Do some checks
|
||||||
|
@ -320,3 +320,84 @@ void socks5_init(struct evt_core_ctx* ctx) {
|
||||||
char* socks5_rep (enum socks5_rep rep) {
|
char* socks5_rep (enum socks5_rep rep) {
|
||||||
return rep_msg[rep];
|
return rep_msg[rep];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void socks5_server_handle_req(struct evt_core_ctx* ctx, int fd) {
|
||||||
|
struct evt_core_fdinfo fdinfo;
|
||||||
|
struct evt_core_cat cat;
|
||||||
|
struct socks5_ctx* s5ctx;
|
||||||
|
struct evt_core_fdinfo* reg_fdinfo;
|
||||||
|
char url[1024];
|
||||||
|
|
||||||
|
// 1. Create fdinfo
|
||||||
|
fdinfo.cat = &cat;
|
||||||
|
fdinfo.cat->name = "socks5-server-recv-handshake";
|
||||||
|
fdinfo.fd = fd;
|
||||||
|
fdinfo.other = malloc(sizeof(struct socks5_ctx));
|
||||||
|
if (fdinfo.other == NULL) {
|
||||||
|
perror("malloc failed");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
memset(fdinfo.other, 0, sizeof(struct socks5_ctx));
|
||||||
|
fdinfo.free_other = socks5_free_ctx;
|
||||||
|
sprintf(url, "socks5:%d", fd);
|
||||||
|
fdinfo.url = url;
|
||||||
|
|
||||||
|
// 2. Configure our context
|
||||||
|
s5ctx = fdinfo.other;
|
||||||
|
s5ctx->port = 0;
|
||||||
|
s5ctx->addr = NULL;
|
||||||
|
|
||||||
|
|
||||||
|
// 3. Set our handshake answer
|
||||||
|
s5ctx->sh.ver = VER_SOCKS5;
|
||||||
|
s5ctx->sh.method = METHOD_NOAUTH;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int on_socks5_server_recv_handshake(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
|
||||||
|
|
||||||
|
return EVT_CORE_FD_UNFINISHED;
|
||||||
|
}
|
||||||
|
|
||||||
|
int on_socks5_server_send_handshake(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
|
||||||
|
|
||||||
|
return EVT_CORE_FD_UNFINISHED;
|
||||||
|
}
|
||||||
|
|
||||||
|
int on_socks5_server_recv_client_req(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
|
||||||
|
|
||||||
|
return EVT_CORE_FD_UNFINISHED;
|
||||||
|
}
|
||||||
|
|
||||||
|
int on_socks5_server_send_server_reply(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
|
||||||
|
|
||||||
|
return EVT_CORE_FD_UNFINISHED;
|
||||||
|
}
|
||||||
|
|
||||||
|
void socks5_server_init(struct evt_core_ctx* ctx) {
|
||||||
|
struct evt_core_cat template = {0};
|
||||||
|
|
||||||
|
template.cb = on_socks5_server_recv_handshake;
|
||||||
|
template.err_cb = on_socks5_err;
|
||||||
|
template.name = "socks5-server-recv-handshake";
|
||||||
|
template.flags = EPOLLIN | EPOLLET;
|
||||||
|
evt_core_add_cat (ctx, &template);
|
||||||
|
|
||||||
|
template.cb = on_socks5_server_send_handshake;
|
||||||
|
template.err_cb = on_socks5_err;
|
||||||
|
template.name = "socks5-server-send-handshake";
|
||||||
|
template.flags = EPOLLOUT | EPOLLET;
|
||||||
|
evt_core_add_cat (ctx, &template);
|
||||||
|
|
||||||
|
template.cb = on_socks5_server_recv_client_req;
|
||||||
|
template.err_cb = on_socks5_err;
|
||||||
|
template.name = "socks5-server-recv-client-req";
|
||||||
|
template.flags = EPOLLIN | EPOLLET;
|
||||||
|
evt_core_add_cat(ctx, &template);
|
||||||
|
|
||||||
|
template.cb = on_socks5_server_send_server_reply;
|
||||||
|
template.err_cb = on_socks5_err;
|
||||||
|
template.name = "socks5-server-send-server-reply";
|
||||||
|
template.flags = EPOLLIN | EPOLLET;
|
||||||
|
evt_core_add_cat(ctx, &template);
|
||||||
|
}
|
||||||
|
|
|
@ -128,3 +128,6 @@ struct socks5_ctx {
|
||||||
void socks5_init(struct evt_core_ctx* ctx);
|
void socks5_init(struct evt_core_ctx* ctx);
|
||||||
void socks5_create_dns_client(struct evt_core_ctx* ctx, char* proxy_host, char* proxy_port, char* addr, uint16_t port);
|
void socks5_create_dns_client(struct evt_core_ctx* ctx, char* proxy_host, char* proxy_port, char* addr, uint16_t port);
|
||||||
char* socks5_rep (enum socks5_rep rep);
|
char* socks5_rep (enum socks5_rep rep);
|
||||||
|
|
||||||
|
void socks5_server_init(struct evt_core_ctx* ctx);
|
||||||
|
void socks5_server_handle_req(struct evt_core_ctx* ctx, int fd);
|
||||||
|
|
Loading…
Reference in a new issue