Global pipeline has been rewritten (broken+untested)

This commit is contained in:
Quentin Dufour 2019-03-22 17:58:48 +01:00
parent a2bf7ddec1
commit d94f935327

View file

@ -9,10 +9,10 @@ struct socks5_ctx {
uint64_t sh_cursor; uint64_t sh_cursor;
uint64_t cr_cursor; uint64_t cr_cursor;
uint64_t sr_cursor; uint64_t sr_cursor;
uint8_t sr_atyp_read;
char cr_buffer[262]; char cr_buffer[262];
size_t ch_size; size_t ch_size;
size_t cr_size; size_t cr_size;
uint8_t sr_host_read;
}; };
int on_socks5_send_handshake(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo); int on_socks5_send_handshake(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo);
@ -88,7 +88,7 @@ void create_socks5_dns_client(struct evt_core_ctx* ctx, char* proxy_host, char*
int on_socks5_send_handshake(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { int on_socks5_send_handshake(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
struct socks5_ctx* s5ctx = fdinfo->other; struct socks5_ctx* s5ctx = fdinfo->other;
size_t written = write(fdinfo->fd, &s5ctx->ch + s5ctx->ch_cursor, s5ctx->ch_size - s5ctx->ch_cursor); size_t written = write(fdinfo->fd, (char*)&s5ctx->ch + s5ctx->ch_cursor, s5ctx->ch_size - s5ctx->ch_cursor);
if (written == -1 && errno == EAGAIN) return 1; if (written == -1 && errno == EAGAIN) return 1;
if (written < 0) { if (written < 0) {
perror("write failed on tcp socket in socks5"); perror("write failed on tcp socket in socks5");
@ -107,7 +107,7 @@ int on_socks5_recv_handshake(struct evt_core_ctx* ctx, struct evt_core_fdinfo* f
struct socks5_ctx* s5ctx = fdinfo->other; struct socks5_ctx* s5ctx = fdinfo->other;
int readn = 0; int readn = 0;
readn = read(fdinfo->fd, &s5ctx->sh + s5ctx->sh_cursor, sizeof(s5ctx->sh) - s5ctx->sh_cursor); readn = read(fdinfo->fd, (char*)&s5ctx->sh + s5ctx->sh_cursor, sizeof(s5ctx->sh) - s5ctx->sh_cursor);
if (readn == -1 && errno == EAGAIN) return 1; if (readn == -1 && errno == EAGAIN) return 1;
if (readn < 0) { if (readn < 0) {
perror("sock5 handshake failed read"); perror("sock5 handshake failed read");
@ -126,7 +126,7 @@ int on_socks5_recv_handshake(struct evt_core_ctx* ctx, struct evt_core_fdinfo* f
fdinfo->cat->cb(ctx, fdinfo); fdinfo->cat->cb(ctx, fdinfo);
return 1; return 1;
} }
printf("[server_handshake] ver=%d, method=%d\n", s5ctx->sh.ver, s5ctx->sh.method); printf("[socks5_server_handshake] fd=%d, ver=%d, method=%d\n", fdinfo->fd, s5ctx->sh.ver, s5ctx->sh.method);
evt_core_mv_fd2(ctx, fdinfo, "socks5-send-client-req"); evt_core_mv_fd2(ctx, fdinfo, "socks5-send-client-req");
fdinfo->cat->cb(ctx, fdinfo); fdinfo->cat->cb(ctx, fdinfo);
return 1; return 1;
@ -136,7 +136,7 @@ int on_socks5_send_client_req(struct evt_core_ctx* ctx, struct evt_core_fdinfo*
struct socks5_ctx* s5ctx = fdinfo->other; struct socks5_ctx* s5ctx = fdinfo->other;
int written = 0; int written = 0;
written = write(fdinfo->fd, s5ctx->cr_buffer + s5ctx->cr_cursor, s5ctx->cr_size - s5ctx->cr_cursor); written = write(fdinfo->fd, (char*)s5ctx->cr_buffer + s5ctx->cr_cursor, s5ctx->cr_size - s5ctx->cr_cursor);
if (written == -1 && errno == EAGAIN) return 1; if (written == -1 && errno == EAGAIN) return 1;
if (written < 0) { if (written < 0) {
fprintf(stderr, "socks5 send client request failed\n"); fprintf(stderr, "socks5 send client request failed\n");
@ -159,7 +159,7 @@ int socks5_server_reply_atyp_ipv4(struct evt_core_ctx* ctx, struct evt_core_fdin
int nread = 0; int nread = 0;
nread = read(fdinfo->fd, nread = read(fdinfo->fd,
s5ctx->sr.bind_addr.ipv4 + relative_cursor, (char*)s5ctx->sr.bind_addr.ipv4 + relative_cursor,
host_size - relative_cursor); host_size - relative_cursor);
if (nread == -1 && errno == EAGAIN) return 1; if (nread == -1 && errno == EAGAIN) return 1;
@ -173,7 +173,7 @@ int socks5_server_reply_atyp_ipv4(struct evt_core_ctx* ctx, struct evt_core_fdin
s5ctx->sr_cursor += nread; s5ctx->sr_cursor += nread;
if (s5ctx->sr_cursor < fixed_headers_size + host_size) return 0; if (s5ctx->sr_cursor < fixed_headers_size + host_size) return 0;
s5ctx->sr_atyp_read = 1; s5ctx->sr_host_read = 1;
return 0; return 0;
} }
@ -185,7 +185,7 @@ int socks5_server_reply_atyp_ipv6(struct evt_core_ctx* ctx, struct evt_core_fdin
int nread = 0; int nread = 0;
nread = read(fdinfo->fd, nread = read(fdinfo->fd,
s5ctx->sr.bind_addr.ipv6 + relative_cursor, (char*)s5ctx->sr.bind_addr.ipv6 + relative_cursor,
host_size - relative_cursor); host_size - relative_cursor);
if (nread == -1 && errno == EAGAIN) return 1; if (nread == -1 && errno == EAGAIN) return 1;
@ -198,7 +198,7 @@ int socks5_server_reply_atyp_ipv6(struct evt_core_ctx* ctx, struct evt_core_fdin
s5ctx->sr_cursor += nread; s5ctx->sr_cursor += nread;
if (s5ctx->sr_cursor < fixed_headers_size + host_size) return 0; if (s5ctx->sr_cursor < fixed_headers_size + host_size) return 0;
s5ctx->sr_atyp_read = 1; s5ctx->sr_host_read = 1;
return 0; return 0;
} }
@ -211,7 +211,7 @@ int socks5_server_reply_atyp_dn(struct evt_core_ctx* ctx, struct evt_core_fdinfo
if (s5ctx->sr_cursor < fixed_headers_size + dn_size_size) { if (s5ctx->sr_cursor < fixed_headers_size + dn_size_size) {
relative_cursor = (s5ctx->sr_cursor - fixed_headers_size); relative_cursor = (s5ctx->sr_cursor - fixed_headers_size);
nread = read(fdinfo->fd, &s5ctx->sr.bind_addr.dns.len + relative_cursor, dn_size_size - relative_cursor); nread = read(fdinfo->fd, (char*)&s5ctx->sr.bind_addr.dns.len + relative_cursor, dn_size_size - relative_cursor);
if (nread == -1 && errno == EAGAIN) return 1; if (nread == -1 && errno == EAGAIN) return 1;
if (nread < 0) { if (nread < 0) {
perror("write failed on tcp socket in socks5"); perror("write failed on tcp socket in socks5");
@ -224,77 +224,86 @@ int socks5_server_reply_atyp_dn(struct evt_core_ctx* ctx, struct evt_core_fdinfo
} }
relative_cursor = s5ctx->sr_cursor - fixed_headers_size - sizeof(s5ctx->sr.bind_addr.dns.len); relative_cursor = s5ctx->sr_cursor - fixed_headers_size - sizeof(s5ctx->sr.bind_addr.dns.len);
nread = read(fdinfo->fd, &s5ctx->sr.bind_addr.dns.str + relative_cursor, s5ctx->sr.bind_addr.dns.len - relative_cursor); nread = read(fdinfo->fd, (char*)&s5ctx->sr.bind_addr.dns.str + relative_cursor, s5ctx->sr.bind_addr.dns.len - relative_cursor);
if (nread == -1 && errno == EAGAIN) return 1;
if (nread < 0) {
perror("write failed on tcp socket in socks5");
evt_core_mv_fd2(ctx, fdinfo, "socks5-failed");
fdinfo->cat->cb(ctx, fdinfo);
return 1;
}
s5ctx->sr_cursor += nread;
if (s5ctx->sr_cursor < fixed_headers_size + dn_size_size + s5ctx->sr.bind_addr.dns.len) return 0;
s5ctx->sr_host_read = 1;
return 0; return 0;
} }
size_t socks5_server_reply_size(struct server_reply* sr) {
size_t fixed_headers_size = (char*)&sr->bind_addr - (char*)&sr;
size_t fixed_tail_size = (char*)(sr + 1) - (char*)&sr->port;
size_t host_size = 0;
if (sr->atyp == ATYP_IPV4) {
host_size = sizeof(sr->bind_addr.ipv4);
} else if (sr->atyp == ATYP_IPV6) {
host_size = sizeof(sr->bind_addr.ipv6);
} else if (sr->atyp == ATYP_DOMAINNAME) {
host_size = sizeof(sr->bind_addr.dns.len) + sr->bind_addr.dns.len;
} else {
fprintf(stderr, "Unsupported ATYP for SOCK5\n");
exit(EXIT_FAILURE);
}
return fixed_headers_size + host_size + fixed_tail_size;
}
int on_socks5_recv_server_reply(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { int on_socks5_recv_server_reply(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
struct socks5_ctx* s5ctx = fdinfo->other; struct socks5_ctx* s5ctx = fdinfo->other;
int readn = 0; int readn = 0;
size_t fixed_headers_size = (void*)&s5ctx->sr.bind_addr - (void*)&s5ctx->sr; size_t fixed_headers_size = (char*)&s5ctx->sr.bind_addr - (char*)&s5ctx->sr;
size_t fixed_tail_size = (void*)(&s5ctx->sr + 1) - (void*)&s5ctx->sr.port ;
// Read headers
if (s5ctx->sr_cursor < fixed_headers_size) { if (s5ctx->sr_cursor < fixed_headers_size) {
readn = read(fdinfo->fd, &s5ctx->sr + s5ctx->sr_cursor, fixed_headers_size - s5ctx->sr_cursor); readn = read(fdinfo->fd, (char*)&s5ctx->sr + s5ctx->sr_cursor, fixed_headers_size - s5ctx->sr_cursor);
if (readn == -1 && errno == EAGAIN) return 1; if (readn == -1 && errno == EAGAIN) return 1;
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 0; // Needed as we might have not read enough bytes and free us from writing a loop
} }
if (!s5ctx->sr_atyp_read) { // Read host
if (!s5ctx->sr_host_read) {
if (s5ctx->sr.atyp == ATYP_IPV4) return socks5_server_reply_atyp_ipv4(ctx, fdinfo); if (s5ctx->sr.atyp == ATYP_IPV4) return socks5_server_reply_atyp_ipv4(ctx, fdinfo);
else if (s5ctx->sr.atyp == ATYP_IPV6) return socks5_server_reply_atyp_ipv6(ctx, fdinfo); else if (s5ctx->sr.atyp == ATYP_IPV6) return socks5_server_reply_atyp_ipv6(ctx, fdinfo);
else if (s5ctx->sr.atyp == ATYP_DOMAINNAME) return socks5_server_reply_atyp_dn(ctx, fdinfo); else if (s5ctx->sr.atyp == ATYP_DOMAINNAME) return socks5_server_reply_atyp_dn(ctx, fdinfo);
else goto move_to_failed; else goto move_to_failed;
} }
// Read port
size_t final_size = socks5_server_reply_size(&s5ctx->sr);
if (s5ctx->sr_cursor < final_size) {
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);
if (readn == -1 && errno == EAGAIN) return 1;
if (readn < 0) goto move_to_failed;
s5ctx->sr_cursor += readn;
return 0; // Needed as we might have not read enough bytes and free us from writing a loop
}
// Do some checks
if (s5ctx->sr.rep > 0x08) goto move_to_failed;
printf("[socks5_server_reply] fd=%d, ver=%d, rep=%s, atyp=%d, port=%d\n", fdinfo->fd, s5ctx->sr.ver, rep_msg[s5ctx->sr.rep], s5ctx->sr.atyp, s5ctx->sr.port);
if (s5ctx->sr.rep != SOCKS5_REP_SUCCESS) goto move_to_failed;
evt_core_mv_fd2 (ctx, fdinfo, "socks5-success");
fdinfo->cat->cb(ctx, fdinfo);
return 1;
move_to_failed: move_to_failed:
evt_core_mv_fd2 (ctx, fdinfo, "socks5-failed"); evt_core_mv_fd2 (ctx, fdinfo, "socks5-failed");
fdinfo->cat->cb(ctx, fdinfo); fdinfo->cat->cb(ctx, fdinfo);
return 1; return 1;
/*int res;
struct server_reply sr = {0};
res = read_entity(sock, &sr, sizeof(uint8_t) * 4);
if (res == -1) goto read_error;
switch(sr.atyp) {
case ATYP_IPV4:
if (read_entity(sock, sr.bind_addr.ipv4, sizeof(uint8_t) * 4) == -1)
goto read_error;
break;
case ATYP_DOMAINNAME:
if (read_entity(sock, &sr.bind_addr.dns.len, sizeof(uint8_t) * 4) == -1)
goto read_error;
if (read_entity(sock, sr.bind_addr.dns.str, sizeof(char) * sr.bind_addr.dns.len) == -1)
goto read_error;
break;
case ATYP_IPV6:
if (read_entity(sock, sr.bind_addr.ipv6, sizeof(uint8_t) * 16) == -1)
goto read_error;
break;
default:
fprintf(stderr, "Unsupported ATYP in server reply\n");
return -128;
}
res = read_entity(sock, &sr.port, sizeof(uint16_t));
if (res == -1) {
perror("read_entity");
exit(EXIT_FAILURE);
}
if (sr.rep < 0 || sr.rep > 0x08) {
fprintf(stderr, "Invalid reply field\n");
return -128;
}
printf("[server_reply] ver=%d, rep=%s, atyp=%d, port=%d\n", sr.ver, rep_msg[sr.rep], sr.atyp, sr.port);
return -sr.rep;
read_error:
fprintf(stderr, "Unable to read ATYP\n");
return -128;*/
} }
char* socks5_rep (enum socks5_rep rep) { char* socks5_rep (enum socks5_rep rep) {