Skip to content

Commit

Permalink
HevSocks5Tunnel: Use socks5 core.
Browse files Browse the repository at this point in the history
  • Loading branch information
heiher committed May 19, 2021
1 parent a3d5e3b commit b684976
Show file tree
Hide file tree
Showing 18 changed files with 1,027 additions and 1,076 deletions.
4 changes: 3 additions & 1 deletion Android.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2018 The Android Open Source Project
# Copyright (C) 2021 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,6 +33,8 @@ include $(LOCAL_PATH)/build.mk
LOCAL_MODULE := hev-socks5-tunnel
LOCAL_SRC_FILES := $(patsubst $(SRCDIR)/%,src/%,$(SRCFILES))
LOCAL_C_INCLUDES := \
$(LOCAL_PATH)/src/misc \
$(LOCAL_PATH)/src/core/include \
$(LOCAL_PATH)/third-part/yaml/include \
$(LOCAL_PATH)/third-part/lwip/include \
$(LOCAL_PATH)/third-part/lwip/include/ports/unix \
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ PP=$(CROSS_PREFIX)cpp
CC=$(CROSS_PREFIX)gcc
STRIP=$(CROSS_PREFIX)strip
CCFLAGS=-O3 -pipe -Wall -Werror $(CFLAGS) \
-I$(SRCDIR)/misc \
-I$(SRCDIR)/core/include \
-I$(THIRDPARTDIR)/yaml/include \
-I$(THIRDPARTDIR)/lwip/include \
-I$(THIRDPARTDIR)/lwip/include/ports/unix \
Expand Down
4 changes: 2 additions & 2 deletions conf/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ socks5:
address: 127.0.0.1

#misc:
# null, stdout, stderr or file-path
# log-file: null
# stdout, stderr or file-path
# log-file: stderr
# debug, info, warn or error
# log-level: warn
# If present, run as a daemon with this pid file
Expand Down
11 changes: 10 additions & 1 deletion src/hev-config-const.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
============================================================================
Name : hev-config-const.h
Author : Heiher <[email protected]>
Copyright : Copyright (c) 2019 - 2021 Everyone.
Copyright : Copyright (c) 2019 - 2021 hev
Description : Config Constants
============================================================================
*/
Expand All @@ -14,4 +14,13 @@
#define MINOR_VERSION (2)
#define MICRO_VERSION (1)

static const int TCP_BUF_SIZE = 8192;
static const int UDP_BUF_SIZE = 1500;
static const int UDP_POOL_SIZE = 512;

static const int IO_TIMEOUT = 60000;
static const int CONNECT_TIMEOUT = 3000;

static const int TASK_STACK_SIZE = 20480;

#endif /* __HEV_CONFIG_CONST_H__ */
81 changes: 38 additions & 43 deletions src/hev-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
============================================================================
Name : hev-config.c
Author : Heiher <[email protected]>
Copyright : Copyright (c) 2019 - 2020 Everyone.
Copyright : Copyright (c) 2019 - 2021 hev
Description : Config
============================================================================
*/
Expand All @@ -11,8 +11,8 @@
#include <arpa/inet.h>
#include <yaml.h>

#include "hev-logger.h"
#include "hev-config.h"
#include "hev-config-const.h"

static char tun_name[64];
static unsigned int tun_mtu = 8192;
Expand All @@ -25,30 +25,13 @@ static char tun_ipv6_address[64];
static char tun_ipv6_gateway[64];
static unsigned int tun_ipv6_prefix;

static struct sockaddr_in6 socks5_address;
static char srv_address[256];
static char srv_port[8];

static char log_file[1024];
static char log_level[16];
static char pid_file[1024];
static int limit_nofile = -2;

static int
address_to_sockaddr (const char *address, unsigned short port,
struct sockaddr_in6 *addr)
{
__builtin_bzero (addr, sizeof (*addr));

addr->sin6_family = AF_INET6;
addr->sin6_port = htons (port);
if (inet_pton (AF_INET, address, &addr->sin6_addr.s6_addr[12]) == 1) {
((uint16_t *)&addr->sin6_addr)[5] = 0xffff;
} else {
if (inet_pton (AF_INET6, address, &addr->sin6_addr) != 1)
return -1;
}

return 0;
}
static int log_level = HEV_LOGGER_WARN;

static int
hev_config_parse_tunnel_ipv4 (yaml_document_t *doc, yaml_node_t *base)
Expand Down Expand Up @@ -186,8 +169,8 @@ static int
hev_config_parse_socks5 (yaml_document_t *doc, yaml_node_t *base)
{
yaml_node_pair_t *pair;
int port = 0;
const char *addr = NULL;
const char *port = NULL;

if (!base || YAML_MAPPING_NODE != base->type)
return -1;
Expand All @@ -211,7 +194,7 @@ hev_config_parse_socks5 (yaml_document_t *doc, yaml_node_t *base)
value = (const char *)node->data.scalar.value;

if (0 == strcmp (key, "port"))
port = strtoul (value, NULL, 10);
port = value;
else if (0 == strcmp (key, "address"))
addr = value;
}
Expand All @@ -226,7 +209,23 @@ hev_config_parse_socks5 (yaml_document_t *doc, yaml_node_t *base)
return -1;
}

return address_to_sockaddr (addr, port, &socks5_address);
strncpy (srv_address, addr, 256 - 1);
strncpy (srv_port, port, 8 - 1);

return 0;
}

static int
hev_config_parse_log_level (const char *value)
{
if (0 == strcmp (value, "debug"))
return HEV_LOGGER_DEBUG;
else if (0 == strcmp (value, "info"))
return HEV_LOGGER_INFO;
else if (0 == strcmp (value, "error"))
return HEV_LOGGER_ERROR;

return HEV_LOGGER_WARN;
}

static int
Expand Down Expand Up @@ -260,7 +259,7 @@ hev_config_parse_misc (yaml_document_t *doc, yaml_node_t *base)
else if (0 == strcmp (key, "log-file"))
strncpy (log_file, value, 1024 - 1);
else if (0 == strcmp (key, "log-level"))
strncpy (log_level, value, 16 - 1);
log_level = hev_config_parse_log_level (value);
else if (0 == strcmp (key, "limit-nofile"))
limit_nofile = strtol (value, NULL, 10);
}
Expand Down Expand Up @@ -410,11 +409,18 @@ hev_config_get_tunnel_ipv6_prefix (void)
return tun_ipv6_prefix;
}

struct sockaddr *
hev_config_get_socks5_address (socklen_t *addr_len)
const char *
hev_config_get_socks5_address (int *port)
{
*addr_len = sizeof (socks5_address);
return (struct sockaddr *)&socks5_address;
*port = strtoul (srv_port, NULL, 10);

return srv_address;
}

int
hev_config_get_misc_limit_nofile (void)
{
return limit_nofile;
}

const char *
Expand All @@ -426,28 +432,17 @@ hev_config_get_misc_pid_file (void)
return pid_file;
}

int
hev_config_get_misc_limit_nofile (void)
{
return limit_nofile;
}

const char *
hev_config_get_misc_log_file (void)
{
if (!log_file[0])
return NULL;
if (0 == strcmp (log_file, "null"))
return NULL;
return "stderr";

return log_file;
}

const char *
int
hev_config_get_misc_log_level (void)
{
if (!log_level[0])
return "warn";

return log_level;
}
10 changes: 4 additions & 6 deletions src/hev-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
============================================================================
Name : hev-config.h
Author : Heiher <[email protected]>
Copyright : Copyright (c) 2019 - 2020 Everyone.
Copyright : Copyright (c) 2019 - 2021 hev
Description : Config
============================================================================
*/
Expand All @@ -24,13 +24,11 @@ const char *hev_config_get_tunnel_ipv6_address (void);
const char *hev_config_get_tunnel_ipv6_gateway (void);
unsigned int hev_config_get_tunnel_ipv6_prefix (void);

struct sockaddr *hev_config_get_socks5_address (socklen_t *addr_len);

const char *hev_config_get_misc_pid_file (void);
const char *hev_config_get_socks5_address (int *port);

int hev_config_get_misc_limit_nofile (void);

const char *hev_config_get_misc_pid_file (void);
const char *hev_config_get_misc_log_file (void);
const char *hev_config_get_misc_log_level (void);
int hev_config_get_misc_log_level (void);

#endif /* __HEV_CONFIG_H__ */
41 changes: 29 additions & 12 deletions src/hev-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
============================================================================
Name : hev-main.c
Author : Heiher <[email protected]>
Copyright : Copyright (c) 2019 - 2020 Everyone.
Copyright : Copyright (c) 2019 - 2021 hev
Description : Main
============================================================================
*/
Expand All @@ -21,6 +21,7 @@
#include "hev-config.h"
#include "hev-config-const.h"
#include "hev-logger.h"
#include "hev-socks5-logger.h"
#include "hev-socks5-tunnel.h"

#include "hev-main.h"
Expand All @@ -39,7 +40,7 @@ run_as_daemon (const char *pid_file)

fp = fopen (pid_file, "w+");
if (!fp) {
LOG_E ("Open pid file %s failed!", pid_file);
LOG_E ("open pid file %s", pid_file);
return;
}

Expand Down Expand Up @@ -94,32 +95,47 @@ int
main (int argc, char *argv[])
{
const char *pid_file;
int tun_fd = -1, limit_nofile;
const char *log_file;
int tun_fd = -1;
int log_level;
int nofile;
int res;

if (2 > argc) {
if (argc < 2) {
show_help (argv[0]);
return -1;
}

if (2 < argc)
if (argc > 2)
tun_fd = strtol (argv[2], NULL, 10);

if (0 > hev_config_init (argv[1]))
res = hev_config_init (argv[1]);
if (res < 0)
return -2;

if (0 > hev_logger_init ())
log_file = hev_config_get_misc_log_file ();
log_level = hev_config_get_misc_log_level ();

res = hev_logger_init (log_level, log_file);
if (res < 0)
return -3;

limit_nofile = hev_config_get_misc_limit_nofile ();
if (0 > set_limit_nofile (limit_nofile)) {
LOG_E ("Set limit nofile failed!");
res = hev_socks5_logger_init (log_level, log_file);
if (res < 0)
return -4;

nofile = hev_config_get_misc_limit_nofile ();
res = set_limit_nofile (nofile);
if (res < 0) {
LOG_E ("set limit nofile");
return -5;
}

lwip_init ();

if (0 > hev_socks5_tunnel_init (tun_fd))
return -5;
res = hev_socks5_tunnel_init (tun_fd);
if (res < 0)
return -6;

pid_file = hev_config_get_misc_pid_file ();
if (pid_file)
Expand All @@ -128,6 +144,7 @@ main (int argc, char *argv[])
hev_socks5_tunnel_run ();

hev_socks5_tunnel_fini ();
hev_socks5_logger_fini ();
hev_logger_fini ();
hev_config_fini ();
lwip_fini ();
Expand Down
2 changes: 1 addition & 1 deletion src/hev-main.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
============================================================================
Name : hev-main.h
Author : Heiher <[email protected]>
Copyright : Copyright (c) 2019 - 2020 Everyone.
Copyright : Copyright (c) 2019 - 2021 hev
Description : Main
============================================================================
*/
Expand Down
Loading

0 comments on commit b684976

Please sign in to comment.