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

Add support for udp listen port and address #46

Merged
merged 2 commits into from
Apr 9, 2024
Merged
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ main:
port: 1080
# Listen address (ipv4|ipv6)
listen-address: '::'
# UDP listen port
udp-port: 1080
# UDP listen address (ipv4|ipv6)
# udp-listen-address: '::1'
# Listen ipv6 only
listen-ipv6-only: false
# Bind source address (ipv4|ipv6)
Expand Down
4 changes: 4 additions & 0 deletions conf/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ main:
port: 1080
# Listen address (ipv4|ipv6)
listen-address: '::'
# UDP listen port
udp-port: 1080
# UDP listen address (ipv4|ipv6)
# udp-listen-address: '::1'
# Listen ipv6 only
listen-ipv6-only: false
# Bind source address (ipv4|ipv6)
Expand Down
2 changes: 1 addition & 1 deletion src/core
31 changes: 31 additions & 0 deletions src/hev-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ static unsigned int workers;
static int listen_ipv6_only;
static char listen_address[256];
static char listen_port[8];
static char udp_listen_address[256];
static char udp_listen_port[8];
static char bind_address[256];
static char bind_interface[256];
static char auth_file[1024];
Expand All @@ -42,6 +44,8 @@ hev_config_parse_main (yaml_document_t *doc, yaml_node_t *base)
yaml_node_pair_t *pair;
const char *addr = NULL;
const char *port = NULL;
const char *udp_addr = NULL;
const char *udp_port = NULL;
const char *bind_saddr = NULL;
const char *bind_iface = NULL;
const char *addr_type = NULL;
Expand Down Expand Up @@ -73,6 +77,10 @@ hev_config_parse_main (yaml_document_t *doc, yaml_node_t *base)
port = value;
else if (0 == strcmp (key, "listen-address"))
addr = value;
else if (0 == strcmp (key, "udp-port"))
udp_port = value;
else if (0 == strcmp (key, "udp-listen-address"))
udp_addr = value;
else if (0 == strcmp (key, "listen-ipv6-only"))
listen_ipv6_only = (0 == strcasecmp (value, "true")) ? 1 : 0;
else if (0 == strcmp (key, "bind-address"))
Expand Down Expand Up @@ -101,6 +109,11 @@ hev_config_parse_main (yaml_document_t *doc, yaml_node_t *base)
strncpy (listen_port, port, 8 - 1);
strncpy (listen_address, addr, 256 - 1);

if (udp_port)
strncpy (udp_listen_port, udp_port, 8 - 1);
if (udp_addr)
strncpy (udp_listen_address, udp_addr, 256 - 1);

if (bind_saddr)
strncpy (bind_address, bind_saddr, 256 - 1);

Expand Down Expand Up @@ -319,6 +332,24 @@ hev_config_get_listen_port (void)
return listen_port;
}

const char *
hev_config_get_udp_listen_address (void)
{
if ('\0' == udp_listen_address[0])
return NULL;

return udp_listen_address;
}

const char *
hev_config_get_udp_listen_port (void)
{
if ('\0' == udp_listen_port[0])
return NULL;

return udp_listen_port;
}

int
hev_config_get_listen_ipv6_only (void)
{
Expand Down
2 changes: 2 additions & 0 deletions src/hev-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ unsigned int hev_config_get_workers (void);

const char *hev_config_get_listen_address (void);
const char *hev_config_get_listen_port (void);
const char *hev_config_get_udp_listen_address (void);
const char *hev_config_get_udp_listen_port (void);
int hev_config_get_listen_ipv6_only (void);

const char *hev_config_get_bind_address (void);
Expand Down
42 changes: 42 additions & 0 deletions src/hev-socks5-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
============================================================================
*/

#include <stdlib.h>
#include <string.h>

#include <hev-memory-allocator.h>
Expand Down Expand Up @@ -92,6 +93,43 @@ hev_socks5_session_bind (HevSocks5 *self, int fd, const struct sockaddr *dest)
return 0;
}

static int
hev_socks5_session_udp_bind (HevSocks5Server *self, int sock)
{
struct sockaddr_in6 addr;
const char *saddr;
const char *sport;
socklen_t alen;
int res;

LOG_D ("%p socks5 session udp bind", self);

alen = sizeof (struct sockaddr_in6);
saddr = hev_config_get_udp_listen_address ();
sport = hev_config_get_udp_listen_port ();

if (saddr) {
res = hev_netaddr_resolve (&addr, saddr, NULL);
if (res < 0)
return -1;
} else {
int fd;

fd = HEV_SOCKS5 (self)->fd;
res = getsockname (fd, (struct sockaddr *)&addr, &alen);
if (res < 0)
return -1;
}

addr.sin6_port = sport ? htons (strtoul (sport, NULL, 10)) : 0;

res = bind (sock, (struct sockaddr *)&addr, alen);
if (res < 0)
return -1;

return 0;
}

int
hev_socks5_session_construct (HevSocks5Session *self, int fd)
{
Expand Down Expand Up @@ -137,6 +175,7 @@ hev_socks5_session_class (void)
HevObjectClass *okptr = HEV_OBJECT_CLASS (kptr);

if (!okptr->name) {
HevSocks5ServerClass *sskptr;
HevSocks5Class *skptr;

memcpy (kptr, HEV_SOCKS5_SERVER_TYPE, sizeof (HevSocks5ServerClass));
Expand All @@ -146,6 +185,9 @@ hev_socks5_session_class (void)

skptr = HEV_SOCKS5_CLASS (kptr);
skptr->binder = hev_socks5_session_bind;

sskptr = HEV_SOCKS5_SERVER_CLASS (kptr);
sskptr->binder = hev_socks5_session_udp_bind;
}

return okptr;
Expand Down
Loading