Skip to content

Commit

Permalink
HevSocks5Server: Refine the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
heiher committed Jan 1, 2024
1 parent 4502027 commit 4e05d83
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 305 deletions.
39 changes: 2 additions & 37 deletions src/hev-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@
============================================================================
Name : hev-main.c
Author : Heiher <[email protected]>
Copyright : Copyright (c) 2017 - 2023 hev
Copyright : Copyright (c) 2017 - 2024 hev
Description : Main
============================================================================
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/resource.h>

#include <hev-task.h>
#include <hev-socks5-misc.h>
#include <hev-socks5-logger.h>

#include "hev-misc.h"
#include "hev-config.h"
#include "hev-config-const.h"
#include "hev-logger.h"
Expand All @@ -31,39 +29,6 @@ show_help (const char *self_path)
MICRO_VERSION, COMMIT_ID);
}

static void
run_as_daemon (const char *pid_file)
{
FILE *fp;

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

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
if (daemon (0, 0)) {
/* ignore return value */
}
#pragma GCC diagnostic pop

fprintf (fp, "%u\n", getpid ());
fclose (fp);
}

static int
set_limit_nofile (int limit_nofile)
{
struct rlimit limit = {
.rlim_cur = limit_nofile,
.rlim_max = limit_nofile,
};

return setrlimit (RLIMIT_NOFILE, &limit);
}

int
main (int argc, char *argv[])
{
Expand Down
203 changes: 97 additions & 106 deletions src/hev-socks5-proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
============================================================================
Name : hev-socks5-proxy.c
Author : Heiher <[email protected]>
Copyright : Copyright (c) 2017 - 2022 hev
Copyright : Copyright (c) 2017 - 2024 hev
Description : Socks5 Proxy
============================================================================
*/
Expand Down Expand Up @@ -35,7 +35,93 @@ static pthread_t *work_threads;
static HevSocketFactory *factory;
static HevSocks5Worker **worker_list;

static void hev_socks5_proxy_load (void);
static void
hev_socks5_proxy_load_file (HevSocks5Authenticator *auth, const char *file)
{
char *line = NULL;
size_t len = 0;
ssize_t nread;
FILE *fp;

fp = fopen (file, "r");
if (!fp) {
hev_object_unref (HEV_OBJECT (auth));
return;
}

while ((nread = getline (&line, &len, fp)) != -1) {
HevSocks5UserMark *user;
unsigned int nlen;
unsigned int plen;
char name[256];
char pass[256];
long mark = 0;
int res;

res = sscanf (line, "%255s %255s %lx\n", name, pass, &mark);
if (res < 2) {
LOG_E ("socks5 proxy user/pass format");
continue;
}

nlen = strlen (name);
plen = strlen (pass);
user = hev_socks5_user_mark_new (name, nlen, pass, plen, mark);
hev_object_set_atomic (HEV_OBJECT (user), 1);
res = hev_socks5_authenticator_add (auth, HEV_SOCKS5_USER (user));
if (res < 0) {
LOG_E ("socks5 proxy user conflict");
hev_object_unref (HEV_OBJECT (user));
}
}

free (line);
fclose (fp);
}

static void
hev_socks5_proxy_load (void)
{
HevSocks5Authenticator *auth;
const char *file, *name, *pass;
int i;

LOG_D ("socks5 proxy load");

file = hev_config_get_auth_file ();
name = hev_config_get_auth_username ();
pass = hev_config_get_auth_password ();

if (!file && !name && !pass)
return;

auth = hev_socks5_authenticator_new ();
if (!auth)
return;

hev_object_set_atomic (HEV_OBJECT (auth), 1);

if (file) {
hev_socks5_proxy_load_file (auth, file);
} else {
HevSocks5UserMark *user;

user = hev_socks5_user_mark_new (name, strlen (name), pass,
strlen (pass), 0);
hev_socks5_authenticator_add (auth, HEV_SOCKS5_USER (user));
hev_object_set_atomic (HEV_OBJECT (user), 1);
}

for (i = 0; i < workers; i++) {
HevSocks5Worker *worker;

worker = worker_list[i];
hev_socks5_worker_set_auth (worker, auth);
hev_socks5_worker_reload (worker);
}

hev_object_unref (HEV_OBJECT (auth));
}

static void
sigint_handler (int signum)
Expand Down Expand Up @@ -68,54 +154,41 @@ hev_socks5_proxy_init (void)
goto exit;
}

if (signal (SIGPIPE, SIG_IGN) == SIG_ERR) {
LOG_E ("socks5 proxy sigpipe");
goto free;
}

if (signal (SIGINT, sigint_handler) == SIG_ERR) {
LOG_E ("socks5 proxy sigint");
goto free;
}

if (signal (SIGUSR1, sigint_handler) == SIG_ERR) {
LOG_E ("socks5 proxy sigusr1");
goto free;
}

task = hev_task_new (-1);
if (!task) {
LOG_E ("socks5 proxy task");
goto free;
goto exit;
}

workers = hev_config_get_workers ();

work_threads = hev_malloc0 (sizeof (pthread_t) * workers);
if (!work_threads) {
LOG_E ("socks5 proxy work threads");
goto free;
goto exit;
}

worker_list = hev_malloc0 (sizeof (HevSocks5Worker *) * workers);
if (!worker_list) {
LOG_E ("socks5 proxy worker list");
goto free;
goto exit;
}

factory = hev_socket_factory_new (hev_config_get_listen_address (),
hev_config_get_listen_port (),
hev_config_get_listen_ipv6_only ());
if (!factory) {
LOG_E ("socks5 proxy socket factory");
goto free;
goto exit;
}

signal (SIGPIPE, SIG_IGN);
signal (SIGINT, sigint_handler);
signal (SIGUSR1, sigint_handler);

return 0;

free:
hev_socks5_proxy_fini ();
exit:
hev_socks5_proxy_fini ();
return -1;
}

Expand Down Expand Up @@ -238,85 +311,3 @@ hev_socks5_proxy_run (void)
worker_list[0] = NULL;
}
}

static void
hev_socks5_proxy_load (void)
{
HevSocks5Authenticator *auth;
const char *file, *name, *pass;
int i;

LOG_D ("socks5 proxy load");

file = hev_config_get_auth_file ();
name = hev_config_get_auth_username ();
pass = hev_config_get_auth_password ();

if (!file && !name && !pass)
return;

auth = hev_socks5_authenticator_new ();
if (!auth)
return;

hev_object_set_atomic (HEV_OBJECT (auth), 1);

if (!file) {
HevSocks5UserMark *user;

user = hev_socks5_user_mark_new (name, strlen (name), pass,
strlen (pass), 0);
hev_socks5_authenticator_add (auth, HEV_SOCKS5_USER (user));
hev_object_set_atomic (HEV_OBJECT (user), 1);
} else {
char *line = NULL;
size_t len = 0;
ssize_t nread;
FILE *fp;

fp = fopen (file, "r");
if (!fp) {
hev_object_unref (HEV_OBJECT (auth));
return;
}

while ((nread = getline (&line, &len, fp)) != -1) {
HevSocks5UserMark *user;
unsigned int nlen;
unsigned int plen;
char name[256];
char pass[256];
long mark = 0;
int res;

res = sscanf (line, "%255s %255s %lx\n", name, pass, &mark);
if (res < 2) {
LOG_E ("socks5 proxy user/pass format");
continue;
}

nlen = strlen (name);
plen = strlen (pass);
user = hev_socks5_user_mark_new (name, nlen, pass, plen, mark);
hev_object_set_atomic (HEV_OBJECT (user), 1);
res = hev_socks5_authenticator_add (auth, HEV_SOCKS5_USER (user));
if (res < 0) {
LOG_E ("socks5 proxy user conflict");
hev_object_unref (HEV_OBJECT (user));
}
}

free (line);
fclose (fp);
}

for (i = 0; i < workers; i++) {
HevSocks5Worker *worker;

worker = worker_list[i];
hev_socks5_worker_set_auth (worker, auth);
hev_socks5_worker_reload (worker);
}

hev_object_unref (HEV_OBJECT (auth));
}
36 changes: 5 additions & 31 deletions src/hev-socks5-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@
============================================================================
Name : hev-socks5-session.c
Author : Heiher <[email protected]>
Copyright : Copyright (c) 2017 - 2021 hev
Copyright : Copyright (c) 2017 - 2024 hev
Description : Socks5 Session
============================================================================
*/

#include <string.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>

#include <hev-memory-allocator.h>

Expand Down Expand Up @@ -55,8 +51,10 @@ hev_socks5_session_terminate (HevSocks5Session *self)
static int
hev_socks5_session_bind (HevSocks5 *self, int fd, const struct sockaddr *dest)
{
HevSocks5Server *srv = HEV_SOCKS5_SERVER (self);
const char *saddr;
const char *iface;
int res;

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

Expand All @@ -65,7 +63,6 @@ hev_socks5_session_bind (HevSocks5 *self, int fd, const struct sockaddr *dest)

if (saddr) {
struct sockaddr_in6 addr;
int res;

res = hev_netaddr_resolve (&addr, saddr, NULL);
if (res < 0)
Expand All @@ -77,43 +74,20 @@ hev_socks5_session_bind (HevSocks5 *self, int fd, const struct sockaddr *dest)
}

if (iface) {
int res = 0;
#if defined(__linux__)
struct ifreq ifr = { 0 };

strncpy (ifr.ifr_name, iface, sizeof (ifr.ifr_name) - 1);
res = setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof (ifr));
#elif defined(__APPLE__) || defined(__MACH__)
int i;

i = if_nametoindex (iface);
if (i == 0) {
return -1;
}

res = setsockopt (fd, IPPROTO_IPV6, IPV6_BOUND_IF, &i, sizeof (i));
#endif
res = set_sock_bind (fd, iface);
if (res < 0)
return -1;
}

#if defined(__linux__)
HevSocks5Server *srv = HEV_SOCKS5_SERVER (self);

if (srv->user) {
HevSocks5UserMark *user = HEV_SOCKS5_USER_MARK (srv->user);

if (user->mark) {
int mark;
int res;

mark = user->mark;
res = setsockopt (fd, SOL_SOCKET, SO_MARK, &mark, sizeof (mark));
res = set_sock_mark (fd, user->mark);
if (res < 0)
return -1;
}
}
#endif

return 0;
}
Expand Down
Loading

0 comments on commit 4e05d83

Please sign in to comment.