• Stars
    star
    265
  • Rank 154,577 (Top 4 %)
  • Language
    Python
  • License
    Other
  • Created over 13 years ago
  • Updated over 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

simple TCP routing proxy

tproxy

tproxy is a simple TCP routing proxy (layer 7) built on Gevent that lets you configure the routine logic in Python. It's heavily inspired from proxy machine but have some unique features like the pre-fork worker model borrowed to Gunicorn.

Instalation

tproxy requires Python 2.x >= 2.5. Python 3.x support is planned.

$ pip install gevent
$ pip install tproxy

To install from source:

$ git clone git://github.com/benoitc/tproxy.git
$ cd tproxy
$ pip install -r requirements.txt
$ python setup.py install

Test your installation by running the command line:

$ tproxy examples/transparent.py

And go on http://127.0.0.1:5000 , you should see the google homepage.

Usage

$ tproxy -h

Usage: tproxy [OPTIONS] script_path

Options:
  --version                     show program's version number and exit
  -h, --help                    show this help message and exit
  --log-file=FILE               The log file to write to. [-]
  --log-level=LEVEL             The granularity of log outputs. [info]
  --log-config=FILE             The log config file to use. [None]
  -n STRING, --name=STRING      A base to use with setproctitle for process naming.
                                [None]
  -D, --daemon                  Daemonize the tproxy process. [False]
  -p FILE, --pid=FILE           A filename to use for the PID file. [None]
  -u USER, --user=USER          Switch worker processes to run as this user. [501]
  -g GROUP, --group=GROUP
                                Switch worker process to run as this group. [20]
  -m INT, --umask=INT           A bit mask for the file mode on files written by
                                tproxy. [0]
  -b ADDRESS, --bind=ADDRESS    The socket to bind. [127.0.0.1:8000]
  --backlog=INT                 The maximum number of pending connections.     [2048]
  --ssl-keyfile=STRING          Ssl key file [None]
  --ssl-certfile=STRING         Ssl ca certs file. contains concatenated
                                "certification [None]
  --ssl-ca-certs=STRING         Ssl ca certs file. contains concatenated
                                "certification [None]
  --ssl-cert-reqs=INT           Specifies whether a certificate is required from the
                                other [0]
  -w INT, --workers=INT         The number of worker process for handling requests. [1]
  --worker-connections=INT      The maximum number of simultaneous clients per worker.
                                [1000]
  -t INT, --timeout=INT         Workers silent for more than this many seconds are
                                killed and restarted. [30]

Signals

QUIT    -   Graceful shutdown. Stop accepting connections immediatly
            and wait until all connections close

TERM    -   Fast shutdown. Stop accepting and close all conections
            after 10s.
INT     -   Same as TERM

HUP     -   Graceful reloading. Reload all workers with the new code
            in your routing script.

USR2    -   Upgrade tproxy on the fly

TTIN    -   Increase the number of worker from 1

TTOU    -   Decrease the number of worker from 1

Exemple of routing script

import re
re_host = re.compile("Host:\s*(.*)\r\n")

class CouchDBRouter(object):
    # look at the routing table and return a couchdb node to use
    def lookup(self, name):
        """ do something """

router = CouchDBRouter()

# Perform content-aware routing based on the stream data. Here, the
# Host header information from the HTTP protocol is parsed to find the
# username and a lookup routine is run on the name to find the correct
# couchdb node. If no match can be made yet, do nothing with the
# connection. (make your own couchone server...)

def proxy(data):
    matches = re_host.findall(data)
    if matches:
        host = router.lookup(matches.pop())
        return {"remote": host}
    return None

Example SOCKS4 Proxy in 18 Lines

import socket
import struct

def proxy(data):
    if len(data) < 9:
        return

    command = ord(data[1])
    ip, port = socket.inet_ntoa(data[4:8]), struct.unpack(">H", data[2:4])[0]
    idx = data.index("\0")
    userid = data[8:idx]

    if command == 1: #connect
        return dict(remote="%s:%s" % (ip, port),
                reply="\0\x5a\0\0\0\0\0\0",
                data=data[idx:])
    else:
        return {"close": "\0\x5b\0\0\0\0\0\0"}

Example of returning a file

import os

WELCOME_FILE = os.path.join(os.path.dirname(__file__), "welcome.txt")

def proxy(data):
    fno = os.open(WELCOME_FILE, os.O_RDONLY)
    return {
            "file": fno,
            "reply": "HTTP/1.1 200 OK\r\n\r\n"
           }

Valid return values

  • { "remote:": string or tuple } - String is the host:port of the server that will be proxied.
  • { "remote": String, "data": String} - Same as above, but send the given data instead.
  • { "remote": String, "data": String, "reply": String} - Same as above, but reply with given data back to the client
  • None - Do nothing.
  • { "close": True } - Close the connection.
  • { "close": String } - Close the connection after sending the String.
  • { "file": String } - Return a file specify by the file path and close the connection.
  • { "file": String, "reply": String } - Return a file specify by the file path and close the connection.
  • { "file": Int, "reply": String} - Same as above but reply with given data back to the client
  • { "file": Int } - Return a file specify by its file descriptor
  • { "file": Int, "reply": String} - Same as above but reply with given data back to the client

Notes:

If sendfile API available it will be used to send a file with "file" command.

The file command can have 2 optionnnal parameters:

  • offset: argument specifies where to begin in the file.
  • nbytes: specifies how many bytes of the file should be sent

To handle ssl for remote connection you can add these optionals arguments:

  • ssl: True or False, if you want to connect with ssl
  • ssl_args: dict, optionals ssl arguments. Read the ssl documentation for more informations about them.

Handle errors

You can easily handling error by adding a proxy_error function in your script:

def proxy_error(client, e):
    pass

This function get the ClientConnection instance (current connection) as first arguments and the error exception in second argument.

Rewrite requests & responses

Main goal of tproxy is to allows you to route transparently tcp to your applications. But some case you want to do more. For example you need in HTTP 1.1 to change the Host header to make sure remote HTTP server will know what to do if uses virtual hosting.

To do that, add a rewrite_request function in your function to simply rewrite clienrt request and rewrite_response to rewrite the remote response. Both functions take a tproxy.rewrite.RewriteIO instance which is based on io.RawIOBase class.

See the httprewrite.py example for an example of HTTP rewrite.

Copyright

2011 (c) Benoît Chesneau <[email protected]>

More Repositories

1

gunicorn

gunicorn 'Green Unicorn' is a WSGI HTTP Server for UNIX, fast clients and sleepy applications.
Python
9,757
star
2

hackney

simple HTTP client in Erlang
Erlang
1,322
star
3

restkit

an HTTP resource kit for Python
Python
405
star
4

offset

Python
377
star
5

gaffer

control, watch and launch your applications and jobs over HTTP.
Python
363
star
6

http-parser

HTTP request/response parser for python in C
C
337
star
7

erica

tool to manage couchapps
JavaScript
269
star
8

couchdbkit

CouchDB python framework
Python
265
star
9

couchbeam

Apache CouchDB client in Erlang
Erlang
238
star
10

flower

collection of modules to build distributed and reliable concurrent systems in Python.
Python
207
star
11

socketpool

Generic socket pool
Python
163
star
12

cowboy_revproxy

simple TCP routing proxy (layer 7) in erlang
Erlang
86
star
13

econfig

simple Erlang config handler using INI files
Erlang
79
star
14

hooks

generic plugin & hook system for Erlang applications
Erlang
72
star
15

erlang-metrics

A generic interface to different metrics systems in Erlang.
Erlang
70
star
16

dj-webmachine

Django REST layer
Python
59
star
17

erlang-nat

implements NAT handling facilities for Erlang applications
Erlang
57
star
18

couchdbproxy

Simple multinode couchdb proxy
Erlang
57
star
19

dnssd_erlang

Erlang interface to Apple's Bonjour DNS Service Discovery implementation
Erlang
46
star
20

nat_upnp

Erlang library to map your internal port to an external using UNP IGD
Erlang
42
star
21

uzmq

libuv interface for ZeroMQ
Python
34
star
22

ejson

EJSON - decode and encode JSON into/from Erlang terms (from CouchDB project)
C
32
star
23

mochicow

mochiweb adapter for cowboy.
Erlang
29
star
24

dj-revproxy

simple reverse proxy for django.
Python
28
star
25

hroute

simple HTTP proxy based on tproxy
Python
28
star
26

inet_cidr

CIDR erlang library
Erlang
27
star
27

mimerl

library to handle mimetypes
Erlang
27
star
28

sieve

sieve is a simple TCP routing proxy (layer 7) in erlang
Erlang
26
star
29

opencouch

A embeddable document oriented database compatible with Apache CouchDB
Erlang
22
star
30

gunicorn-recipes

Collection of recipe and examples to help in gunicorn deployement, installation and configuration.
19
star
31

nymphormation

Nymphormation is a Couchapp that allow people to share links or news .
JavaScript
19
star
32

rebar3_path_deps

A rebar plugin to specify path dependencies.
Erlang
19
star
33

couch_zmq

zeromq endpoint for couchdb.
Erlang
18
star
34

noddycouch

minimal couchdb toolkit for nodejs.
JavaScript
17
star
35

afgwardiary

couchapp to render afgwardiary data from wikileaks
JavaScript
16
star
36

upnp

Erlang UPNP Module
Erlang
14
star
37

couchc

minimal couchdb internal API wrapper
Erlang
14
star
38

couchapp-ng

Couchapp Engine
Erlang
14
star
39

couchdb

My CouchDB hack repository
JavaScript
14
star
40

cbt

multi-layer MVCC log append-only database library based on the Apache CouchDB btree.
Erlang
14
star
41

inet_ext

inet extensions library
Erlang
13
star
42

hackney_lib

WEB toolkit including miscellaneous modules to play with HTTP and Web protocols
Erlang
11
star
43

memdb

Erlang memory backend K/V store
Erlang
10
star
44

rebar3_protobuffs

rebar3 protobuffs provider using protobuffs from Basho
Erlang
10
star
45

unicode_util_compat

unicode_util compatibility library for Erlang < 20
Erlang
10
star
46

fserve

simple file server in python on unix systems with sendfile support using pistil
Python
8
star
47

pywebmachine

Python port of Basho's WebMachine
Python
8
star
48

hypercouch

Full text indexing of CouchDB via HyperEstraier
Python
7
star
49

natpmp

Erlang Nat-PMP client
Erlang
7
star
50

rcouch_template

Rebar templates for generating custom couchdb releases
Shell
7
star
51

dotfiles

repository of dotifile in my home
Vim Script
6
star
52

hackney_disp

Load-balanced Pool dispatcher based on dispcount for hackney.
Erlang
6
star
53

benoitc.org

My own site
JavaScript
6
star
54

couchdb_internals

Miscelleaneous docs about the couchdb protocol and couchdb implementation
6
star
55

dcouch

an Alternative to Mnesia with Unique Features
5
star
56

backbone.py

just some code to initiate construction of objects from any remote repo.
Python
5
star
57

nplib

library collecting modules to decode a bunch of network protocoles
Erlang
5
star
58

qrkit

simple and stupid qr code binding extracted from qrurl
Python
5
star
59

couchdocs

Documentation for CouchDB
Objective-C
5
star
60

lhttpc-old

GIT clone of http://bitbucket.org/etc/lhttpc/
Erlang
4
star
61

blanket

couchdb/refuge client.
4
star
62

couch_randomdoc

simple couchdb module to add support of random document fetching.
Erlang
4
star
63

erlang-pbkdf2-nif

PBKDF2 NIF implementation
C
4
star
64

couchdb-old

Couchdb mirror repo to handle some custom dev
Erlang
4
star
65

rebar3_cargo

Erlang
4
star
66

erl_stun

Erlang
4
star
67

hackney_pooler

Experiment an API to limit the number of hackney requests launched concurrently
Erlang
4
star
68

emonk_helper

Some extensiosn to ease the use of emonk (http://github.com/davisp/emonk)
Erlang
3
star
69

couchjs

rebar couchjs version for CouchDB
JavaScript
3
star
70

dj-cookieauth

Secure Cookie Auth module for Django
Python
3
star
71

cablesgate

Script to put wikileaks diploamatic cables in a couchdb
Python
3
star
72

echohttp

simple service echoing any requests made to http://echohttp.com/echo
Erlang
3
star
73

mt-compono

minimalist cms using Django
JavaScript
3
star
74

dj-pages

minimal content cms/renderer
JavaScript
3
star
75

build-android

Shell
3
star
76

vrac

Simple app to manage all data fragments in vurt
JavaScript
3
star
77

erlang-idna-nif

C
3
star
78

esync

simple tool to sync files
Erlang
3
star
79

epygments

Simple wrapper to Pygments - prettify source code in your erlang program.
Erlang
3
star
80

libcouch

Pure Erlang CouchDB Store library.
Erlang
3
star
81

overlay

CSS
3
star
82

buildout_couchdb

zc buildout recipe for Apache CouchDB server
Python
3
star
83

dummy_app

Erlang
2
star
84

hello_world

Erlang
2
star
85

wineoverip

JavaScript
2
star
86

foodoverip

Handle #foodoverip
JavaScript
2
star
87

couchit_experiment

Standalone couch.it powered by CouchDB
Erlang
2
star
88

benoitc.github.com

Github Homepage
2
star
89

dataporn

intensive data usage
2
star
90

qrurl

C
2
star
91

enki

Document-oriented database framework
2
star
92

mod_percept2

percept2 ejabberd module
JavaScript
1
star
93

tiptoe

1
star
94

mz_counter

simple atomic counter for erlang applications
C
1
star
95

enki--multimedia.org

1
star
96

unicode_extra

Extra function not available in Erlang unicode_util module
Erlang
1
star
97

erlang-ucs

1
star
98

osdc2013_demo

demo for OSDC 2013
Erlang
1
star