Skip to content

Commit

Permalink
HevSocks5Server: Add support for set socket mark for user.
Browse files Browse the repository at this point in the history
  • Loading branch information
heiher committed Sep 16, 2023
1 parent e9918ba commit 83f446b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ main:
### Authentication file

```
<USERNAME> <SPACE> <PASSWORD> <LF>
<USERNAME> <SPACE> <PASSWORD> <SPACE> <MARK> <LF>
```

### Run
Expand Down
2 changes: 1 addition & 1 deletion conf/auth.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
tom pass
jerry pass
jerry pass 1a
19 changes: 11 additions & 8 deletions src/hev-socks5-proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "hev-logger.h"
#include "hev-socks5-worker.h"
#include "hev-socket-factory.h"
#include "hev-socks5-user-mark.h"

#include "hev-socks5-proxy.h"

Expand Down Expand Up @@ -261,10 +262,11 @@ hev_socks5_proxy_load (void)
hev_object_set_atomic (HEV_OBJECT (auth), 1);

if (!file) {
HevSocks5User *user;
HevSocks5UserMark *user;

user = hev_socks5_user_new (name, strlen (name), pass, strlen (pass));
hev_socks5_authenticator_add (auth, 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;
Expand All @@ -279,24 +281,25 @@ hev_socks5_proxy_load (void)
}

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

res = sscanf (line, "%255s %255s\n", name, pass);
if (res != 2) {
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_new (name, nlen, pass, plen);
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, user);
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));
Expand Down
18 changes: 18 additions & 0 deletions src/hev-socks5-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "hev-misc.h"
#include "hev-logger.h"
#include "hev-config.h"
#include "hev-socks5-user-mark.h"

#include "hev-socks5-session.h"

Expand Down Expand Up @@ -54,6 +55,7 @@ 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;

Expand Down Expand Up @@ -96,6 +98,22 @@ hev_socks5_session_bind (HevSocks5 *self, int fd, const struct sockaddr *dest)
return -1;
}

#if defined(__linux__)
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));
if (res < 0)
return -1;
}
}
#endif

return 0;
}

Expand Down

0 comments on commit 83f446b

Please sign in to comment.