Skip to content

Commit

Permalink
Comments and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jadeblaquiere committed Oct 24, 2018
1 parent ff2ca12 commit 6c64d25
Showing 1 changed file with 22 additions and 42 deletions.
64 changes: 22 additions & 42 deletions examples/socks5proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,90 +42,73 @@ int auth_fn(const char *user, const char* pass) {
return 1;
}

// forwards input to output. return on error (connection closed)
// forwards input to output. signal and return on error (connection closed)
coroutine void forward(int in_s, int out_s, int ch) {
while (1) {
uint8_t d[1];
int err = brecv(in_s, d, 1, -1);
if (err) break ;
err = bsend(out_s, d, 1, -1);
if(err) break;
if(brecv(in_s, d, 1, -1)) break;
if(bsend(out_s, d, 1, -1)) break;
}
chdone(ch);
return;
}

coroutine void do_proxy(int s) {
int err;
if((!auth_user) || (!auth_pass)){
err = socks5_proxy_auth(s, NULL, -1);
if(socks5_proxy_auth(s, NULL, -1)) goto in_close;
} else {
err = socks5_proxy_auth(s, auth_fn, -1);
if(socks5_proxy_auth(s, auth_fn, -1)) goto in_close;
}
if(err) goto in_close;

struct ipaddr addr;
int cmd = socks5_proxy_recvcommand(s, &addr, -1);
if(cmd != SOCKS5_CONNECT) goto in_close;
if(cmd != SOCKS5_CONNECT) {
socks5_proxy_sendreply(s, SOCKS5_COMMAND_NOT_SUPPORTED, &addr, -1);
goto in_close;
}

int s_rem = tcp_connect(&addr, -1);
if(s_rem < 0) goto in_close;

err = ipaddr_remote(&addr, "0.0.0.0", 0, IPADDR_IPV4, -1);
if(err) goto both_close;
if(ipaddr_remote(&addr, "0.0.0.0", 0, IPADDR_IPV4, -1)) goto both_close;

err = socks5_proxy_sendreply(s, SOCKS5_SUCCESS, &addr, -1);
if(err) goto both_close;
if(socks5_proxy_sendreply(s, SOCKS5_SUCCESS, &addr, -1)) goto both_close;

// channels for outboud, inbound to signal done (to close connection)
int och[2], ich[2];
err = chmake(och);
if(err) goto both_close;
err = chmake(ich);
if(err) goto both_close;
if(chmake(och)) goto both_close;
if(chmake(ich)) goto both_close;

// start coroutines to handle inbound and outbound forwarding
int ob = go(forward(s, s_rem, och[1]));
if(ob < 0) goto both_close;
int ib = go(forward(s_rem, s, ich[1]));
if(ib < 0) goto both_close;

struct chclause cc[] = {{CHRECV, och[0], &err, 1},
{CHRECV, ich[0], &err, 1}};
// wait for message in channel - one side closed
int c = choose(cc, 2, -1);
switch(c) {
// one side closed, close the other
// wait for message in channel - one side closed, tcp_done the other
struct chclause cc[] = {{CHRECV, och[0], &cmd, 1},
{CHRECV, ich[0], &cmd, 1}};
switch(choose(cc, 2, -1)) {
case 0:
err = bundle_wait(ob, -1);
if(err) break;
tcp_done(s, -1);
if(bundle_wait(ob, -1)) break;
tcp_done(s_rem, -1);
err = bundle_wait(ib, -1);
if(err) break;
//tcp_close(s_rem, -1);
bundle_wait(ib, -1);
break;
case 1:
err = bundle_wait(ib, -1);
if(err) break;
tcp_done(s_rem, -1);
if(bundle_wait(ib, -1)) break;
tcp_done(s, -1);
err = bundle_wait(ob, -1);
if(err) break;
//tcp_close(s, -1);
bundle_wait(ob, -1);
break;
case -1:
// error
break;
default:
// unexpected answer
assert(0);
}

both_close:
tcp_close(s_rem, -1);
in_close:
tcp_close(s, -1);

return;
}

Expand All @@ -135,7 +118,6 @@ int main(int argc, char** argv) {
auth_user = argv[1];
auth_pass = argv[2];
}

int workers = bundle();
struct ipaddr addr;
int rc = ipaddr_local(&addr, NULL, 1080, 0);
Expand All @@ -146,8 +128,6 @@ int main(int argc, char** argv) {
while(1) {
struct ipaddr caddr;
int s = tcp_accept(ls, &caddr, -1);
char caddr_str[IPADDR_MAXSTRLEN];
ipaddr_str(&caddr, caddr_str);
assert(s >= 0);
rc = bundle_go(workers, do_proxy(s));
assert(rc == 0);
Expand Down

0 comments on commit 6c64d25

Please sign in to comment.