Write bridge 1/2

This commit is contained in:
Quentin 2020-02-24 19:37:37 +01:00
parent 7eab91cae8
commit c3f99c35a9
3 changed files with 55 additions and 4 deletions

View file

@ -368,3 +368,15 @@ struct evt_core_fdinfo* evt_core_get_first_from_cat(struct evt_core_ctx* ctx, ch
void evt_core_free_app_ctx_simple(void* v) { void evt_core_free_app_ctx_simple(void* v) {
free(v); free(v);
} }
struct evt_core_fdinfo* evt_core_dup(struct evt_core_ctx *ctx, struct evt_core_fdinfo *fdinfo, char *newurl) {
struct evt_core_fdinfo newfdinfo;
struct evt_core_cat newcat;
newfdinfo.cat = &newcat;
newfdinfo.cat->name = fdinfo->cat->name;
newfdinfo.fd = dup(fdinfo->fd);
newfdinfo.url = strdup(newurl);
return evt_core_add_fd (ctx, &newfdinfo);
}

View file

@ -55,6 +55,7 @@ void evt_core_init(struct evt_core_ctx* ctx, uint8_t verbose);
void evt_core_add_cat(struct evt_core_ctx* ctx, struct evt_core_cat* cat); void evt_core_add_cat(struct evt_core_ctx* ctx, struct evt_core_cat* cat);
void evt_core_mv_fd(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, struct evt_core_cat* to_cat); void evt_core_mv_fd(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, struct evt_core_cat* to_cat);
void evt_core_mv_fd2(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, char* to_cat); void evt_core_mv_fd2(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, char* to_cat);
struct evt_core_fdinfo* evt_core_dup(struct evt_core_ctx *ctx, struct evt_core_fdinfo *fdinfo, char* new_url);
struct evt_core_fdinfo* evt_core_add_fd(struct evt_core_ctx* ctx, struct evt_core_fdinfo* user_data); struct evt_core_fdinfo* evt_core_add_fd(struct evt_core_ctx* ctx, struct evt_core_fdinfo* user_data);
struct evt_core_cat* evt_core_rm_fd(struct evt_core_ctx* ctx, int fd); struct evt_core_cat* evt_core_rm_fd(struct evt_core_ctx* ctx, int fd);
void evt_core_free(struct evt_core_ctx* ctx); void evt_core_free(struct evt_core_ctx* ctx);

View file

@ -50,23 +50,61 @@ co_error:
int faketor_socks5_server_success(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { int faketor_socks5_server_success(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
printf("success socks5!\n"); printf("success socks5!\n");
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");
int faketor_socks5_server_failed(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
printf("failure socks5!\n");
return EVT_CORE_FD_EXHAUSTED; return EVT_CORE_FD_EXHAUSTED;
} }
int faketor_torctl_server_success(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { int faketor_torctl_server_success(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
printf("success torctl!\n"); printf("success torctl!\n");
// 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");
return EVT_CORE_FD_EXHAUSTED;
}
int faketor_socks5_server_failed(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
printf("failure socks5!\n");
evt_core_rm_fd(ctx, fdinfo->fd);
return EVT_CORE_FD_EXHAUSTED; return EVT_CORE_FD_EXHAUSTED;
} }
int faketor_ctos(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { int faketor_ctos(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
char buffer[1600];
ssize_t nread, nwrite;
size_t skip = strlen("socket:");
char url[512];
// 1. read some data
nread = recv(fdinfo->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);
printf("source url: %s, target url: %s", fdinfo->url, url);
struct evt_core_fdinfo *target = evt_core_get_from_url (ctx, url);
// 3. write to target
nwrite = send(fdinfo->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);
if (nread == -1) goto error;
return EVT_CORE_FD_UNFINISHED; return EVT_CORE_FD_UNFINISHED;
error:
perror("oopsie ctos");
exit(EXIT_FAILURE);
} }
int faketor_stoc(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { int faketor_stoc(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {