Skip to content

Commit

Permalink
TCP Support
Browse files Browse the repository at this point in the history
  • Loading branch information
expressvpn-raihaan-m committed Aug 15, 2023
1 parent 9932e9a commit 713781b
Show file tree
Hide file tree
Showing 15 changed files with 397 additions and 18 deletions.
3 changes: 2 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ RUN apt-get update && apt-get install -qqy --no-install-recommends \
valgrind \
wget \
strace \
vim
vim \
cmake

# Set up ceedling
RUN gem install ceedling
Expand Down
6 changes: 6 additions & 0 deletions include/lw.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ typedef struct lw_state {
char const *dns_ip;
int mtu;

//TCP Client socket
uv_connect_t tcp_connect;
uv_tcp_t tcp_client;

// The external IP viewable to the outside world
uint32_t assigned_ip;

Expand All @@ -92,6 +96,8 @@ typedef struct lw_state {

// Server or not -- most things don't actually care but some do
bool is_server;
// is streaming
bool is_streaming;

} lw_state_t;

Expand Down
5 changes: 2 additions & 3 deletions project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@
:fetch:
:method: :git
:source: https://github.com/expressvpn/lightway-core.git
:branch: main
:environment:
- CFLAGS= -DLARGE_STATIC_BUFFERS -DWOLFSSL_DTLS_ALLOW_FUTURE -DWOLFSSL_MIN_RSA_BITS=2048 -DWOLFSSL_MIN_ECC_BITS=256 -fPIC
:branch: dtls13-testing
:build:
- /usr/local/bin/ceedling verbosity[4] release project:linux
:artifacts:
Expand All @@ -87,6 +85,7 @@
:static_libraries:
- build/artifacts/release/libhelium.a
- third_party/builds/wolfssl_build/lib/libwolfssl.a
# - third_party/liboqs/build/lib/liboqs.a
- :name: libuv
:source_path: third_party/libuv
:artifact_path: third_party/builds/libuv
Expand Down
4 changes: 2 additions & 2 deletions scripts/run_iperf_client.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ echo "Resolved to target ${TARGET} server ${SERVER}"
echo "Check that we have connectivity to the lightway server"
ping -w1 "${SERVER}"

build/release/lw.out --client --protocol udp --username test --password test --server_ip ${SERVER} --server_port 19655 --cert certs/shared.crt --tun helium-test &
build/release/lw.out --client --protocol tcp --username test --password test --server_ip ${SERVER} --server_port 19655 --cert certs/shared.crt --tun helium-test &

sleep 2
sleep 15

echo "Setting route to ${TARGET} via ${HELIUM_GATE}"
ip route add "${TARGET}" via "${HELIUM_GATE}"
Expand Down
2 changes: 1 addition & 1 deletion scripts/run_server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -e
scripts/setup_nat_tun.sh

build/release/lw.out --server \
--protocol udp \
--protocol tcp \
--username test \
--password test \
--server_ip '0.0.0.0' \
Expand Down
19 changes: 19 additions & 0 deletions src/he/helium.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,23 @@ he_return_code_t state_change_cb(he_conn_t *client, he_conn_state_t new_state, v

zlogf_time(ZLOG_INFO_LOG_MSG, "State changed to %s\n", he_client_state_name(new_state));

static bool reneg = false;

if(new_state == HE_STATE_DISCONNECTED) {
zlogf_time(ZLOG_INFO_LOG_MSG, "Helium connection was disconnected\n");
lw_state_post_disconnect_cleanup(state);
}

if(new_state == HE_STATE_ONLINE) {
// zlogf_time(ZLOG_INFO_LOG_MSG, "CURVE: %s\n", he_conn_get_curve_name(client));

if (!state->is_server && !reneg) {
reneg = true;
he_conn_schedule_renegotiation(client);
zlogf_time(ZLOG_INFO_LOG_MSG, "SCHEDULED RENEGOTIATION\n");
}
}

return HE_SUCCESS;
}

Expand Down Expand Up @@ -145,6 +157,7 @@ void start_helium_server(lw_state_t *state) {

void start_helium_server_connection(lw_state_t *state) {
state->he_conn = he_conn_create();
he_conn_enable_debugging(state->he_conn, NULL);
LW_CHECK_WITH_MSG(state->he_conn, "Unable to allocate new Helium connection");

int res = he_conn_set_outside_mtu(state->he_conn, LW_MAX_WIRE_MTU);
Expand All @@ -155,6 +168,8 @@ void start_helium_server_connection(lw_state_t *state) {

res = he_conn_server_connect(state->he_conn, state->he_ctx, NULL, NULL);
LW_CHECK_WITH_MSG(res == HE_SUCCESS, "Helium connect failed");
zlogf_time(ZLOG_INFO_LOG_MSG, "Server connection start\n");
zlog_flush_buffer();
}

void configure_helium_client(lw_config_t *config, lw_state_t *state) {
Expand All @@ -172,6 +187,7 @@ void configure_helium_client(lw_config_t *config, lw_state_t *state) {
LW_CHECK_WITH_MSG(res == HE_SUCCESS, "Failed to set the server key path");

state->he_conn = he_conn_create();
he_conn_enable_debugging(state->he_conn, NULL);
LW_CHECK_WITH_MSG(state->he_conn, "Failed to create connection");

res = he_conn_set_username(state->he_conn, config->username);
Expand All @@ -195,5 +211,8 @@ void start_helium_client(lw_state_t *state) {
LW_CHECK_WITH_MSG(res == HE_SUCCESS, "Failed to start He context!");

res = he_conn_client_connect(state->he_conn, state->he_ctx, NULL, NULL);
if (res != HE_SUCCESS) {
zlogf_time(ZLOG_INFO_LOG_MSG, "CONNECT FAILED: %d\n", res);
}
LW_CHECK_WITH_MSG(res == HE_SUCCESS, "Failed to connect!");
}
26 changes: 17 additions & 9 deletions src/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include "util.h"
#include "he/helium.h"
#include "tcp/tcp_client.h"
#include "tcp/tcp_server.h"
#include "udp/server.h"
#include "udp/client.h"
#include "tun/tun.h"
Expand Down Expand Up @@ -54,6 +56,7 @@ lw_state_t *lw_start_server(lw_config_t *config) {

// Also copy the tun_name as-is
strncpy(state->tun_name, config->tun_name, sizeof(state->tun_name));
state->is_streaming = config->streaming;

// Initialise these w/ hardcoded values for now
state->peer_ip = "10.125.0.1";
Expand All @@ -68,8 +71,7 @@ lw_state_t *lw_start_server(lw_config_t *config) {
configure_helium_server(config, state);

if(config->streaming) {
zlogf_time(ZLOG_INFO_LOG_MSG, "Streaming is not supported yet");
LW_EXIT_WITH_FAILURE();
configure_tcp_server(config, state);
} else {
configure_udp_server(config, state);
}
Expand All @@ -80,8 +82,7 @@ lw_state_t *lw_start_server(lw_config_t *config) {
start_helium_server(state);

if(config->streaming) {
zlogf_time(ZLOG_INFO_LOG_MSG, "Streaming is not supported yet");
LW_EXIT_WITH_FAILURE();
start_tcp_server(state);
} else {
start_udp_server(state);
}
Expand All @@ -95,10 +96,15 @@ void on_client_kickstart(uv_timer_t *timer) {
lw_state_t *state = (lw_state_t *)timer->data;

zlogf_time(ZLOG_INFO_LOG_MSG, "Kickstarting client\n");
zlog_flush_buffer();

start_helium_client(state);
if (!state->is_streaming) {
start_helium_client(state);

start_udp_client(state);
start_udp_client(state);
} else {
start_tcp_client(state);
}

// We don't start the tunnel here, but instead during the network_config callback
}
Expand Down Expand Up @@ -131,6 +137,7 @@ lw_state_t *lw_start_client(lw_config_t *config) {
// Also copy the tun_name as-is
strncpy(state->tun_name, config->tun_name, sizeof(state->tun_name));
state->password[HE_CONFIG_TEXT_FIELD_LENGTH - 1] = '\0';
state->is_streaming = config->streaming;

// Initialise libuv
state->loop = uv_default_loop();
Expand All @@ -139,8 +146,7 @@ lw_state_t *lw_start_client(lw_config_t *config) {
configure_helium_client(config, state);

if(config->streaming) {
zlogf_time(ZLOG_INFO_LOG_MSG, "Streaming is not supported yet");
LW_EXIT_WITH_FAILURE();
configure_tcp_client(config, state);
} else {
configure_udp_client(config, state);
}
Expand Down Expand Up @@ -176,7 +182,9 @@ void lw_state_server_connect(lw_state_t *state, const struct sockaddr *addr) {
start_helium_server_connection(state);

// Copy the IP address and session
memcpy(&state->send_addr, addr, sizeof(struct sockaddr));
if (addr) {
memcpy(&state->send_addr, addr, sizeof(struct sockaddr));
}
state->session = he_conn_get_session_id(state->he_conn);
state->assigned_ip = ip2int("10.125.0.42");
}
60 changes: 60 additions & 0 deletions src/tcp/tcp_client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* *
* Lightway Laser
* Copyright (C) 2021 Express VPN International Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "tcp_client.h"
#include "tcp_flow.h"
#include "helium.h"
#include "util.h"

void configure_tcp_client(lw_config_t *config, lw_state_t *state) {
zlogf_time(ZLOG_INFO_LOG_MSG, "Configuring TCP Client...\n");

int res = uv_tcp_init(state->loop, &state->tcp_client);
LW_CHECK_WITH_MSG(res == 0, "Unable to initialise TCP socket");

res = uv_ip4_addr(config->server_ip, config->server_port, &state->send_addr);
LW_CHECK_WITH_MSG(res == 0, "Invalid IP address or port");

state->tcp_client.data = state;
he_ssl_ctx_set_outside_write_cb(state->he_ctx, tcp_write_cb);

return;
}

void on_tcp_connect(uv_connect_t *connect, int status) {
lw_state_t *state = (lw_state_t *)connect->data;

if (status < 0) {
zlogf(ZLOG_INFO_LOG_MSG, "Connect failed\n");
return;
}
connect->handle->data = state;
status = uv_read_start(connect->handle, alloc_tcp_buffer, on_tcp_read);

zlogf(ZLOG_INFO_LOG_MSG, "TCP Connected\n");
start_helium_client(state);
}

void start_tcp_client(lw_state_t *state) {
state->tcp_connect.data = state;
int res = uv_tcp_connect(&state->tcp_connect, &state->tcp_client, &state->send_addr, on_tcp_connect);
LW_CHECK_WITH_MSG(res == 0, "Unable to connect on tcp socket");

return;
}
29 changes: 29 additions & 0 deletions src/tcp/tcp_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* *
* Lightway Laser
* Copyright (C) 2021 Express VPN International Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#ifndef LW_UDP_CLIENT_H
#define LW_UDP_CLIENT_H

#include <lw.h>

void configure_tcp_client(lw_config_t *config, lw_state_t *state);

void start_tcp_client(lw_state_t *state);

#endif // LW_UDP_SERVER_H
Loading

0 comments on commit 713781b

Please sign in to comment.