Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ config.h
tags
redsocks
.depend
http-parser*
gen/*
28 changes: 22 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
OBJS := parser.o main.o redsocks.o log.o http-connect.o socks4.o socks5.o http-relay.o base.o base64.o md5.o http-auth.o utils.o redudp.o dnstc.o gen/version.o
LIBHTTP_VERSION := 2.6.0
LIBHTTP_NAME := http-parser-$(LIBHTTP_VERSION)
LIBHTTP_CFLAGS := -I./http-parser-$(LIBHTTP_VERSION) -L./http-parser-$(LIBHTTP_VERSION)

OBJS := tls.o parser.o main.o redsocks.o log.o http-connect.o socks4.o socks5.o http-relay.o base.o base64.o md5.o http-auth.o utils.o redudp.o dnstc.o gen/version.o
SRCS := $(OBJS:.o=.c)
CONF := config.h
DEPS := .depend
OUT := redsocks
VERSION := 0.4
VERSION := 0.4.1-adallom

LIBS := -levent
LIBS := -levent -lhttp_parser
CFLAGS += $(LIBHTTP_CFLAGS)
CFLAGS += -g -O2
override CFLAGS += -std=c99 -D_XOPEN_SOURCE=600 -D_BSD_SOURCE -D_DEFAULT_SOURCE -Wall

all: $(OUT)

.PHONY: all clean distclean
.PHONY: all clean distclean http-parser

tags: *.c *.h
ctags -R

$(LIBHTTP_NAME):
wget https://github.com/nodejs/http-parser/archive/v$(LIBHTTP_VERSION).tar.gz
tar -zxf v$(LIBHTTP_VERSION).tar.gz
rm -f v$(LIBHTTP_VERSION).tar.gz

$(LIBHTTP_NAME)/libhttp_parser.o:
cd $(LIBHTTP_NAME) && make package

http-parser: $(LIBHTTP_NAME) $(LIBHTTP_NAME)/libhttp_parser.o

$(CONF):
@case `uname` in \
Linux*) \
Expand Down Expand Up @@ -78,12 +93,13 @@ $(DEPS): $(SRCS)

-include $(DEPS)

$(OUT): $(OBJS)
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)
$(OUT): http-parser $(OBJS)
$(CC) $(CFLAGS) -o $@ $(OBJS) $(LDFLAGS) $(LIBS)

clean:
$(RM) $(OUT) $(CONF) $(OBJS)

distclean: clean
$(RM) tags $(DEPS)
$(RM) -r gen
$(RM) -rf $(LIBHTTP_NAME)
21 changes: 17 additions & 4 deletions http-connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ typedef enum httpc_state_t {
#define HTTP_HEAD_WM_HIGH 4096 // that should be enough for one HTTP line.


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

static void httpc_client_init(redsocks_client *client)
{
client->state = httpc_new;
Expand Down Expand Up @@ -196,6 +200,14 @@ static struct evbuffer *httpc_mkconnect(redsocks_client *client)

const char *auth_scheme = NULL;
char *auth_string = NULL;
char *hostname = NULL;

if (client->hostname) {
hostname = client->hostname;
} else {
hostname = inet_ntoa(client->destaddr.sin_addr);
}


if (auth->last_auth_query != NULL) {
/* find previous auth challange */
Expand All @@ -205,8 +217,9 @@ static struct evbuffer *httpc_mkconnect(redsocks_client *client)
auth_scheme = "Basic";
} else if (strncasecmp(auth->last_auth_query, "Digest", 6) == 0) {
/* calculate uri */
char uri[128];
snprintf(uri, 128, "%s:%u", inet_ntoa(client->destaddr.sin_addr), ntohs(client->destaddr.sin_port));
char uri[MAX_CONNECT_HOST_LENGTH] = {0};

snprintf(uri, MAX_CONNECT_HOST_LENGTH, "%s:%u", hostname, ntohs(client->destaddr.sin_port));

/* prepare an random string for cnounce */
char cnounce[17];
Expand All @@ -223,13 +236,13 @@ static struct evbuffer *httpc_mkconnect(redsocks_client *client)
if (auth_string == NULL) {
len = evbuffer_add_printf(buff,
"CONNECT %s:%u HTTP/1.0\r\n\r\n",
inet_ntoa(client->destaddr.sin_addr),
hostname,
ntohs(client->destaddr.sin_port)
);
} else {
len = evbuffer_add_printf(buff,
"CONNECT %s:%u HTTP/1.0\r\n%s %s %s\r\n\r\n",
inet_ntoa(client->destaddr.sin_addr),
hostname,
ntohs(client->destaddr.sin_port),
auth_response_header,
auth_scheme,
Expand Down
Loading