capture ports

This commit is contained in:
Quentin 2020-02-24 15:59:22 +01:00
parent 6d0b048cff
commit 61b66a9198
2 changed files with 93 additions and 1 deletions

View file

@ -59,6 +59,23 @@ int faketor_socks5_server_failed(struct evt_core_ctx* ctx, struct evt_core_fdinf
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};
@ -102,12 +119,66 @@ int main(void) {
.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};

View file

@ -156,6 +156,8 @@ int on_torctl_server_auth_write(struct evt_core_ctx* ctx, struct evt_core_fdinfo
int on_torctl_server_add_onion_read(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
char buffer[1024] = {0};
char *strtok_ptr, *str_target, *token;
int i, port;
ssize_t nread = recv(fdinfo->fd, buffer, sizeof(buffer), MSG_PEEK);
if (nread == -1 && errno == EAGAIN) return EVT_CORE_FD_EXHAUSTED;
@ -165,8 +167,27 @@ int on_torctl_server_add_onion_read(struct evt_core_ctx* ctx, struct evt_core_fd
}
if (buffer[nread-1] != '\n') return EVT_CORE_FD_EXHAUSTED;
nread = recv(fdinfo->fd, buffer, sizeof(buffer), 0);
printf("[%s][torctl] Received command: %s\n", current_human_datetime (), buffer);
for (i = 0, str_target = buffer ; ; str_target = NULL, i++) {
token = strtok_r(str_target, " ", &strtok_ptr);
if (i == 0 && (token == NULL || strcmp(token, "add_onion") != 0)) {
fprintf(stderr, "wrong command. expected 'add_onion' but got '%s'\n", token);
exit(EXIT_FAILURE);
}
if (i == 1 && token == NULL) {
fprintf(stderr, "command add_onion requires at least one parameter, specifying key algo\n");
exit(EXIT_FAILURE);
}
if (i < 2) continue;
if (token == NULL) break;
int captured = sscanf(token, "Port=%d", &port);
if (captured != 1) continue;
printf("Captured port %d\n", port);
}
evt_core_mv_fd2 (ctx, fdinfo, "torctl-server-add-onion-write");
return EVT_CORE_FD_EXHAUSTED;
}