Skip to content

Commit 55d2aeb

Browse files
author
andronoob
committed
renew and port PR # 71 originally authored by @aviramc
1 parent 1951b49 commit 55d2aeb

File tree

10 files changed

+617
-8
lines changed

10 files changed

+617
-8
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "http-parser"]
2+
path = http-parser
3+
url = https://github.com/nodejs/http-parser/

Makefile

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
ifdef DISABLE_SHADOWSOCKS
22
OBJS := parser.o main.o redsocks.o log.o direct.o ipcache.o autoproxy.o http-connect.o \
33
socks4.o socks5.o http-relay.o base.o base64.o md5.o http-auth.o utils.o redudp.o socks5-udp.o \
4-
tcpdns.o gen/version.o
4+
tcpdns.o tls.o gen/version.o
55
CFLAGS +=-fPIC -O3 -DDISABLE_SHADOWSOCKS
66
FEATURES += DISABLE_SHADOWSOCKS
77
else
88
OBJS := parser.o main.o redsocks.o log.o direct.o ipcache.o autoproxy.o encrypt.o shadowsocks.o http-connect.o \
99
socks4.o socks5.o http-relay.o base.o base64.o md5.o http-auth.o utils.o redudp.o socks5-udp.o shadowsocks-udp.o \
10-
tcpdns.o gen/version.o
10+
tcpdns.o tls.o gen/version.o
1111
CFLAGS +=-fPIC -O3
1212
endif
13+
14+
LIBHTTP_CFLAGS := -I./http-parser -L./http-parser
15+
1316
SRCS := $(OBJS:.o=.c)
1417
CONF := config.h
1518
DEPS := .depend
@@ -18,6 +21,10 @@ VERSION := 0.68
1821
OS := $(shell uname)
1922

2023
LIBS := -levent
24+
25+
LIBS += -lhttp_parser
26+
CFLAGS += $(LIBHTTP_CFLAGS)
27+
2128
override CFLAGS += -D_BSD_SOURCE -D_DEFAULT_SOURCE -Wall
2229
ifeq ($(OS), Linux)
2330
override CFLAGS += -std=c99 -D_XOPEN_SOURCE=600
@@ -63,11 +70,19 @@ endif
6370

6471
all: $(OUT)
6572

66-
.PHONY: all clean distclean
73+
.PHONY: all clean distclean http-parser
6774

6875
tags: *.c *.h
6976
ctags -R
7077

78+
http-parser-download:
79+
git submodule update --init
80+
81+
http-parser-build:
82+
cd http-parser && make package
83+
84+
http-parser: http-parser-download http-parser-build
85+
7186
$(CONF):
7287
@case $(OS) in \
7388
Linux*) \
@@ -149,8 +164,8 @@ $(DEPS): $(OSX_HEADERS) $(SRCS)
149164

150165
-include $(DEPS)
151166

152-
$(OUT): $(OBJS)
153-
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)
167+
$(OUT): http-parser $(OBJS)
168+
$(CC) $(CFLAGS) -o $@ $(OBJS) $(LDFLAGS) $(LIBS)
154169

155170
clean:
156171
$(RM) $(CONF) $(OBJS)
@@ -160,3 +175,4 @@ distclean: clean
160175
$(RM) tags $(DEPS)
161176
$(RM) -r gen
162177
$(RM) -r $(OSX_ROOT_PATH)
178+
cd http-parser && make clean

http-connect.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ typedef enum httpc_state_t {
4242

4343
#define HTTP_HEAD_WM_HIGH 8192 // that should be enough for one HTTP line.
4444

45+
#define MAX_SERVER_NAME (253) /* Max DNS is 253 characters */
46+
#define MAX_PORT_STR_LENGTH (6) /* Ports are 5 digits decimax max */
47+
#define MAX_CONNECT_HOST_LENGTH (MAX_SERVER_NAME + MAX_PORT_STR_LENGTH + 1) /* Add one byte for \0 */
48+
4549

4650
static void httpc_client_init(redsocks_client *client)
4751
{
@@ -174,6 +178,14 @@ struct evbuffer *httpc_mkconnect(redsocks_client *client)
174178
{
175179
struct evbuffer *buff = NULL, *retval = NULL;
176180
int len;
181+
char *hostname = NULL;
182+
183+
184+
if (client->hostname) {
185+
hostname = client->hostname;
186+
} else {
187+
hostname = inet_ntoa(client->destaddr.sin_addr);
188+
}
177189

178190
buff = evbuffer_new();
179191
if (!buff) {
@@ -188,8 +200,8 @@ struct evbuffer *httpc_mkconnect(redsocks_client *client)
188200
char *auth_string = NULL;
189201

190202
/* calculate uri */
191-
char uri[RED_INET_ADDRSTRLEN];
192-
red_inet_ntop(&client->destaddr, uri, sizeof(uri));
203+
char uri[MAX_CONNECT_HOST_LENGTH] = {0};
204+
snprintf(uri, MAX_CONNECT_HOST_LENGTH, "%s:%u", hostname, ntohs(client->destaddr.sin_port));
193205

194206
if (auth->last_auth_query != NULL) {
195207
/* find previous auth challange */

http-parser

Submodule http-parser added at 2343fd6

protocol.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2014, Dustin Lundquist <[email protected]>
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24+
* POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
#ifndef PROTOCOL_H
27+
#define PROTOCOL_H
28+
29+
#include <inttypes.h>
30+
31+
struct Protocol {
32+
const char *const name;
33+
const uint16_t default_port;
34+
int (*const parse_packet)(const char*, size_t, char **);
35+
const char *const abort_message;
36+
const size_t abort_message_len;
37+
};
38+
39+
#endif

0 commit comments

Comments
 (0)