It works!
This commit is contained in:
parent
50e2cf4ccc
commit
e5228ed0af
1 changed files with 45 additions and 27 deletions
|
@ -49,25 +49,26 @@ co_error:
|
|||
}
|
||||
|
||||
int faketor_socks5_server_success(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
|
||||
printf("success socks5!\n");
|
||||
if (strstr(fdinfo->url, ":write") != NULL) return EVT_CORE_FD_EXHAUSTED;
|
||||
// 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");
|
||||
|
||||
printf("success socks5:\n\t+read: %s\n\t+write: %s\n", fdinfo->url, fdinfo2->url);
|
||||
return EVT_CORE_FD_EXHAUSTED;
|
||||
}
|
||||
|
||||
int faketor_torctl_server_success(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
|
||||
printf("success torctl!\n");
|
||||
if (strstr(fdinfo->url, ":write") != NULL) return EVT_CORE_FD_EXHAUSTED;
|
||||
// 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");
|
||||
printf("success torctl:\n\t+read: %s\n\t+write: %s\n", fdinfo->url, fdinfo2->url);
|
||||
return EVT_CORE_FD_EXHAUSTED;
|
||||
}
|
||||
|
||||
|
@ -78,47 +79,64 @@ int faketor_socks5_server_failed(struct evt_core_ctx* ctx, struct evt_core_fdinf
|
|||
return EVT_CORE_FD_EXHAUSTED;
|
||||
}
|
||||
|
||||
int faketor_ctos(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
|
||||
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);
|
||||
}
|
||||
if (*source == NULL || *destination == NULL) {
|
||||
fprintf(stderr, "[%s][faketor][WARN] Called with %s, computed %s but is not in fdpool.\n", current_human_datetime (), fdinfo->url, url_build);
|
||||
}
|
||||
}
|
||||
|
||||
int faketor_bridge(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
|
||||
char buffer[1600];
|
||||
ssize_t nread, nwrite;
|
||||
size_t skip_socket = strlen("socket:"), skip_torctl = strlen("torctl:");
|
||||
char url[512];
|
||||
struct evt_core_fdinfo *target, *source;
|
||||
|
||||
// 0. find source
|
||||
if (strstr(fdinfo->url, "socket:") != NULL) target = fdinfo;
|
||||
else {
|
||||
sprintf(url, "socket:%s", fdinfo->url);
|
||||
}
|
||||
get_fdinfo_couple (ctx, fdinfo, &source, &target);
|
||||
if (source == NULL || target == NULL) return EVT_CORE_FD_EXHAUSTED;
|
||||
printf("triggered event: %s, source: %s, target: %s\n", fdinfo->url, source->url, target->url);
|
||||
|
||||
// 1. read some data
|
||||
nread = recv(fdinfo->fd, buffer, sizeof(buffer), MSG_PEEK);
|
||||
nread = recv(source->fd, buffer, sizeof(buffer), MSG_PEEK);
|
||||
if (nread == -1 && errno == EAGAIN) return EVT_CORE_FD_EXHAUSTED;
|
||||
if (nread == -1) goto error;
|
||||
|
||||
// 2. find target
|
||||
sprintf(url, "torctl:%s:write", fdinfo->url + skip_socket);
|
||||
printf("source url: %s, target url: %s", fdinfo->url, url);
|
||||
target = evt_core_get_from_url (ctx, url);
|
||||
|
||||
// 3. write to target
|
||||
// 2. write to target
|
||||
nwrite = send(target->fd, buffer, nread, 0);
|
||||
if (nwrite == -1 && errno == EAGAIN) return EVT_CORE_FD_EXHAUSTED;
|
||||
if (nwrite == -1) goto error;
|
||||
nread = recv(fdinfo->fd, buffer, nwrite, 0);
|
||||
nread = recv(source->fd, buffer, nwrite, 0);
|
||||
if (nread == -1) goto error;
|
||||
|
||||
return EVT_CORE_FD_UNFINISHED;
|
||||
|
||||
error:
|
||||
perror("oopsie ctos");
|
||||
perror("oopsie");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
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};
|
||||
|
||||
|
@ -175,7 +193,7 @@ int main(void) {
|
|||
struct evt_core_cat ctos_read = {
|
||||
.app_ctx = NULL,
|
||||
.free_app_ctx = NULL,
|
||||
.cb = faketor_ctos,
|
||||
.cb = faketor_bridge,
|
||||
.err_cb = NULL,
|
||||
.name = "ctos-read",
|
||||
.flags = EPOLLIN | EPOLLET,
|
||||
|
@ -185,7 +203,7 @@ int main(void) {
|
|||
struct evt_core_cat ctos_write = {
|
||||
.app_ctx = NULL,
|
||||
.free_app_ctx = NULL,
|
||||
.cb = faketor_ctos,
|
||||
.cb = faketor_bridge,
|
||||
.err_cb = NULL,
|
||||
.name = "ctos-write",
|
||||
.flags = EPOLLOUT | EPOLLET,
|
||||
|
@ -195,7 +213,7 @@ int main(void) {
|
|||
struct evt_core_cat stoc_read = {
|
||||
.app_ctx = NULL,
|
||||
.free_app_ctx = NULL,
|
||||
.cb = faketor_stoc,
|
||||
.cb = faketor_bridge,
|
||||
.err_cb = NULL,
|
||||
.name = "stoc-read",
|
||||
.flags = EPOLLIN | EPOLLET,
|
||||
|
@ -205,7 +223,7 @@ int main(void) {
|
|||
struct evt_core_cat stoc_write = {
|
||||
.app_ctx = NULL,
|
||||
.free_app_ctx = NULL,
|
||||
.cb = faketor_stoc,
|
||||
.cb = faketor_bridge,
|
||||
.err_cb = NULL,
|
||||
.name = "stoc-write",
|
||||
.flags = EPOLLOUT | EPOLLET,
|
||||
|
|
Loading…
Reference in a new issue