static void connect_to_remote_cb(uv_connect_t* req, int status) { server_ctx *ctx = (server_ctx *)req->data; free(req); if (status) { SHOW_UV_ERROR(ctx->client.loop); HANDLE_CLOSE((uv_handle_t*)(void *)&ctx->client, client_established_close_cb); return; } LOGI("Connected to remote server"); uv_buf_t buf; buf.base = (char *)ctx->handshake_buffer; buf.len = HANDSHAKE_BUFFER_SIZE; shadow_encrypt((unsigned char *)buf.base, encrypt_table, ctx->buffer_len); client_established_read_cb((uv_stream_t *)(void *)&ctx->client, ctx->buffer_len, buf); // Deal with ramaining data, only once ctx->handshake_buffer = NULL; ctx->buffer_len = 0; int n = uv_read_start((uv_stream_t *)(void *)&ctx->client, established_alloc_cb, client_established_read_cb); if (n) { SHOW_UV_ERROR(ctx->client.loop); HANDLE_CLOSE((uv_handle_t*)(void *)&ctx->client, client_established_close_cb); return; } n = uv_read_start((uv_stream_t *)(void *)&ctx->remote, established_alloc_cb, remote_established_read_cb); if (n) { SHOW_UV_ERROR(ctx->client.loop); HANDLE_CLOSE((uv_handle_t*)(void *)&ctx->client, client_established_close_cb); return; } }
static void connect_to_remote_cb(uv_connect_t* req, int status) { server_ctx *ctx = (server_ctx *)req->data; if (status) { if (uv_last_error(req->handle->loop).code != UV_ECANCELED) { SHOW_UV_ERROR(ctx->client.loop); HANDLE_CLOSE((uv_handle_t*)(void *)&ctx->remote, remote_established_close_cb); free(ctx->handshake_buffer); free(req); } return; } free(req); LOGCONN(&ctx->remote, "Connected to %s"); uv_buf_t buf; buf.base = (char *)ctx->handshake_buffer; buf.len = HANDSHAKE_BUFFER_SIZE; shadow_encrypt((uint8_t *)buf.base, encrypt_table, ctx->buffer_len); client_established_read_cb((uv_stream_t *)(void *)&ctx->client, ctx->buffer_len, buf); // Deal with ramaining data, only once ctx->handshake_buffer = NULL; ctx->buffer_len = 0; if (uv_is_closing((uv_handle_t *)(void *)&ctx->remote) || uv_is_closing((uv_handle_t *)(void *)&ctx->client)) { LOGE("Connection failed, remote or client already closed"); return; } int n = uv_read_start((uv_stream_t *)(void *)&ctx->client, established_alloc_cb, client_established_read_cb); if (n) { SHOW_UV_ERROR(ctx->client.loop); HANDLE_CLOSE((uv_handle_t*)(void *)&ctx->remote, remote_established_close_cb); return; } n = uv_read_start((uv_stream_t *)(void *)&ctx->remote, established_alloc_cb, remote_established_read_cb); if (n) { SHOW_UV_ERROR(ctx->client.loop); HANDLE_CLOSE((uv_handle_t*)(void *)&ctx->remote, remote_established_close_cb); return; } }