Skip to content

Commit 4e05d83

Browse files
committed
HevSocks5Server: Refine the code.
1 parent 4502027 commit 4e05d83

7 files changed

+311
-305
lines changed

src/hev-main.c

+2-37
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@
22
============================================================================
33
Name : hev-main.c
44
Author : Heiher <[email protected]>
5-
Copyright : Copyright (c) 2017 - 2023 hev
5+
Copyright : Copyright (c) 2017 - 2024 hev
66
Description : Main
77
============================================================================
88
*/
99

1010
#include <stdio.h>
11-
#include <stdlib.h>
12-
#include <unistd.h>
13-
#include <sys/resource.h>
1411

1512
#include <hev-task.h>
1613
#include <hev-socks5-misc.h>
1714
#include <hev-socks5-logger.h>
1815

16+
#include "hev-misc.h"
1917
#include "hev-config.h"
2018
#include "hev-config-const.h"
2119
#include "hev-logger.h"
@@ -31,39 +29,6 @@ show_help (const char *self_path)
3129
MICRO_VERSION, COMMIT_ID);
3230
}
3331

34-
static void
35-
run_as_daemon (const char *pid_file)
36-
{
37-
FILE *fp;
38-
39-
fp = fopen (pid_file, "w+");
40-
if (!fp) {
41-
LOG_E ("open pid file %s", pid_file);
42-
return;
43-
}
44-
45-
#pragma GCC diagnostic push
46-
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
47-
if (daemon (0, 0)) {
48-
/* ignore return value */
49-
}
50-
#pragma GCC diagnostic pop
51-
52-
fprintf (fp, "%u\n", getpid ());
53-
fclose (fp);
54-
}
55-
56-
static int
57-
set_limit_nofile (int limit_nofile)
58-
{
59-
struct rlimit limit = {
60-
.rlim_cur = limit_nofile,
61-
.rlim_max = limit_nofile,
62-
};
63-
64-
return setrlimit (RLIMIT_NOFILE, &limit);
65-
}
66-
6732
int
6833
main (int argc, char *argv[])
6934
{

src/hev-socks5-proxy.c

+97-106
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
============================================================================
33
Name : hev-socks5-proxy.c
44
Author : Heiher <[email protected]>
5-
Copyright : Copyright (c) 2017 - 2022 hev
5+
Copyright : Copyright (c) 2017 - 2024 hev
66
Description : Socks5 Proxy
77
============================================================================
88
*/
@@ -35,7 +35,93 @@ static pthread_t *work_threads;
3535
static HevSocketFactory *factory;
3636
static HevSocks5Worker **worker_list;
3737

38-
static void hev_socks5_proxy_load (void);
38+
static void
39+
hev_socks5_proxy_load_file (HevSocks5Authenticator *auth, const char *file)
40+
{
41+
char *line = NULL;
42+
size_t len = 0;
43+
ssize_t nread;
44+
FILE *fp;
45+
46+
fp = fopen (file, "r");
47+
if (!fp) {
48+
hev_object_unref (HEV_OBJECT (auth));
49+
return;
50+
}
51+
52+
while ((nread = getline (&line, &len, fp)) != -1) {
53+
HevSocks5UserMark *user;
54+
unsigned int nlen;
55+
unsigned int plen;
56+
char name[256];
57+
char pass[256];
58+
long mark = 0;
59+
int res;
60+
61+
res = sscanf (line, "%255s %255s %lx\n", name, pass, &mark);
62+
if (res < 2) {
63+
LOG_E ("socks5 proxy user/pass format");
64+
continue;
65+
}
66+
67+
nlen = strlen (name);
68+
plen = strlen (pass);
69+
user = hev_socks5_user_mark_new (name, nlen, pass, plen, mark);
70+
hev_object_set_atomic (HEV_OBJECT (user), 1);
71+
res = hev_socks5_authenticator_add (auth, HEV_SOCKS5_USER (user));
72+
if (res < 0) {
73+
LOG_E ("socks5 proxy user conflict");
74+
hev_object_unref (HEV_OBJECT (user));
75+
}
76+
}
77+
78+
free (line);
79+
fclose (fp);
80+
}
81+
82+
static void
83+
hev_socks5_proxy_load (void)
84+
{
85+
HevSocks5Authenticator *auth;
86+
const char *file, *name, *pass;
87+
int i;
88+
89+
LOG_D ("socks5 proxy load");
90+
91+
file = hev_config_get_auth_file ();
92+
name = hev_config_get_auth_username ();
93+
pass = hev_config_get_auth_password ();
94+
95+
if (!file && !name && !pass)
96+
return;
97+
98+
auth = hev_socks5_authenticator_new ();
99+
if (!auth)
100+
return;
101+
102+
hev_object_set_atomic (HEV_OBJECT (auth), 1);
103+
104+
if (file) {
105+
hev_socks5_proxy_load_file (auth, file);
106+
} else {
107+
HevSocks5UserMark *user;
108+
109+
user = hev_socks5_user_mark_new (name, strlen (name), pass,
110+
strlen (pass), 0);
111+
hev_socks5_authenticator_add (auth, HEV_SOCKS5_USER (user));
112+
hev_object_set_atomic (HEV_OBJECT (user), 1);
113+
}
114+
115+
for (i = 0; i < workers; i++) {
116+
HevSocks5Worker *worker;
117+
118+
worker = worker_list[i];
119+
hev_socks5_worker_set_auth (worker, auth);
120+
hev_socks5_worker_reload (worker);
121+
}
122+
123+
hev_object_unref (HEV_OBJECT (auth));
124+
}
39125

40126
static void
41127
sigint_handler (int signum)
@@ -68,54 +154,41 @@ hev_socks5_proxy_init (void)
68154
goto exit;
69155
}
70156

71-
if (signal (SIGPIPE, SIG_IGN) == SIG_ERR) {
72-
LOG_E ("socks5 proxy sigpipe");
73-
goto free;
74-
}
75-
76-
if (signal (SIGINT, sigint_handler) == SIG_ERR) {
77-
LOG_E ("socks5 proxy sigint");
78-
goto free;
79-
}
80-
81-
if (signal (SIGUSR1, sigint_handler) == SIG_ERR) {
82-
LOG_E ("socks5 proxy sigusr1");
83-
goto free;
84-
}
85-
86157
task = hev_task_new (-1);
87158
if (!task) {
88159
LOG_E ("socks5 proxy task");
89-
goto free;
160+
goto exit;
90161
}
91162

92163
workers = hev_config_get_workers ();
93-
94164
work_threads = hev_malloc0 (sizeof (pthread_t) * workers);
95165
if (!work_threads) {
96166
LOG_E ("socks5 proxy work threads");
97-
goto free;
167+
goto exit;
98168
}
99169

100170
worker_list = hev_malloc0 (sizeof (HevSocks5Worker *) * workers);
101171
if (!worker_list) {
102172
LOG_E ("socks5 proxy worker list");
103-
goto free;
173+
goto exit;
104174
}
105175

106176
factory = hev_socket_factory_new (hev_config_get_listen_address (),
107177
hev_config_get_listen_port (),
108178
hev_config_get_listen_ipv6_only ());
109179
if (!factory) {
110180
LOG_E ("socks5 proxy socket factory");
111-
goto free;
181+
goto exit;
112182
}
113183

184+
signal (SIGPIPE, SIG_IGN);
185+
signal (SIGINT, sigint_handler);
186+
signal (SIGUSR1, sigint_handler);
187+
114188
return 0;
115189

116-
free:
117-
hev_socks5_proxy_fini ();
118190
exit:
191+
hev_socks5_proxy_fini ();
119192
return -1;
120193
}
121194

@@ -238,85 +311,3 @@ hev_socks5_proxy_run (void)
238311
worker_list[0] = NULL;
239312
}
240313
}
241-
242-
static void
243-
hev_socks5_proxy_load (void)
244-
{
245-
HevSocks5Authenticator *auth;
246-
const char *file, *name, *pass;
247-
int i;
248-
249-
LOG_D ("socks5 proxy load");
250-
251-
file = hev_config_get_auth_file ();
252-
name = hev_config_get_auth_username ();
253-
pass = hev_config_get_auth_password ();
254-
255-
if (!file && !name && !pass)
256-
return;
257-
258-
auth = hev_socks5_authenticator_new ();
259-
if (!auth)
260-
return;
261-
262-
hev_object_set_atomic (HEV_OBJECT (auth), 1);
263-
264-
if (!file) {
265-
HevSocks5UserMark *user;
266-
267-
user = hev_socks5_user_mark_new (name, strlen (name), pass,
268-
strlen (pass), 0);
269-
hev_socks5_authenticator_add (auth, HEV_SOCKS5_USER (user));
270-
hev_object_set_atomic (HEV_OBJECT (user), 1);
271-
} else {
272-
char *line = NULL;
273-
size_t len = 0;
274-
ssize_t nread;
275-
FILE *fp;
276-
277-
fp = fopen (file, "r");
278-
if (!fp) {
279-
hev_object_unref (HEV_OBJECT (auth));
280-
return;
281-
}
282-
283-
while ((nread = getline (&line, &len, fp)) != -1) {
284-
HevSocks5UserMark *user;
285-
unsigned int nlen;
286-
unsigned int plen;
287-
char name[256];
288-
char pass[256];
289-
long mark = 0;
290-
int res;
291-
292-
res = sscanf (line, "%255s %255s %lx\n", name, pass, &mark);
293-
if (res < 2) {
294-
LOG_E ("socks5 proxy user/pass format");
295-
continue;
296-
}
297-
298-
nlen = strlen (name);
299-
plen = strlen (pass);
300-
user = hev_socks5_user_mark_new (name, nlen, pass, plen, mark);
301-
hev_object_set_atomic (HEV_OBJECT (user), 1);
302-
res = hev_socks5_authenticator_add (auth, HEV_SOCKS5_USER (user));
303-
if (res < 0) {
304-
LOG_E ("socks5 proxy user conflict");
305-
hev_object_unref (HEV_OBJECT (user));
306-
}
307-
}
308-
309-
free (line);
310-
fclose (fp);
311-
}
312-
313-
for (i = 0; i < workers; i++) {
314-
HevSocks5Worker *worker;
315-
316-
worker = worker_list[i];
317-
hev_socks5_worker_set_auth (worker, auth);
318-
hev_socks5_worker_reload (worker);
319-
}
320-
321-
hev_object_unref (HEV_OBJECT (auth));
322-
}

src/hev-socks5-session.c

+5-31
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22
============================================================================
33
Name : hev-socks5-session.c
44
Author : Heiher <[email protected]>
5-
Copyright : Copyright (c) 2017 - 2021 hev
5+
Copyright : Copyright (c) 2017 - 2024 hev
66
Description : Socks5 Session
77
============================================================================
88
*/
99

1010
#include <string.h>
11-
#include <net/if.h>
12-
#include <arpa/inet.h>
13-
#include <netinet/in.h>
14-
#include <sys/socket.h>
1511

1612
#include <hev-memory-allocator.h>
1713

@@ -55,8 +51,10 @@ hev_socks5_session_terminate (HevSocks5Session *self)
5551
static int
5652
hev_socks5_session_bind (HevSocks5 *self, int fd, const struct sockaddr *dest)
5753
{
54+
HevSocks5Server *srv = HEV_SOCKS5_SERVER (self);
5855
const char *saddr;
5956
const char *iface;
57+
int res;
6058

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

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

6664
if (saddr) {
6765
struct sockaddr_in6 addr;
68-
int res;
6966

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

7976
if (iface) {
80-
int res = 0;
81-
#if defined(__linux__)
82-
struct ifreq ifr = { 0 };
83-
84-
strncpy (ifr.ifr_name, iface, sizeof (ifr.ifr_name) - 1);
85-
res = setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof (ifr));
86-
#elif defined(__APPLE__) || defined(__MACH__)
87-
int i;
88-
89-
i = if_nametoindex (iface);
90-
if (i == 0) {
91-
return -1;
92-
}
93-
94-
res = setsockopt (fd, IPPROTO_IPV6, IPV6_BOUND_IF, &i, sizeof (i));
95-
#endif
77+
res = set_sock_bind (fd, iface);
9678
if (res < 0)
9779
return -1;
9880
}
9981

100-
#if defined(__linux__)
101-
HevSocks5Server *srv = HEV_SOCKS5_SERVER (self);
102-
10382
if (srv->user) {
10483
HevSocks5UserMark *user = HEV_SOCKS5_USER_MARK (srv->user);
10584

10685
if (user->mark) {
107-
int mark;
108-
int res;
109-
110-
mark = user->mark;
111-
res = setsockopt (fd, SOL_SOCKET, SO_MARK, &mark, sizeof (mark));
86+
res = set_sock_mark (fd, user->mark);
11287
if (res < 0)
11388
return -1;
11489
}
11590
}
116-
#endif
11791

11892
return 0;
11993
}

0 commit comments

Comments
 (0)