Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have an SSH tunnel like this:

ssh -D 10.1.2.1:8080 -f -C -q -N name@ssh_server_ip

I have a computer on the local network (in fact VPN, but it's not important in this case) with an IP of 10.1.2.2.

I want to redirect all traffic from 10.1.2.2 (in fact from 10.1.2.2 - 10.1.2.80) to 10.1.2.1. I don't want to set a proxy in the web browser.

After all, 10.1.2.2 should connect via 10.1.2.1 to ssh_server_ip without any adjustment on 10.1.2.2.

This is necessary because some of the "computers" will be mobile devices and I can't force them to use a SOCKS5 pseudo-proxy.

I am using Debian.

share|improve this question
    
Seriously? It can't so hard... I have tried this: iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to 10.1.2.1:8080 but it won't help –  user1443373 Jul 27 '12 at 12:05
add comment

1 Answer

You can use iptables and redsocks to redirect all your traffic to the socks5 proxy that you have already established. It's nicely described on http://darkk.net.ru/redsocks/.

This is how your redsocks.conf could look like if redsocks is running on your client (10.1.2.2):

redsocks {
    /* `local_ip' defaults to 127.0.0.1 for security reasons,
     * use 0.0.0.0 if you want to listen on every interface.
     * `local_*' are used as port to redirect to.
     */
    local_ip = 127.0.0.1;
    local_port = 12345;

    // `ip' and `port' are IP and tcp-port of proxy-server
    ip = 10.1.2.1;
    port = 8080;

    // known types: socks4, socks5, http-connect, http-relay
    type = socks5;

    // login = "foobar";
    // password = "baz";
}

In this case redsocks in running on port 12345 and you just have to tell iptables to redirect the tcp connections to redsocks:

# Create new chain
iptables -t nat -N REDSOCKS

# Ignore LANs and some other reserved addresses.
iptables -t nat -A REDSOCKS -d 0.0.0.0/8 -j RETURN
iptables -t nat -A REDSOCKS -d 10.0.0.0/8 -j RETURN
iptables -t nat -A REDSOCKS -d 127.0.0.0/8 -j RETURN
iptables -t nat -A REDSOCKS -d 169.254.0.0/16 -j RETURN
iptables -t nat -A REDSOCKS -d 172.16.0.0/12 -j RETURN
iptables -t nat -A REDSOCKS -d 192.168.0.0/16 -j RETURN
iptables -t nat -A REDSOCKS -d 224.0.0.0/4 -j RETURN
iptables -t nat -A REDSOCKS -d 240.0.0.0/4 -j RETURN

# Anything else should be redirected to port 12345
iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-ports 12345

# Any tcp connection made by `luser' should be redirected, put your username here.
iptables -t nat -A OUTPUT -p tcp -m owner --uid-owner luser -j REDSOCKS

For more information (e.g. on how to clean up iptables again) take a look at the redsocks website.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.