Skip to content

Commit

Permalink
renew and port PR # 71 originally authored by @aviramc
Browse files Browse the repository at this point in the history
  • Loading branch information
andronoob committed Apr 25, 2021
1 parent 1951b49 commit 55d2aeb
Show file tree
Hide file tree
Showing 10 changed files with 617 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "http-parser"]
path = http-parser
url = https://github.com/nodejs/http-parser/
26 changes: 21 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
ifdef DISABLE_SHADOWSOCKS
OBJS := parser.o main.o redsocks.o log.o direct.o ipcache.o autoproxy.o http-connect.o \
socks4.o socks5.o http-relay.o base.o base64.o md5.o http-auth.o utils.o redudp.o socks5-udp.o \
tcpdns.o gen/version.o
tcpdns.o tls.o gen/version.o
CFLAGS +=-fPIC -O3 -DDISABLE_SHADOWSOCKS
FEATURES += DISABLE_SHADOWSOCKS
else
OBJS := parser.o main.o redsocks.o log.o direct.o ipcache.o autoproxy.o encrypt.o shadowsocks.o http-connect.o \
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 \
tcpdns.o gen/version.o
tcpdns.o tls.o gen/version.o
CFLAGS +=-fPIC -O3
endif

LIBHTTP_CFLAGS := -I./http-parser -L./http-parser

SRCS := $(OBJS:.o=.c)
CONF := config.h
DEPS := .depend
Expand All @@ -18,6 +21,10 @@ VERSION := 0.68
OS := $(shell uname)

LIBS := -levent

LIBS += -lhttp_parser
CFLAGS += $(LIBHTTP_CFLAGS)

override CFLAGS += -D_BSD_SOURCE -D_DEFAULT_SOURCE -Wall
ifeq ($(OS), Linux)
override CFLAGS += -std=c99 -D_XOPEN_SOURCE=600
Expand Down Expand Up @@ -63,11 +70,19 @@ endif

all: $(OUT)

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

tags: *.c *.h
ctags -R

http-parser-download:
git submodule update --init

http-parser-build:
cd http-parser && make package

http-parser: http-parser-download http-parser-build

$(CONF):
@case $(OS) in \
Linux*) \
Expand Down Expand Up @@ -149,8 +164,8 @@ $(DEPS): $(OSX_HEADERS) $(SRCS)

-include $(DEPS)

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

clean:
$(RM) $(CONF) $(OBJS)
Expand All @@ -160,3 +175,4 @@ distclean: clean
$(RM) tags $(DEPS)
$(RM) -r gen
$(RM) -r $(OSX_ROOT_PATH)
cd http-parser && make clean
16 changes: 14 additions & 2 deletions http-connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ typedef enum httpc_state_t {

#define HTTP_HEAD_WM_HIGH 8192 // 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)
{
Expand Down Expand Up @@ -174,6 +178,14 @@ struct evbuffer *httpc_mkconnect(redsocks_client *client)
{
struct evbuffer *buff = NULL, *retval = NULL;
int len;
char *hostname = NULL;


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

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

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

if (auth->last_auth_query != NULL) {
/* find previous auth challange */
Expand Down
1 change: 1 addition & 0 deletions http-parser
Submodule http-parser added at 2343fd
39 changes: 39 additions & 0 deletions protocol.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2014, Dustin Lundquist <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef PROTOCOL_H
#define PROTOCOL_H

#include <inttypes.h>

struct Protocol {
const char *const name;
const uint16_t default_port;
int (*const parse_packet)(const char*, size_t, char **);
const char *const abort_message;
const size_t abort_message_len;
};

#endif
Loading

0 comments on commit 55d2aeb

Please sign in to comment.