Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite from scratch #19

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
69 changes: 12 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ An `nginx_http_proxy_module` fork with SOCKS5 support

## Building

nginx >= **1.9.1** is supported.
nginx >= **1.18.0** is supported.

```bash
# apt-get install git build-essential zlib1g-dev libpcre3 libpcre3-dev unzip

$ git clone https://github.com/dannote/socks-nginx-module
$ wget http://nginx.org/download/nginx-1.9.15.tar.gz
$ wget http://nginx.org/download/nginx-1.18.0.tar.gz

$ tar -xzvf nginx-1.9.15.tar.gz
$ tar -xzvf nginx-1.18.0.tar.gz

$ cd nginx-1.9.15
$ cd nginx-1.18.0

# See http://nginx.org/en/docs/configure.html for more configuration options
$ ./configure --add-module=../socks-nginx-module
$ ./configure --add-dynamic-module=../socks-nginx-module

$ make
# make install
Expand All @@ -29,62 +29,17 @@ Sample HTTP to SOCKS5 proxy configuration:

```
location / {
socks_set_header Host $http_host;
socks_set_header Proxy-Connection '';
socks_pass_header Server;
socks_redirect off;
socks_http_version 1.1;
socks_tunnel_header X-Connect;
socks_buffers 16 16k;
socks_buffer_size 32k;
socks_cache proxy;
socks_cache_valid 30s;
socks_cache_use_stale error timeout invalid_header updating
http_500 http_502 http_503 http_504;
socks_pass socks5://127.0.0.1:1080;
proxy_pass http://httpbin.org/get;
socks_pass socks5://proxy:1080;
}
```

All [ngx_http_proxy_module](http://nginx.org/en/docs/http/ngx_http_proxy_module.html) directives are supported.

### socks_tunnel_header

Context: `http`, `server`, `location`

As nginx HTTP parser doesn't support HTTP CONNECT method, a special header can be set to indicate tunnel connection.

This directive can be exploited with the following HAProxy configuration:
## Debugging

```
frontend local
bind *:8080
mode http
http-request set-method GET if METH_CONNECT
http-request set-uri https://%[req.hdr(Host)]/ if METH_CONNECT
http-request add-header X-Connect true if METH_CONNECT
default_backend nginx

backend nginx
mode http
server proxy 127.0.0.1:8080 maxconn 100000
```

### socks_set_host
cd debug
docker-compose run --service-ports nginx

Context: `http`, `server`, `location`

Default: `socks_set_host $http_host;`

Overrides the endpoint server.

This example will proxy requests to `ipinfo.io` via local Tor daemon:

```
location /ip {
socks_pass socks5://127.0.0.1:9050;
socks_set_host ipinfo.io;
socks_set_header Host ipinfo.io;
socks_redirect off;
socks_http_version 1.1;
}
(gdb) set follow-fork-mode child
(gdb) run
```
14 changes: 9 additions & 5 deletions config
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
ngx_addon_name=ngx_http_socks_module

HTTP_MODULES="$HTTP_MODULES ngx_http_socks_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_socks_module.c $ngx_addon_dir/src/ngx_http_socks_upstream.c"
NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/src/ngx_http_socks_module.h"

have=NGX_HTTP_SOCKS_MODULE . auto/have
if test -n "$ngx_module_link"; then
ngx_module_type=HTTP
ngx_module_name=ngx_http_socks_module
ngx_module_srcs="$ngx_addon_dir/ngx_http_socks_module.c"
. auto/module
else
HTTP_MODULES="$HTTP_MODULES ngx_http_socks_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_socks_module.c"
fi
1 change: 1 addition & 0 deletions debug/.gdbinit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
set follow-fork-mode child
18 changes: 18 additions & 0 deletions debug/3proxy.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
writable

nserver 8.8.8.8
nserver 8.8.4.4
nscache 65536

log
logformat "L%t%. L%t.%. %N.%p %E %U %C:%c %R:%r %O %I %h %T"

monitor /etc/3proxy/cfg/3proxy.cfg

auth none

proxy -p3128
socks -p1080
admin -p8080

flush
23 changes: 23 additions & 0 deletions debug/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM ubuntu:20.04

EXPOSE 80

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && \
apt-get install -y wget build-essential libpcre3-dev zlib1g-dev gdb && \
rm -rf /var/lib/apt/lists/*

WORKDIR /root

COPY .gdbinit .

ARG NGX_VERSION

RUN wget https://nginx.org/download/nginx-$NGX_VERSION.tar.gz && \
tar xzvf nginx-$NGX_VERSION.tar.gz && \
rm nginx-$NGX_VERSION.tar.gz

WORKDIR nginx-$NGX_VERSION

CMD /bin/bash /code/debug/run.sh
34 changes: 34 additions & 0 deletions debug/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
version: '2.4'
services:
nginx:
build:
context: .
args:
NGX_VERSION: 1.19.6
ports:
- 8080:80
volumes:
- ..:/code
- build:/build
depends_on:
- proxy
links:
- proxy
networks:
- local

proxy:
image: riftbit/3proxy
ports:
- 1080
networks:
- local
volumes:
- .:/etc/3proxy/cfg

volumes:
build:

networks:
local:
driver: bridge
35 changes: 35 additions & 0 deletions debug/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
daemon off;
pid /tmp/nginx.pid;
error_log /dev/stdout debug;

load_module /build/ngx_http_socks_module.so;

events {
worker_connections 4096;
}

http {
access_log /dev/stdout;

proxy_temp_path /tmp;
client_body_temp_path /tmp;
fastcgi_temp_path /tmp;
uwsgi_temp_path /tmp;
scgi_temp_path /tmp;

resolver 127.0.0.11 ipv6=off;

server {
listen 80;

location / {
proxy_next_upstream off;
proxy_connect_timeout 3000;
proxy_send_timeout 3000;
proxy_read_timeout 3000;
send_timeout 3000;
proxy_pass http://httpbin.org/get;
socks_pass socks5://proxy:1080;
}
}
}
8 changes: 8 additions & 0 deletions debug/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

if [ ! -f /build/Makefile ]
then
./configure --with-debug --builddir=/build --add-dynamic-module=/code
fi

make -f /build/Makefile && gdb --args /build/nginx -c /code/debug/nginx.conf -p $PWD
Loading