Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC][WIP] SOCKS5 proxy server implementation #12180

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions include/net/socks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2018 Antmicro Ltd
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef ZEPHYR_INCLUDE_NET_SOCKS_H_
#define ZEPHYR_INCLUDE_NET_SOCKS_H_

#include <net/net_app.h>

enum socks_connection_state {
SOCKS5_STATE_IDLE,
SOCKS5_STATE_NEGOTIATING,
SOCKS5_STATE_NEGOTIATED,
SOCKS5_STATE_CONNECTED,
SOCKS5_STATE_ERROR,
};

struct socks_client {
struct net_app_ctx app_ctx;
struct net_app_ctx *socks_app;

struct sockaddr_in incoming;
struct sockaddr_in outgoing;

struct net_context *in_ctx;

enum socks_connection_state state;
};

struct socks_ctx {
struct net_app_ctx app_ctx;
struct sockaddr local;

struct socks_client clients[3] ; //CONFIG_NET_APP_SERVER_NUM_CONN];

/** Is this context setup or not */
u8_t is_init : 1;
};

int socks_init(struct socks_ctx *ctx, u16_t port);

/* XXX */
#define SOCKS5_STACK_SIZE 2048
#define SOCKS5_MAX_CONNECTIONS 1

#define SOCKS5_BSIZE 2048

struct socks5_connection {
int fd;
//int port;
int len;

struct sockaddr_in addr;
struct k_thread thread;
K_THREAD_STACK_MEMBER(stack, SOCKS5_STACK_SIZE);
};

struct socks5_proxy {
int id;
struct socks5_connection client;
struct socks5_connection destination;

enum socks_connection_state state;

struct k_sem exit_sem;
struct k_sem *busy_sem;

char buffer_rcv[SOCKS5_BSIZE];
char buffer_snd[SOCKS5_BSIZE];
char buffer_dst[SOCKS5_BSIZE];
};

struct socks5_ctx {
int fd;

struct sockaddr_in addr;
struct k_sem busy_sem;

struct socks5_proxy proxy[SOCKS5_MAX_CONNECTIONS];
};
/* XXX */

#endif /* ZEPHYR_INCLUDE_NET_SOCKS_H_ */
16 changes: 16 additions & 0 deletions samples/net/socks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.8.2)
macro(set_conf_file)
if(EXISTS ${APPLICATION_SOURCE_DIR}/prj_${BOARD}.conf)
set(CONF_FILE "${APPLICATION_SOURCE_DIR}/prj_${BOARD}.conf")
elseif(EXISTS ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf)
set(CONF_FILE
"prj.conf ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf")
else()
set(CONF_FILE "prj.conf")
endif()
endmacro()

include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(socks)

target_sources(app PRIVATE src/main.c)
39 changes: 39 additions & 0 deletions samples/net/socks/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
CONFIG_NETWORKING=y
CONFIG_NET_LOG=y
CONFIG_LOG=y
CONFIG_NET_IPV6=y
CONFIG_NET_IPV4=y
CONFIG_NET_DHCPV4=n
CONFIG_NET_UDP=y
CONFIG_NET_TCP=y

CONFIG_NET_SHELL=y
CONFIG_NET_L2_ETHERNET=y

# CONFIG_NET_CONFIG_NEED_IPV6 is not set
# CONFIG_NET_IPV6 is not set

CONFIG_NET_CONFIG_NEED_IPV4=y

CONFIG_NET_SHELL=y
CONFIG_NET_TCP=y
CONFIG_NET_MAX_CONN=10
CONFIG_NET_MAX_CONTEXTS=20

CONFIG_SOCKS=y

CONFIG_NET_CONFIG_NEED_IPV4=y
CONFIG_NET_CONFIG_SETTINGS=y
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.168.0.150"
CONFIG_NET_CONFIG_MY_IPV4_GW="192.168.0.1"


# TO RECONSIDER
CONFIG_ETHERNET_LOG_LEVEL_INF=y
CONFIG_SYS_LOG=y
CONFIG_LOG_INPLACE_PROCESS=y
CONFIG_LOG_STRDUP_MAX_STRING=60
CONFIG_LOG_STRDUP_BUF_COUNT=9
CONFIG_NET_TCP_LOG_LEVEL_WRN=y
CONFIG_NET_TCP_BACKLOG_SIZE=5
# CONFIG_UART_INTERRUPT_DRIVEN is not set
21 changes: 21 additions & 0 deletions samples/net/socks/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2018 Antmicro Ltd
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdio.h>
#include <net/socks.h>

#define SOCKS_PROXY_SERVER_PORT 1080

static struct socks_ctx socks_ctx;

int main(void)
{
printf("Starting SOCKS5 proxy server sample");

socks_init(&socks_ctx, SOCKS_PROXY_SERVER_PORT);

return 0;
}
1 change: 1 addition & 0 deletions subsys/net/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_subdirectory_if_kconfig(coap)
add_subdirectory_if_kconfig(lwm2m)
add_subdirectory_if_kconfig(socks)
add_subdirectory_if_kconfig(sntp)
add_subdirectory_ifdef(CONFIG_DNS_RESOLVER dns)
add_subdirectory_ifdef(CONFIG_MQTT_LEGACY_LIB mqtt)
Expand Down
2 changes: 2 additions & 0 deletions subsys/net/lib/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ source "subsys/net/lib/http/Kconfig"

source "subsys/net/lib/lwm2m/Kconfig"

source "subsys/net/lib/socks/Kconfig"

source "subsys/net/lib/sntp/Kconfig"

source "subsys/net/lib/websocket/Kconfig"
Expand Down
3 changes: 3 additions & 0 deletions subsys/net/lib/socks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
zephyr_library()

zephyr_library_sources_if_kconfig(socks.c)
22 changes: 22 additions & 0 deletions subsys/net/lib/socks/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#
# Copyright (c) 2018 Antmicro Ltd
#
# SPDX-License-Identifier: Apache-2.0
#

menuconfig SOCKS
bool "SOCKS proxy"
select NET_SOCKETS
select NET_SOCKETS_POSIX_NAMES
help
Enable SOCKS proxy server

if SOCKS

module = SOCKS
module-dep = NET_LOG
module-str = Log level for SOCKS proxy
module-help = Enable debug messages for SOCKS5 protocol
source "subsys/net/Kconfig.template.log_config.net"

endif # SOCKS
Loading