Skip to content

Commit 867dc0e

Browse files
author
Ken
committed
Merge branch 'release/0.2.0'
2 parents ba89c5b + 0b13c0d commit 867dc0e

27 files changed

Lines changed: 2156 additions & 175 deletions

3rd/libuv

Submodule libuv updated 105 files

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
v0.2.0 (2015-5-11)
2+
-----------
3+
* Feature: Support UDP relay
4+
5+
16
v0.1.0 (2015-4-03)
27
-----------
38
* The first public version.

Makefile

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55

66
MAJOR = 0
7-
MINOR = 1
7+
MINOR = 2
88
PATCH = 0
99
NAME = socksd
1010

@@ -32,8 +32,21 @@ export TOPDIR SRCTREE OBJTREE
3232

3333
#########################################################################
3434

35+
ifdef HOST
36+
CROSS_COMPILE = $(HOST)-
37+
endif
38+
39+
# for OpenWrt
3540
ifdef CROSS
3641
CROSS_COMPILE = $(CROSS)
42+
HOST = $(patsubst %-,%,$(CROSS_COMPILE))
43+
ifneq (,$(findstring openwrt,$(CROSS_COMPILE)))
44+
OPENWRT = 1
45+
endif
46+
endif
47+
48+
ifdef CROSS_COMPILE
49+
CPPFLAGS = -DCROSS_COMPILE
3750
endif
3851

3952
CFLAGS = \
@@ -42,7 +55,7 @@ CFLAGS = \
4255
-Wall \
4356
$(PLATFORM_CFLAGS)
4457

45-
# CFLAGS += -g
58+
CFLAGS += -fomit-frame-pointer -fdata-sections -ffunction-sections
4659

4760
EXTRA_CFLAGS =
4861

@@ -51,9 +64,12 @@ EXTRA_CFLAGS =
5164
CPPFLAGS += -Isrc
5265
CPPFLAGS += -I3rd/libuv/include -I3rd/udns
5366

54-
LDFLAGS = -Wl,-E
55-
LDFLAGS += -pthread -ldl -lrt
56-
LDFLAGS += -L3rd/libuv/.libs -luv -L3rd/udns -ludns
67+
LDFLAGS = -Wl,--gc-sections
68+
69+
LIBS += -pthread -ldl -lrt
70+
LIBS += 3rd/libuv/.libs/libuv.a 3rd/udns/libudns.a
71+
72+
LDFLAGS += $(LIBS)
5773

5874
#########################################################################
5975
include $(TOPDIR)/config.mk
@@ -65,7 +81,7 @@ all: libuv udns socksd
6581
$(Q)git submodule update --init
6682

6783
3rd/libuv/Makefile: | 3rd/libuv/autogen.sh
68-
$(Q)cd 3rd/libuv && ./autogen.sh && ./configure && $(MAKE)
84+
$(Q)cd 3rd/libuv && ./autogen.sh && ./configure --host=$(HOST) LDFLAGS= && $(MAKE)
6985

7086
libuv: 3rd/libuv/Makefile
7187

@@ -85,6 +101,9 @@ socksd: \
85101
src/dispatcher.o \
86102
src/daemon.o \
87103
src/signal.o \
104+
src/cache.o \
105+
src/md5.o \
106+
src/udprelay.o \
88107
src/client.o \
89108
src/remote.o \
90109
src/main.o
@@ -95,6 +114,7 @@ clean:
95114
\( -name '*.bak' -o -name '*~' \
96115
-o -name '*.o' -o -name '*.tmp' \) -print \
97116
| xargs rm -f
117+
@rm -f socksd
98118

99119
distclean: clean
100120
$(Q)cd 3rd/libuv && make distclean

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,33 @@
1-
# socksd
2-
a socks5 server implements RFC 1928 (SOCKS V5) using libuv
1+
socksd
2+
=================
3+
a socks5 server implements [RFC 1928](http://tools.ietf.org/html/rfc1928) (SOCKS V5)
4+
5+
Features
6+
------------
7+
* CONNECT and UDP support
8+
* Hostname resolution
9+
* Concurrency support
10+
11+
## License
12+
13+
The MIT License (MIT)
14+
15+
Copyright (c) 2014-2015 Ken <ken.i18n@gmail.com>
16+
17+
Permission is hereby granted, free of charge, to any person obtaining a copy of
18+
this software and associated documentation files (the "Software"), to deal in
19+
the Software without restriction, including without limitation the rights to
20+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
21+
the Software, and to permit persons to whom the Software is furnished to do so,
22+
subject to the following conditions:
23+
24+
The above copyright notice and this permission notice shall be included in all
25+
copies or substantial portions of the Software.
26+
27+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
29+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
30+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
31+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33+

src/cache.c

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
#define _GNU_SOURCE
2+
/*
3+
* Original Author: Oliver Lorenz (ol), olli@olorenz.org, https://olorenz.org
4+
* License: This is licensed under the same terms as uthash itself
5+
*/
6+
7+
#include <stdlib.h>
8+
#include <errno.h>
9+
#include <string.h>
10+
11+
#include "cache.h"
12+
13+
int
14+
cache_create(struct cache **dst, const size_t capacity,
15+
void (*free_cb)(void *element)) {
16+
struct cache *new = NULL;
17+
18+
if (!dst) {
19+
return EINVAL;
20+
}
21+
22+
if ((new = malloc(sizeof(*new))) == NULL) {
23+
return ENOMEM;
24+
}
25+
26+
new->max_entries = capacity;
27+
new->entries = NULL;
28+
new->free_cb = free_cb;
29+
*dst = new;
30+
31+
return 0;
32+
}
33+
34+
int
35+
cache_delete(struct cache *cache, int keep_data) {
36+
struct cache_entry *entry, *tmp;
37+
38+
if (!cache) {
39+
return EINVAL;
40+
}
41+
42+
if (keep_data) {
43+
HASH_CLEAR(hh, cache->entries);
44+
} else {
45+
HASH_ITER(hh, cache->entries, entry, tmp){
46+
HASH_DEL(cache->entries, entry);
47+
if (cache->free_cb) {
48+
cache->free_cb(entry->data);
49+
}
50+
free(entry);
51+
}
52+
}
53+
54+
free(cache);
55+
cache = NULL;
56+
57+
return 0;
58+
}
59+
60+
int
61+
cache_remove(struct cache *cache, char *key) {
62+
struct cache_entry *tmp;
63+
64+
if (!cache || !key) {
65+
return EINVAL;
66+
}
67+
68+
HASH_FIND_STR(cache->entries, key, tmp);
69+
70+
if (tmp) {
71+
HASH_DEL(cache->entries, tmp);
72+
if (cache->free_cb) {
73+
cache->free_cb(tmp->data);
74+
}
75+
free(tmp);
76+
}
77+
78+
return 0;
79+
}
80+
81+
int
82+
cache_removeall(struct cache *cache, void *opaque, int (*select_cb)(void *element, void *opaque)) {
83+
struct cache_entry *entry, *tmp;
84+
85+
if (!cache) {
86+
return EINVAL;
87+
}
88+
89+
HASH_ITER(hh, cache->entries, entry, tmp){
90+
if (select_cb(entry->data, opaque)) {
91+
HASH_DEL(cache->entries, entry);
92+
if (cache->free_cb) {
93+
cache->free_cb(entry->data);
94+
}
95+
free(entry);
96+
}
97+
}
98+
99+
return 0;
100+
}
101+
102+
/** Checks if a given key is in the cache
103+
104+
@param cache
105+
The cache object
106+
107+
@param key
108+
The key to look-up
109+
110+
@param result
111+
Where to store the result if key is found.
112+
113+
A warning: Even though result is just a pointer,
114+
you have to call this function with a **ptr,
115+
otherwise this will blow up in your face.
116+
117+
@return EINVAL if cache is NULL, 0 otherwise
118+
*/
119+
int
120+
cache_lookup(struct cache *cache, char *key, void *result)
121+
{
122+
struct cache_entry *tmp = NULL;
123+
char **dirty_hack = result;
124+
125+
if (!cache || !key || !result) {
126+
return EINVAL;
127+
}
128+
129+
HASH_FIND_STR(cache->entries, key, tmp);
130+
if (tmp) {
131+
size_t key_len = strnlen(tmp->key, KEY_MAX_LENGTH);
132+
HASH_DELETE(hh, cache->entries, tmp);
133+
HASH_ADD_KEYPTR(hh, cache->entries, tmp->key, key_len, tmp);
134+
*dirty_hack = tmp->data;
135+
} else {
136+
*dirty_hack = result = NULL;
137+
}
138+
139+
return 0;
140+
}
141+
142+
int
143+
cache_insert(struct cache *cache, char *key, void *data) {
144+
struct cache_entry *entry = NULL;
145+
struct cache_entry *tmp_entry = NULL;
146+
size_t key_len = 0;
147+
148+
if (!cache || !data) {
149+
return EINVAL;
150+
}
151+
152+
if ((entry = malloc(sizeof(*entry))) == NULL) {
153+
return ENOMEM;
154+
}
155+
156+
entry->key = key;
157+
entry->data = data;
158+
key_len = strnlen(entry->key, KEY_MAX_LENGTH);
159+
HASH_ADD_KEYPTR(hh, cache->entries, entry->key, key_len, entry);
160+
161+
if (HASH_COUNT(cache->entries) >= cache->max_entries) {
162+
HASH_ITER(hh, cache->entries, entry, tmp_entry){
163+
HASH_DELETE(hh, cache->entries, entry);
164+
if (cache->free_cb) {
165+
cache->free_cb(entry->data);
166+
} else {
167+
free(entry->data);
168+
}
169+
/* free(key->key) if data has been copied */
170+
free(entry);
171+
break;
172+
}
173+
}
174+
175+
return 0;
176+
}

src/cache.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Original Author: Oliver Lorenz (ol), olli@olorenz.org, https://olorenz.org
3+
* License: This is licensed under the same terms as uthash itself
4+
*/
5+
6+
#ifndef _CACHE_
7+
#define _CACHE_
8+
9+
#include "uthash.h"
10+
11+
#define KEY_MAX_LENGTH 32
12+
13+
/**
14+
* A cache entry
15+
*/
16+
struct cache_entry {
17+
char *key; /**<The key */
18+
void *data; /**<Payload */
19+
UT_hash_handle hh; /**<Hash Handle for uthash */
20+
};
21+
22+
/**
23+
* A cache object
24+
*/
25+
struct cache {
26+
size_t max_entries; /**<Amount of entries this cache object can hold */
27+
struct cache_entry *entries; /**<Head pointer for uthash */
28+
void (*free_cb) (void *element); /**<Callback function to free cache entries */
29+
};
30+
31+
32+
extern int cache_create(struct cache **dst, const size_t capacity, void (*free_cb)(void *element));
33+
extern int cache_delete(struct cache *cache, int keep_data);
34+
extern int cache_lookup(struct cache *cache, char *key, void *result);
35+
extern int cache_insert(struct cache *cache, char *key, void *data);
36+
extern int cache_remove(struct cache *cache, char *key);
37+
extern int cache_removeall(struct cache *cache, void *opaque, int (*select_cb)(void *element, void *opaque));
38+
39+
#endif

0 commit comments

Comments
 (0)