• Stars
    star
    129
  • Rank 279,262 (Top 6 %)
  • Language
    C
  • Created over 11 years ago
  • Updated over 3 years ago

Reviews

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

Repository Details

An example of how TPROXY can be used to proxy tcp connections.

tproxy-example

The Linux iptables-firewall is one of the most powerful networking tools out there. One of my favourite features is the TPROXY-target, which, as the name implies, enables you to proxy different types of connections.

When looking for examples of how to use TPROXY, I came up short. The only examples I could find was the sources of large projects like Squid. I therefore decided to create a small example showing how TPROXY can be used to proxy TCP connections.

The example transparent proxy application accepts TCP connections on the specified port (set to 9876 in tproxy_test.h) and attempts a TCP connection to the original host. If it is successful, the application starts forwarding data between the two connections (using splice()). The application supports multiple simultaneous connections and handles connections which fail (at least the scenarios I have tested).

Please note that TPROXY only works in iptables PREROUTING-chain, which is only hit by forwarded packets. I.e., it can't be used on the same machine as the traffic originates from.

For the transparent proxy example to work you need to configure routing and the firewall. The steps are found in the TPROXY-documentation. The only required steps are the routing and the TPROXY iptables-rule, the DIVERT-rule is an optimisation to prevent unnecessary processing of packets in the TPROXY target (-m socket checks for a socket matching the network packet header). Note that that the --tproxy-mark and fwmark must be the same, and that --on-port is the same port as used in the transparent proxy. If you experience any problems, the Squid website has some general tips on how to get TPROXY to work.

One thing worth being aware of is that the proxy example, to avoid over-complicating it, uses blocking sockets. So the performance might suffer with a large number of connections.

Ideas, suggestion and fixes are more than welcome. I hope you find this example useful!

More Repositories

1

inet-diag-example

Example application of how to passively monitor multiple sockets on Linux.
C
70
star
2

multihomed-routing

Tools and scripts for configuring multihomed routing on Linux
C
63
star
3

multi

MULTI is a command line network manager for Linux with multihoming support.
C
39
star
4

tcp_closer

Command-line Linux-tool for closing TCP connections to/from given ports
C
27
star
5

libuv-multiple-loops

An example showing how to use multiple event loops in libuv. The loops run in two different threads, and the example also shows how threads can communicate.
C
22
star
6

streaming-mp3-websockets-webaudio

An example of how to stream an MP3 file to a browser using WebSockets, and play it back with the WebAudio API
CoffeeScript
15
star
7

zte-mf910-scripts

Different scripts for changing config on the ZTE MF910 mifi.
Shell
14
star
8

mhi-for-kernel-4.14

Backport of the MHI bus, the client interface driver and the network driver
C
8
star
9

mhi-for-kernel-5.4

Backport of the MHI bus/mhi_net network driver/wwan framework to kernel 5.4, enabling the use of PCIe-based Qualcomm modems.
C
6
star
10

bandwidth-estimator

A bandwidth estimation tool designed for mobile broadband networks
C
6
star
11

usb-monitor

A daemon for monitoring and restarting devices connected to switchable USB ports, with support for user-specified restarting of devices.
C
6
star
12

qmi-dialer

qmi-dialer is a (hopefully) easy-to-follow example of how to use QMI to establish a connection and extract some information.
C
6
star
13

isc-dhcp-qmi

dhclient with raw ip support
C
2
star
14

data-exporter

A generic tool for receiving and exporting data in a multitude of formats
C
1
star
15

portpilot-linux

Linux logging software for the Portpilot USB port power monitor
C
1
star